1 /* 2 * Copyright (c) 2005, 2006 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.7 2006/06/28 23:57:59 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 int 31 main(argc, argv) 32 int argc; 33 char **argv; 34 { 35 int r, r2, i, l, slp, sz; 36 long v; 37 char *resource; 38 39 l = 1; 40 sz = slp = 0; 41 resource = NULL; 42 while ((r = getopt(argc, argv, "l:m:r:s:")) != -1) 43 { 44 switch ((char) r) 45 { 46 case 'l': 47 l = strtol(optarg, NULL, 0); 48 break; 49 50 case 'm': 51 sz = strtol(optarg, NULL, 0); 52 break; 53 54 case 'r': 55 resource = strdup(optarg); 56 break; 57 58 case 's': 59 slp = strtol(optarg, NULL, 0); 60 break; 61 62 default: 63 break; 64 } 65 } 66 67 r = sm_memstat_open(); 68 r2 = -1; 69 for (i = 0; i < l; i++) 70 { 71 char *mem; 72 73 r2 = sm_memstat_get(resource, &v); 74 if (slp > 0 && i + 1 < l && 0 == r) 75 { 76 printf("open=%d, memstat=%d, %s=%ld\n", r, r2, 77 resource != NULL ? resource : "default-value", 78 v); 79 sleep(slp); 80 if (sz > 0) 81 { 82 /* 83 ** Just allocate some memory to test the 84 ** values that are returned. 85 ** Note: this is a memory leak, but that 86 ** doesn't matter here. 87 */ 88 89 mem = malloc(sz); 90 if (NULL == mem) 91 printf("malloc(%d) failed\n", sz); 92 } 93 } 94 } 95 printf("open=%d, memstat=%d, %s=%ld\n", r, r2, 96 resource != NULL ? resource : "default-value", v); 97 r = sm_memstat_close(); 98 return r; 99 } 100