History

Merge tag 'turbostat-fixes-for-7.0' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux

Pull turbostat fixes from Len Brown: - Fix a memory allocation issue that could corrupt output values or SEGV - Fix a perf initilization issue that could exit on some HW + kernels - Minor fixes * tag 'turbostat-fixes-for-7.0' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux: tools/power turbostat: Allow execution to continue after perf_l2_init() failure tools/power turbostat: Fix delimiter bug in print functions tools/power turbostat: Fix --show/--hide for individual cpuidle counters tools/power turbostat: Fix incorrect format variable tools/power turbostat: Consistently use print_float_value() tools/power/turbostat: Fix microcode patch level output for AMD/Hygon tools/power turbostat: Eliminate unnecessary data structure allocation tools/power turbostat: Fix swidle header vs data display tools/power turbostat: Fix illegal memory access when SMT is present and disabled

Linus Torvalds committed 7 months ago

Commit 96463e42 parents 017102b ba893ca1 file · +54 −46Browse files

Unified · Split

tools/power/x86/turbostat/turbostat.c+54−46
@@ -2409,7 +2409,7 @@ struct topo_params { · 2408 unmodified lines
24092409 int max_l3_id;
24102410 int max_node_num;
24112411 int nodes_per_pkg;
2412− int cores_per_node;
2412+ int cores_per_pkg;
24132413 int threads_per_core;
24142414 } topo;
24152415
@@ -2837,30 +2837,29 @@ static inline int print_name(int width, int *printed, char *delim, char *name, e · 421 unmodified lines
28372837 UNUSED(type);
28382838
28392839 if (format == FORMAT_RAW && width >= 64)
2840− return (sprintf(outp, "%s%-8s", (*printed++ ? delim : ""), name));
2840+ return (sprintf(outp, "%s%-8s", ((*printed)++ ? delim : ""), name));
28412841 else
2842− return (sprintf(outp, "%s%s", (*printed++ ? delim : ""), name));
2842+ return (sprintf(outp, "%s%s", ((*printed)++ ? delim : ""), name));
28432843 }
28442844
28452845 static inline int print_hex_value(int width, int *printed, char *delim, unsigned long long value)
28462846 {
28472847 if (width <= 32)
2848− return (sprintf(outp, "%s%08x", (*printed++ ? delim : ""), (unsigned int)value));
2848+ return (sprintf(outp, "%s%08x", ((*printed)++ ? delim : ""), (unsigned int)value));
28492849 else
2850− return (sprintf(outp, "%s%016llx", (*printed++ ? delim : ""), value));
2850+ return (sprintf(outp, "%s%016llx", ((*printed)++ ? delim : ""), value));
28512851 }
28522852
28532853 static inline int print_decimal_value(int width, int *printed, char *delim, unsigned long long value)
28542854 {
2855− if (width <= 32)
2856− return (sprintf(outp, "%s%d", (*printed++ ? delim : ""), (unsigned int)value));
2857− else
2858− return (sprintf(outp, "%s%-8lld", (*printed++ ? delim : ""), value));
2855+ UNUSED(width);
2856+
2857+ return (sprintf(outp, "%s%lld", ((*printed)++ ? delim : ""), value));
28592858 }
28602859
28612860 static inline int print_float_value(int *printed, char *delim, double value)
28622861 {
2863− return (sprintf(outp, "%s%0.2f", (*printed++ ? delim : ""), value));
2862+ return (sprintf(outp, "%s%0.2f", ((*printed)++ ? delim : ""), value));
28642863 }
28652864
28662865 void print_header(char *delim)
@@ -3469,7 +3468,7 @@ int format_counters(PER_THREAD_PARAMS) · 602 unmodified lines
34693468 for (i = 0, pp = sys.perf_tp; pp; ++i, pp = pp->next) {
34703469 if (pp->format == FORMAT_RAW)
34713470 outp += print_hex_value(pp->width, &printed, delim, t->perf_counter[i]);
3472− else if (pp->format == FORMAT_DELTA || mp->format == FORMAT_AVERAGE)
3471+ else if (pp->format == FORMAT_DELTA || pp->format == FORMAT_AVERAGE)
34733472 outp += print_decimal_value(pp->width, &printed, delim, t->perf_counter[i]);
34743473 else if (pp->format == FORMAT_PERCENT) {
34753474 if (pp->type == COUNTER_USEC)
@@ -3490,12 +3489,12 @@ int format_counters(PER_THREAD_PARAMS) · 14 unmodified lines
34903489
34913490 case PMT_TYPE_XTAL_TIME:
34923491 value_converted = pct(value_raw / crystal_hz, interval_float);
3493− outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), value_converted);
3492+ outp += print_float_value(&printed, delim, value_converted);
34943493 break;
34953494
34963495 case PMT_TYPE_TCORE_CLOCK:
34973496 value_converted = pct(value_raw / tcore_clock_freq_hz, interval_float);
3498− outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), value_converted);
3497+ outp += print_float_value(&printed, delim, value_converted);
34993498 }
35003499 }
35013500
@@ -3539,7 +3538,7 @@ int format_counters(PER_THREAD_PARAMS) · 37 unmodified lines
35393538 for (i = 0, pp = sys.perf_cp; pp; i++, pp = pp->next) {
35403539 if (pp->format == FORMAT_RAW)
35413540 outp += print_hex_value(pp->width, &printed, delim, c->perf_counter[i]);
3542− else if (pp->format == FORMAT_DELTA || mp->format == FORMAT_AVERAGE)
3541+ else if (pp->format == FORMAT_DELTA || pp->format == FORMAT_AVERAGE)
35433542 outp += print_decimal_value(pp->width, &printed, delim, c->perf_counter[i]);
35443543 else if (pp->format == FORMAT_PERCENT)
35453544 outp += print_float_value(&printed, delim, pct(c->perf_counter[i], tsc));
@@ -3695,7 +3694,7 @@ int format_counters(PER_THREAD_PARAMS) · 149 unmodified lines
36953694 outp += print_hex_value(pp->width, &printed, delim, p->perf_counter[i]);
36963695 else if (pp->type == COUNTER_K2M)
36973696 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), (unsigned int)p->perf_counter[i] / 1000);
3698− else if (pp->format == FORMAT_DELTA || mp->format == FORMAT_AVERAGE)
3697+ else if (pp->format == FORMAT_DELTA || pp->format == FORMAT_AVERAGE)
36993698 outp += print_decimal_value(pp->width, &printed, delim, p->perf_counter[i]);
37003699 else if (pp->format == FORMAT_PERCENT)
37013700 outp += print_float_value(&printed, delim, pct(p->perf_counter[i], tsc));
@@ -9122,10 +9121,13 @@ void process_cpuid() · 5420 unmodified lines
91229121 cpuid_has_hv = ecx_flags & (1 << 31);
91239122
91249123 if (!no_msr) {
9125− if (get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch))
9124+ if (get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch)) {
91269125 warnx("get_msr(UCODE)");
9127− else
9126+ } else {
91289127 ucode_patch_valid = true;
9128+ if (!authentic_amd && !hygon_genuine)
9129+ ucode_patch >>= 32;
9130+ }
91299131 }
91309132
91319133 /*
@@ -9139,7 +9141,7 @@ void process_cpuid() · 7 unmodified lines
91399141 if (!quiet) {
91409142 fprintf(outf, "CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d)", family, model, stepping, family, model, stepping);
91419143 if (ucode_patch_valid)
9142− fprintf(outf, " microcode 0x%x", (unsigned int)((ucode_patch >> 32) & 0xFFFFFFFF));
9144+ fprintf(outf, " microcode 0x%x", (unsigned int)ucode_patch);
91439145 fputc('\n', outf);
91449146
91459147 fprintf(outf, "CPUID(0x80000000): max_extended_levels: 0x%x\n", max_extended_level);
@@ -9403,13 +9405,13 @@ void perf_l2_init(void) · 257 unmodified lines
94039405 if (!is_hybrid) {
94049406 fd_l2_percpu[cpu] = open_perf_counter(cpu, perf_pmu_types.uniform, perf_model_support->first.refs, -1, PERF_FORMAT_GROUP);
94059407 if (fd_l2_percpu[cpu] == -1) {
9406− err(-1, "%s(cpu%d, 0x%x, 0x%llx) REFS", __func__, cpu, perf_pmu_types.uniform, perf_model_support->first.refs);
9408+ warnx("%s(cpu%d, 0x%x, 0x%llx) REFS", __func__, cpu, perf_pmu_types.uniform, perf_model_support->first.refs);
94079409 free_fd_l2_percpu();
94089410 return;
94099411 }
94109412 retval = open_perf_counter(cpu, perf_pmu_types.uniform, perf_model_support->first.hits, fd_l2_percpu[cpu], PERF_FORMAT_GROUP);
94119413 if (retval == -1) {
9412− err(-1, "%s(cpu%d, 0x%x, 0x%llx) HITS", __func__, cpu, perf_pmu_types.uniform, perf_model_support->first.hits);
9414+ warnx("%s(cpu%d, 0x%x, 0x%llx) HITS", __func__, cpu, perf_pmu_types.uniform, perf_model_support->first.hits);
94139415 free_fd_l2_percpu();
94149416 return;
94159417 }
@@ -9418,39 +9420,39 @@ void perf_l2_init(void) · 2 unmodified lines
94189420 if (perf_pcore_set && CPU_ISSET_S(cpu, cpu_possible_setsize, perf_pcore_set)) {
94199421 fd_l2_percpu[cpu] = open_perf_counter(cpu, perf_pmu_types.pcore, perf_model_support->first.refs, -1, PERF_FORMAT_GROUP);
94209422 if (fd_l2_percpu[cpu] == -1) {
9421− err(-1, "%s(cpu%d, 0x%x, 0x%llx) REFS", __func__, cpu, perf_pmu_types.pcore, perf_model_support->first.refs);
9423+ warnx("%s(cpu%d, 0x%x, 0x%llx) REFS", __func__, cpu, perf_pmu_types.pcore, perf_model_support->first.refs);
94229424 free_fd_l2_percpu();
94239425 return;
94249426 }
94259427 retval = open_perf_counter(cpu, perf_pmu_types.pcore, perf_model_support->first.hits, fd_l2_percpu[cpu], PERF_FORMAT_GROUP);
94269428 if (retval == -1) {
9427− err(-1, "%s(cpu%d, 0x%x, 0x%llx) HITS", __func__, cpu, perf_pmu_types.pcore, perf_model_support->first.hits);
9429+ warnx("%s(cpu%d, 0x%x, 0x%llx) HITS", __func__, cpu, perf_pmu_types.pcore, perf_model_support->first.hits);
94289430 free_fd_l2_percpu();
94299431 return;
94309432 }
94319433 } else if (perf_ecore_set && CPU_ISSET_S(cpu, cpu_possible_setsize, perf_ecore_set)) {
94329434 fd_l2_percpu[cpu] = open_perf_counter(cpu, perf_pmu_types.ecore, perf_model_support->second.refs, -1, PERF_FORMAT_GROUP);
94339435 if (fd_l2_percpu[cpu] == -1) {
9434− err(-1, "%s(cpu%d, 0x%x, 0x%llx) REFS", __func__, cpu, perf_pmu_types.pcore, perf_model_support->second.refs);
9436+ warnx("%s(cpu%d, 0x%x, 0x%llx) REFS", __func__, cpu, perf_pmu_types.ecore, perf_model_support->second.refs);
94359437 free_fd_l2_percpu();
94369438 return;
94379439 }
94389440 retval = open_perf_counter(cpu, perf_pmu_types.ecore, perf_model_support->second.hits, fd_l2_percpu[cpu], PERF_FORMAT_GROUP);
94399441 if (retval == -1) {
9440− err(-1, "%s(cpu%d, 0x%x, 0x%llx) HITS", __func__, cpu, perf_pmu_types.pcore, perf_model_support->second.hits);
9442+ warnx("%s(cpu%d, 0x%x, 0x%llx) HITS", __func__, cpu, perf_pmu_types.ecore, perf_model_support->second.hits);
94419443 free_fd_l2_percpu();
94429444 return;
94439445 }
94449446 } else if (perf_lcore_set && CPU_ISSET_S(cpu, cpu_possible_setsize, perf_lcore_set)) {
94459447 fd_l2_percpu[cpu] = open_perf_counter(cpu, perf_pmu_types.lcore, perf_model_support->third.refs, -1, PERF_FORMAT_GROUP);
94469448 if (fd_l2_percpu[cpu] == -1) {
9447− err(-1, "%s(cpu%d, 0x%x, 0x%llx) REFS", __func__, cpu, perf_pmu_types.pcore, perf_model_support->third.refs);
9449+ warnx("%s(cpu%d, 0x%x, 0x%llx) REFS", __func__, cpu, perf_pmu_types.lcore, perf_model_support->third.refs);
94489450 free_fd_l2_percpu();
94499451 return;
94509452 }
94519453 retval = open_perf_counter(cpu, perf_pmu_types.lcore, perf_model_support->third.hits, fd_l2_percpu[cpu], PERF_FORMAT_GROUP);
94529454 if (retval == -1) {
9453− err(-1, "%s(cpu%d, 0x%x, 0x%llx) HITS", __func__, cpu, perf_pmu_types.pcore, perf_model_support->third.hits);
9455+ warnx("%s(cpu%d, 0x%x, 0x%llx) HITS", __func__, cpu, perf_pmu_types.lcore, perf_model_support->third.hits);
94549456 free_fd_l2_percpu();
94559457 return;
94569458 }
@@ -9634,9 +9636,9 @@ void topology_probe(bool startup) · 177 unmodified lines
96349636 topo.max_core_id = max_core_id; /* within a package */
96359637 topo.max_package_id = max_package_id;
96369638
9637− topo.cores_per_node = max_core_id + 1;
9639+ topo.cores_per_pkg = max_core_id + 1;
96389640 if (debug > 1)
9639− fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", max_core_id, topo.cores_per_node);
9641+ fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", max_core_id, topo.cores_per_pkg);
96409642 if (!summary_only)
96419643 BIC_PRESENT(BIC_Core);
96429644
@@ -9701,14 +9703,13 @@ error: · 58 unmodified lines
97019703 void allocate_counters(struct counters *counters)
97029704 {
97039705 int i;
9704− int num_cores = topo.cores_per_node * topo.nodes_per_pkg * topo.num_packages;
9705− int num_threads = topo.threads_per_core * num_cores;
9706+ int num_cores = topo.cores_per_pkg * topo.num_packages;
97069707
9707− counters->threads = calloc(num_threads, sizeof(struct thread_data));
9708+ counters->threads = calloc(topo.max_cpu_num + 1, sizeof(struct thread_data));
97089709 if (counters->threads == NULL)
97099710 goto error;
97109711
9711− for (i = 0; i < num_threads; i++)
9712+ for (i = 0; i < topo.max_cpu_num + 1; i++)
97129713 (counters->threads)[i].cpu_id = -1;
97139714
97149715 counters->cores = calloc(num_cores, sizeof(struct core_data));
@@ -11284,6 +11285,14 @@ void probe_cpuidle_residency(void) · 1569 unmodified lines
1128411285 }
1128511286 }
1128611287
11288+static bool cpuidle_counter_wanted(char *name)
11289+{
11290+ if (is_deferred_skip(name))
11291+ return false;
11292+
11293+ return DO_BIC(BIC_cpuidle) || is_deferred_add(name);
11294+}
11295+
1128711296 void probe_cpuidle_counts(void)
1128811297 {
1128911298 char path[64];
@@ -11293,7 +11302,7 @@ void probe_cpuidle_counts(void) · 3 unmodified lines
1129311302 int min_state = 1024, max_state = 0;
1129411303 char *sp;
1129511304
11296− if (!DO_BIC(BIC_cpuidle))
11305+ if (!DO_BIC(BIC_cpuidle) && !deferred_add_index)
1129711306 return;
1129811307
1129911308 for (state = 10; state >= 0; --state) {
@@ -11308,12 +11317,6 @@ void probe_cpuidle_counts(void) · 8 unmodified lines
1130811317
1130911318 remove_underbar(name_buf);
1131011319
11311− if (!DO_BIC(BIC_cpuidle) && !is_deferred_add(name_buf))
11312− continue;
11313−
11314− if (is_deferred_skip(name_buf))
11315− continue;
11316−
1131711320 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
1131811321 sp = strchr(name_buf, '-');
1131911322 if (!sp)
@@ -11328,16 +11331,19 @@ void probe_cpuidle_counts(void) · 8 unmodified lines
1132811331 * Add 'C1+' for C1, and so on. The 'below' sysfs file always contains 0 for
1132911332 * the last state, so do not add it.
1133011333 */
11331−
1133211334 *sp = '+';
1133311335 *(sp + 1) = '\0';
11334− sprintf(path, "cpuidle/state%d/below", state);
11335− add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU, 0);
11336+ if (cpuidle_counter_wanted(name_buf)) {
11337+ sprintf(path, "cpuidle/state%d/below", state);
11338+ add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU, 0);
11339+ }
1133611340 }
1133711341
1133811342 *sp = '\0';
11339− sprintf(path, "cpuidle/state%d/usage", state);
11340− add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU, 0);
11343+ if (cpuidle_counter_wanted(name_buf)) {
11344+ sprintf(path, "cpuidle/state%d/usage", state);
11345+ add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU, 0);
11346+ }
1134111347
1134211348 /*
1134311349 * The 'above' sysfs file always contains 0 for the shallowest state (smallest
@@ -11346,8 +11352,10 @@ void probe_cpuidle_counts(void) · 2 unmodified lines
1134611352 if (state != min_state) {
1134711353 *sp = '-';
1134811354 *(sp + 1) = '\0';
11349− sprintf(path, "cpuidle/state%d/above", state);
11350− add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU, 0);
11355+ if (cpuidle_counter_wanted(name_buf)) {
11356+ sprintf(path, "cpuidle/state%d/above", state);
11357+ add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU, 0);
11358+ }
1135111359 }
1135211360 }
1135311361 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t