xref: /freebsd/contrib/sendmail/libsm/t-memstat.c (revision d0cef73d)
14e4196cbSGregory Neil Shapiro /*
2d0cef73dSGregory Neil Shapiro  * Copyright (c) 2005-2007 Proofpoint, Inc. and its suppliers.
34e4196cbSGregory Neil Shapiro  *      All rights reserved.
44e4196cbSGregory Neil Shapiro  *
54e4196cbSGregory Neil Shapiro  * By using this file, you agree to the terms and conditions set
64e4196cbSGregory Neil Shapiro  * forth in the LICENSE file which can be found at the top level of
74e4196cbSGregory Neil Shapiro  * the sendmail distribution.
84e4196cbSGregory Neil Shapiro  */
94e4196cbSGregory Neil Shapiro 
104e4196cbSGregory Neil Shapiro #include <sm/gen.h>
11d0cef73dSGregory Neil Shapiro SM_IDSTR(id, "@(#)$Id: t-memstat.c,v 1.11 2013-11-22 20:51:43 ca Exp $")
12d0cef73dSGregory Neil Shapiro 
13d0cef73dSGregory Neil Shapiro #include <sm/misc.h>
144e4196cbSGregory Neil Shapiro 
154e4196cbSGregory Neil Shapiro /*
164e4196cbSGregory Neil Shapiro **  Simple test program for memstat
174e4196cbSGregory Neil Shapiro */
184e4196cbSGregory Neil Shapiro 
194e4196cbSGregory Neil Shapiro #include <stdlib.h>
204e4196cbSGregory Neil Shapiro #include <unistd.h>
214e4196cbSGregory Neil Shapiro #include <stdio.h>
224e4196cbSGregory Neil Shapiro #include <strings.h>
23af9557fdSGregory Neil Shapiro #include <string.h>
24af9557fdSGregory Neil Shapiro 
25af9557fdSGregory Neil Shapiro extern char *optarg;
26af9557fdSGregory Neil Shapiro extern int optind;
274e4196cbSGregory Neil Shapiro 
28d0cef73dSGregory Neil Shapiro void
usage(prg)29d0cef73dSGregory Neil Shapiro usage(prg)
30d0cef73dSGregory Neil Shapiro 	char *prg;
31d0cef73dSGregory Neil Shapiro {
32d0cef73dSGregory Neil Shapiro 	fprintf(stderr, "usage: %s [options]\n", prg);
33d0cef73dSGregory Neil Shapiro 	fprintf(stderr, "options:\n");
34d0cef73dSGregory Neil Shapiro 	fprintf(stderr, "-l n    loop n times\n");
35d0cef73dSGregory Neil Shapiro 	fprintf(stderr, "-m n    allocate n bytes per iteration\n");
36d0cef73dSGregory Neil Shapiro 	fprintf(stderr, "-r name use name as resource to query\n");
37d0cef73dSGregory Neil Shapiro 	fprintf(stderr, "-s n    sleep n seconds per iteration\n");
38d0cef73dSGregory Neil Shapiro }
39d0cef73dSGregory Neil Shapiro 
404e4196cbSGregory Neil Shapiro int
main(argc,argv)414e4196cbSGregory Neil Shapiro main(argc, argv)
424e4196cbSGregory Neil Shapiro 	int argc;
434e4196cbSGregory Neil Shapiro 	char **argv;
444e4196cbSGregory Neil Shapiro {
454e4196cbSGregory Neil Shapiro 	int r, r2, i, l, slp, sz;
464e4196cbSGregory Neil Shapiro 	long v;
474e4196cbSGregory Neil Shapiro 	char *resource;
484e4196cbSGregory Neil Shapiro 
494e4196cbSGregory Neil Shapiro 	l = 1;
504e4196cbSGregory Neil Shapiro 	sz = slp = 0;
514e4196cbSGregory Neil Shapiro 	resource = NULL;
524e4196cbSGregory Neil Shapiro 	while ((r = getopt(argc, argv, "l:m:r:s:")) != -1)
534e4196cbSGregory Neil Shapiro 	{
544e4196cbSGregory Neil Shapiro 		switch ((char) r)
554e4196cbSGregory Neil Shapiro 		{
564e4196cbSGregory Neil Shapiro 		  case 'l':
574e4196cbSGregory Neil Shapiro 			l = strtol(optarg, NULL, 0);
584e4196cbSGregory Neil Shapiro 			break;
594e4196cbSGregory Neil Shapiro 
604e4196cbSGregory Neil Shapiro 		  case 'm':
614e4196cbSGregory Neil Shapiro 			sz = strtol(optarg, NULL, 0);
624e4196cbSGregory Neil Shapiro 			break;
634e4196cbSGregory Neil Shapiro 
644e4196cbSGregory Neil Shapiro 		  case 'r':
654e4196cbSGregory Neil Shapiro 			resource = strdup(optarg);
664e4196cbSGregory Neil Shapiro 			if (resource == NULL)
674e4196cbSGregory Neil Shapiro 			{
684e4196cbSGregory Neil Shapiro 				fprintf(stderr, "strdup(%s) failed\n",
694e4196cbSGregory Neil Shapiro 					optarg);
704e4196cbSGregory Neil Shapiro 				exit(1);
714e4196cbSGregory Neil Shapiro 			}
724e4196cbSGregory Neil Shapiro 			break;
73d0cef73dSGregory Neil Shapiro 
74d0cef73dSGregory Neil Shapiro 		  case 's':
754e4196cbSGregory Neil Shapiro 			slp = strtol(optarg, NULL, 0);
764e4196cbSGregory Neil Shapiro 			break;
774e4196cbSGregory Neil Shapiro 
784e4196cbSGregory Neil Shapiro 		  default:
794e4196cbSGregory Neil Shapiro 			usage(argv[0]);
804e4196cbSGregory Neil Shapiro 			exit(1);
814e4196cbSGregory Neil Shapiro 		}
824e4196cbSGregory Neil Shapiro 	}
834e4196cbSGregory Neil Shapiro 
844e4196cbSGregory Neil Shapiro 	r = sm_memstat_open();
854e4196cbSGregory Neil Shapiro 	r2 = -1;
864e4196cbSGregory Neil Shapiro 	for (i = 0; i < l; i++)
874e4196cbSGregory Neil Shapiro 	{
884e4196cbSGregory Neil Shapiro 		char *mem;
894e4196cbSGregory Neil Shapiro 
904e4196cbSGregory Neil Shapiro 		r2 = sm_memstat_get(resource, &v);
914e4196cbSGregory Neil Shapiro 		if (slp > 0 && i + 1 < l && 0 == r)
924e4196cbSGregory Neil Shapiro 		{
934e4196cbSGregory Neil Shapiro 			printf("open=%d, memstat=%d, %s=%ld\n", r, r2,
944e4196cbSGregory Neil Shapiro 				resource != NULL ? resource : "default-value",
954e4196cbSGregory Neil Shapiro 				v);
964e4196cbSGregory Neil Shapiro 			sleep(slp);
974e4196cbSGregory Neil Shapiro 			if (sz > 0)
984e4196cbSGregory Neil Shapiro 			{
994e4196cbSGregory Neil Shapiro 				/*
1004e4196cbSGregory Neil Shapiro 				**  Just allocate some memory to test the
1014e4196cbSGregory Neil Shapiro 				**  values that are returned.
1024e4196cbSGregory Neil Shapiro 				**  Note: this is a memory leak, but that
1034e4196cbSGregory Neil Shapiro 				**  doesn't matter here.
1044e4196cbSGregory Neil Shapiro 				*/
1054e4196cbSGregory Neil Shapiro 
1064e4196cbSGregory Neil Shapiro 				mem = malloc(sz);
1074e4196cbSGregory Neil Shapiro 				if (NULL == mem)
1084e4196cbSGregory Neil Shapiro 					printf("malloc(%d) failed\n", sz);
1094e4196cbSGregory Neil Shapiro 			}
1104e4196cbSGregory Neil Shapiro 		}
111 	}
112 	printf("open=%d, memstat=%d, %s=%ld\n", r, r2,
113 		resource != NULL ? resource : "default-value", v);
114 	r = sm_memstat_close();
115 	return r;
116 }
117