Skip to contentSkip to conversation

lsmem: fix total online/offline memory calculation

PATCH Applied Gerald Schaefer 2 participants 3 messages Mar 20, 2018 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lsmem: fix total online/offline memory calculation

lsmem currently calculates the total online/offline memory by iterating over all lsmem->blocks. Depending on the lsmem options, there may be only one lsmem->block, because all sysfs memory blocks could be merged into one. In this case, the calculation is wrong, because the individual online/offline state of the sysfs memory blocks is not preserved, but rather lsmem->blocks[0].state is set to the state of the first sysfs memory block, typically MEMORY_STATE_ONLINE (at least on s390).

This means that "Total offline memory" will always be calculated as 0 in such cases, e.g. when using "lsmem --summary", or any options that would merge the table output to one line, like "lsmem -o RANGE":

~# lsmem --summary

Memory block size:         1G
Total online memory:      20G
Total offline memory:      0B

Adding the "-a" option shows the real summary, since there is no block merging going on, and the calculation is therefore correct:

~# lsmem -a --summary

Memory block size:         1G
Total online memory:      16G
Total offline memory:      4G

Fix this by moving the online/offline calculation into the loop that is iterating over all sysfs memory blocks, instead of iterating over potentially merged lsmem->blocks.

---
 sys-utils/lsmem.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
sys-utils/lsmem.c +4 -6
index 3e06e43be74e..517207431c83 100644
@@ -443,6 +443,10 @@ static void read_info(struct lsmem *lsmem)
 
 	for (i = 0; i < lsmem->ndirs; i++) {
 		memory_block_read_attrs(lsmem, lsmem->dirs[i]->d_name, &blk);
+		if (blk.state == MEMORY_STATE_ONLINE)
+			lsmem->mem_online += lsmem->block_size;
+		else
+			lsmem->mem_offline += lsmem->block_size;
 		if (is_mergeable(lsmem, &blk)) {
 			lsmem->blocks[lsmem->nblocks - 1].count++;
 			continue;
@@ -451,12 +455,6 @@ static void read_info(struct lsmem *lsmem)
 		lsmem->blocks = xrealloc(lsmem->blocks, lsmem->nblocks * sizeof(blk));
 		*&lsmem->blocks[lsmem->nblocks - 1] = blk;
 	}
-	for (i = 0; i < lsmem->nblocks; i++) {
-		if (lsmem->blocks[i].state == MEMORY_STATE_ONLINE)
-			lsmem->mem_online += lsmem->block_size * lsmem->blocks[i].count;
-		else
-			lsmem->mem_offline += lsmem->block_size * lsmem->blocks[i].count;
-	}
 }
 
 static int memory_block_filter(const struct dirent *de)
Signature · 1 line
2.13.5
#1 lore raw
Quoted 25 lines · On Tue, 20 Mar 2018 17:17:38 +0100 Gerald Schaefer…

lsmem currently calculates the total online/offline memory by iterating over all lsmem->blocks. Depending on the lsmem options, there may be only one lsmem->block, because all sysfs memory blocks could be merged into one. In this case, the calculation is wrong, because the individual online/offline state of the sysfs memory blocks is not preserved, but rather lsmem->blocks[0].state is set to the state of the first sysfs memory block, typically MEMORY_STATE_ONLINE (at least on s390).

This means that "Total offline memory" will always be calculated as 0 in such cases, e.g. when using "lsmem --summary", or any options that would merge the table output to one line, like "lsmem -o RANGE":

~# lsmem --summary

Memory block size:         1G
Total online memory:      20G
Total offline memory:      0B

Adding the "-a" option shows the real summary, since there is no block merging going on, and the calculation is therefore correct:

~# lsmem -a --summary

Memory block size:         1G
Total online memory:      16G
Total offline memory:      4G
Quoted 5 lines

Fix this by moving the online/offline calculation into the loop that is iterating over all sysfs memory blocks, instead of iterating over potentially merged lsmem->blocks.

Sorry, I mixed that up, this bug was

maintainer Applied
#2 lore raw
Quoted 28 lines · Gerald Schaefer
Quoted 25 lines · On Tue, 20 Mar 2018 17:17:38 +0100 Gerald Schaefer…

lsmem currently calculates the total online/offline memory by iterating over all lsmem->blocks. Depending on the lsmem options, there may be only one lsmem->block, because all sysfs memory blocks could be merged into one. In this case, the calculation is wrong, because the individual online/offline state of the sysfs memory blocks is not preserved, but rather lsmem->blocks[0].state is set to the state of the first sysfs memory block, typically MEMORY_STATE_ONLINE (at least on s390).

This means that "Total offline memory" will always be calculated as 0 in such cases, e.g. when using "lsmem --summary", or any options that would merge the table output to one line, like "lsmem -o RANGE":

~# lsmem --summary

Memory block size:         1G
Total online memory:      20G
Total offline memory:      0B

Adding the "-a" option shows the real summary, since there is no block merging going on, and the calculation is therefore correct:

~# lsmem -a --summary

Memory block size:         1G
Total online memory:      16G
Total offline memory:      4G
Quoted 8 lines
Quoted 5 lines

Fix this by moving the online/offline calculation into the loop that is iterating over all sysfs memory blocks, instead of iterating over potentially merged lsmem->blocks.

Sorry, I mixed that up, this bug was

Applied, thanks!

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com