1 #!/usr/bin/env @PYTHON_SHEBANG@
3 # Print out ZFS ARC Statistics exported via kstat(1)
4 # For a definition of fields, or usage, use arcstat -v
6 # This script was originally a fork of the original arcstat.pl (0.1)
7 # by Neelakanth Nadgir, originally published on his Sun blog on
9 # http://blogs.sun.com/realneel/entry/zfs_arc_statistics
11 # A new version aimed to improve upon the original by adding features
12 # and fixing bugs as needed. This version was maintained by Mike
13 # Harsch and was hosted in a public open source repository:
14 # http://github.com/mharsch/arcstat
16 # but has since moved to the illumos-gate repository.
18 # This Python port was written by John Hixson for FreeNAS, introduced
20 # https://github.com/freenas/freenas
22 # and has been improved by many people since.
26 # The contents of this file are subject to the terms of the
27 # Common Development and Distribution License, Version 1.0 only
28 # (the "License"). You may not use this file except in compliance
31 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 # or http://www.opensolaris.org/os/licensing.
33 # See the License for the specific language governing permissions
34 # and limitations under the License.
36 # When distributing Covered Code, include this CDDL HEADER in each
37 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 # If applicable, add the following below this CDDL HEADER, with the
39 # fields enclosed by brackets "[]" replaced with your own identifying
40 # information: Portions Copyright [yyyy] [name of copyright owner]
45 # Fields have a fixed width. Every interval, we fill the "v"
46 # hash with its corresponding value (v[field]=value) using calculate().
47 # @hdr is the array of fields that needs to be printed, so we
48 # just iterate over this array and print the values using our pretty printer.
50 # This script must remain compatible with Python 2.6+ and Python 3.4+.
59 from signal import signal, SIGINT, SIGWINCH, SIG_DFL
63 # HDR: [Size, Scale, Description]
64 "time": [8, -1, "Time"],
65 "hits": [4, 1000, "ARC reads per second"],
66 "miss": [4, 1000, "ARC misses per second"],
67 "read": [4, 1000, "Total ARC accesses per second"],
68 "hit%": [4, 100, "ARC hit percentage"],
69 "miss%": [5, 100, "ARC miss percentage"],
70 "dhit": [4, 1000, "Demand hits per second"],
71 "dmis": [4, 1000, "Demand misses per second"],
72 "dh%": [3, 100, "Demand hit percentage"],
73 "dm%": [3, 100, "Demand miss percentage"],
74 "phit": [4, 1000, "Prefetch hits per second"],
75 "pmis": [4, 1000, "Prefetch misses per second"],
76 "ph%": [3, 100, "Prefetch hits percentage"],
77 "pm%": [3, 100, "Prefetch miss percentage"],
78 "mhit": [4, 1000, "Metadata hits per second"],
79 "mmis": [4, 1000, "Metadata misses per second"],
80 "mread": [5, 1000, "Metadata accesses per second"],
81 "mh%": [3, 100, "Metadata hit percentage"],
82 "mm%": [3, 100, "Metadata miss percentage"],
83 "arcsz": [5, 1024, "ARC size"],
84 "size": [4, 1024, "ARC size"],
85 "c": [4, 1024, "ARC target size"],
86 "mfu": [4, 1000, "MFU list hits per second"],
87 "mru": [4, 1000, "MRU list hits per second"],
88 "mfug": [4, 1000, "MFU ghost list hits per second"],
89 "mrug": [4, 1000, "MRU ghost list hits per second"],
90 "eskip": [5, 1000, "evict_skip per second"],
91 "mtxmis": [6, 1000, "mutex_miss per second"],
92 "dread": [5, 1000, "Demand accesses per second"],
93 "pread": [5, 1000, "Prefetch accesses per second"],
94 "l2hits": [6, 1000, "L2ARC hits per second"],
95 "l2miss": [6, 1000, "L2ARC misses per second"],
96 "l2read": [6, 1000, "Total L2ARC accesses per second"],
97 "l2hit%": [6, 100, "L2ARC access hit percentage"],
98 "l2miss%": [7, 100, "L2ARC access miss percentage"],
99 "l2asize": [7, 1024, "Actual (compressed) size of the L2ARC"],
100 "l2size": [6, 1024, "Size of the L2ARC"],
101 "l2bytes": [7, 1024, "Bytes read per second from the L2ARC"],
102 "grow": [4, 1000, "ARC grow disabled"],
103 "need": [4, 1024, "ARC reclaim need"],
104 "free": [4, 1024, "ARC free memory"],
105 "avail": [5, 1024, "ARC available memory"],
106 "waste": [5, 1024, "Wasted memory due to round up to pagesize"],
110 hdr = ["time", "read", "miss", "miss%", "dmis", "dm%", "pmis", "pm%", "mmis",
111 "mm%", "size", "c", "avail"]
112 xhdr = ["time", "mfu", "mru", "mfug", "mrug", "eskip", "mtxmis", "dread",
114 sint = 1 # Default interval is 1 second
115 count = 1 # Default count is 1
116 hdr_intr = 20 # Print header every 20 lines of output
118 sep = " " # Default separator is 2 spaces
121 cmd = ("Usage: arcstat [-hvx] [-f fields] [-o file] [-s string] [interval "
129 if sys.platform.startswith('freebsd'):
130 # Requires py27-sysctl on FreeBSD
136 k = sysctl.filter('kstat.zfs.misc.arcstats')
147 name, value = s.name, s.value
148 # Trims 'kstat.zfs.misc.arcstats' from the name
149 kstat[name[24:]] = int(value)
151 elif sys.platform.startswith('linux'):
155 k = [line.strip() for line in open('/proc/spl/kstat/zfs/arcstats')]
167 name, unused, value = s.split()
168 kstat[name] = int(value)
171 def detailed_usage():
172 sys.stderr.write("%s\n" % cmd)
173 sys.stderr.write("Field definitions are as follows:\n")
175 sys.stderr.write("%11s : %s\n" % (key, cols[key][2]))
176 sys.stderr.write("\n")
182 sys.stderr.write("%s\n" % cmd)
183 sys.stderr.write("\t -h : Print this help message\n")
184 sys.stderr.write("\t -v : List all possible field headers and definitions"
186 sys.stderr.write("\t -x : Print extended stats\n")
187 sys.stderr.write("\t -f : Specify specific fields to print (see -v)\n")
188 sys.stderr.write("\t -o : Redirect output to the specified file\n")
189 sys.stderr.write("\t -s : Override default field separator with custom "
190 "character or string\n")
191 sys.stderr.write("\nExamples:\n")
192 sys.stderr.write("\tarcstat -o /tmp/a.log 2 10\n")
193 sys.stderr.write("\tarcstat -s \",\" -o /tmp/a.log 2 10\n")
194 sys.stderr.write("\tarcstat -v\n")
195 sys.stderr.write("\tarcstat -f time,hit%,dh%,ph%,mh% 1\n")
196 sys.stderr.write("\n")
205 prev = copy.deepcopy(cur)
210 if re.match(key, "class"):
213 d[key] = cur[key] - prev[key]
218 def prettynum(sz, scale, num=0):
219 suffix = [' ', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']
223 # Special case for date field
227 # Rounding error, return 0
231 while abs(num) > scale and index < 5:
237 return "%*d" % (sz, num)
239 if abs(save / scale) < 10:
240 return "%*.1f%s" % (sz - 1, num, suffix[index])
242 return "%*d%s" % (sz - 1, num, suffix[index])
250 sys.stdout.write(sep.join(
251 prettynum(cols[col][0], cols[col][1], v[col]) for col in hdr))
253 sys.stdout.write("\n")
261 sys.stdout.write(sep.join("%*s" % (cols[col][0], col) for col in hdr))
263 sys.stdout.write("\n")
266 def get_terminal_lines():
271 data = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, '1234')
272 sz = struct.unpack('hh', data)
278 def update_hdr_intr():
281 lines = get_terminal_lines()
282 if lines and lines > 3:
286 def resize_handler(signum, frame):
307 opts, args = getopt.getopt(
319 except getopt.error as msg:
320 sys.stderr.write("Error: %s\n" % str(msg))
324 for opt, arg in opts:
325 if opt in ('-x', '--extended'):
327 if opt in ('-o', '--outfile'):
330 if opt in ('-h', '--help'):
332 if opt in ('-v', '--verbose'):
334 if opt in ('-s', '--separator'):
337 if opt in ('-f', '--columns'):
343 sint = int(argv[0]) if argv else sint
344 count = int(argv[1]) if len(argv) > 1 else (0 if len(argv) > 0 else 1)
346 if hflag or (xflag and desired_cols):
357 # check if L2ARC exists
359 l2_size = cur.get("l2_size")
364 hdr = desired_cols.split(",")
371 elif not l2exist and ele.startswith("l2"):
372 sys.stdout.write("No L2ARC Here\n%s\n" % ele)
376 sys.stderr.write("Invalid column definition! -- %s\n" % invalid)
379 if len(incompat) > 0:
380 sys.stderr.write("Incompatible field specified! -- %s\n" %
386 out = open(opfile, "w")
390 sys.stderr.write("Cannot open %s for writing\n" % opfile)
400 v["time"] = time.strftime("%H:%M:%S", time.localtime())
401 v["hits"] = d["hits"] / sint
402 v["miss"] = d["misses"] / sint
403 v["read"] = v["hits"] + v["miss"]
404 v["hit%"] = 100 * v["hits"] / v["read"] if v["read"] > 0 else 0
405 v["miss%"] = 100 - v["hit%"] if v["read"] > 0 else 0
407 v["dhit"] = (d["demand_data_hits"] + d["demand_metadata_hits"]) / sint
408 v["dmis"] = (d["demand_data_misses"] + d["demand_metadata_misses"]) / sint
410 v["dread"] = v["dhit"] + v["dmis"]
411 v["dh%"] = 100 * v["dhit"] / v["dread"] if v["dread"] > 0 else 0
412 v["dm%"] = 100 - v["dh%"] if v["dread"] > 0 else 0
414 v["phit"] = (d["prefetch_data_hits"] + d["prefetch_metadata_hits"]) / sint
415 v["pmis"] = (d["prefetch_data_misses"] +
416 d["prefetch_metadata_misses"]) / sint
418 v["pread"] = v["phit"] + v["pmis"]
419 v["ph%"] = 100 * v["phit"] / v["pread"] if v["pread"] > 0 else 0
420 v["pm%"] = 100 - v["ph%"] if v["pread"] > 0 else 0
422 v["mhit"] = (d["prefetch_metadata_hits"] +
423 d["demand_metadata_hits"]) / sint
424 v["mmis"] = (d["prefetch_metadata_misses"] +
425 d["demand_metadata_misses"]) / sint
427 v["mread"] = v["mhit"] + v["mmis"]
428 v["mh%"] = 100 * v["mhit"] / v["mread"] if v["mread"] > 0 else 0
429 v["mm%"] = 100 - v["mh%"] if v["mread"] > 0 else 0
431 v["arcsz"] = cur["size"]
432 v["size"] = cur["size"]
434 v["mfu"] = d["mfu_hits"] / sint
435 v["mru"] = d["mru_hits"] / sint
436 v["mrug"] = d["mru_ghost_hits"] / sint
437 v["mfug"] = d["mfu_ghost_hits"] / sint
438 v["eskip"] = d["evict_skip"] / sint
439 v["mtxmis"] = d["mutex_miss"] / sint
442 v["l2hits"] = d["l2_hits"] / sint
443 v["l2miss"] = d["l2_misses"] / sint
444 v["l2read"] = v["l2hits"] + v["l2miss"]
445 v["l2hit%"] = 100 * v["l2hits"] / v["l2read"] if v["l2read"] > 0 else 0
447 v["l2miss%"] = 100 - v["l2hit%"] if v["l2read"] > 0 else 0
448 v["l2asize"] = cur["l2_asize"]
449 v["l2size"] = cur["l2_size"]
450 v["l2bytes"] = d["l2_read_bytes"] / sint
452 v["grow"] = 0 if cur["arc_no_grow"] else 1
453 v["need"] = cur["arc_need_free"]
454 v["free"] = cur["memory_free_bytes"]
455 v["avail"] = cur["memory_available_bytes"]
456 v["waste"] = cur["abd_chunk_waste_size"]
471 signal(SIGINT, SIG_DFL)
472 signal(SIGWINCH, resize_handler)
486 i = 0 if i >= hdr_intr else i + 1
493 if __name__ == '__main__':