File fix-negative-memory.patch of Package conky
110
1
commit 0d449029eaee689da88fcf1d0d572188f4e757d6
2
Author: Shark-Teeth <50938330+CtrlSequence@users.noreply.github.com>
3
Date: Tue Aug 27 13:03:41 2019 -0400
4
5
Fix negative RAM usage (#878)
6
7
This seems to be causing some issues with clobbering memory values, and
8
since there is callback functionality already working, this seems
9
unnecessary.
10
11
* Make all calculations local to function
12
13
I moved from making the assignments and calculations of certain memory
14
values to doing the calculations on local variables and assigning them
15
at the end of the function for update_meminfo().
16
17
This is to keep the info.memX variables from having 'intermediary'
18
values that may give wrong values to other functions if the structure is
19
read from while the function is currently executing.
20
21
As a matter of keeping the variables consistent across function calls, I
22
removed the zeroing out of certain info struct variables so that if they
23
are read from, they'll still report a sane value. Since the only change
24
to the value a direct assignment at the end of the function, they
25
shouldn't need zeroing out in the first place.
26
27
diff --git a/src/linux.cc b/src/linux.cc
28
index 57760fac..c1e0634e 100644
29
--- a/src/linux.cc
30
+++ b/src/linux.cc
31
32
/* unsigned int a; */
33
char buf[256];
34
35
- unsigned long long shmem = 0, sreclaimable = 0;
36
-
37
- info.mem = info.memwithbuffers = info.memmax = info.memdirty = info.swap =
38
- info.swapfree = info.swapmax = info.bufmem = info.buffers = info.cached =
39
- info.memfree = info.memeasyfree = 0;
40
+ /* With multi-threading, calculations that require
41
+ * multple steps to reach a final result can cause havok
42
+ * if the intermediary calculations are directly assigned to the
43
+ * information struct (they may be read by other functions in the meantime).
44
+ * These variables keep the calculations local to the function and finish off
45
+ * the function by assigning the results to the information struct */
46
+ unsigned long long shmem = 0, sreclaimable = 0, curmem = 0, curbufmem = 0,
47
+ cureasyfree = 0;
48
+
49
+ info.memmax = info.memdirty = info.swap = info.swapfree = info.swapmax =
50
+ info.memwithbuffers = info.buffers = info.cached = info.memfree =
51
+ info.memeasyfree = 0;
52
53
if (!(meminfo_fp = open_file("/proc/meminfo", &reported))) { return 0; }
54
55
56
}
57
}
58
59
- info.mem = info.memwithbuffers = info.memmax - info.memfree;
60
- info.memeasyfree = info.memfree;
61
+ curmem = info.memwithbuffers = info.memmax - info.memfree;
62
+ cureasyfree = info.memfree;
63
info.swap = info.swapmax - info.swapfree;
64
65
/* Reclaimable memory: does not include shared memory, which is part of cached
66
67
Note: when shared memory is swapped out, shmem decreases and swapfree
68
decreases - we want this.
69
*/
70
- info.bufmem = (info.cached - shmem) + info.buffers + sreclaimable;
71
+ curbufmem = (info.cached - shmem) + info.buffers + sreclaimable;
72
73
- /* Now (info.mem - info.bufmem) is the *really used* (aka unreclaimable)
74
+ /* Now ('info.mem' - 'info.bufmem') is the *really used* (aka unreclaimable)
75
memory. When this value reaches the size of the physical RAM, and swap is
76
full or non-present, OOM happens. Therefore this is the value users want to
77
monitor, regarding their RAM.
78
*/
79
if (no_buffers.get(*state)) {
80
- info.mem -= info.bufmem;
81
- info.memeasyfree += info.bufmem;
82
+ curmem -= curbufmem;
83
+ cureasyfree += curbufmem;
84
}
85
+
86
+ /* Now that we know that every calculation is finished we can wrap up
87
+ * by assigning the values to the information structure */
88
+ info.mem = curmem;
89
+ info.bufmem = curbufmem;
90
+ info.memeasyfree = cureasyfree;
91
+
92
fclose(meminfo_fp);
93
return 0;
94
}
95
diff --git a/src/top.cc b/src/top.cc
96
index d3c5a7ad..a80e3b7a 100644
97
--- a/src/top.cc
98
+++ b/src/top.cc
99
100
}
101
102
int update_top() {
103
- // XXX: this was a separate callback. and it should be again, as soon as it's
104
- // possible
105
- update_meminfo();
106
-
107
process_find_top(info.cpu, info.memu, info.time
108
#ifdef BUILD_IOSTATS
109
,
110