Check-in [99558b2093]
Overview
SHA1:99558b2093a3a0acf5d05352a0c1e29101a1a4ef
Date: 2014-01-21 00:25:01
User: rkeene
Comment:Refactored code
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2014-01-21
00:31
[eec9ca6159] Updated to attempt to free allocated memory from statgrab (user: rkeene, tags: trunk)
00:25
[99558b2093] Refactored code (user: rkeene, tags: trunk)
00:24
[fc514afcf6] Rebuilt configure script (user: rkeene, tags: trunk)
Changes

Modified freecolor.c from [62178048bb] to [23ac99bb04].

    64     64   #ifndef NO_GETOPT
    65     65   #include <getopt.h>
    66     66   #endif
    67     67   #define BARLEN 35
    68     68   #define HEADERLEN 14
    69     69   #define VERSION "0.8.10"
    70     70   
    71         -
    72     71   extern char *optarg;
    73     72   extern int optind, opterr, optopt;
    74     73   
    75         -void bargraph(float percent, float secondper, char marks[BARLEN+HEADERLEN+1],int usefull) {
    76         -  char percentone[BARLEN+1], percenttwo[BARLEN+1], remain[BARLEN+1];
    77         -  unsigned int numberofmarks, numofmarkstwo, remainnum;
    78         -  numberofmarks=(int) ((float) (BARLEN*(percent/100)));
    79         -  if (!usefull) numofmarkstwo=(int) ((float) (BARLEN*(secondper/100))); else numofmarkstwo=(BARLEN-numberofmarks);
    80         -  remainnum=BARLEN-(numberofmarks+numofmarkstwo);
    81         -  memset(percentone, '#', numberofmarks);
    82         -  memset(percenttwo, '%', numofmarkstwo);
    83         -  memset(remain, '.', remainnum);
    84         -  percentone[numberofmarks]=0;
    85         -  percenttwo[numofmarkstwo]=0;
    86         -  remain[remainnum]=0;
    87         -  snprintf(marks, BARLEN+HEADERLEN+1, "%s\033[1;31m%s\033[1;37m%s", percentone, percenttwo, remain);
    88         -  return;
           74  +struct freecolor_meminfo {
           75  +	unsigned long totalram;  /* Total usable main memory size */
           76  +	unsigned long freeram;   /* Available memory size */
           77  +	unsigned long sharedram; /* Amount of shared memory */
           78  +	unsigned long bufferram; /* Memory used by buffers */
           79  +	unsigned long cachedram; /* Memory used for I/O cache */
           80  +	unsigned long totalswap; /* Total swap space size */
           81  +	unsigned long freeswap;  /* swap space still available */
           82  +};
           83  +
           84  +void bargraph(float percent, float secondper, char marks[BARLEN + HEADERLEN + 1], int usefull) {
           85  +	char percentone[BARLEN + 1], percenttwo[BARLEN + 1], remain[BARLEN + 1];
           86  +	unsigned int numberofmarks, numofmarkstwo, remainnum;
           87  +
           88  +	numberofmarks=(int) ((float) (BARLEN * (percent / 100)));
           89  +
           90  +	if (!usefull) {
           91  +		numofmarkstwo = (int) ((float) (BARLEN * (secondper / 100)));
           92  +	} else {
           93  +		numofmarkstwo = (BARLEN - numberofmarks);
           94  +	}
           95  +
           96  +	remainnum = BARLEN - (numberofmarks + numofmarkstwo);
           97  +	memset(percentone, '#', numberofmarks);
           98  +	memset(percenttwo, '%', numofmarkstwo);
           99  +	memset(remain, '.', remainnum);
          100  +
          101  +	percentone[numberofmarks] = 0;
          102  +	percenttwo[numofmarkstwo] = 0;
          103  +
          104  +	remain[remainnum] = 0;
          105  +
          106  +	snprintf(marks, BARLEN + HEADERLEN + 1, "%s\033[1;31m%s\033[1;37m%s", percentone, percenttwo, remain);
          107  +
          108  +	return;
    89    109   }
    90    110   
    91    111   #ifndef HAVE_LIBSTATGRAB
    92    112   /*
    93    113     Extracted alot from sysinfo.c (part of the procps package?) by
    94    114     Michael K. Johnson <johnsonm@redhat.com>
    95    115   */
    96    116   unsigned long get_meminfo(char searchfor[12]) {
    97         -  FILE *fd;
    98         -  unsigned long value=0, results=0;
    99         -  char buffer[256]; /* Paranoia, reallly big buffer. */
   100         -  char feildname[12];
   101         -  if ((fd=fopen(PROC_MEMINFO, "r"))==NULL) return(0);
   102         -  while (!feof(fd)) {
   103         -    fgets(buffer, sizeof(buffer), fd);
   104         -    sscanf(buffer,"%11s%lu",feildname,&value);
   105         -    if (strcmp(feildname,searchfor)==0) results+=(value*1024);
   106         -  }
   107         -  fclose(fd);
   108         -  return(results);
          117  +	FILE *fd;
          118  +	unsigned long value = 0, results = 0;
          119  +	char buffer[256];
          120  +	char fieldname[12];
          121  +
          122  +	fd = fopen(PROC_MEMINFO, "r");
          123  +	if (fd == NULL) {
          124  +		return(0);
          125  +	}
          126  +
          127  +	while (!feof(fd)) {
          128  +		fgets(buffer, sizeof(buffer), fd);
          129  +		sscanf(buffer,"%11s%lu",fieldname,&value);
          130  +		if (strcmp(fieldname, searchfor) == 0) {
          131  +			results+=(value*1024);
          132  +		}
          133  +	}
          134  +
          135  +	fclose(fd);
          136  +
          137  +	return(results);
          138  +}
          139  +
          140  +int get_all_meminfo(struct freecolor_meminfo *retval) {
          141  +	struct sysinfo sinfo;
          142  +	unsigned long meminfo_cached;
          143  +
          144  +	meminfo_cached = get_meminfo("Cached:");
          145  +	sysinfo(&sinfo);
          146  +
          147  +	retval->totalram  = sinfo.totalram;
          148  +	retval->freeram   = sinfo.freeram;
          149  +	retval->sharedram = sinfo.sharedram;
          150  +	retval->bufferram = sinfo.bufferram;
          151  +	retval->totalswap = sinfo.totalswap;
          152  +	retval->freeswap  = sinfo.freeswap;
          153  +
          154  +	retval->cachedram = meminfo_cached;
          155  +
          156  +	return(0);
          157  +}
          158  +#else
          159  +int get_all_meminfo(struct freecolor_meminfo *retval) {
          160  +	static int init_called = 0;
          161  +	sg_mem_stats *mem_stats;
          162  +	sg_swap_stats *swap_stats;
          163  +
          164  +	if (init_called == 0) {
          165  +		init_called = 1;
          166  +
          167  +		sg_init();
          168  +
          169  +#ifdef HAVE_STATGRAB_DROP_PRIVILEGES
          170  +		/* Drop setuid/setgid privileges. */
          171  +		if (sg_drop_privileges() != 0) {
          172  +			perror("Error. Failed to drop privileges");
          173  +			return(-1);
          174  +		}
          175  +#endif
          176  +	}
          177  +
          178  +	mem_stats = sg_get_mem_stats();
          179  +	swap_stats = sg_get_swap_stats();
          180  +
          181  +	if (mem_stats == NULL || swap_stats == NULL) {
          182  +		return(-1);
          183  +	}
          184  +
          185  +	retval->totalram  = mem_stats->total;
          186  +	retval->freeram   = mem_stats->free;
          187  +	retval->cachedram = mem_stats->cache;
          188  +	retval->sharedram = 0;
          189  +	retval->bufferram = 0;
          190  +	retval->totalswap = swap_stats->total;
          191  +	retval->freeswap  = swap_stats->free;
          192  +
          193  +	return(0);
   109    194   }
   110    195   #endif
   111    196   
   112         -int main(int argc, char *argv[]) {
   113         -  float percentram, percentswap, percentbuffer, percentused, percentfree;
   114         -  float ramfree, ramtotal, swapfree, swaptotal, doloop=0, cachedbuffer, totaltotal;
   115         -  char realbarchart[BARLEN+HEADERLEN+1], swapbarchart[BARLEN+HEADERLEN+1], totalbarchart[BARLEN+HEADERLEN+1];
   116         -  int i, dototals=0, doold=0, linestoup=2;
   117         -  unsigned long meminfo_cached, divisor=1024;
   118         -#ifndef HAVE_LIBSTATGRAB
   119         -  struct sysinfo sinfo;
   120         -
   121         -  meminfo_cached=get_meminfo("Cached:");
   122         -#else
   123         -  sg_mem_stats *mem_stats=NULL;
   124         -  sg_swap_stats *swap_stats=NULL;
   125         -  struct systeminfo_st {
   126         -    unsigned long totalram;  /* Total usable main memory size */
   127         -    unsigned long freeram;   /* Available memory size */
   128         -    unsigned long sharedram; /* Amount of shared memory */
   129         -    unsigned long bufferram; /* Memory used by buffers */
   130         -    unsigned long totalswap; /* Total swap space size */
   131         -    unsigned long freeswap;  /* swap space still available */
   132         -  } sinfo;
   133         -
   134         -  sg_init();
   135         -
   136         -#ifdef HAVE_STATGRAB_DROP_PRIVILEGES
   137         -  /* Drop setuid/setgid privileges. */
   138         -  if (sg_drop_privileges() != 0) {
   139         -    perror("Error. Failed to drop privileges");
   140         -    return 1;
   141         -  }
   142         -#endif
   143         -
   144         -#endif
          197  +int main(int argc, char **argv) {
          198  +	struct freecolor_meminfo sinfo;
          199  +	unsigned long cachedbuffer, divisor;
          200  +	float percentram, percentswap, percentbuffer, percentused, percentfree;
          201  +	float ramfree, ramtotal, swapfree, swaptotal, doloop, totaltotal;
          202  +	char realbarchart[BARLEN  +HEADERLEN + 1], swapbarchart[BARLEN + HEADERLEN + 1], totalbarchart[BARLEN + HEADERLEN + 1];
          203  +	int i, dototals, doold, linestoup;
          204  +	int gam_ret;
          205  +
          206  +	/* Set defaults */
          207  +	divisor = 1024;
          208  +	doold = 0;
          209  +	linestoup = 2;
          210  +	dototals = 0;
          211  +	doloop = 0;
          212  +
          213  +	/* Get arguments */
   145    214   #ifdef NO_GETOPT
   146         -  for (i=1;i<argc;i++) {
   147         -    if (argv[i][0]=='-') {
   148         -      switch(argv[i][1]) {
   149         -        case 's' : 
   150         -           if (argc>(i+1)) { doloop=atof(argv[i+1]); i++; }
   151         -           break;
          215  +	for (i=1;i<argc;i++) {
          216  +		if (argv[i][0] != '-') {
          217  +			break;
          218  +		}
          219  +
          220  +		switch(argv[i][1]) {
          221  +			case 's' : 
          222  +				if (argc > (i+1)) {
          223  +					doloop=atof(argv[i+1]); i++;
          224  +				}
          225  +				break;
   152    226   #else
   153         -  while ((i=getopt(argc, argv, "obkmVs:t")) != -1) {
   154         -  if (i=='?') { printf("usage: %s [-b|-k|-m] [-o] [-t] [-s delay] [-V]\n",argv[0]); return(1); }
   155         -    switch(i) {
   156         -        case 's' : 
   157         -           doloop=atof(optarg);
   158         -           break;
   159         -#endif
   160         -        case 'b' : divisor=1; break;
   161         -        case 'k' : divisor=1024; break;
   162         -        case 'm' : divisor=1048576; break;
   163         -        case 'V' : printf("freecolor version %s\n", VERSION); return(0);
   164         -        case 't' : dototals=1; linestoup++; break;
   165         -        case 'o' : doold=1; linestoup++; break;
   166         -#ifdef NO_GETOPT
   167         -        default  : printf("%s: illegal option -- %c\n",argv[0],argv[i][1]); printf("usage: %s [-b|-k|-m] [-t] [-s delay] [-V]\n",argv[0]); return(1);
   168         -      }
   169         -    }
   170         -  }
   171         -#else
          227  +	while ((i=getopt(argc, argv, "obkmVs:t")) != -1) {
          228  +		switch(i) {
          229  +			case 's' : 
          230  +				doloop=atof(optarg);
          231  +				break;
          232  +#endif /* NO_GETOPT */
          233  +			case 'b' : divisor=1; break;
          234  +			case 'k' : divisor=1024; break;
          235  +			case 'm' : divisor=1048576; break;
          236  +			case 'V' : printf("freecolor version %s\n", VERSION); return(0);
          237  +			case 't' : dototals=1; linestoup++; break;
          238  +			case 'o' : doold=1; linestoup++; break;
          239  +			case '?' :  printf("usage: %s [-b|-k|-m] [-o] [-t] [-s delay] [-V]\n",argv[0]); return(1);
          240  +			default  : printf("%s: illegal option -- %c\n",argv[0],argv[i][1]); printf("usage: %s [-b|-k|-m] [-t] [-s delay] [-V]\n",argv[0]); return(1);
          241  +		}
   172    242     	}
   173         -  }
   174         -#endif
   175         -  while(1) {
   176         -#ifndef HAVE_LIBSTATGRAB
   177         -    sysinfo(&sinfo);
   178         -#else
   179         -  if(((mem_stats=sg_get_mem_stats()) == NULL) || (swap_stats=sg_get_swap_stats()) == NULL) {
   180         -    printf("Couldn't get memory information, exiting.\n");
   181         -    return(-1);
   182         -  }
   183         -  meminfo_cached=mem_stats->cache;
   184         -  sinfo.totalram=mem_stats->total;
   185         -  sinfo.freeram=mem_stats->free;
   186         -  sinfo.sharedram=0;
   187         -  sinfo.bufferram=0;
   188         -  sinfo.totalswap=swap_stats->total;
   189         -  sinfo.freeswap=swap_stats->free;
   190         -#endif
   191         -    ramtotal=sinfo.totalram;
   192         -    cachedbuffer=sinfo.bufferram+meminfo_cached;
   193         -    ramfree=sinfo.freeram;
   194         -    swapfree=sinfo.freeswap;
   195         -    swaptotal=sinfo.totalswap;
   196         -    totaltotal=(sinfo.totalram+sinfo.totalswap);
   197         -    percentram=(float) ((ramfree / ramtotal)*100);
   198         -    percentbuffer=(float) ((cachedbuffer/ramtotal)*100);
   199         -    percentfree=(float) (((sinfo.freeram+sinfo.freeswap+meminfo_cached+sinfo.bufferram)/totaltotal)*100);
   200         -    percentused=(int) (100-((int) percentfree));
   201         -    if (!(swaptotal==0)) { percentswap=(float) ((swapfree/swaptotal)*100); } else { percentswap=0; }
   202         -    bargraph(percentswap,0,swapbarchart,0);
   203         -    bargraph(percentram,percentbuffer,realbarchart,0);
   204         -    bargraph((int) percentfree,(int) percentused,totalbarchart,1);
   205         -    if (!doold) {
   206         -      printf("Physical  : \033[1;30m[\033[1;32m%s\033[1;30m]\033[1;37m %i\033[0;31m%%\033[0m\t(%lu/%lu)\n", realbarchart ,(int) (percentram+percentbuffer), (unsigned long) (ramfree+cachedbuffer)/divisor, (unsigned long) ramtotal/divisor);
   207         -      printf("Swap      : \033[1;30m[\033[1;32m%s\033[1;30m]\033[1;37m %i\033[0;31m%%\033[0m\t(%lu/%lu)\n",swapbarchart ,(int) percentswap,(unsigned long) swapfree/divisor, (unsigned long) swaptotal/divisor);
   208         -      if (dototals) 
   209         -      printf("Total     : \033[1;30m[\033[1;32m%s\033[1;30m]\033[1;37m \033[0m(%lu=%lu+%lu)\n", totalbarchart, (unsigned long) (sinfo.totalram+sinfo.totalswap)/divisor,(unsigned long) ((sinfo.freeram+sinfo.freeswap)/divisor), (unsigned long) ((totaltotal-((sinfo.freeram+sinfo.freeswap+cachedbuffer)))/divisor));
   210         -    } else {
   211         -      printf("             total       used       free     shared    buffers     cached\n");
   212         -      printf("Mem:  %12lu %10lu %10lu %10lu %10lu %10lu\n",(sinfo.totalram/divisor), (sinfo.totalram-sinfo.freeram)/divisor, (sinfo.freeram/divisor), (sinfo.sharedram/divisor), (sinfo.bufferram/divisor), (((unsigned long) cachedbuffer)/divisor)); 
   213         -      printf("Swap: %12lu %10lu %10lu\n",(sinfo.totalswap/divisor), ((sinfo.totalswap-sinfo.freeswap)/divisor), (sinfo.freeswap/divisor));
   214         -      if (dototals) printf("Total: %11lu = (%8lu (used) + %8lu (free))\n",(long) totaltotal/divisor,(((long) totaltotal)-((sinfo.freeram+sinfo.freeswap)))/divisor,(sinfo.freeram+sinfo.freeswap+((unsigned long) cachedbuffer))/divisor);
   215         -    }
   216         -    if (doloop==0) { return(0); } else { usleep(doloop*1000000); printf("\033[%iA",linestoup); }
   217         -  }
   218         -  return(0);
          243  +
          244  +	/* Display information */
          245  +	while(1) {
          246  +		gam_ret = get_all_meminfo(&sinfo);
          247  +		if (gam_ret < 0) {
          248  +			fprintf(stderr, "Error getting memory information.\n");
          249  +
          250  +			return(1);
          251  +		}
          252  +
          253  +		ramtotal  = sinfo.totalram;
          254  +		ramfree   = sinfo.freeram;
          255  +		swapfree  = sinfo.freeswap;
          256  +		swaptotal = sinfo.totalswap;
          257  +
          258  +		totaltotal   = sinfo.totalram + sinfo.totalswap;
          259  +		cachedbuffer = sinfo.bufferram + sinfo.cachedram;
          260  +
          261  +		percentram    = (float) ((ramfree / ramtotal) * 100);
          262  +		percentbuffer = (float) ((cachedbuffer / ramtotal) * 100);
          263  +		percentfree   = (float) (((sinfo.freeram + sinfo.freeswap + cachedbuffer) / totaltotal) * 100);
          264  +		percentused   = (int) (100 - ((int) percentfree));
          265  +
          266  +		if (swaptotal!=0) {
          267  +			percentswap = (float) (( swapfree / swaptotal) * 100);
          268  +		} else {
          269  +			percentswap = 0;
          270  +		}
          271  +
          272  +		bargraph(percentswap, 0, swapbarchart, 0);
          273  +		bargraph(percentram, percentbuffer, realbarchart, 0);
          274  +		bargraph((int) percentfree,(int) percentused, totalbarchart,1);
          275  +
          276  +		if (!doold) {
          277  +			printf("Physical  : \033[1;30m[\033[1;32m%s\033[1;30m]\033[1;37m %i\033[0;31m%%\033[0m\t(%lu/%lu)\n",
          278  +			    realbarchart,
          279  +			    (int) (percentram + percentbuffer),
          280  +			    (unsigned long) (ramfree + cachedbuffer) / divisor,
          281  +			    (unsigned long) ramtotal/divisor
          282  +			);
          283  +
          284  +			printf("Swap      : \033[1;30m[\033[1;32m%s\033[1;30m]\033[1;37m %i\033[0;31m%%\033[0m\t(%lu/%lu)\n",
          285  +			    swapbarchart,
          286  +			     (int) percentswap,
          287  +			     (unsigned long) swapfree / divisor,
          288  +			     (unsigned long) swaptotal/divisor
          289  +			);
          290  +
          291  +			if (dototals) {
          292  +				printf("Total     : \033[1;30m[\033[1;32m%s\033[1;30m]\033[1;37m \033[0m(%lu=%lu+%lu)\n",
          293  +				    totalbarchart,
          294  +				    (unsigned long) (sinfo.totalram + sinfo.totalswap) / divisor,
          295  +				    (unsigned long) ((sinfo.freeram + sinfo.freeswap) / divisor),
          296  +				    (unsigned long) ((totaltotal - ((sinfo.freeram + sinfo.freeswap + cachedbuffer))) / divisor)
          297  +				);
          298  +			}
          299  +		} else {
          300  +			printf("             total       used       free     shared    buffers     cached\n");
          301  +			printf("Mem:  %12lu %10lu %10lu %10lu %10lu %10lu\n",
          302  +			    (unsigned long) (sinfo.totalram / divisor),
          303  +			    (unsigned long) ((sinfo.totalram - sinfo.freeram) / divisor),
          304  +			    (unsigned long) (sinfo.freeram / divisor),
          305  +			    (unsigned long) (sinfo.sharedram / divisor),
          306  +			    (unsigned long) (sinfo.bufferram / divisor),
          307  +			    (unsigned long) (cachedbuffer / divisor)
          308  +			); 
          309  +
          310  +			printf("Swap: %12lu %10lu %10lu\n",
          311  +			    (unsigned long) (sinfo.totalswap / divisor),
          312  +			    (unsigned long) ((sinfo.totalswap - sinfo.freeswap) / divisor),
          313  +			    (unsigned long) (sinfo.freeswap / divisor)
          314  +			);
          315  +
          316  +			if (dototals) {
          317  +				printf("Total: %11lu = (%8lu (used) + %8lu (free))\n",
          318  +				    (unsigned long) (totaltotal / divisor),
          319  +				    (((long) totaltotal) - ((sinfo.freeram+sinfo.freeswap))) / divisor,
          320  +				    (sinfo.freeram+sinfo.freeswap+((unsigned long) cachedbuffer)) / divisor
          321  +				);
          322  +			}
          323  +		}
          324  +
          325  +		if (doloop==0) {
          326  +			break;
          327  +		}
          328  +
          329  +		usleep(doloop*1000000);
          330  +		printf("\033[%iA",linestoup);
          331  +	}
          332  +
          333  +	return(0);
   219    334   }