xref: /freebsd/usr.bin/systat/zarc.c (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
127aa4769SMichael Reifenberger /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
39e16b953SKirk McKusick  *
44bde6353SMichael Reifenberger  * Copyright (c) 2014 - 2017, 2019 Yoshihiro Ota
527aa4769SMichael Reifenberger  *
627aa4769SMichael Reifenberger  * Redistribution and use in source and binary forms, with or without
727aa4769SMichael Reifenberger  * modification, are permitted provided that the following conditions
827aa4769SMichael Reifenberger  * are met:
927aa4769SMichael Reifenberger  * 1. Redistributions of source code must retain the above copyright
1027aa4769SMichael Reifenberger  *    notice, this list of conditions and the following disclaimer.
1127aa4769SMichael Reifenberger  * 2. Redistributions in binary form must reproduce the above copyright
1227aa4769SMichael Reifenberger  *    notice, this list of conditions and the following disclaimer in the
1327aa4769SMichael Reifenberger  *    documentation and/or other materials provided with the distribution.
1427aa4769SMichael Reifenberger  *
159e16b953SKirk McKusick  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1627aa4769SMichael Reifenberger  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1727aa4769SMichael Reifenberger  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
189e16b953SKirk McKusick  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1927aa4769SMichael Reifenberger  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2027aa4769SMichael Reifenberger  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2127aa4769SMichael Reifenberger  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2227aa4769SMichael Reifenberger  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2327aa4769SMichael Reifenberger  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2427aa4769SMichael Reifenberger  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2527aa4769SMichael Reifenberger  * SUCH DAMAGE.
2627aa4769SMichael Reifenberger  */
2727aa4769SMichael Reifenberger 
2827aa4769SMichael Reifenberger #include <sys/types.h>
2927aa4769SMichael Reifenberger #include <sys/sysctl.h>
3027aa4769SMichael Reifenberger 
31970bdbf5SMichael Tuexen #include <inttypes.h>
3227aa4769SMichael Reifenberger #include <string.h>
3345518845SMichael Reifenberger #include <err.h>
344bde6353SMichael Reifenberger #include <libutil.h>
3527aa4769SMichael Reifenberger 
3627aa4769SMichael Reifenberger #include "systat.h"
3727aa4769SMichael Reifenberger #include "extern.h"
3845518845SMichael Reifenberger #include "devs.h"
3927aa4769SMichael Reifenberger 
4027aa4769SMichael Reifenberger struct zfield {
4127aa4769SMichael Reifenberger 	uint64_t arcstats;
4227aa4769SMichael Reifenberger 	uint64_t arcstats_demand_data;
4327aa4769SMichael Reifenberger 	uint64_t arcstats_demand_metadata;
4427aa4769SMichael Reifenberger 	uint64_t arcstats_prefetch_data;
4527aa4769SMichael Reifenberger 	uint64_t arcstats_prefetch_metadata;
4627aa4769SMichael Reifenberger 	uint64_t zfetchstats;
4727aa4769SMichael Reifenberger 	uint64_t arcstats_l2;
4827aa4769SMichael Reifenberger };
4927aa4769SMichael Reifenberger 
5027aa4769SMichael Reifenberger static struct zarcstats {
5127aa4769SMichael Reifenberger 	struct zfield hits;
5227aa4769SMichael Reifenberger 	struct zfield misses;
5327aa4769SMichael Reifenberger } curstat, initstat, oldstat;
5427aa4769SMichael Reifenberger 
554bde6353SMichael Reifenberger struct zarcrates {
564bde6353SMichael Reifenberger 	struct zfield current;
574bde6353SMichael Reifenberger 	struct zfield total;
584bde6353SMichael Reifenberger };
594bde6353SMichael Reifenberger 
6027aa4769SMichael Reifenberger static void
6127aa4769SMichael Reifenberger getinfo(struct zarcstats *ls);
6227aa4769SMichael Reifenberger 
6327aa4769SMichael Reifenberger WINDOW *
openzarc(void)6427aa4769SMichael Reifenberger openzarc(void)
6527aa4769SMichael Reifenberger {
664bde6353SMichael Reifenberger 
6727aa4769SMichael Reifenberger 	return (subwin(stdscr, LINES - 3 - 1, 0, MAINWIN_ROW, 0));
6827aa4769SMichael Reifenberger }
6927aa4769SMichael Reifenberger 
7027aa4769SMichael Reifenberger void
closezarc(WINDOW * w)7127aa4769SMichael Reifenberger closezarc(WINDOW *w)
7227aa4769SMichael Reifenberger {
734bde6353SMichael Reifenberger 
7427aa4769SMichael Reifenberger 	if (w == NULL)
7527aa4769SMichael Reifenberger 		return;
7627aa4769SMichael Reifenberger 	wclear(w);
7727aa4769SMichael Reifenberger 	wrefresh(w);
7827aa4769SMichael Reifenberger 	delwin(w);
7927aa4769SMichael Reifenberger }
8027aa4769SMichael Reifenberger 
8127aa4769SMichael Reifenberger void
labelzarc(void)8227aa4769SMichael Reifenberger labelzarc(void)
8327aa4769SMichael Reifenberger {
8445518845SMichael Reifenberger 	int row = 1;
854bde6353SMichael Reifenberger 
8627aa4769SMichael Reifenberger 	wmove(wnd, 0, 0); wclrtoeol(wnd);
874bde6353SMichael Reifenberger 	mvwprintw(wnd, 0, 31+1, "%4.4s %6.6s %6.6s | Total %4.4s %6.6s %6.6s",
884bde6353SMichael Reifenberger 		"Rate", "Hits", "Misses", "Rate", "Hits", "Misses");
894bde6353SMichael Reifenberger #define L(str) mvwprintw(wnd, row++, 5, \
904bde6353SMichael Reifenberger 		"%-26.26s:   %%               |          %%", #str)
9145518845SMichael Reifenberger 	L(arcstats);
9245518845SMichael Reifenberger 	L(arcstats.demand_data);
9345518845SMichael Reifenberger 	L(arcstats.demand_metadata);
9445518845SMichael Reifenberger 	L(arcstats.prefetch_data);
9545518845SMichael Reifenberger 	L(arcstats.prefetch_metadata);
9645518845SMichael Reifenberger 	L(zfetchstats);
9745518845SMichael Reifenberger 	L(arcstats.l2);
9827aa4769SMichael Reifenberger #undef L
9945518845SMichael Reifenberger 	dslabel(12, 0, 18);
10027aa4769SMichael Reifenberger }
10127aa4769SMichael Reifenberger 
1024bde6353SMichael Reifenberger static int
calc_rate(uint64_t hits,uint64_t misses)1034bde6353SMichael Reifenberger calc_rate(uint64_t hits, uint64_t misses)
10427aa4769SMichael Reifenberger {
10527aa4769SMichael Reifenberger     if(hits)
10627aa4769SMichael Reifenberger 	return 100 * hits / (hits + misses);
10727aa4769SMichael Reifenberger     else
10827aa4769SMichael Reifenberger 	return 0;
10927aa4769SMichael Reifenberger }
11027aa4769SMichael Reifenberger 
11127aa4769SMichael Reifenberger static void
domode(struct zarcstats * delta,struct zarcrates * rate)1124bde6353SMichael Reifenberger domode(struct zarcstats *delta, struct zarcrates *rate)
11327aa4769SMichael Reifenberger {
11427aa4769SMichael Reifenberger #define DO(stat) \
11527aa4769SMichael Reifenberger 	delta->hits.stat = (curstat.hits.stat - oldstat.hits.stat); \
11627aa4769SMichael Reifenberger 	delta->misses.stat = (curstat.misses.stat - oldstat.misses.stat); \
1174bde6353SMichael Reifenberger 	rate->current.stat = calc_rate(delta->hits.stat, delta->misses.stat); \
1184bde6353SMichael Reifenberger 	rate->total.stat = calc_rate(curstat.hits.stat, curstat.misses.stat)
11927aa4769SMichael Reifenberger 	DO(arcstats);
12027aa4769SMichael Reifenberger 	DO(arcstats_demand_data);
12127aa4769SMichael Reifenberger 	DO(arcstats_demand_metadata);
12227aa4769SMichael Reifenberger 	DO(arcstats_prefetch_data);
12327aa4769SMichael Reifenberger 	DO(arcstats_prefetch_metadata);
12427aa4769SMichael Reifenberger 	DO(zfetchstats);
12527aa4769SMichael Reifenberger 	DO(arcstats_l2);
12627aa4769SMichael Reifenberger 	DO(arcstats);
12727aa4769SMichael Reifenberger 	DO(arcstats_demand_data);
12827aa4769SMichael Reifenberger 	DO(arcstats_demand_metadata);
12927aa4769SMichael Reifenberger 	DO(arcstats_prefetch_data);
13027aa4769SMichael Reifenberger 	DO(arcstats_prefetch_metadata);
13127aa4769SMichael Reifenberger 	DO(zfetchstats);
13227aa4769SMichael Reifenberger 	DO(arcstats_l2);
13327aa4769SMichael Reifenberger #undef DO
13427aa4769SMichael Reifenberger }
13527aa4769SMichael Reifenberger 
13627aa4769SMichael Reifenberger void
showzarc(void)13727aa4769SMichael Reifenberger showzarc(void)
13827aa4769SMichael Reifenberger {
13945518845SMichael Reifenberger 	int row = 1;
1404bde6353SMichael Reifenberger 	struct zarcstats delta = {};
1414bde6353SMichael Reifenberger 	struct zarcrates rate = {};
14227aa4769SMichael Reifenberger 
14327aa4769SMichael Reifenberger 	domode(&delta, &rate);
14427aa4769SMichael Reifenberger 
1454bde6353SMichael Reifenberger #define DO(stat, col, width) \
1464bde6353SMichael Reifenberger 	sysputuint64(wnd, row, col, width, stat, HN_DIVISOR_1000)
1474bde6353SMichael Reifenberger #define	RATES(stat) mvwprintw(wnd, row, 31+1, "%3"PRIu64, rate.current.stat);\
1484bde6353SMichael Reifenberger 	mvwprintw(wnd, row, 31+1+5+7+7+8, "%3"PRIu64, rate.total.stat)
1494bde6353SMichael Reifenberger #define	HITS(stat) DO(delta.hits.stat, 31+1+5, 6); \
1504bde6353SMichael Reifenberger 	DO(curstat.hits.stat, 31+1+5+7+7+8+5, 6)
1514bde6353SMichael Reifenberger #define	MISSES(stat) DO(delta.misses.stat, 31+1+5+7, 6); \
1524bde6353SMichael Reifenberger 	DO(curstat.misses.stat, 31+1+5+7+7+8+5+7, 6)
1534bde6353SMichael Reifenberger #define	E(stat) RATES(stat); HITS(stat); MISSES(stat); ++row
15445518845SMichael Reifenberger 	E(arcstats);
15545518845SMichael Reifenberger 	E(arcstats_demand_data);
15645518845SMichael Reifenberger 	E(arcstats_demand_metadata);
15745518845SMichael Reifenberger 	E(arcstats_prefetch_data);
15845518845SMichael Reifenberger 	E(arcstats_prefetch_metadata);
15945518845SMichael Reifenberger 	E(zfetchstats);
16045518845SMichael Reifenberger 	E(arcstats_l2);
16127aa4769SMichael Reifenberger #undef DO
16227aa4769SMichael Reifenberger #undef E
1634bde6353SMichael Reifenberger #undef MISSES
1644bde6353SMichael Reifenberger #undef HITS
1654bde6353SMichael Reifenberger #undef RATES
16645518845SMichael Reifenberger 	dsshow(12, 0, 18, &cur_dev, &last_dev);
16727aa4769SMichael Reifenberger }
16827aa4769SMichael Reifenberger 
16927aa4769SMichael Reifenberger int
initzarc(void)17027aa4769SMichael Reifenberger initzarc(void)
17127aa4769SMichael Reifenberger {
17245518845SMichael Reifenberger 	dsinit(12);
17327aa4769SMichael Reifenberger 	getinfo(&initstat);
17427aa4769SMichael Reifenberger 	curstat = oldstat = initstat;
17545518845SMichael Reifenberger 
17627aa4769SMichael Reifenberger 	return 1;
17727aa4769SMichael Reifenberger }
17827aa4769SMichael Reifenberger 
17927aa4769SMichael Reifenberger void
resetzarc(void)18027aa4769SMichael Reifenberger resetzarc(void)
18127aa4769SMichael Reifenberger {
1824bde6353SMichael Reifenberger 
18327aa4769SMichael Reifenberger 	initzarc();
18427aa4769SMichael Reifenberger }
18527aa4769SMichael Reifenberger 
18627aa4769SMichael Reifenberger static void
getinfo(struct zarcstats * ls)18727aa4769SMichael Reifenberger getinfo(struct zarcstats *ls)
18827aa4769SMichael Reifenberger {
18945518845SMichael Reifenberger 	struct devinfo *tmp_dinfo;
19045518845SMichael Reifenberger 
19145518845SMichael Reifenberger 	tmp_dinfo = last_dev.dinfo;
19245518845SMichael Reifenberger 	last_dev.dinfo = cur_dev.dinfo;
19345518845SMichael Reifenberger 	cur_dev.dinfo = tmp_dinfo;
19445518845SMichael Reifenberger 
19545518845SMichael Reifenberger 	last_dev.snap_time = cur_dev.snap_time;
19645518845SMichael Reifenberger 	dsgetinfo(&cur_dev);
19745518845SMichael Reifenberger 
19827aa4769SMichael Reifenberger 	size_t size = sizeof(ls->hits.arcstats);
19927aa4769SMichael Reifenberger 	if (sysctlbyname("kstat.zfs.misc.arcstats.hits",
20027aa4769SMichael Reifenberger 		&ls->hits.arcstats, &size, NULL, 0) != 0)
20127aa4769SMichael Reifenberger 		return;
20227aa4769SMichael Reifenberger 	GETSYSCTL("kstat.zfs.misc.arcstats.misses",
20327aa4769SMichael Reifenberger 		ls->misses.arcstats);
20427aa4769SMichael Reifenberger 	GETSYSCTL("kstat.zfs.misc.arcstats.demand_data_hits",
20527aa4769SMichael Reifenberger 		ls->hits.arcstats_demand_data);
20627aa4769SMichael Reifenberger 	GETSYSCTL("kstat.zfs.misc.arcstats.demand_data_misses",
20727aa4769SMichael Reifenberger 		ls->misses.arcstats_demand_data);
20827aa4769SMichael Reifenberger 	GETSYSCTL("kstat.zfs.misc.arcstats.demand_metadata_hits",
20927aa4769SMichael Reifenberger 		ls->hits.arcstats_demand_metadata);
21027aa4769SMichael Reifenberger 	GETSYSCTL("kstat.zfs.misc.arcstats.demand_metadata_misses",
21127aa4769SMichael Reifenberger 		ls->misses.arcstats_demand_metadata);
21227aa4769SMichael Reifenberger 	GETSYSCTL("kstat.zfs.misc.arcstats.prefetch_data_hits",
21327aa4769SMichael Reifenberger 		ls->hits.arcstats_prefetch_data);
21427aa4769SMichael Reifenberger 	GETSYSCTL("kstat.zfs.misc.arcstats.prefetch_data_misses",
21527aa4769SMichael Reifenberger 		ls->misses.arcstats_prefetch_data);
21627aa4769SMichael Reifenberger 	GETSYSCTL("kstat.zfs.misc.arcstats.prefetch_metadata_hits",
21727aa4769SMichael Reifenberger 		ls->hits.arcstats_prefetch_metadata);
21827aa4769SMichael Reifenberger 	GETSYSCTL("kstat.zfs.misc.arcstats.prefetch_metadata_misses",
21927aa4769SMichael Reifenberger 		ls->misses.arcstats_prefetch_metadata);
22027aa4769SMichael Reifenberger 	GETSYSCTL("kstat.zfs.misc.zfetchstats.hits",
22127aa4769SMichael Reifenberger 		ls->hits.zfetchstats);
22227aa4769SMichael Reifenberger 	GETSYSCTL("kstat.zfs.misc.zfetchstats.misses",
22327aa4769SMichael Reifenberger 		ls->misses.zfetchstats);
22427aa4769SMichael Reifenberger 	GETSYSCTL("kstat.zfs.misc.arcstats.l2_hits",
22527aa4769SMichael Reifenberger 		ls->hits.arcstats_l2);
22627aa4769SMichael Reifenberger 	GETSYSCTL("kstat.zfs.misc.arcstats.l2_misses",
22727aa4769SMichael Reifenberger 		ls->misses.arcstats_l2);
22827aa4769SMichael Reifenberger }
22927aa4769SMichael Reifenberger 
23027aa4769SMichael Reifenberger void
fetchzarc(void)23127aa4769SMichael Reifenberger fetchzarc(void)
23227aa4769SMichael Reifenberger {
2334bde6353SMichael Reifenberger 
23427aa4769SMichael Reifenberger 	oldstat = curstat;
23527aa4769SMichael Reifenberger 	getinfo(&curstat);
23627aa4769SMichael Reifenberger }
237