3 # Print out statistics for all cached dmu buffers. This information
4 # is available through the dbufs kstat and may be post-processed as
5 # needed by the script.
9 # The contents of this file are subject to the terms of the
10 # Common Development and Distribution License, Version 1.0 only
11 # (the "License"). You may not use this file except in compliance
14 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
15 # or http://www.opensolaris.org/os/licensing.
16 # See the License for the specific language governing permissions
17 # and limitations under the License.
19 # When distributing Covered Code, include this CDDL HEADER in each
20 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
21 # If applicable, add the following below this CDDL HEADER, with the
22 # fields enclosed by brackets "[]" replaced with your own identifying
23 # information: Portions Copyright [yyyy] [name of copyright owner]
27 # Copyright (C) 2013 Lawrence Livermore National Security, LLC.
28 # Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
36 bhdr = ["pool", "objset", "object", "level", "blkid", "offset", "dbsize"]
37 bxhdr = ["pool", "objset", "object", "level", "blkid", "offset", "dbsize",
38 "meta", "state", "dbholds", "dbc", "list", "atype", "flags",
39 "count", "asize", "access", "mru", "gmru", "mfu", "gmfu", "l2",
40 "l2_dattr", "l2_asize", "l2_comp", "aholds", "dtype", "btype",
41 "data_bs", "meta_bs", "bsize", "lvls", "dholds", "blocks", "dsize"]
42 bincompat = ["cached", "direct", "indirect", "bonus", "spill"]
44 dhdr = ["pool", "objset", "object", "dtype", "cached"]
45 dxhdr = ["pool", "objset", "object", "dtype", "btype", "data_bs", "meta_bs",
46 "bsize", "lvls", "dholds", "blocks", "dsize", "cached", "direct",
47 "indirect", "bonus", "spill"]
48 dincompat = ["level", "blkid", "offset", "dbsize", "meta", "state", "dbholds",
49 "dbc", "list", "atype", "flags", "count", "asize", "access",
50 "mru", "gmru", "mfu", "gmfu", "l2", "l2_dattr", "l2_asize",
53 thdr = ["pool", "objset", "dtype", "cached"]
54 txhdr = ["pool", "objset", "dtype", "cached", "direct", "indirect",
56 tincompat = ["object", "level", "blkid", "offset", "dbsize", "meta", "state",
57 "dbc", "dbholds", "list", "atype", "flags", "count", "asize",
58 "access", "mru", "gmru", "mfu", "gmfu", "l2", "l2_dattr",
59 "l2_asize", "l2_comp", "aholds", "btype", "data_bs", "meta_bs",
60 "bsize", "lvls", "dholds", "blocks", "dsize"]
63 # hdr: [size, scale, description]
64 "pool": [15, -1, "pool name"],
65 "objset": [6, -1, "dataset identification number"],
66 "object": [10, -1, "object number"],
67 "level": [5, -1, "indirection level of buffer"],
68 "blkid": [8, -1, "block number of buffer"],
69 "offset": [12, 1024, "offset in object of buffer"],
70 "dbsize": [7, 1024, "size of buffer"],
71 "meta": [4, -1, "is this buffer metadata?"],
72 "state": [5, -1, "state of buffer (read, cached, etc)"],
73 "dbholds": [7, 1000, "number of holds on buffer"],
74 "dbc": [3, -1, "in dbuf cache"],
75 "list": [4, -1, "which ARC list contains this buffer"],
76 "atype": [7, -1, "ARC header type (data or metadata)"],
77 "flags": [9, -1, "ARC read flags"],
78 "count": [5, -1, "ARC data count"],
79 "asize": [7, 1024, "size of this ARC buffer"],
80 "access": [10, -1, "time this ARC buffer was last accessed"],
81 "mru": [5, 1000, "hits while on the ARC's MRU list"],
82 "gmru": [5, 1000, "hits while on the ARC's MRU ghost list"],
83 "mfu": [5, 1000, "hits while on the ARC's MFU list"],
84 "gmfu": [5, 1000, "hits while on the ARC's MFU ghost list"],
85 "l2": [5, 1000, "hits while on the L2ARC"],
86 "l2_dattr": [8, -1, "L2ARC disk address/offset"],
87 "l2_asize": [8, 1024, "L2ARC alloc'd size (depending on compression)"],
88 "l2_comp": [21, -1, "L2ARC compression algorithm for buffer"],
89 "aholds": [6, 1000, "number of holds on this ARC buffer"],
90 "dtype": [27, -1, "dnode type"],
91 "btype": [27, -1, "bonus buffer type"],
92 "data_bs": [7, 1024, "data block size"],
93 "meta_bs": [7, 1024, "metadata block size"],
94 "bsize": [6, 1024, "bonus buffer size"],
95 "lvls": [6, -1, "number of indirection levels"],
96 "dholds": [6, 1000, "number of holds on dnode"],
97 "blocks": [8, 1000, "number of allocated blocks"],
98 "dsize": [12, 1024, "size of dnode"],
99 "cached": [6, 1024, "bytes cached for all blocks"],
100 "direct": [6, 1024, "bytes cached for direct blocks"],
101 "indirect": [8, 1024, "bytes cached for indirect blocks"],
102 "bonus": [5, 1024, "bytes cached for bonus buffer"],
103 "spill": [5, 1024, "bytes cached for spill block"],
108 sep = " " # Default separator is 2 spaces
109 cmd = ("Usage: dbufstat.py [-bdhnrtvx] [-i file] [-f fields] [-o file] "
110 "[-s string] [-F filter]\n")
114 def print_incompat_helper(incompat):
116 for key in sorted(incompat):
118 sys.stderr.write("\t")
120 sys.stderr.write(",\n\t")
123 sys.stderr.write(", ")
125 sys.stderr.write("%s" % key)
128 sys.stderr.write("\n\n")
131 def detailed_usage():
132 sys.stderr.write("%s\n" % cmd)
134 sys.stderr.write("Field definitions incompatible with '-b' option:\n")
135 print_incompat_helper(bincompat)
137 sys.stderr.write("Field definitions incompatible with '-d' option:\n")
138 print_incompat_helper(dincompat)
140 sys.stderr.write("Field definitions incompatible with '-t' option:\n")
141 print_incompat_helper(tincompat)
143 sys.stderr.write("Field definitions are as follows:\n")
144 for key in sorted(cols.keys()):
145 sys.stderr.write("%11s : %s\n" % (key, cols[key][2]))
146 sys.stderr.write("\n")
152 sys.stderr.write("%s\n" % cmd)
153 sys.stderr.write("\t -b : Print table of information for each dbuf\n")
154 sys.stderr.write("\t -d : Print table of information for each dnode\n")
155 sys.stderr.write("\t -h : Print this help message\n")
156 sys.stderr.write("\t -n : Exclude header from output\n")
157 sys.stderr.write("\t -r : Print raw values\n")
158 sys.stderr.write("\t -t : Print table of information for each dnode type"
160 sys.stderr.write("\t -v : List all possible field headers and definitions"
162 sys.stderr.write("\t -x : Print extended stats\n")
163 sys.stderr.write("\t -i : Redirect input from the specified file\n")
164 sys.stderr.write("\t -f : Specify specific fields to print (see -v)\n")
165 sys.stderr.write("\t -o : Redirect output to the specified file\n")
166 sys.stderr.write("\t -s : Override default field separator with custom "
167 "character or string\n")
168 sys.stderr.write("\t -F : Filter output by value or regex\n")
169 sys.stderr.write("\nExamples:\n")
170 sys.stderr.write("\tdbufstat.py -d -o /tmp/d.log\n")
171 sys.stderr.write("\tdbufstat.py -t -s \",\" -o /tmp/t.log\n")
172 sys.stderr.write("\tdbufstat.py -v\n")
173 sys.stderr.write("\tdbufstat.py -d -f pool,object,objset,dsize,cached\n")
174 sys.stderr.write("\tdbufstat.py -bx -F dbc=1,objset=54,pool=testpool\n")
175 sys.stderr.write("\n")
180 def prettynum(sz, scale, num=0):
183 suffix = [' ', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']
187 if raw or scale == -1:
188 return "%*s" % (sz, num)
190 # Rounding error, return 0
194 while num > scale and index < 5:
200 return "%*d" % (sz, num)
202 if (save / scale) < 10:
203 return "%*.1f%s" % (sz - 1, num, suffix[index])
205 return "%*d%s" % (sz - 1, num, suffix[index])
214 sys.stdout.write("%s%s" % (
215 prettynum(cols[col][0], cols[col][1], v[col]), sep))
216 sys.stdout.write("\n")
218 if e.errno == errno.EPIPE:
228 sys.stdout.write("%*s%s" % (cols[col][0], col, sep))
229 sys.stdout.write("\n")
231 if e.errno == errno.EPIPE:
235 def get_typestring(t):
239 "DMU_OT_OBJECT_DIRECTORY",
240 "DMU_OT_OBJECT_ARRAY",
241 "DMU_OT_PACKED_NVLIST",
242 "DMU_OT_PACKED_NVLIST_SIZE",
246 "DMU_OT_SPACE_MAP_HEADER",
255 "DMU_OT_DSL_DIR_CHILD_MAP",
256 "DMU_OT_DSL_DS_SNAP_MAP",
258 "DMU_OT_DSL_DATASET",
262 "DMU_OT_PLAIN_FILE_CONTENTS",
263 "DMU_OT_DIRECTORY_CONTENTS",
264 "DMU_OT_MASTER_NODE",
265 "DMU_OT_UNLINKED_SET",
269 # other; for testing only!
270 "DMU_OT_PLAIN_OTHER",
271 "DMU_OT_UINT64_OTHER",
275 "DMU_OT_SPA_HISTORY",
276 "DMU_OT_SPA_HISTORY_OFFSETS",
283 "DMU_OT_NEXT_CLONES",
285 "DMU_OT_USERGROUP_USED",
286 "DMU_OT_USERGROUP_QUOTA",
291 "DMU_OT_SA_MASTER_NODE",
292 "DMU_OT_SA_ATTR_REGISTRATION",
293 "DMU_OT_SA_ATTR_LAYOUTS",
297 "DMU_OT_DEADLIST_HDR",
299 "DMU_OT_BPOBJ_SUBOBJ"]
301 0x80: "DMU_OTN_UINT8_DATA",
302 0xc0: "DMU_OTN_UINT8_METADATA",
303 0x81: "DMU_OTN_UINT16_DATA",
304 0xc1: "DMU_OTN_UINT16_METADATA",
305 0x82: "DMU_OTN_UINT32_DATA",
306 0xc2: "DMU_OTN_UINT32_METADATA",
307 0x83: "DMU_OTN_UINT64_DATA",
308 0xc3: "DMU_OTN_UINT64_METADATA",
309 0x84: "DMU_OTN_ZAP_DATA",
310 0xc4: "DMU_OTN_ZAP_METADATA",
311 0xa0: "DMU_OTN_UINT8_ENC_DATA",
312 0xe0: "DMU_OTN_UINT8_ENC_METADATA",
313 0xa1: "DMU_OTN_UINT16_ENC_DATA",
314 0xe1: "DMU_OTN_UINT16_ENC_METADATA",
315 0xa2: "DMU_OTN_UINT32_ENC_DATA",
316 0xe2: "DMU_OTN_UINT32_ENC_METADATA",
317 0xa3: "DMU_OTN_UINT64_ENC_DATA",
318 0xe3: "DMU_OTN_UINT64_ENC_METADATA",
319 0xa4: "DMU_OTN_ZAP_ENC_DATA",
320 0xe4: "DMU_OTN_ZAP_ENC_METADATA"}
322 # If "-rr" option is used, don't convert to string representation
327 if t < len(ot_strings):
330 return otn_strings[t]
331 except (IndexError, KeyError):
335 def get_compstring(c):
336 comp_strings = ["ZIO_COMPRESS_INHERIT", "ZIO_COMPRESS_ON",
337 "ZIO_COMPRESS_OFF", "ZIO_COMPRESS_LZJB",
338 "ZIO_COMPRESS_EMPTY", "ZIO_COMPRESS_GZIP_1",
339 "ZIO_COMPRESS_GZIP_2", "ZIO_COMPRESS_GZIP_3",
340 "ZIO_COMPRESS_GZIP_4", "ZIO_COMPRESS_GZIP_5",
341 "ZIO_COMPRESS_GZIP_6", "ZIO_COMPRESS_GZIP_7",
342 "ZIO_COMPRESS_GZIP_8", "ZIO_COMPRESS_GZIP_9",
343 "ZIO_COMPRESS_ZLE", "ZIO_COMPRESS_LZ4",
344 "ZIO_COMPRESS_FUNCTION"]
346 # If "-rr" option is used, don't convert to string representation
351 return comp_strings[c]
356 def parse_line(line, labels):
362 # These are "special" fields computed in the update_dict
363 # function, prevent KeyError exception on labels[col] for these.
364 if col not in ['bonus', 'cached', 'direct', 'indirect', 'spill']:
365 val = line[labels[col]]
367 if col in ['pool', 'flags']:
369 elif col in ['dtype', 'btype']:
370 new[col] = get_typestring(int(val))
371 elif col in ['l2_comp']:
372 new[col] = get_compstring(int(val))
379 def update_dict(d, k, line, labels):
380 pool = line[labels['pool']]
381 objset = line[labels['objset']]
382 key = line[labels[k]]
384 dbsize = int(line[labels['dbsize']])
385 blkid = int(line[labels['blkid']])
386 level = int(line[labels['level']])
391 if objset not in d[pool]:
392 d[pool][objset] = dict()
394 if key not in d[pool][objset]:
395 d[pool][objset][key] = parse_line(line, labels)
396 d[pool][objset][key]['bonus'] = 0
397 d[pool][objset][key]['cached'] = 0
398 d[pool][objset][key]['direct'] = 0
399 d[pool][objset][key]['indirect'] = 0
400 d[pool][objset][key]['spill'] = 0
402 d[pool][objset][key]['cached'] += dbsize
405 d[pool][objset][key]['bonus'] += dbsize
407 d[pool][objset][key]['spill'] += dbsize
410 d[pool][objset][key]['direct'] += dbsize
412 d[pool][objset][key]['indirect'] += dbsize
417 def skip_line(vals, filters):
419 Determines if a line should be skipped during printing
420 based on a set of filters
422 if len(filters) == 0:
427 val = prettynum(cols[key][0], cols[key][1], vals[key]).strip()
428 # we want a full match here
429 if re.match("(?:" + filters[key] + r")\Z", val) is None:
435 def print_dict(d, filters, noheader):
438 for pool in list(d.keys()):
439 for objset in list(d[pool].keys()):
440 for v in list(d[pool][objset].values()):
441 if not skip_line(v, filters):
445 def dnodes_build_dict(filehandle):
449 # First 3 lines are header information, skip the first two
453 # The third line contains the labels and index locations
454 for i, v in enumerate(next(filehandle).split()):
457 # The rest of the file is buffer information
458 for line in filehandle:
459 update_dict(dnodes, 'object', line.split(), labels)
464 def types_build_dict(filehandle):
468 # First 3 lines are header information, skip the first two
472 # The third line contains the labels and index locations
473 for i, v in enumerate(next(filehandle).split()):
476 # The rest of the file is buffer information
477 for line in filehandle:
478 update_dict(types, 'dtype', line.split(), labels)
483 def buffers_print_all(filehandle, filters, noheader):
486 # First 3 lines are header information, skip the first two
490 # The third line contains the labels and index locations
491 for i, v in enumerate(next(filehandle).split()):
497 # The rest of the file is buffer information
498 for line in filehandle:
499 vals = parse_line(line.split(), labels)
500 if not skip_line(vals, filters):
522 opts, args = getopt.getopt(
524 "bdf:hi:o:rs:tvxF:n",
543 for opt, arg in opts:
544 if opt in ('-b', '--buffers'):
546 if opt in ('-d', '--dnodes'):
548 if opt in ('-f', '--columns'):
550 if opt in ('-h', '--help'):
552 if opt in ('-i', '--infile'):
554 if opt in ('-o', '--outfile'):
556 if opt in ('-r', '--raw'):
558 if opt in ('-s', '--separator'):
560 if opt in ('-t', '--types'):
562 if opt in ('-v', '--verbose'):
564 if opt in ('-x', '--extended'):
566 if opt in ('-n', '--noheader'):
568 if opt in ('-F', '--filter'):
569 fils = [x.strip() for x in arg.split(",")]
572 f = [x.strip() for x in fil.split("=")]
575 sys.stderr.write("Invalid filter '%s'.\n" % fil)
579 sys.stderr.write("Invalid field '%s' in filter.\n" % f[0])
583 sys.stderr.write("Field '%s' specified multiple times in "
588 re.compile("(?:" + f[1] + r")\Z")
590 sys.stderr.write("Invalid regex for field '%s' in "
596 if hflag or (xflag and desired_cols):
602 # Ensure at most only one of b, d, or t flags are set
603 if (bflag and dflag) or (bflag and tflag) or (dflag and tflag):
607 hdr = bxhdr if xflag else bhdr
609 hdr = txhdr if xflag else thdr
610 else: # Even if dflag is False, it's the default if none set
612 hdr = dxhdr if xflag else dhdr
615 hdr = desired_cols.split(",")
622 elif ((bflag and bincompat and ele in bincompat) or
623 (dflag and dincompat and ele in dincompat) or
624 (tflag and tincompat and ele in tincompat)):
628 sys.stderr.write("Invalid column definition! -- %s\n" % invalid)
631 if len(incompat) > 0:
632 sys.stderr.write("Incompatible field specified! -- %s\n" %
638 tmp = open(ofile, "w")
642 sys.stderr.write("Cannot open %s for writing\n" % ofile)
646 ifile = '/proc/spl/kstat/zfs/dbufs'
650 tmp = open(ifile, "r")
653 sys.stderr.write("Cannot open %s for reading\n" % ifile)
657 buffers_print_all(sys.stdin, filters, nflag)
660 print_dict(dnodes_build_dict(sys.stdin), filters, nflag)
663 print_dict(types_build_dict(sys.stdin), filters, nflag)
666 if __name__ == '__main__':