From b4cc30ac10f200613824686a8be49820ed9c98ce Mon Sep 17 00:00:00 2001 From: Thomas Richter Date: Thu, 27 Mar 2025 14:09:57 +0100 Subject: [PATCH] cpumf/lshwc: Add command line flag for run time MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a command line flag to specify run time of lshwc command in terms of days, hours, minutes and seconds. The command line flag is named -t and has a number as argument followed by one of the letters 's' for seconds (default), 'm' for minutes, 'h' for hours and 'd' for days. Default is seconds when no trailing letter is detected. Signed-off-by: Thomas Richter Reviewed-by: Jan Höppner Signed-off-by: Jan Höppner --- cpumf/lshwc.c | 37 ++++++++++++++++++++++++++++++++++++- cpumf/man/lshwc.8 | 21 ++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/cpumf/lshwc.c b/cpumf/lshwc.c index d5868273..af1ba7b0 100644 --- a/cpumf/lshwc.c +++ b/cpumf/lshwc.c @@ -51,7 +51,7 @@ static unsigned int read_interval = IOCTLSLEEP; static int cfvn, csvn, authorization; -static unsigned long loop_count = 1; +static unsigned long loop_count = 1, timeout; static unsigned char *ioctlbuffer; static bool allcpu; static char *ctrformat = "%ld"; @@ -684,6 +684,11 @@ static struct util_opt opt_vec[] = { .option = { "delta", no_argument, NULL, 'd' }, .desc = "Display delta counter values" }, + { + .option = { "timeout", required_argument, NULL, 't' }, + .argument = "NUMBER", + .desc = "run time in s (seconds) m (minutes) h (hours) and d (days)" + }, UTIL_OPT_HELP, UTIL_OPT_VERSION, UTIL_OPT_END @@ -713,6 +718,7 @@ static void have_support(void) int main(int argc, char **argv) { + unsigned long no; char *slash; int ch; @@ -763,9 +769,38 @@ int main(int argc, char **argv) delta = true; firstread = true; break; + case 't': + errno = 0; + no = strtoul(optarg, &slash, 0); + if (errno) + errx(EXIT_FAILURE, "Invalid argument for -%c", ch); + switch (*slash) { + case 's': + case '\0': + timeout += no; + break; + case 'm': + timeout += no * 60; + break; + case 'h': + timeout += no * 60 * 60; + break; + case 'd': + timeout += no * 60 * 60 * 24; + break; + default: + errx(EXIT_FAILURE, "Invalid argument for -%c", ch); + break; + } + break; } } + if (timeout && timeout < read_interval) + read_interval = timeout; + /* If no timeout specified, simply add zero */ + loop_count += timeout / read_interval; + have_support(); if (!libcpumf_cpumcf_info(&cfvn, &csvn, &authorization)) return EXIT_FAILURE; diff --git a/cpumf/man/lshwc.8 b/cpumf/man/lshwc.8 index c0dc2624..d650848f 100644 --- a/cpumf/man/lshwc.8 +++ b/cpumf/man/lshwc.8 @@ -14,11 +14,13 @@ lshwc \- extract CPU Measurement Facilities counter sets . .SH SYNOPSIS \*c -.RB [ \-a ][ \-d][ \-H][ \-s ][ \-x ][ \-X] +.RB [ \-a ][ \-d ][ \-H ][ \-s ][ \-x ][ \-X ] .RB [ \-l .IR count ] .RB [ \-i .IR interval ] +.RB [ \-t +.IR time ] \fR[\fIcpulist\fR][:\fIsets\fR]\fP .br \*c @@ -99,6 +101,23 @@ Undefined counter. .RE . .TP +.BR \-t ", " \-\-time +Specifies the runtime in form of days, hours, minutes or seconds. +This option can be specified multiple times and the runtime adds up. +Time is a number followed by one of the letters +.RS +.IP d +Specifies number as days. +.IP h +Specifies number as hours. +.IP m +Specifies number as minutes. +.IP s +Specifies number as seconds. This is the default when no +letter follows the number. +.RE +. +.TP .BR \-x ", " \-\-hex Displays counter values as hexadecimal values. .