8 from collections import defaultdict
11 # flag definitions based on the kmem.h
38 def __init__(self, name, flags, size, alloc, slabsize, objsize):
40 self._flags = self.f2str(flags)
43 self._slabsize = slabsize
44 self._objsize = objsize
46 def f2str(self, flags):
48 for k in Stat.fdefs.keys():
50 fstring = fstring + Stat.fdefs[k] + '|'
52 fstring = fstring[:-1]
56 def __init__(self, skey="a"):
62 re.compile('(\w+)\s+(\w+)\s+(\w+)\s+(\w+)\s+(\w+)\s+(\w+)\s+');
63 self._stats = defaultdict(list)
65 # Add another stat to the dictionary and re-calculate the totals
72 self._stats[key].append(s)
73 self._size = self._size + s._size
74 self._alloc = self._alloc + s._alloc
76 self._pct = self._alloc * 100 / self._size
80 # Parse the slab info in the procfs
81 # Calculate cumulative stats
82 def slab_update(self):
83 k = [line.strip() for line in open('/proc/spl/kmem/slab')]
86 sys.stderr.write("No SPL slab stats found\n")
94 m = self._regexp.match(s)
96 self.add(Stat(m.group(1), int(m.group(2),16), int(m.group(3)),
97 int(m.group(4)), int(m.group(5)), int(m.group(6))))
99 sys.stderr.write("Error: unexpected input format\n" % s)
102 def show_header(self):
103 sys.stdout.write("\n%25s %20s %15s %15s %15s %15s\n\n" % \
104 ("cache name", "flags", "size", "alloc", "slabsize", "objsize"))
106 # Show up to the number of 'rows' of output sorted in descending order
107 # by the key specified earlier; if rows == 0, all rows are shown
108 def show(self, rows):
112 for k in reversed(sorted(self._stats.keys())):
113 for s in self._stats[k]:
114 sys.stdout.write("%25s %20s %15d %15d %15d %15d\n" % \
115 (s._name, s._flags, s._size, s._alloc, \
116 s._slabsize, s._objsize))
118 if rows != 0 and i > rows:
123 sys.stdout.write("%25s %36d %15d (%d%%)\n\n" % \
124 ("Totals:", self._size, self._alloc, self._pct))
127 cmd = "Usage: splslab.py [-n|--num-rows] number [-s|--sort-by] " + \
128 "[interval] [count]";
129 sys.stderr.write("%s\n" % cmd)
130 sys.stderr.write("\t-h : print help\n")
131 sys.stderr.write("\t-n : --num-rows N : limit output to N top " +
132 "largest slabs (default: all)\n")
133 sys.stderr.write("\t-s : --sort-by key : sort output in descending " +
134 "order by total size (s)\n\t\tor allocated size (a) " +
136 sys.stderr.write("\tinterval : repeat every interval seconds\n")
137 sys.stderr.write("\tcount : output statistics count times and exit\n")
147 signal.signal(signal.SIGINT, signal.SIG_DFL)
150 opts, args = getopt.getopt(
159 except getopt.error as e:
160 sys.stderr.write("Error: %s\n" % e.msg)
165 for opt, arg in opts:
166 if opt in ('-n', '--num-rows'):
169 elif opt in ('-s', '--sort-by'):
170 if arg != "s" and arg != "a":
171 sys.stderr.write("Error: invalid sorting key \"%s\"\n" % arg)
176 elif opt in ('-h', '--help'):
184 interval = int(args[0]) if len(args) else interval
185 count = int(args[1]) if len(args) > 1 else count
189 cs = CumulativeStat(skey)
194 if count and i >= count:
201 if __name__ == '__main__':