Skip to content

Commit 0d44902

Browse files
Shark-Teethlasers
authored andcommitted
Fix negative RAM usage (#878)
This seems to be causing some issues with clobbering memory values, and since there is callback functionality already working, this seems unnecessary. * Make all calculations local to function I moved from making the assignments and calculations of certain memory values to doing the calculations on local variables and assigning them at the end of the function for update_meminfo(). This is to keep the info.memX variables from having 'intermediary' values that may give wrong values to other functions if the structure is read from while the function is currently executing. As a matter of keeping the variables consistent across function calls, I removed the zeroing out of certain info struct variables so that if they are read from, they'll still report a sane value. Since the only change to the value a direct assignment at the end of the function, they shouldn't need zeroing out in the first place.
1 parent 84b9ab1 commit 0d44902

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/linux.cc

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,18 @@ int update_meminfo(void) {
179179
/* unsigned int a; */
180180
char buf[256];
181181

182-
unsigned long long shmem = 0, sreclaimable = 0;
183-
184-
info.mem = info.memwithbuffers = info.memmax = info.memdirty = info.swap =
185-
info.swapfree = info.swapmax = info.bufmem = info.buffers = info.cached =
186-
info.memfree = info.memeasyfree = 0;
182+
/* With multi-threading, calculations that require
183+
* multple steps to reach a final result can cause havok
184+
* if the intermediary calculations are directly assigned to the
185+
* information struct (they may be read by other functions in the meantime).
186+
* These variables keep the calculations local to the function and finish off
187+
* the function by assigning the results to the information struct */
188+
unsigned long long shmem = 0, sreclaimable = 0, curmem = 0, curbufmem = 0,
189+
cureasyfree = 0;
190+
191+
info.memmax = info.memdirty = info.swap = info.swapfree = info.swapmax =
192+
info.memwithbuffers = info.buffers = info.cached = info.memfree =
193+
info.memeasyfree = 0;
187194

188195
if (!(meminfo_fp = open_file("/proc/meminfo", &reported))) { return 0; }
189196

@@ -211,26 +218,33 @@ int update_meminfo(void) {
211218
}
212219
}
213220

214-
info.mem = info.memwithbuffers = info.memmax - info.memfree;
215-
info.memeasyfree = info.memfree;
221+
curmem = info.memwithbuffers = info.memmax - info.memfree;
222+
cureasyfree = info.memfree;
216223
info.swap = info.swapmax - info.swapfree;
217224

218225
/* Reclaimable memory: does not include shared memory, which is part of cached
219226
but unreclaimable. Includes the reclaimable part of the Slab cache though.
220227
Note: when shared memory is swapped out, shmem decreases and swapfree
221228
decreases - we want this.
222229
*/
223-
info.bufmem = (info.cached - shmem) + info.buffers + sreclaimable;
230+
curbufmem = (info.cached - shmem) + info.buffers + sreclaimable;
224231

225-
/* Now (info.mem - info.bufmem) is the *really used* (aka unreclaimable)
232+
/* Now ('info.mem' - 'info.bufmem') is the *really used* (aka unreclaimable)
226233
memory. When this value reaches the size of the physical RAM, and swap is
227234
full or non-present, OOM happens. Therefore this is the value users want to
228235
monitor, regarding their RAM.
229236
*/
230237
if (no_buffers.get(*state)) {
231-
info.mem -= info.bufmem;
232-
info.memeasyfree += info.bufmem;
238+
curmem -= curbufmem;
239+
cureasyfree += curbufmem;
233240
}
241+
242+
/* Now that we know that every calculation is finished we can wrap up
243+
* by assigning the values to the information structure */
244+
info.mem = curmem;
245+
info.bufmem = curbufmem;
246+
info.memeasyfree = cureasyfree;
247+
234248
fclose(meminfo_fp);
235249
return 0;
236250
}

src/top.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,6 @@ static void process_find_top(struct process **cpu, struct process **mem,
390390
}
391391

392392
int update_top() {
393-
// XXX: this was a separate callback. and it should be again, as soon as it's
394-
// possible
395-
update_meminfo();
396-
397393
process_find_top(info.cpu, info.memu, info.time
398394
#ifdef BUILD_IOSTATS
399395
,

0 commit comments

Comments
 (0)