Index | Thread | Search

From:
"Ted Unangst" <tedu@tedunangst.com>
Subject:
iostat cpu stats
To:
tech@openbsd.org
Date:
Tue, 06 May 2025 17:36:05 -0400

Download raw body.

Thread
  • Ted Unangst:

    iostat cpu stats

Today's featured column width is iostat.

Running iostat -w 1 I see a lot of messy output in the idle column.

   0   68  0.00    0    0.00   0.00    0    0.00   0  0  1  0  0 99
   0   70  0.00    0    0.00   0.00    0    0.00   0  0  0  0  0100
   0   69  2.00    2    0.00   2.00    2    0.00   0  0  0  0  0100
   0   69  0.00    0    0.00   0.00    0    0.00   0  0  0  0  1 99

Looking at the code, there's even a comment that 100% is too rare to
worry about. But my laptop is faster than the vaxen of yore, and I see
100 all the time. I think a passable fix is to simply truncate to 99.
The columns are never guaranteed to sum up, and 100 in one column is
always a rounding error, so I think this is sufficiently accurate.

   0   69  0.00    0    0.00   0.00    0    0.00   0  0  0  0  0 99
   0   69 16.00    2    0.03  16.00    2    0.03   0  0  0  0  0 99
   0   69 14.25   16    0.22  14.25   16    0.22   0  0  0  0  0 99
   0   68  0.00    0    0.00   0.00    0    0.00   0  0  0  0  1 99

Index: iostat.c
===================================================================
RCS file: /home/cvs/src/usr.sbin/iostat/iostat.c,v
diff -u -p -r1.47 iostat.c
--- iostat.c	8 Mar 2023 04:43:13 -0000	1.47
+++ iostat.c	6 May 2025 21:30:33 -0000
@@ -354,8 +354,12 @@ cpustats(void)
 	if (!t)
 		t = 1.0;
 	/* States are generally never 100% and can use %3.0f. */
-	for (state = 0; state < CPUSTATES; ++state)
-		printf("%3.0f", 100. * cur.cp_time[state] / t);
+	for (state = 0; state < CPUSTATES; ++state) {
+		double v = 100. * cur.cp_time[state] / t;
+		if (v > 99)
+			v = 99;
+		printf("%3.0f", v);
+	}
 }
 
 static void