# -*- tab-width: 4 -*- ;; Emacs
# vi: set filetype=sh tabstop=8 shiftwidth=8 noexpandtab :: Vi/ViM
############################################################ IDENT(1)
#
# $Title: dwatch(8) JSON module for dtrace_io(4) $
# $Copyright: 2014-2022 Devin Teske. All rights reserved. $
# $FrauBSD: dwatch-json/json-io-raw 2022-08-22 15:36:11 -0700 freebsdfrau $
# $Version: 1.0 $
#
############################################################ DESCRIPTION
#
# Produce JSON custom log format for disk I/O
#
############################################################ PROBE

: "${PROBE:=io:::start,io:::done}"

############################################################ EVENT ACTION

EVENT_TEST='this->devinfo.dev_name != "" && this->event != ""'

############################################################ ACTIONS

exec 9<<EOF
this bufinfo_t	bufinfo;
this devinfo_t	devinfo;
this int	b_flags;
this long	bio_length;
this string	bio_cmd;
this string	bio_flags;
this string	device_entry;
this string	device_if;
this string	device_type;
this string	event;

inline string append_bio_flag[int flags, int flag] = this->bio_flags =
	strjoin(this->bio_flags,
	strjoin(this->bio_flags == "" ? "" : (flags & flag) == flag ? "|" : "",
		bio_flag_string[flags & flag]));

/****************************** I/O ******************************/

$PROBE /(struct bio *)args[0] != NULL/ /* probe ID $ID */
{${TRACE:+
	printf("<$ID>");
}
	/*
	 * dtrace_io(4)
	 */
	this->event = probename;

	/*
	 * struct bio *
	 */
	this->bufinfo = xlate <bufinfo_t> ((struct bio *)args[0]);
	this->bio_cmd = bio_cmd_string[(int)this->bufinfo.b_cmd];
	this->b_flags = (int)this->bufinfo.b_flags;
	this->bio_flags = bio_flag_string[this->b_flags & BIO_ERROR];
	this->bio_flags = strjoin(this->bio_flags, this->bufinfo.b_error ?
		strjoin(this->bio_flags == "" ?
			bio_flag_string[BIO_ERROR] : "",
			strjoin("#", lltostr(this->bufinfo.b_error))) :
		"");
	append_bio_flag[this->b_flags, BIO_DONE];
	append_bio_flag[this->b_flags, BIO_ONQUEUE];
	append_bio_flag[this->b_flags, BIO_ORDERED];
	append_bio_flag[this->b_flags, BIO_UNMAPPED];
	append_bio_flag[this->b_flags, BIO_TRANSIENT_MAPPING];
	append_bio_flag[this->b_flags, BIO_VLIST];
	this->bio_flags = this->bio_flags == "" ? "-" : this->bio_flags;
	this->bio_length = (long)this->bufinfo.b_bcount;

	/*
	 * struct devstat *
	 */
	this->devinfo = xlate <devinfo_t> ((struct devstat *)args[1]);
	this->device_type = device_type[(int)this->devinfo.dev_type];
	this->device_if = device_if[(int)this->devinfo.dev_type];
	this->device_entry = strjoin(this->devinfo.dev_name,
		lltostr(this->devinfo.dev_minor));
}
EOF
ACTIONS=$( cat <&9 )
ID=$(( $ID + 1 ))

############################################################ EVENT DETAILS

exec 9<<EOF
	/*
	 * Print event details
	 */
	printf("{\"report_type\":\"${PROFILE%-raw}\",\"epoch\":%u,\"io\":\"%s\",\"type\":\"%s\",\"if\":\"%s\",\"device\":\"%s\",\"bio_cmd\":\"%s\",\"bio_flags\":\"%s\",\"length\":%u}",
		walltimestamp / 1000000000,
		this->event,
		this->device_type,
		this->device_if,
		this->device_entry,
		this->bio_cmd,
		this->bio_flags,
		this->bio_length);

	this->event = "";
EOF
EVENT_DETAILS=$( cat <&9 )

################################################################################
# END
################################################################################
