#!/bin/bash
# -*- mode: shell-script; indent-tabs-mode: t; sh-basic-offset: 8; sh-indentation: 8; tab-width: 8 -*-

PROG="$(basename ${0})"

interval="${1}"
if [[ -z "${interval}" ]]; then
	printf -- "%s: missing required 'interval' argument\n" "${PROG}" >&2
	exit 1
fi

command -v turbostat > /dev/null
if [[ ${?} -ne 0 ]]; then
	printf -- "%s: missing required 'turbostat' command\n" "${PROG}" >&2
	exit 1
fi

command -v systemd-detect-virt > /dev/null
if [[ ${?} -ne 0 ]]; then
	printf -- "%s: missing required 'systemd-detect-virt' command\n" "${PROG}" >&2
	exit 1
fi

virtenv="$(systemd-detect-virt --vm 2> /dev/null)"
if [[ "${virtenv}" != "none" ]]; then
	printf -- "%s: running inside a '%s' guest; disabling turbostat\n" "${PROG}" "${virtenv}" >&2
	exit 1
fi

exec turbostat -i ${interval} > >(pbench-log-timestamp)
