1058561cbSjbeck /* 2*4aac33d3Sjbeck * Copyright (c) 2005-2007 Sendmail, Inc. and its suppliers. 3058561cbSjbeck * All rights reserved. 4058561cbSjbeck * 5058561cbSjbeck * By using this file, you agree to the terms and conditions set 6058561cbSjbeck * forth in the LICENSE file which can be found at the top level of 7058561cbSjbeck * the sendmail distribution. 8058561cbSjbeck */ 9058561cbSjbeck 10058561cbSjbeck #pragma ident "%Z%%M% %I% %E% SMI" 11058561cbSjbeck 12058561cbSjbeck #include <sm/gen.h> 13*4aac33d3Sjbeck SM_IDSTR(id, "@(#)$Id: t-memstat.c,v 1.9 2007/03/14 21:41:09 ca Exp $") 14058561cbSjbeck 15058561cbSjbeck #include <sm/misc.h> 16058561cbSjbeck 17058561cbSjbeck /* 18058561cbSjbeck ** Simple test program for memstat 19058561cbSjbeck */ 20058561cbSjbeck 21058561cbSjbeck #include <stdlib.h> 22058561cbSjbeck #include <unistd.h> 23058561cbSjbeck #include <stdio.h> 24058561cbSjbeck #include <strings.h> 25058561cbSjbeck #include <string.h> 26058561cbSjbeck 27058561cbSjbeck extern char *optarg; 28058561cbSjbeck extern int optind; 29058561cbSjbeck 30*4aac33d3Sjbeck void 31*4aac33d3Sjbeck usage(prg) 32*4aac33d3Sjbeck char *prg; 33*4aac33d3Sjbeck { 34*4aac33d3Sjbeck fprintf(stderr, "usage: %s [options]\n", prg); 35*4aac33d3Sjbeck fprintf(stderr, "options:\n"); 36*4aac33d3Sjbeck fprintf(stderr, "-l n loop n times\n"); 37*4aac33d3Sjbeck fprintf(stderr, "-m n allocate n bytes per iteration\n"); 38*4aac33d3Sjbeck fprintf(stderr, "-r name use name as resource to query\n"); 39*4aac33d3Sjbeck fprintf(stderr, "-s n sleep n seconds per iteration\n"); 40*4aac33d3Sjbeck } 41*4aac33d3Sjbeck 42058561cbSjbeck int 43058561cbSjbeck main(argc, argv) 44058561cbSjbeck int argc; 45058561cbSjbeck char **argv; 46058561cbSjbeck { 47058561cbSjbeck int r, r2, i, l, slp, sz; 48058561cbSjbeck long v; 49058561cbSjbeck char *resource; 50058561cbSjbeck 51058561cbSjbeck l = 1; 52058561cbSjbeck sz = slp = 0; 53058561cbSjbeck resource = NULL; 54058561cbSjbeck while ((r = getopt(argc, argv, "l:m:r:s:")) != -1) 55058561cbSjbeck { 56058561cbSjbeck switch ((char) r) 57058561cbSjbeck { 58058561cbSjbeck case 'l': 59058561cbSjbeck l = strtol(optarg, NULL, 0); 60058561cbSjbeck break; 61058561cbSjbeck 62058561cbSjbeck case 'm': 63058561cbSjbeck sz = strtol(optarg, NULL, 0); 64058561cbSjbeck break; 65058561cbSjbeck 66058561cbSjbeck case 'r': 67058561cbSjbeck resource = strdup(optarg); 68058561cbSjbeck break; 69058561cbSjbeck 70058561cbSjbeck case 's': 71058561cbSjbeck slp = strtol(optarg, NULL, 0); 72058561cbSjbeck break; 73058561cbSjbeck 74058561cbSjbeck default: 75*4aac33d3Sjbeck usage(argv[0]); 76*4aac33d3Sjbeck exit(1); 77058561cbSjbeck } 78058561cbSjbeck } 79058561cbSjbeck 80058561cbSjbeck r = sm_memstat_open(); 81058561cbSjbeck r2 = -1; 82058561cbSjbeck for (i = 0; i < l; i++) 83058561cbSjbeck { 84058561cbSjbeck char *mem; 85058561cbSjbeck 86058561cbSjbeck r2 = sm_memstat_get(resource, &v); 87058561cbSjbeck if (slp > 0 && i + 1 < l && 0 == r) 88058561cbSjbeck { 89058561cbSjbeck printf("open=%d, memstat=%d, %s=%ld\n", r, r2, 90058561cbSjbeck resource != NULL ? resource : "default-value", 91058561cbSjbeck v); 92058561cbSjbeck sleep(slp); 93058561cbSjbeck if (sz > 0) 94058561cbSjbeck { 95058561cbSjbeck /* 96058561cbSjbeck ** Just allocate some memory to test the 97058561cbSjbeck ** values that are returned. 98058561cbSjbeck ** Note: this is a memory leak, but that 99058561cbSjbeck ** doesn't matter here. 100058561cbSjbeck */ 101058561cbSjbeck 102058561cbSjbeck mem = malloc(sz); 103058561cbSjbeck if (NULL == mem) 104058561cbSjbeck printf("malloc(%d) failed\n", sz); 105058561cbSjbeck } 106058561cbSjbeck } 107058561cbSjbeck } 108058561cbSjbeck printf("open=%d, memstat=%d, %s=%ld\n", r, r2, 109058561cbSjbeck resource != NULL ? resource : "default-value", v); 110058561cbSjbeck r = sm_memstat_close(); 111058561cbSjbeck return r; 112058561cbSjbeck } 113