Skip to content

Commit 8808292

Browse files
committed
tools/power turbostat: Print "nan" for out of range percentages
Sometimes counters return junk. For the cases where values > 100% is invalid, print "nan". Signed-off-by: Len Brown <len.brown@intel.com>
1 parent 951845d commit 8808292

1 file changed

Lines changed: 53 additions & 39 deletions

File tree

tools/power/x86/turbostat/turbostat.c

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,6 +2999,25 @@ void print_header(char *delim)
29992999
outp += sprintf(outp, "\n");
30003000
}
30013001

3002+
/*
3003+
* pct()
3004+
*
3005+
* If absolute value is < 1.1, return percentage
3006+
* otherwise, return nan
3007+
*
3008+
* return value is appropriate for printing percentages with %f
3009+
* while flagging some obvious erroneous values.
3010+
*/
3011+
double pct(double d)
3012+
{
3013+
3014+
double abs = fabs(d);
3015+
3016+
if (abs < 1.10)
3017+
return (100.0 * d);
3018+
return nan("");
3019+
}
3020+
30023021
int dump_counters(PER_THREAD_PARAMS)
30033022
{
30043023
int i;
@@ -3026,7 +3045,7 @@ int dump_counters(PER_THREAD_PARAMS)
30263045

30273046
outp += sprintf(outp, "LLC refs: %lld", t->llc.references);
30283047
outp += sprintf(outp, "LLC miss: %lld", t->llc.misses);
3029-
outp += sprintf(outp, "LLC Hit%%: %.2f", 100.0 * (t->llc.references - t->llc.misses) / t->llc.references);
3048+
outp += sprintf(outp, "LLC Hit%%: %.2f", pct((t->llc.references - t->llc.misses) / t->llc.references));
30303049

30313050
for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
30323051
outp +=
@@ -3248,7 +3267,7 @@ int format_counters(PER_THREAD_PARAMS)
32483267
outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 / units * t->aperf / interval_float);
32493268

32503269
if (DO_BIC(BIC_Busy))
3251-
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf / tsc);
3270+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(t->mperf / tsc));
32523271

32533272
if (DO_BIC(BIC_Bzy_MHz)) {
32543273
if (has_base_hz)
@@ -3291,7 +3310,7 @@ int format_counters(PER_THREAD_PARAMS)
32913310
outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), t->llc.references / interval_float / 1000);
32923311

32933312
if (DO_BIC(BIC_LLC_HIT))
3294-
outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * (t->llc.references - t->llc.misses) / t->llc.references);
3313+
outp += sprintf(outp, fmt8, (printed++ ? delim : ""), pct((t->llc.references - t->llc.misses) / t->llc.references));
32953314
}
32963315

32973316

@@ -3305,7 +3324,7 @@ int format_counters(PER_THREAD_PARAMS)
33053324
if (mp->type == COUNTER_USEC)
33063325
outp += print_float_value(&printed, delim, t->counter[i] / interval_float / 10000);
33073326
else
3308-
outp += print_float_value(&printed, delim, 100.0 * t->counter[i] / tsc);
3327+
outp += print_float_value(&printed, delim, pct(t->counter[i] / tsc));
33093328
}
33103329
}
33113330

@@ -3319,7 +3338,7 @@ int format_counters(PER_THREAD_PARAMS)
33193338
if (pp->type == COUNTER_USEC)
33203339
outp += print_float_value(&printed, delim, t->perf_counter[i] / interval_float / 10000);
33213340
else
3322-
outp += print_float_value(&printed, delim, 100.0 * t->perf_counter[i] / tsc);
3341+
outp += print_float_value(&printed, delim, pct(t->perf_counter[i] / tsc));
33233342
}
33243343
}
33253344

@@ -3333,34 +3352,34 @@ int format_counters(PER_THREAD_PARAMS)
33333352
break;
33343353

33353354
case PMT_TYPE_XTAL_TIME:
3336-
value_converted = 100.0 * value_raw / crystal_hz / interval_float;
3355+
value_converted = pct(value_raw / crystal_hz / interval_float);
33373356
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), value_converted);
33383357
break;
33393358

