1
0
mirror of https://github.com/gryf/gryf-overlay.git synced 2026-01-08 23:04:16 +01:00
Files
gryf-overlay/x11-misc/bubblemon/files/bubblemon-1.46-correct_mem-swap_values.patch
2010-02-09 21:25:29 +01:00

170 lines
4.4 KiB
Diff

--- bubblemon-dockapp-1.46/sys_linux.c 2003-10-01 04:30:41.000000000 +0200
+++ bubblemon-dockapp-1.46-patched/sys_linux.c 2007-02-18 17:43:46.000000000 +0100
@@ -69,89 +69,92 @@ int system_cpu(void)
return cpuload;
}
+/* code was taken from wmsysmon which is better */
int system_memory(void)
{
- u_int64_t my_mem_used, my_mem_max;
- u_int64_t my_swap_used, my_swap_max;
-#ifdef KERNEL_26
- char *p;
-#endif
-
- static int mem_delay = 0;
- FILE *mem;
- static u_int64_t aa, ab, ac, ad;
-#ifndef KERNEL_26
- static u_int64_t ae, af, ag, ah;
-#endif
- /* put this in permanent storage instead of stack */
- static char shit[2048];
-
- /* we might as well get both swap and memory at the same time.
- * sure beats opening the same file twice */
+ static int mem_delay = 0;
if (mem_delay-- <= 0) {
+ FILE *memfp;
+ char buf[1024];
+ long mem_total = 0;
+ long mem_buffers = 0;
+ long mem_cache = 0;
+ long swap_total = 0;
+ long swap_free = 0;
#ifdef KERNEL_26
- mem = fopen("/proc/meminfo", "r");
- memset(shit, 0, sizeof(shit));
- fread(shit, 2048, 1, mem);
- p = strstr(shit, "MemTotal");
- if (p) {
- sscanf(p, "MemTotal:%Ld", &aa);
- my_mem_max = aa << 10;
-
- p = strstr(p, "Active");
- if (p) {
- sscanf(p, "Active:%Ld", &ab);
- my_mem_used = ab << 10;
-
- p = strstr(p, "SwapTotal");
- if (p) {
- sscanf(p, "SwapTotal:%Ld", &ac);
- my_swap_max = ac << 10;
-
- p = strstr(p, "SwapFree");
- if (p) {
- sscanf(p, "SwapFree:%Ld", &ad);
- my_swap_used = my_swap_max - (ad << 10);
+ static char *p_mem_tot=NULL, *p_mem_free, *p_mem_buffers, *p_mem_cache;
+ static char *p_swap_total, *p_swap_free;
+
+ static long mem_free = 0;
+
+ memfp = fopen("/proc/meminfo", "r");
+ fread(buf, 1024, 1, memfp);
- bm.mem_used = my_mem_used;
- bm.mem_max = my_mem_max;
- bm.swap_used = my_swap_used;
- bm.swap_max = my_swap_max;
- }
+ if (!p_mem_tot)
+ {
+ p_mem_tot = strstr(buf, "MemTotal:" ) + 13;
+ p_mem_free = strstr(buf, "MemFree:" ) + 13;
+ p_mem_buffers = strstr(buf, "Buffers:" ) + 13;
+ p_mem_cache = strstr(buf, "Cached:" ) + 13;
+ p_swap_total = strstr(buf, "SwapTotal:") + 13;
+ p_swap_free = strstr(buf, "SwapFree:" ) + 13;
}
- }
- }
- fclose(mem);
- mem_delay = 25;
+
+
+ sscanf(p_mem_tot, "%ld", &mem_total );
+ sscanf(p_mem_free, "%ld", &mem_free );
+ sscanf(p_mem_buffers, "%ld", &mem_buffers);
+ sscanf(p_mem_cache, "%ld", &mem_cache );
+ sscanf(p_swap_total, "%ld", &swap_total );
+ sscanf(p_swap_free, "%ld", &swap_free );
+
+ fclose(memfp);
+
+ bm.mem_used = (mem_total - mem_free - mem_buffers - mem_cache) * 1024;
+ bm.mem_max = mem_total * 1024;
+ bm.swap_used = (swap_total - swap_free) * 1024;
+ bm.swap_max = swap_total * 1024;
+ mem_delay = 25;
#else
- mem = fopen("/proc/meminfo", "r");
- fgets(shit, 2048, mem);
-
- fscanf(mem, "%*s %Ld %Ld %Ld %Ld %Ld %Ld", &aa, &ab, &ac,
- &ad, &ae, &af);
- fscanf(mem, "%*s %Ld %Ld", &ag, &ah);
- fclose(mem);
- mem_delay = 25;
-
- /* calculate it */
- my_mem_max = aa; /* memory.total; */
- my_swap_max = ag; /* swap.total; */
-
- my_mem_used = ah + ab - af - ae; /* swap.used + memory.used - memory.cached - memory.buffer; */
-
- if (my_mem_used > my_mem_max) {
- my_swap_used = my_mem_used - my_mem_max;
- my_mem_used = my_mem_max;
- } else {
- my_swap_used = 0;
- }
+ int last_mem = 0, last_swap = 0, first = 1;
+ long swap_used = 0;
+ long mem_used = 0;
+ int Mem_l;
+ int Swap_l;
+ int i, ents;
+
+ memfp = fopen("/proc/meminfo", "r");
+ for(i = 0; fgets(buf, 1024, memfp); i++) {
+ if(strstr(buf, "Mem:")) Mem_l = i;
+ else if(strstr(buf, "Swap:")) Swap_l = i;
+ }
+
+ memfp = freopen("/proc/meminfo", "r", memfp);
- bm.mem_used = my_mem_used;
- bm.mem_max = my_mem_max;
- bm.swap_used = my_swap_used;
- bm.swap_max = my_swap_max;
+ for(i = 0, ents = 0; ents < 2 && fgets(buf, 1024, memfp); i++) {
+ if(i == Mem_l) {
+ sscanf(buf, "%*s %ld %ld %*d %*d %ld %ld",
+ &mem_total,
+ &mem_used,
+ &mem_buffers,
+ &mem_cache);
+ ents++;
+ } else if(i == Swap_l) {
+ sscanf(buf, "%*s %ld %ld %ld",
+ &swap_total,
+ &swap_used,
+ &swap_free);
+ ents++;
+ }
+ }
+ fclose(memfp);
+
+ bm.mem_used = mem_used - mem_buffers - mem_cache;
+ bm.mem_max = mem_total;
+ bm.swap_used = swap_used;
+ bm.swap_max = swap_total;
+ mem_delay = 25;
#endif
-
/* memory info changed - update things */
return 1;
}