xref: /freebsd/sys/contrib/openzfs/cmd/arcstat.in (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
1#!/usr/bin/env @PYTHON_SHEBANG@
2#
3# Print out ZFS ARC Statistics exported via kstat(1)
4# For a definition of fields, or usage, use arcstat -v
5#
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
8# 09/18/2007
9#     http://blogs.sun.com/realneel/entry/zfs_arc_statistics
10#
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
15#
16# but has since moved to the illumos-gate repository.
17#
18# This Python port was written by John Hixson for FreeNAS, introduced
19# in commit e2c29f:
20#    https://github.com/freenas/freenas
21#
22# and has been improved by many people since.
23#
24# CDDL HEADER START
25#
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
29# with the License.
30#
31# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32# or https://opensource.org/licenses/CDDL-1.0.
33# See the License for the specific language governing permissions
34# and limitations under the License.
35#
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]
41#
42# CDDL HEADER END
43#
44#
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.
49#
50# This script must remain compatible with Python 3.6+.
51#
52
53import sys
54import time
55import getopt
56import re
57import copy
58
59from signal import signal, SIGINT, SIGWINCH, SIG_DFL
60
61
62cols = {
63    # HDR:        [Size, Scale, Description]
64    "time":       [8, -1, "Time"],
65    "hits":       [4, 1000, "ARC hits per second"],
66    "iohs":       [4, 1000, "ARC I/O hits per second"],
67    "miss":       [4, 1000, "ARC misses per second"],
68    "read":       [4, 1000, "Total ARC accesses per second"],
69    "hit%":       [4, 100, "ARC hit percentage"],
70    "ioh%":       [4, 100, "ARC I/O hit percentage"],
71    "miss%":      [5, 100, "ARC miss percentage"],
72    "dhit":       [4, 1000, "Demand hits per second"],
73    "dioh":       [4, 1000, "Demand I/O hits per second"],
74    "dmis":       [4, 1000, "Demand misses per second"],
75    "dh%":        [3, 100, "Demand hit percentage"],
76    "di%":        [3, 100, "Demand I/O hit percentage"],
77    "dm%":        [3, 100, "Demand miss percentage"],
78    "ddhit":      [5, 1000, "Demand data hits per second"],
79    "ddioh":      [5, 1000, "Demand data I/O hits per second"],
80    "ddmis":      [5, 1000, "Demand data misses per second"],
81    "ddh%":       [4, 100, "Demand data hit percentage"],
82    "ddi%":       [4, 100, "Demand data I/O hit percentage"],
83    "ddm%":       [4, 100, "Demand data miss percentage"],
84    "dmhit":      [5, 1000, "Demand metadata hits per second"],
85    "dmioh":      [5, 1000, "Demand metadata I/O hits per second"],
86    "dmmis":      [5, 1000, "Demand metadata misses per second"],
87    "dmh%":       [4, 100, "Demand metadata hit percentage"],
88    "dmi%":       [4, 100, "Demand metadata I/O hit percentage"],
89    "dmm%":       [4, 100, "Demand metadata miss percentage"],
90    "phit":       [4, 1000, "Prefetch hits per second"],
91    "pioh":       [4, 1000, "Prefetch I/O hits per second"],
92    "pmis":       [4, 1000, "Prefetch misses per second"],
93    "ph%":        [3, 100, "Prefetch hits percentage"],
94    "pi%":        [3, 100, "Prefetch I/O hits percentage"],
95    "pm%":        [3, 100, "Prefetch miss percentage"],
96    "pdhit":      [5, 1000, "Prefetch data hits per second"],
97    "pdioh":      [5, 1000, "Prefetch data I/O hits per second"],
98    "pdmis":      [5, 1000, "Prefetch data misses per second"],
99    "pdh%":       [4, 100, "Prefetch data hits percentage"],
100    "pdi%":       [4, 100, "Prefetch data I/O hits percentage"],
101    "pdm%":       [4, 100, "Prefetch data miss percentage"],
102    "pmhit":      [5, 1000, "Prefetch metadata hits per second"],
103    "pmioh":      [5, 1000, "Prefetch metadata I/O hits per second"],
104    "pmmis":      [5, 1000, "Prefetch metadata misses per second"],
105    "pmh%":       [4, 100, "Prefetch metadata hits percentage"],
106    "pmi%":       [4, 100, "Prefetch metadata I/O hits percentage"],
107    "pmm%":       [4, 100, "Prefetch metadata miss percentage"],
108    "mhit":       [4, 1000, "Metadata hits per second"],
109    "mioh":       [4, 1000, "Metadata I/O hits per second"],
110    "mmis":       [4, 1000, "Metadata misses per second"],
111    "mread":      [5, 1000, "Metadata accesses per second"],
112    "mh%":        [3, 100, "Metadata hit percentage"],
113    "mi%":        [3, 100, "Metadata I/O hit percentage"],
114    "mm%":        [3, 100, "Metadata miss percentage"],
115    "arcsz":      [5, 1024, "ARC size"],
116    "size":       [5, 1024, "ARC size"],
117    "c":          [5, 1024, "ARC target size"],
118    "mfu":        [4, 1000, "MFU list hits per second"],
119    "mru":        [4, 1000, "MRU list hits per second"],
120    "mfug":       [4, 1000, "MFU ghost list hits per second"],
121    "mrug":       [4, 1000, "MRU ghost list hits per second"],
122    "unc":        [4, 1000, "Uncached list hits per second"],
123    "eskip":      [5, 1000, "evict_skip per second"],
124    "el2skip":    [7, 1000, "evict skip, due to l2 writes, per second"],
125    "el2cach":    [7, 1024, "Size of L2 cached evictions per second"],
126    "el2el":      [5, 1024, "Size of L2 eligible evictions per second"],
127    "el2mfu":     [6, 1024, "Size of L2 eligible MFU evictions per second"],
128    "el2mru":     [6, 1024, "Size of L2 eligible MRU evictions per second"],
129    "el2inel":    [7, 1024, "Size of L2 ineligible evictions per second"],
130    "mtxmis":     [6, 1000, "mutex_miss per second"],
131    "dread":      [5, 1000, "Demand accesses per second"],
132    "ddread":     [6, 1000, "Demand data accesses per second"],
133    "dmread":     [6, 1000, "Demand metadata accesses per second"],
134    "pread":      [5, 1000, "Prefetch accesses per second"],
135    "pdread":     [6, 1000, "Prefetch data accesses per second"],
136    "pmread":     [6, 1000, "Prefetch metadata accesses per second"],
137    "l2hits":     [6, 1000, "L2ARC hits per second"],
138    "l2miss":     [6, 1000, "L2ARC misses per second"],
139    "l2read":     [6, 1000, "Total L2ARC accesses per second"],
140    "l2hit%":     [6, 100, "L2ARC access hit percentage"],
141    "l2miss%":    [7, 100, "L2ARC access miss percentage"],
142    "l2pref":     [6, 1024, "L2ARC prefetch allocated size"],
143    "l2mfu":      [5, 1024, "L2ARC MFU allocated size"],
144    "l2mru":      [5, 1024, "L2ARC MRU allocated size"],
145    "l2data":     [6, 1024, "L2ARC data allocated size"],
146    "l2meta":     [6, 1024, "L2ARC metadata allocated size"],
147    "l2pref%":    [7, 100, "L2ARC prefetch percentage"],
148    "l2mfu%":     [6, 100, "L2ARC MFU percentage"],
149    "l2mru%":     [6, 100, "L2ARC MRU percentage"],
150    "l2data%":    [7, 100, "L2ARC data percentage"],
151    "l2meta%":    [7, 100, "L2ARC metadata percentage"],
152    "l2asize":    [7, 1024, "Actual (compressed) size of the L2ARC"],
153    "l2size":     [6, 1024, "Size of the L2ARC"],
154    "l2bytes":    [7, 1024, "Bytes read per second from the L2ARC"],
155    "l2wbytes":   [8, 1024, "Bytes written per second to the L2ARC"],
156    "grow":       [4, 1000, "ARC grow disabled"],
157    "need":       [5, 1024, "ARC reclaim need"],
158    "free":       [5, 1024, "ARC free memory"],
159    "avail":      [5, 1024, "ARC available memory"],
160    "waste":      [5, 1024, "Wasted memory due to round up to pagesize"],
161    "ztotal":     [6, 1000, "zfetch total prefetcher calls per second"],
162    "zhits":      [5, 1000, "zfetch stream hits per second"],
163    "zahead":     [6, 1000, "zfetch hits ahead of streams per second"],
164    "zpast":      [5, 1000, "zfetch hits behind streams per second"],
165    "zmisses":    [7, 1000, "zfetch stream misses per second"],
166    "zmax":       [4, 1000, "zfetch limit reached per second"],
167    "zfuture":    [7, 1000, "zfetch stream future per second"],
168    "zstride":    [7, 1000, "zfetch stream strides per second"],
169    "zissued":    [7, 1000, "zfetch prefetches issued per second"],
170    "zactive":    [7, 1000, "zfetch prefetches active per second"],
171}
172
173# ARC structural breakdown from arc_summary
174structfields = {
175    "cmp":      ["compressed", "Compressed"],
176    "ovh":      ["overhead", "Overhead"],
177    "bon":      ["bonus", "Bonus"],
178    "dno":      ["dnode", "Dnode"],
179    "dbu":      ["dbuf", "Dbuf"],
180    "hdr":      ["hdr", "Header"],
181    "l2h":      ["l2_hdr", "L2 header"],
182    "abd":      ["abd_chunk_waste", "ABD chunk waste"],
183}
184structstats = {                             # size stats
185    "percent":  "size",                     # percentage of this value
186    "sz":       ["_size", "size"],
187}
188
189# ARC types breakdown from arc_summary
190typefields = {
191    "data":     ["data", "ARC data"],
192    "meta":     ["metadata", "ARC metadata"],
193}
194typestats = {                               # size stats
195    "percent":  "cachessz",                 # percentage of this value
196    "tg":       ["_target", "target"],
197    "sz":       ["_size", "size"],
198}
199
200# ARC states breakdown from arc_summary
201statefields = {
202    "ano":      ["anon", "Anonymous"],
203    "mfu":      ["mfu", "MFU"],
204    "mru":      ["mru", "MRU"],
205    "unc":      ["uncached", "Uncached"],
206}
207targetstats = {
208    "percent":  "cachessz",                 # percentage of this value
209    "fields":   ["mfu", "mru"],             # only applicable to these fields
210    "tg":       ["_target", "target"],
211    "dt":       ["_data_target", "data target"],
212    "mt":       ["_metadata_target", "metadata target"],
213}
214statestats = {                              # size stats
215    "percent":  "cachessz",                 # percentage of this value
216    "sz":       ["_size", "size"],
217    "da":       ["_data", "data size"],
218    "me":       ["_metadata", "metadata size"],
219    "ed":       ["_evictable_data", "evictable data size"],
220    "em":       ["_evictable_metadata", "evictable metadata size"],
221}
222ghoststats = {
223    "fields":   ["mfu", "mru"],             # only applicable to these fields
224    "gsz":      ["_ghost_size", "ghost size"],
225    "gd":       ["_ghost_data", "ghost data size"],
226    "gm":       ["_ghost_metadata", "ghost metadata size"],
227}
228
229# fields and stats
230fieldstats = [
231    [structfields, structstats],
232    [typefields, typestats],
233    [statefields, targetstats, statestats, ghoststats],
234]
235for fs in fieldstats:
236    fields, stats = fs[0], fs[1:]
237    for field, fieldval in fields.items():
238        for group in stats:
239            for stat, statval in group.items():
240                if stat in ["fields", "percent"] or \
241                    ("fields" in group and field not in group["fields"]):
242                    continue
243                colname = field + stat
244                coldesc = fieldval[1] + " " + statval[1]
245                cols[colname] = [len(colname), 1024, coldesc]
246                if "percent" in group:
247                    cols[colname + "%"] = [len(colname) + 1, 100, \
248                        coldesc + " percentage"]
249
250v = {}
251hdr = ["time", "read", "ddread", "ddh%", "dmread", "dmh%", "pread", "ph%",
252       "size", "c", "avail"]
253xhdr = ["time", "mfu", "mru", "mfug", "mrug", "unc", "eskip", "mtxmis",
254        "dread", "pread", "read"]
255zhdr = ["time", "ztotal", "zhits", "zahead", "zpast", "zmisses", "zmax",
256        "zfuture", "zstride", "zissued", "zactive"]
257sint = 1               # Default interval is 1 second
258count = 1              # Default count is 1
259hdr_intr = 20          # Print header every 20 lines of output
260opfile = None
261sep = "  "              # Default separator is 2 spaces
262l2exist = False
263cmd = ("Usage: arcstat [-havxp] [-f fields] [-o file] [-s string] [interval "
264       "[count]]\n")
265cur = {}
266d = {}
267out = None
268kstat = None
269pretty_print = True
270
271
272if sys.platform.startswith('freebsd'):
273    # Requires py-sysctl on FreeBSD
274    import sysctl
275
276    def kstat_update():
277        global kstat
278
279        k = [ctl for ctl in sysctl.filter('kstat.zfs.misc.arcstats')
280             if ctl.type != sysctl.CTLTYPE_NODE]
281        k += [ctl for ctl in sysctl.filter('kstat.zfs.misc.zfetchstats')
282             if ctl.type != sysctl.CTLTYPE_NODE]
283
284        if not k:
285            sys.exit(1)
286
287        kstat = {}
288
289        for s in k:
290            if not s:
291                continue
292
293            name, value = s.name, s.value
294
295            if "arcstats" in name:
296                # Trims 'kstat.zfs.misc.arcstats' from the name
297                kstat[name[24:]] = int(value)
298            else:
299                kstat["zfetch_" + name[27:]] = int(value)
300
301elif sys.platform.startswith('linux'):
302    def kstat_update():
303        global kstat
304
305        k1 = [line.strip() for line in open('/proc/spl/kstat/zfs/arcstats')]
306
307        k2 = ["zfetch_" + line.strip() for line in
308             open('/proc/spl/kstat/zfs/zfetchstats')]
309
310        if k1 is None or k2 is None:
311            sys.exit(1)
312
313        del k1[0:2]
314        del k2[0:2]
315        k = k1 + k2
316        kstat = {}
317
318        for s in k:
319            if not s:
320                continue
321
322            name, unused, value = s.split()
323            kstat[name] = int(value)
324
325
326def detailed_usage():
327    sys.stderr.write("%s\n" % cmd)
328    sys.stderr.write("Field definitions are as follows:\n")
329    for key in cols:
330        sys.stderr.write("%11s : %s\n" % (key, cols[key][2]))
331    sys.stderr.write("\n")
332
333    sys.exit(0)
334
335
336def usage():
337    sys.stderr.write("%s\n" % cmd)
338    sys.stderr.write("\t -h : Print this help message\n")
339    sys.stderr.write("\t -a : Print all possible stats\n")
340    sys.stderr.write("\t -v : List all possible field headers and definitions"
341                     "\n")
342    sys.stderr.write("\t -x : Print extended stats\n")
343    sys.stderr.write("\t -z : Print zfetch stats\n")
344    sys.stderr.write("\t -f : Specify specific fields to print (see -v)\n")
345    sys.stderr.write("\t -o : Redirect output to the specified file\n")
346    sys.stderr.write("\t -s : Override default field separator with custom "
347                     "character or string\n")
348    sys.stderr.write("\t -p : Disable auto-scaling of numerical fields\n")
349    sys.stderr.write("\nExamples:\n")
350    sys.stderr.write("\tarcstat -o /tmp/a.log 2 10\n")
351    sys.stderr.write("\tarcstat -s \",\" -o /tmp/a.log 2 10\n")
352    sys.stderr.write("\tarcstat -v\n")
353    sys.stderr.write("\tarcstat -f time,hit%,dh%,ph%,mh% 1\n")
354    sys.stderr.write("\n")
355
356    sys.exit(1)
357
358
359def snap_stats():
360    global cur
361    global kstat
362
363    prev = copy.deepcopy(cur)
364    kstat_update()
365
366    cur = kstat
367
368    # fill in additional values from arc_summary
369    cur["caches_size"] = caches_size = cur["anon_data"]+cur["anon_metadata"]+\
370        cur["mfu_data"]+cur["mfu_metadata"]+cur["mru_data"]+cur["mru_metadata"]+\
371        cur["uncached_data"]+cur["uncached_metadata"]
372    s = 4294967296
373    pd = cur["pd"]
374    pm = cur["pm"]
375    meta = cur["meta"]
376    v = (s-int(pd))*(s-int(meta))/s
377    cur["mfu_data_target"] = v / 65536 * caches_size / 65536
378    v = (s-int(pm))*int(meta)/s
379    cur["mfu_metadata_target"] = v / 65536 * caches_size / 65536
380    v = int(pd)*(s-int(meta))/s
381    cur["mru_data_target"] = v / 65536 * caches_size / 65536
382    v = int(pm)*int(meta)/s
383    cur["mru_metadata_target"] = v / 65536 * caches_size / 65536
384
385    cur["data_target"] = cur["mfu_data_target"] + cur["mru_data_target"]
386    cur["metadata_target"] = cur["mfu_metadata_target"] + cur["mru_metadata_target"]
387    cur["mfu_target"] = cur["mfu_data_target"] + cur["mfu_metadata_target"]
388    cur["mru_target"] = cur["mru_data_target"] + cur["mru_metadata_target"]
389
390    for key in cur:
391        if re.match(key, "class"):
392            continue
393        if key in prev:
394            d[key] = cur[key] - prev[key]
395        else:
396            d[key] = cur[key]
397
398
399def isint(num):
400    if isinstance(num, float):
401        return num.is_integer()
402    if isinstance(num, int):
403        return True
404    return False
405
406
407def prettynum(sz, scale, num=0):
408    suffix = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']
409    index = 0
410
411    # Special case for date field
412    if scale == -1:
413        return "%s" % num
414
415    if scale != 100:
416        while abs(num) > scale and index < 5:
417            num = num / scale
418            index += 1
419
420    width = sz - (0 if index == 0 else 1)
421    intlen = len("%.0f" % num)              # %.0f rounds to nearest int
422    if sint == 1 and isint(num) or width < intlen + 2:
423        decimal = 0
424    else:
425        decimal = 1
426    return "%*.*f%s" % (width, decimal, num, suffix[index])
427
428
429def print_values():
430    global hdr
431    global sep
432    global v
433    global pretty_print
434
435    if pretty_print:
436        fmt = lambda col: prettynum(cols[col][0], cols[col][1], v[col])
437    else:
438        fmt = lambda col: str(v[col])
439
440    sys.stdout.write(sep.join(fmt(col) for col in hdr))
441    sys.stdout.write("\n")
442    sys.stdout.flush()
443
444
445def print_header():
446    global hdr
447    global sep
448    global pretty_print
449
450    if pretty_print:
451        fmt = lambda col: "%*s" % (cols[col][0], col)
452    else:
453        fmt = lambda col: col
454
455    sys.stdout.write(sep.join(fmt(col) for col in hdr))
456    sys.stdout.write("\n")
457
458
459def get_terminal_lines():
460    try:
461        import fcntl
462        import termios
463        import struct
464        data = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, '1234')
465        sz = struct.unpack('hh', data)
466        return sz[0]
467    except Exception:
468        pass
469
470
471def update_hdr_intr():
472    global hdr_intr
473
474    lines = get_terminal_lines()
475    if lines and lines > 3:
476        hdr_intr = lines - 3
477
478
479def resize_handler(signum, frame):
480    update_hdr_intr()
481
482
483def init():
484    global sint
485    global count
486    global hdr
487    global xhdr
488    global zhdr
489    global opfile
490    global sep
491    global out
492    global l2exist
493    global pretty_print
494
495    desired_cols = None
496    aflag = False
497    xflag = False
498    hflag = False
499    vflag = False
500    zflag = False
501    i = 1
502
503    try:
504        opts, args = getopt.getopt(
505            sys.argv[1:],
506            "axzo:hvs:f:p",
507            [
508                "all",
509                "extended",
510                "zfetch",
511                "outfile",
512                "help",
513                "verbose",
514                "separator",
515                "columns",
516                "parsable"
517            ]
518        )
519    except getopt.error as msg:
520        sys.stderr.write("Error: %s\n" % str(msg))
521        usage()
522        opts = None
523
524    for opt, arg in opts:
525        if opt in ('-a', '--all'):
526            aflag = True
527        if opt in ('-x', '--extended'):
528            xflag = True
529        if opt in ('-o', '--outfile'):
530            opfile = arg
531            i += 1
532        if opt in ('-h', '--help'):
533            hflag = True
534        if opt in ('-v', '--verbose'):
535            vflag = True
536        if opt in ('-s', '--separator'):
537            sep = arg
538            i += 1
539        if opt in ('-f', '--columns'):
540            desired_cols = arg
541            i += 1
542        if opt in ('-p', '--parsable'):
543            pretty_print = False
544        if opt in ('-z', '--zfetch'):
545            zflag = True
546        i += 1
547
548    argv = sys.argv[i:]
549    sint = int(argv[0]) if argv else sint
550    count = int(argv[1]) if len(argv) > 1 else (0 if len(argv) > 0 else 1)
551
552    if hflag or (xflag and zflag) or ((zflag or xflag) and desired_cols):
553        usage()
554
555    if vflag:
556        detailed_usage()
557
558    if xflag:
559        hdr = xhdr
560
561    if zflag:
562        hdr = zhdr
563
564    update_hdr_intr()
565
566    # check if L2ARC exists
567    snap_stats()
568    l2_size = cur.get("l2_size")
569    if l2_size:
570        l2exist = True
571
572    if desired_cols:
573        hdr = desired_cols.split(",")
574
575        invalid = []
576        incompat = []
577        for ele in hdr:
578            if ele not in cols:
579                invalid.append(ele)
580            elif not l2exist and ele.startswith("l2"):
581                sys.stdout.write("No L2ARC Here\n%s\n" % ele)
582                incompat.append(ele)
583
584        if len(invalid) > 0:
585            sys.stderr.write("Invalid column definition! -- %s\n" % invalid)
586            usage()
587
588        if len(incompat) > 0:
589            sys.stderr.write("Incompatible field specified! -- %s\n" %
590                             incompat)
591            usage()
592
593    if aflag:
594        if l2exist:
595            hdr = cols.keys()
596        else:
597            hdr = [col for col in cols.keys() if not col.startswith("l2")]
598
599    if opfile:
600        try:
601            out = open(opfile, "w")
602            sys.stdout = out
603
604        except IOError:
605            sys.stderr.write("Cannot open %s for writing\n" % opfile)
606            sys.exit(1)
607
608
609def calculate():
610    global d
611    global v
612    global l2exist
613
614    v = dict()
615    v["time"] = time.strftime("%H:%M:%S", time.localtime())
616    v["hits"] = d["hits"] / sint
617    v["iohs"] = d["iohits"] / sint
618    v["miss"] = d["misses"] / sint
619    v["read"] = v["hits"] + v["iohs"] + v["miss"]
620    v["hit%"] = 100 * v["hits"] / v["read"] if v["read"] > 0 else 0
621    v["ioh%"] = 100 * v["iohs"] / v["read"] if v["read"] > 0 else 0
622    v["miss%"] = 100 - v["hit%"] - v["ioh%"] if v["read"] > 0 else 0
623
624    v["dhit"] = (d["demand_data_hits"] + d["demand_metadata_hits"]) / sint
625    v["dioh"] = (d["demand_data_iohits"] + d["demand_metadata_iohits"]) / sint
626    v["dmis"] = (d["demand_data_misses"] + d["demand_metadata_misses"]) / sint
627
628    v["dread"] = v["dhit"] + v["dioh"] + v["dmis"]
629    v["dh%"] = 100 * v["dhit"] / v["dread"] if v["dread"] > 0 else 0
630    v["di%"] = 100 * v["dioh"] / v["dread"] if v["dread"] > 0 else 0
631    v["dm%"] = 100 - v["dh%"] - v["di%"] if v["dread"] > 0 else 0
632
633    v["ddhit"] = d["demand_data_hits"] / sint
634    v["ddioh"] = d["demand_data_iohits"] / sint
635    v["ddmis"] = d["demand_data_misses"] / sint
636
637    v["ddread"] = v["ddhit"] + v["ddioh"] + v["ddmis"]
638    v["ddh%"] = 100 * v["ddhit"] / v["ddread"] if v["ddread"] > 0 else 0
639    v["ddi%"] = 100 * v["ddioh"] / v["ddread"] if v["ddread"] > 0 else 0
640    v["ddm%"] = 100 - v["ddh%"] - v["ddi%"] if v["ddread"] > 0 else 0
641
642    v["dmhit"] = d["demand_metadata_hits"] / sint
643    v["dmioh"] = d["demand_metadata_iohits"] / sint
644    v["dmmis"] = d["demand_metadata_misses"] / sint
645
646    v["dmread"] = v["dmhit"] + v["dmioh"] + v["dmmis"]
647    v["dmh%"] = 100 * v["dmhit"] / v["dmread"] if v["dmread"] > 0 else 0
648    v["dmi%"] = 100 * v["dmioh"] / v["dmread"] if v["dmread"] > 0 else 0
649    v["dmm%"] = 100 - v["dmh%"] - v["dmi%"] if v["dmread"] > 0 else 0
650
651    v["phit"] = (d["prefetch_data_hits"] + d["prefetch_metadata_hits"]) / sint
652    v["pioh"] = (d["prefetch_data_iohits"] +
653                 d["prefetch_metadata_iohits"]) / sint
654    v["pmis"] = (d["prefetch_data_misses"] +
655                 d["prefetch_metadata_misses"]) / sint
656
657    v["pread"] = v["phit"] + v["pioh"] + v["pmis"]
658    v["ph%"] = 100 * v["phit"] / v["pread"] if v["pread"] > 0 else 0
659    v["pi%"] = 100 * v["pioh"] / v["pread"] if v["pread"] > 0 else 0
660    v["pm%"] = 100 - v["ph%"] - v["pi%"] if v["pread"] > 0 else 0
661
662    v["pdhit"] = d["prefetch_data_hits"] / sint
663    v["pdioh"] = d["prefetch_data_iohits"] / sint
664    v["pdmis"] = d["prefetch_data_misses"] / sint
665
666    v["pdread"] = v["pdhit"] + v["pdioh"] + v["pdmis"]
667    v["pdh%"] = 100 * v["pdhit"] / v["pdread"] if v["pdread"] > 0 else 0
668    v["pdi%"] = 100 * v["pdioh"] / v["pdread"] if v["pdread"] > 0 else 0
669    v["pdm%"] = 100 - v["pdh%"] - v["pdi%"] if v["pdread"] > 0 else 0
670
671    v["pmhit"] = d["prefetch_metadata_hits"] / sint
672    v["pmioh"] = d["prefetch_metadata_iohits"] / sint
673    v["pmmis"] = d["prefetch_metadata_misses"] / sint
674
675    v["pmread"] = v["pmhit"] + v["pmioh"] + v["pmmis"]
676    v["pmh%"] = 100 * v["pmhit"] / v["pmread"] if v["pmread"] > 0 else 0
677    v["pmi%"] = 100 * v["pmioh"] / v["pmread"] if v["pmread"] > 0 else 0
678    v["pmm%"] = 100 - v["pmh%"] - v["pmi%"] if v["pmread"] > 0 else 0
679
680    v["mhit"] = (d["prefetch_metadata_hits"] +
681                 d["demand_metadata_hits"]) / sint
682    v["mioh"] = (d["prefetch_metadata_iohits"] +
683                 d["demand_metadata_iohits"]) / sint
684    v["mmis"] = (d["prefetch_metadata_misses"] +
685                 d["demand_metadata_misses"]) / sint
686
687    v["mread"] = v["mhit"] + v["mioh"] + v["mmis"]
688    v["mh%"] = 100 * v["mhit"] / v["mread"] if v["mread"] > 0 else 0
689    v["mi%"] = 100 * v["mioh"] / v["mread"] if v["mread"] > 0 else 0
690    v["mm%"] = 100 - v["mh%"] - v["mi%"] if v["mread"] > 0 else 0
691
692    v["arcsz"] = cur["size"]
693    v["size"] = cur["size"]
694    v["c"] = cur["c"]
695    v["mfu"] = d["mfu_hits"] / sint
696    v["mru"] = d["mru_hits"] / sint
697    v["mrug"] = d["mru_ghost_hits"] / sint
698    v["mfug"] = d["mfu_ghost_hits"] / sint
699    v["unc"] = d["uncached_hits"] / sint
700    v["eskip"] = d["evict_skip"] / sint
701    v["el2skip"] = d["evict_l2_skip"] / sint
702    v["el2cach"] = d["evict_l2_cached"] / sint
703    v["el2el"] = d["evict_l2_eligible"] / sint
704    v["el2mfu"] = d["evict_l2_eligible_mfu"] / sint
705    v["el2mru"] = d["evict_l2_eligible_mru"] / sint
706    v["el2inel"] = d["evict_l2_ineligible"] / sint
707    v["mtxmis"] = d["mutex_miss"] / sint
708    v["ztotal"] = (d["zfetch_hits"] + d["zfetch_future"] + d["zfetch_stride"] +
709                   d["zfetch_past"] + d["zfetch_misses"]) / sint
710    v["zhits"] = d["zfetch_hits"] / sint
711    v["zahead"] = (d["zfetch_future"] + d["zfetch_stride"]) / sint
712    v["zpast"] = d["zfetch_past"] / sint
713    v["zmisses"] = d["zfetch_misses"] / sint
714    v["zmax"] = d["zfetch_max_streams"] / sint
715    v["zfuture"] = d["zfetch_future"] / sint
716    v["zstride"] = d["zfetch_stride"] / sint
717    v["zissued"] = d["zfetch_io_issued"] / sint
718    v["zactive"] = d["zfetch_io_active"] / sint
719
720    # ARC structural breakdown, ARC types breakdown, ARC states breakdown
721    v["cachessz"] = cur["caches_size"]
722    for fs in fieldstats:
723        fields, stats = fs[0], fs[1:]
724        for field, fieldval in fields.items():
725            for group in stats:
726                for stat, statval in group.items():
727                    if stat in ["fields", "percent"] or \
728                        ("fields" in group and field not in group["fields"]):
729                        continue
730                    colname = field + stat
731                    v[colname] = cur[fieldval[0] + statval[0]]
732                    if "percent" in group:
733                        v[colname + "%"] = 100 * v[colname] / \
734                            v[group["percent"]] if v[group["percent"]] > 0 else 0
735
736    if l2exist:
737        v["l2hits"] = d["l2_hits"] / sint
738        v["l2miss"] = d["l2_misses"] / sint
739        v["l2read"] = v["l2hits"] + v["l2miss"]
740        v["l2hit%"] = 100 * v["l2hits"] / v["l2read"] if v["l2read"] > 0 else 0
741
742        v["l2miss%"] = 100 - v["l2hit%"] if v["l2read"] > 0 else 0
743        v["l2asize"] = cur["l2_asize"]
744        v["l2size"] = cur["l2_size"]
745        v["l2bytes"] = d["l2_read_bytes"] / sint
746        v["l2wbytes"] = d["l2_write_bytes"] / sint
747
748        v["l2pref"] = cur["l2_prefetch_asize"]
749        v["l2mfu"] = cur["l2_mfu_asize"]
750        v["l2mru"] = cur["l2_mru_asize"]
751        v["l2data"] = cur["l2_bufc_data_asize"]
752        v["l2meta"] = cur["l2_bufc_metadata_asize"]
753        v["l2pref%"] = 100 * v["l2pref"] / v["l2asize"]
754        v["l2mfu%"] = 100 * v["l2mfu"] / v["l2asize"]
755        v["l2mru%"] = 100 * v["l2mru"] / v["l2asize"]
756        v["l2data%"] = 100 * v["l2data"] / v["l2asize"]
757        v["l2meta%"] = 100 * v["l2meta"] / v["l2asize"]
758
759    v["grow"] = 0 if cur["arc_no_grow"] else 1
760    v["need"] = cur["arc_need_free"]
761    v["free"] = cur["memory_free_bytes"]
762    v["avail"] = cur["memory_available_bytes"]
763    v["waste"] = cur["abd_chunk_waste_size"]
764
765
766def main():
767    global sint
768    global count
769    global hdr_intr
770
771    i = 0
772    count_flag = 0
773
774    init()
775    if count > 0:
776        count_flag = 1
777
778    signal(SIGINT, SIG_DFL)
779    signal(SIGWINCH, resize_handler)
780    while True:
781        if i == 0:
782            print_header()
783
784        snap_stats()
785        calculate()
786        print_values()
787
788        if count_flag == 1:
789            if count <= 1:
790                break
791            count -= 1
792
793        i = 0 if i >= hdr_intr else i + 1
794        time.sleep(sint)
795
796    if out:
797        out.close()
798
799
800if __name__ == '__main__':
801    main()
802