[Sysfence-commit] sysfence getstats.c,1.23,1.24
Status: Alpha
Brought to you by:
emes
|
From: Michal S. <em...@us...> - 2006-04-30 11:28:38
|
Update of /cvsroot/sysfence/sysfence In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10505 Modified Files: getstats.c Log Message: + added bugfix from Paul Styen <mammooth /at/ scarlet /dot/ be>: """ I tried to use sysfence 1.5 on SuSe 9.3 but the memory data was not correct. I figured out this was due to the fact that the 2 summary lines in /proc/meminfo were not there (I don't know if that is typical for SuSe or for kernel 2.6). I patched the code so that now it looks for the keywords MemTotal, Buffers, Cached etc. The keywords can be in any order and they are there for Redhat 8.0 (2.4 kernel) and Suse 9.3 (2.6 kernel). """ Index: getstats.c =================================================================== RCS file: /cvsroot/sysfence/sysfence/getstats.c,v retrieving revision 1.23 retrieving revision 1.24 diff -u -d -r1.23 -r1.24 --- getstats.c 5 Jan 2005 13:41:24 -0000 1.23 +++ getstats.c 30 Apr 2006 11:28:25 -0000 1.24 @@ -122,7 +122,7 @@ const char *lafile = "/proc/loadavg"; int lafha; const char *memfile = "/proc/meminfo"; -int memfha; +FILE* memfha; char fbuf[ BUFSIZE ]; @@ -150,50 +150,68 @@ void fetch_mem (sf_database *db) { char *ptr; - long long int vmtot, vmused, vmbuf, vmcache; - - lseek (memfha, 0, SEEK_SET); - read (memfha, (char *) &fbuf, BUFSIZE); - - /* scanning /proc/meminfo for needed values */ - - /* 1. memory total is in second line after 'Mem:\ *' */ - ptr = fto_notspace (fto_space (fto_newline ((char *)&fbuf))); - vmtot = atoll (ptr); - /* 2. used mem is next value */ - ptr = fto_notspace (fto_space (ptr)); - vmused = atoll (ptr); - /* 3. then there are: 'free' field, 'shared' field, - * and what we're looking for - buffers - */ - ptr = fto_notspace (fto_space ( - fto_notspace (fto_space ( - fto_notspace (fto_space (ptr)) - )) - )); - vmbuf = atoll (ptr); - /* 4. cached is next field */ - ptr = fto_notspace (fto_space (ptr)); - vmcache = atoll (ptr); - - /* 5. next line, swap values. first is 'Swap:', then total. */ - ptr = fto_notspace (fto_space (fto_newline (ptr))); - db->swap[VA_TOTAL] = atoll (ptr); - - /* 6. next one is swap-used */ - ptr = fto_notspace (fto_space (ptr)); - db->swap[VA_USED] = atoll (ptr); + const char *items[6] = {"MemTotal", + "MemFree", + "Buffers", + "Cached", + "SwapTotal", + "SwapFree"}; + unsigned int found_items = 0; + long long int swap_total = 0; + long long int mem_buffered = 0; + long long int mem_cached = 0; + long long int mem_free = 0; - /* 7. swap-free */ - ptr = fto_notspace (fto_space (ptr)); - db->swap[VA_FREE] = atoll (ptr); - db->swap[VA_AVAIL] = db->swap[VA_FREE]; - + clearerr(memfha); // clear previous EOF + memfha = freopen(memfile, "r", memfha); // reopen the file + + while (!feof(memfha) && found_items < 6) { + /* scanning /proc/meminfo for needed values */ + unsigned int i; + fgets(fbuf, BUFSIZE, memfha); + for (i = 0; i < 6; i++) { + unsigned int length = strlen(items[i]); + ptr = fto_notspace(fbuf + length + 1); + if (strncmp(fbuf, items[i], length) == 0) { + switch (i) { + case 0 : + db->mem[VA_TOTAL] = atoll (ptr) * 1024; + found_items ++; + break; + case 1 : + mem_free = atoll (ptr) * 1024; + found_items ++; + break; + case 2 : + mem_buffered = atoll (ptr) * 1024; + found_items ++; + break; + case 3 : + mem_cached = atoll (ptr) * 1024; + found_items ++; + break; + case 4 : + swap_total = atoll (ptr) * 1024; + found_items ++; + break; + case 5 : + db->swap[VA_FREE] = atoll (ptr) * 1024; + found_items ++; + break; + } + } + } + } + + if (found_items != 6) { + syslog (LOG_ERR, "fetch_mem() incorrect nr of items found: %d", + found_items ); + } + /* calculate real values, i.e. substract buffers and cache size */ - db->mem[VA_TOTAL] = vmtot; - db->mem[VA_USED] = vmused - (vmbuf + vmcache); - db->mem[VA_FREE] = vmtot - db->mem[VA_USED]; - db->mem[VA_AVAIL] = db->mem[VA_FREE]; + db->mem[VA_FREE] = mem_free + mem_buffered + mem_cached; + db->swap[VA_USED] = swap_total - db->swap[VA_FREE]; + db->mem[VA_USED] = db->mem[VA_TOTAL] - db->mem[VA_FREE]; #ifdef DEBUG syslog (LOG_DEBUG, "fetch_mem(): memfree=%llu memused=%llu swapfree=%llu swapused=%llu\n", @@ -356,8 +374,8 @@ lafha = open (lafile, O_RDONLY); if (lafha < 0) bail_out (EXIT_IO, lafile); - memfha = open (memfile, O_RDONLY); - if (memfha < 0) bail_out (EXIT_IO, memfile); + memfha = fopen(memfile, "r"); + if (memfha == 0) bail_out (EXIT_IO, memfile); } long int get_max_threads () |