xref: /spdk/scripts/perf/pm/collect-cpu-load (revision b02581a89058ebaebe03bd0e16e3b58adfe406c1)
1#!/usr/bin/env bash
2#  SPDX-License-Identifier: BSD-3-Clause
3#  Copyright (C) 2023 Intel Corporation
4#  All rights reserved.
5
6shopt -s nullglob extglob
7
8pmdir=$(readlink -f "$(dirname "$0")")
9rootdir=$(readlink -f "$pmdir/../../../")
10source "$rootdir/test/scheduler/common.sh"
11source "$pmdir/common"
12
13help() {
14	cat <<- HELP
15
16		Usage: $0 [-h] [-c count] [-d dir] [-p prefix] [cpu0 cpu1 ...]
17
18		-h - Print this message.
19		-c - Execute count times. 0 is the default and it means to run
20		     indefinitely.
21		-d - Directory where results should be saved. Default is /tmp.
22		-p - Add prefix to saved files.
23		-t - How long to wait before each load read. Default is 1s.
24
25		cpu - Valid cpu id (e.g. 0) to monitor.
26
27		When started, ${0##*/} will enter loop to continuosly monitor cpu
28		load. Each iteration will be logged to stderr and a log file
29		(dir/${0##*/}.pm.log).
30
31	HELP
32}
33
34# get_cpu_time() uses these, so put some stubs in place
35xtrace_disable() { :; }
36xtrace_restore() { :; }
37
38_get_cpu_time() {
39	get_cpu_time "$@" > >(tee "$output_dir/$log_file")
40}
41
42count=0
43interval=1
44output_dir=/tmp
45prefix=""
46
47while getopts c:d:hp:t: opt; do
48	case "$opt" in
49		c) count=$OPTARG ;;
50		d) output_dir=$OPTARG ;;
51		h)
52			help
53			exit 0
54			;;
55		p) prefix=$OPTARG ;;
56		t) interval=$OPTARG ;;
57		*) ;;
58	esac
59done
60shift $((OPTIND - 1))
61
62declare -r log_file=${prefix:+${prefix}_}${0##*/}.pm.log
63
64mkdir -p "$output_dir"
65
66cpus=("$@")
67((${#cpus[@]} > 0)) || cpus=($(get_online_cpus))
68
69trap 'retag' USR1
70
71_get_cpu_time "$count" "" 1 "$interval" "${cpus[@]}"
72