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