33403359
case PMT_TYPE_TCORE_CLOCK:
3341-
value_converted = 100.0 * value_raw / tcore_clock_freq_hz / interval_float;
3360+
value_converted = pct(value_raw / tcore_clock_freq_hz / interval_float);
33423361
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), value_converted);
33433362
}
33443363
}
33453364

33463365
/* C1 */
33473366
if (DO_BIC(BIC_CPU_c1))
3348-
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1 / tsc);
3367+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(t->c1 / tsc));
33493368

33503369
/* print per-core data only for 1st thread in core */
33513370
if (!is_cpu_first_thread_in_core(t, c))
33523371
goto done;
33533372

33543373
if (DO_BIC(BIC_CPU_c3))
3355-
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3 / tsc);
3374+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(c->c3 / tsc));
33563375
if (DO_BIC(BIC_CPU_c6))
3357-
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6 / tsc);
3376+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(c->c6 / tsc));
33583377
if (DO_BIC(BIC_CPU_c7))
3359-
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7 / tsc);
3378+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(c->c7 / tsc));
33603379

33613380
/* Mod%c6 */
33623381
if (DO_BIC(BIC_Mod_c6))
3363-
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
3382+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(c->mc6_us / tsc));
33643383

33653384
if (DO_BIC(BIC_CoreTmp))
33663385
outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
@@ -3376,7 +3395,7 @@ int format_counters(PER_THREAD_PARAMS)
33763395
else if (mp->format == FORMAT_DELTA || mp->format == FORMAT_AVERAGE)
33773396
outp += print_decimal_value(mp->width, &printed, delim, c->counter[i]);
33783397
else if (mp->format == FORMAT_PERCENT)
3379-
outp += print_float_value(&printed, delim, 100.0 * c->counter[i] / tsc);
3398+
outp += print_float_value(&printed, delim, pct(c->counter[i] / tsc));
33803399
}
33813400

33823401
/* Added perf Core counters */
@@ -3386,7 +3405,7 @@ int format_counters(PER_THREAD_PARAMS)
33863405
else if (pp->format == FORMAT_DELTA || mp->format == FORMAT_AVERAGE)
33873406
outp += print_decimal_value(pp->width, &printed, delim, c->perf_counter[i]);
33883407
else if (pp->format == FORMAT_PERCENT)
3389-
outp += print_float_value(&printed, delim, 100.0 * c->perf_counter[i] / tsc);
3408+
outp += print_float_value(&printed, delim, pct(c->perf_counter[i] / tsc));
33903409
}
33913410

33923411
/* Added PMT Core counters */
@@ -3399,12 +3418,12 @@ int format_counters(PER_THREAD_PARAMS)
33993418
break;
34003419

34013420
case PMT_TYPE_XTAL_TIME:
3402-
value_converted = 100.0 * value_raw / crystal_hz / interval_float;
3421+
value_converted = pct(value_raw / crystal_hz / interval_float);
34033422
outp += print_float_value(&printed, delim, value_converted);
34043423
break;
34053424

34063425
case PMT_TYPE_TCORE_CLOCK:
3407-
value_converted = 100.0 * value_raw / tcore_clock_freq_hz / interval_float;
3426+
value_converted = pct(value_raw / tcore_clock_freq_hz / interval_float);
34083427
outp += print_float_value(&printed, delim, value_converted);
34093428
}
34103429
}
@@ -3463,46 +3482,41 @@ int format_counters(PER_THREAD_PARAMS)
34633482

34643483
/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
34653484
if (DO_BIC(BIC_Totl_c0))
3466-
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0 / tsc);
3485+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100 * p->pkg_wtd_core_c0 / tsc); /* can exceed 100% */
34673486
if (DO_BIC(BIC_Any_c0))
3468-
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0 / tsc);
3487+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pkg_any_core_c0 / tsc));
34693488
if (DO_BIC(BIC_GFX_c0))
3470-
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0 / tsc);
3489+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pkg_any_gfxe_c0 / tsc));
34713490
if (DO_BIC(BIC_CPUGFX))
3472-
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0 / tsc);
3491+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pkg_both_core_gfxe_c0 / tsc));
34733492

