1 /*
2  * Copyright (c) 2005-2007 Sendmail, Inc. and its suppliers.
3  *      All rights reserved.
4  *
5  * By using this file, you agree to the terms and conditions set
6  * forth in the LICENSE file which can be found at the top level of
7  * the sendmail distribution.
8  */
9 
10 #include <sm/gen.h>
11 SM_IDSTR(id, "@(#)$Id: t-memstat.c,v 1.9 2007/03/14 21:41:09 ca Exp $")
12 
13 #include <sm/misc.h>
14 
15 /*
16 **  Simple test program for memstat
17 */
18 
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <strings.h>
23 #include <string.h>
24 
25 extern char *optarg;
26 extern int optind;
27 
28 void
usage(prg)29 usage(prg)
30 	char *prg;
31 {
32 	fprintf(stderr, "usage: %s [options]\n", prg);
33 	fprintf(stderr, "options:\n");
34 	fprintf(stderr, "-l n    loop n times\n");
35 	fprintf(stderr, "-m n    allocate n bytes per iteration\n");
36 	fprintf(stderr, "-r name use name as resource to query\n");
37 	fprintf(stderr, "-s n    sleep n seconds per iteration\n");
38 }
39 
40 int
main(argc,argv)41 main(argc, argv)
42 	int argc;
43 	char **argv;
44 {
45 	int r, r2, i, l, slp, sz;
46 	long v;
47 	char *resource;
48 
49 	l = 1;
50 	sz = slp = 0;
51 	resource = NULL;
52 	while ((r = getopt(argc, argv, "l:m:r:s:")) != -1)
53 	{
54 		switch ((char) r)
55 		{
56 		  case 'l':
57 			l = strtol(optarg, NULL, 0);
58 			break;
59 
60 		  case 'm':
61 			sz = strtol(optarg, NULL, 0);
62 			break;
63 
64 		  case 'r':
65 			resource = strdup(optarg);
66 			break;
67 
68 		  case 's':
69 			slp = strtol(optarg, NULL, 0);
70 			break;
71 
72 		  default:
73 			usage(argv[0]);
74 			exit(1);
75 		}
76 	}
77 
78 	r = sm_memstat_open();
79 	r2 = -1;
80 	for (i = 0; i < l; i++)
81 	{
82 		char *mem;
83 
84 		r2 = sm_memstat_get(resource, &v);
85 		if (slp > 0 && i + 1 < l && 0 == r)
86 		{
87 			printf("open=%d, memstat=%d, %s=%ld\n", r, r2,
88 				resource != NULL ? resource : "default-value",
89 				v);
90 			sleep(slp);
91 			if (sz > 0)
92 			{
93 				/*
94 				**  Just allocate some memory to test the
95 				**  values that are returned.
96 				**  Note: this is a memory leak, but that
97 				**  doesn't matter here.
98 				*/
99 
100 				mem = malloc(sz);
101 				if (NULL == mem)
102 					printf("malloc(%d) failed\n", sz);
103 			}
104 		}
105 	}
106 	printf("open=%d, memstat=%d, %s=%ld\n", r, r2,
107 		resource != NULL ? resource : "default-value", v);
108 	r = sm_memstat_close();
109 	return r;
110 }
111