From 7b68171f07bd933aecf864692d8b5d92e7475064 Mon Sep 17 00:00:00 2001 From: Tim Bird Date: Wed, 16 Feb 2011 16:12:50 -0600 Subject: [PATCH] Fix bug in pie chart logic I was getting an error with pie charts on some systems with very small memory usage. $ smem -S data.tar --pie=command Traceback (most recent call last): File "/usr/local/bin/smem", line 636, in showpids() File "/usr/local/bin/smem", line 246, in showpids showtable(pt.keys(), fields, columns.split(), options.sort or 'pss') File "/usr/local/bin/smem", line 455, in showtable showpie(l, sort) File "/usr/local/bin/smem", line 498, in showpie while values and (t + values[-1 - c] < (tm * .02) or IndexError: list index out of range I traced it to a bug in showpie, where there's some confused usage of a list index and list popping. In showpie, c is used to index into the values in a while loop that removes entries from the end of a sorted list, and aggregates their values for use in an "other" entry, added to the list before display. Moving (and using) the index is wrong because the list is being chopped from the end as we go. This warps the value of 'other', but under normal circumstances would probably not be noticeable because these items have very small values. However, if several items are popped, and the list is very short, it can result in the list index error above. Also, truncating the values and labels in the subsequent conditional is redundant with the pop in the loop. Below is a patch to fix these problems. -- Tim --- smem | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) --- smem | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/smem b/smem index d9d9448..c09c134 100755 --- a/smem +++ b/smem @@ -521,15 +521,12 @@ def showpie(l, sort): s = sum(values) unused = tm - s t = 0 - c = 0 - while values and (t + values[-1 - c] < (tm * .02) or - values[-1 - c] < (tm * .005)): - c += 1 + while values and (t + values[-1] < (tm * .02) or + values[-1] < (tm * .005)): t += values.pop() labels.pop() - if c > 1: - values = values[:-c] - labels = labels[:-c] + + if t: values.append(t) labels.append('other')