1#!/usr/bin/env bash
2# shellcheck disable=SC1004
3
4#
5# This file and its contents are supplied under the terms of the
6# Common Development and Distribution License ("CDDL"), version 1.0.
7# You may only use this file in accordance with the terms of version
8# 1.0 of the CDDL.
9#
10# A full copy of the text of the CDDL should have accompanied this
11# source.  A copy of the CDDL is also available via the Internet at
12# http://www.illumos.org/license/CDDL.
13#
14
15#
16# Copyright (c) 2016 by Intel, Corp.
17#
18
19#
20# Linux platform placeholder for collecting prefetch I/O stats
21# TBD if we can add additional kstats to achieve the desired results
22#
23
24zfs_kstats="/proc/spl/kstat/zfs"
25
26function get_prefetch_ios
27{
28	typeset -l data_misses="$(awk '$1 == "prefetch_data_misses" \
29	    { print $3; exit }' "$zfs_kstats/arcstats")"
30	typeset -l metadata_misses="$(awk '$1 == "prefetch_metadata_misses" \
31	    { print $3; exit }' "$zfs_kstats/arcstats")"
32	typeset -l total_misses=$(( data_misses + metadata_misses ))
33
34	echo "$total_misses"
35}
36
37function get_prefetched_demand_reads
38{
39	typeset -l demand_reads="$(awk '$1 == "demand_hit_predictive_prefetch" \
40	    { print $3; exit }' "$zfs_kstats/arcstats")"
41
42	echo "$demand_reads"
43}
44
45function get_async_upgrade_sync
46{
47	typeset -l sync_wait="$(awk '$1 == "async_upgrade_sync" \
48	    { print $3; exit }' "$zfs_kstats/arcstats")"
49
50	echo "$sync_wait"
51}
52
53if [ $# -ne 2 ]
54then
55	echo "Usage: ${0##*/} poolname interval" >&2
56	exit 1
57fi
58
59interval=$2
60prefetch_ios=$(get_prefetch_ios)
61prefetched_demand_reads=$(get_prefetched_demand_reads)
62async_upgrade_sync=$(get_async_upgrade_sync)
63
64while true
65do
66	new_prefetch_ios=$(get_prefetch_ios)
67	printf '%u\n%-24s\t%u\n' "$(date +%s)" "prefetch_ios" \
68	    $(( new_prefetch_ios - prefetch_ios ))
69	prefetch_ios=$new_prefetch_ios
70
71	new_prefetched_demand_reads=$(get_prefetched_demand_reads)
72	printf '%-24s\t%u\n' "prefetched_demand_reads" \
73	    $(( new_prefetched_demand_reads - prefetched_demand_reads ))
74	prefetched_demand_reads=$new_prefetched_demand_reads
75
76	new_async_upgrade_sync=$(get_async_upgrade_sync)
77	printf '%-24s\t%u\n' "async_upgrade_sync" \
78	    $(( new_async_upgrade_sync - async_upgrade_sync ))
79	async_upgrade_sync=$new_async_upgrade_sync
80
81	sleep "$interval"
82done
83