1157001Sgshapiro/* 2261363Sgshapiro * Copyright (c) 2005-2007 Proofpoint, Inc. and its suppliers. 3157001Sgshapiro * All rights reserved. 4157001Sgshapiro * 5157001Sgshapiro * By using this file, you agree to the terms and conditions set 6157001Sgshapiro * forth in the LICENSE file which can be found at the top level of 7157001Sgshapiro * the sendmail distribution. 8157001Sgshapiro */ 9157001Sgshapiro 10157001Sgshapiro#include <sm/gen.h> 11266692SgshapiroSM_IDSTR(id, "@(#)$Id: t-memstat.c,v 1.11 2013-11-22 20:51:43 ca Exp $") 12157001Sgshapiro 13168515Sgshapiro#include <sm/misc.h> 14168515Sgshapiro 15157001Sgshapiro/* 16159609Sgshapiro** Simple test program for memstat 17157001Sgshapiro*/ 18157001Sgshapiro 19157001Sgshapiro#include <stdlib.h> 20157001Sgshapiro#include <unistd.h> 21157001Sgshapiro#include <stdio.h> 22157001Sgshapiro#include <strings.h> 23159609Sgshapiro#include <string.h> 24157001Sgshapiro 25159609Sgshapiroextern char *optarg; 26159609Sgshapiroextern int optind; 27159609Sgshapiro 28168515Sgshapirovoid 29168515Sgshapirousage(prg) 30168515Sgshapiro char *prg; 31168515Sgshapiro{ 32168515Sgshapiro fprintf(stderr, "usage: %s [options]\n", prg); 33168515Sgshapiro fprintf(stderr, "options:\n"); 34168515Sgshapiro fprintf(stderr, "-l n loop n times\n"); 35168515Sgshapiro fprintf(stderr, "-m n allocate n bytes per iteration\n"); 36168515Sgshapiro fprintf(stderr, "-r name use name as resource to query\n"); 37168515Sgshapiro fprintf(stderr, "-s n sleep n seconds per iteration\n"); 38168515Sgshapiro} 39168515Sgshapiro 40157001Sgshapiroint 41157001Sgshapiromain(argc, argv) 42157001Sgshapiro int argc; 43157001Sgshapiro char **argv; 44157001Sgshapiro{ 45157001Sgshapiro int r, r2, i, l, slp, sz; 46157001Sgshapiro long v; 47157001Sgshapiro char *resource; 48157001Sgshapiro 49157001Sgshapiro l = 1; 50157001Sgshapiro sz = slp = 0; 51157001Sgshapiro resource = NULL; 52157001Sgshapiro while ((r = getopt(argc, argv, "l:m:r:s:")) != -1) 53157001Sgshapiro { 54157001Sgshapiro switch ((char) r) 55157001Sgshapiro { 56157001Sgshapiro case 'l': 57157001Sgshapiro l = strtol(optarg, NULL, 0); 58157001Sgshapiro break; 59157001Sgshapiro 60157001Sgshapiro case 'm': 61157001Sgshapiro sz = strtol(optarg, NULL, 0); 62157001Sgshapiro break; 63157001Sgshapiro 64157001Sgshapiro case 'r': 65157001Sgshapiro resource = strdup(optarg); 66261363Sgshapiro if (resource == NULL) 67261363Sgshapiro { 68261363Sgshapiro fprintf(stderr, "strdup(%s) failed\n", 69261363Sgshapiro optarg); 70261363Sgshapiro exit(1); 71261363Sgshapiro } 72157001Sgshapiro break; 73157001Sgshapiro 74157001Sgshapiro case 's': 75157001Sgshapiro slp = strtol(optarg, NULL, 0); 76157001Sgshapiro break; 77157001Sgshapiro 78157001Sgshapiro default: 79168515Sgshapiro usage(argv[0]); 80168515Sgshapiro exit(1); 81157001Sgshapiro } 82157001Sgshapiro } 83157001Sgshapiro 84157001Sgshapiro r = sm_memstat_open(); 85157001Sgshapiro r2 = -1; 86157001Sgshapiro for (i = 0; i < l; i++) 87157001Sgshapiro { 88157001Sgshapiro char *mem; 89157001Sgshapiro 90157001Sgshapiro r2 = sm_memstat_get(resource, &v); 91157001Sgshapiro if (slp > 0 && i + 1 < l && 0 == r) 92157001Sgshapiro { 93157001Sgshapiro printf("open=%d, memstat=%d, %s=%ld\n", r, r2, 94157001Sgshapiro resource != NULL ? resource : "default-value", 95157001Sgshapiro v); 96157001Sgshapiro sleep(slp); 97157001Sgshapiro if (sz > 0) 98157001Sgshapiro { 99157001Sgshapiro /* 100157001Sgshapiro ** Just allocate some memory to test the 101157001Sgshapiro ** values that are returned. 102157001Sgshapiro ** Note: this is a memory leak, but that 103157001Sgshapiro ** doesn't matter here. 104157001Sgshapiro */ 105157001Sgshapiro 106157001Sgshapiro mem = malloc(sz); 107157001Sgshapiro if (NULL == mem) 108157001Sgshapiro printf("malloc(%d) failed\n", sz); 109157001Sgshapiro } 110157001Sgshapiro } 111157001Sgshapiro } 112157001Sgshapiro printf("open=%d, memstat=%d, %s=%ld\n", r, r2, 113157001Sgshapiro resource != NULL ? resource : "default-value", v); 114157001Sgshapiro r = sm_memstat_close(); 115157001Sgshapiro return r; 116157001Sgshapiro} 117