34743493
if (DO_BIC(BIC_Pkgpc2))
3475-
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2 / tsc);
3494+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pc2 / tsc));
34763495
if (DO_BIC(BIC_Pkgpc3))
3477-
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3 / tsc);
3496+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pc3 / tsc));
34783497
if (DO_BIC(BIC_Pkgpc6))
3479-
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6 / tsc);
3498+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pc6 / tsc));
34803499
if (DO_BIC(BIC_Pkgpc7))
3481-
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7 / tsc);
3500+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pc7 / tsc));
34823501
if (DO_BIC(BIC_Pkgpc8))
3483-
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8 / tsc);
3502+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pc8 / tsc));
34843503
if (DO_BIC(BIC_Pkgpc9))
3485-
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9 / tsc);
3504+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pc9 / tsc));
34863505
if (DO_BIC(BIC_Pkgpc10))
3487-
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10 / tsc);
3506+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->pc10 / tsc));
34883507

34893508
if (DO_BIC(BIC_Diec6))
3490-
outp +=
3491-
sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->die_c6 / crystal_hz / interval_float);
3509+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->die_c6 / crystal_hz / interval_float));
34923510

34933511
if (DO_BIC(BIC_CPU_LPI)) {
34943512
if (p->cpu_lpi >= 0)
3495-
outp +=
3496-
sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
3497-
100.0 * p->cpu_lpi / 1000000.0 / interval_float);
3513+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->cpu_lpi / 1000000.0 / interval_float));
34983514
else
34993515
outp += sprintf(outp, "%s(neg)", (printed++ ? delim : ""));
35003516
}
35013517
if (DO_BIC(BIC_SYS_LPI)) {
35023518
if (p->sys_lpi >= 0)
3503-
outp +=
3504-
sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
3505-
100.0 * p->sys_lpi / 1000000.0 / interval_float);
3519+
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), pct(p->sys_lpi / 1000000.0 / interval_float));
35063520
else
35073521
outp += sprintf(outp, "%s(neg)", (printed++ ? delim : ""));
35083522
}
@@ -3556,7 +3570,7 @@ int format_counters(PER_THREAD_PARAMS)
35563570
else if (mp->format == FORMAT_DELTA || mp->format == FORMAT_AVERAGE)
35573571
outp += print_decimal_value(mp->width, &printed, delim, p->counter[i]);
35583572
else if (mp->format == FORMAT_PERCENT)
3559-
outp += print_float_value(&printed, delim, 100.0 * p->counter[i] / tsc);
3573+
outp += print_float_value(&printed, delim, pct(p->counter[i] / tsc));
35603574
}
35613575

35623576
/* Added perf Package Counters */
@@ -3569,7 +3583,7 @@ int format_counters(PER_THREAD_PARAMS)
35693583
else if (pp->format == FORMAT_DELTA || mp->format == FORMAT_AVERAGE)
35703584
outp += print_decimal_value(pp->width, &printed, delim, p->perf_counter[i]);
35713585
else if (pp->format == FORMAT_PERCENT)
3572-
outp += print_float_value(&printed, delim, 100.0 * p->perf_counter[i] / tsc);
3586+
outp += print_float_value(&printed, delim, pct(p->perf_counter[i] / tsc));
35733587
}
35743588

35753589
/* Added PMT Package Counters */
@@ -3582,12 +3596,12 @@ int format_counters(PER_THREAD_PARAMS)
35823596
break;
35833597

35843598
case PMT_TYPE_XTAL_TIME:
3585-
value_converted = 100.0 * value_raw / crystal_hz / interval_float;
3599+
value_converted = pct(value_raw / crystal_hz / interval_float);
35863600
outp += print_float_value(&printed, delim, value_converted);
35873601
break;
35883602

35893603
case PMT_TYPE_TCORE_CLOCK:
3590-
value_converted = 100.0 * value_raw / tcore_clock_freq_hz / interval_float;
3604+
value_converted = pct(value_raw / tcore_clock_freq_hz / interval_float);
35913605
outp += print_float_value(&printed, delim, value_converted);
35923606
}
35933607
}

0 commit comments

Comments
 (0)