1/*
2 * Copyright (c) 2005-2007 Proofpoint, 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>
11SM_IDSTR(id, "@(#)$Id: t-memstat.c,v 1.11 2013-11-22 20:51:43 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
25extern char *optarg;
26extern int optind;
27
28void
29usage(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
40int
41main(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			if (resource == NULL)
67			{
68				fprintf(stderr, "strdup(%s) failed\n",
69					optarg);
70				exit(1);
71			}
72			break;
73
74		  case 's':
75			slp = strtol(optarg, NULL, 0);
76			break;
77
78		  default:
79			usage(argv[0]);
80			exit(1);
81		}
82	}
83
84	r = sm_memstat_open();
85	r2 = -1;
86	for (i = 0; i < l; i++)
87	{
88		char *mem;
89
90		r2 = sm_memstat_get(resource, &v);
91		if (slp > 0 && i + 1 < l && 0 == r)
92		{
93			printf("open=%d, memstat=%d, %s=%ld\n", r, r2,
94				resource != NULL ? resource : "default-value",
95				v);
96			sleep(slp);
97			if (sz > 0)
98			{
99				/*
100				**  Just allocate some memory to test the
101				**  values that are returned.
102				**  Note: this is a memory leak, but that
103				**  doesn't matter here.
104				*/
105
106				mem = malloc(sz);
107				if (NULL == mem)
108					printf("malloc(%d) failed\n", sz);
109			}
110		}
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