turbostat.c (937807d355a375393557674e3233662a7131c46b) turbostat.c (6de68fe15a0fcd0e887d73bd7a549e4dc6446481)
1/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel and AMD processors.
4 *
5 * Copyright (c) 2013 Intel Corporation.
6 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it

--- 258 unchanged lines hidden (view full) ---

267struct system_summary {
268 struct thread_data threads;
269 struct core_data cores;
270 struct pkg_data packages;
271} average;
272
273struct cpu_topology {
274 int physical_package_id;
1/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel and AMD processors.
4 *
5 * Copyright (c) 2013 Intel Corporation.
6 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it

--- 258 unchanged lines hidden (view full) ---

267struct system_summary {
268 struct thread_data threads;
269 struct core_data cores;
270 struct pkg_data packages;
271} average;
272
273struct cpu_topology {
274 int physical_package_id;
275 int die_id;
275 int logical_cpu_id;
276 int physical_node_id;
277 int logical_node_id; /* 0-based count within the package */
278 int physical_core_id;
279 int thread_id;
280 cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
281} *cpus;
282
283struct topo_params {
284 int num_packages;
276 int logical_cpu_id;
277 int physical_node_id;
278 int logical_node_id; /* 0-based count within the package */
279 int physical_core_id;
280 int thread_id;
281 cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
282} *cpus;
283
284struct topo_params {
285 int num_packages;
286 int num_die;
285 int num_cpus;
286 int num_cores;
287 int max_cpu_num;
288 int max_node_num;
289 int nodes_per_pkg;
290 int cores_per_node;
291 int threads_per_core;
292} topo;

--- 142 unchanged lines hidden (view full) ---

435 { 0x0, "Totl%C0" },
436 { 0x0, "Any%C0" },
437 { 0x0, "GFX%C0" },
438 { 0x0, "CPUGFX%" },
439 { 0x0, "Core" },
440 { 0x0, "CPU" },
441 { 0x0, "APIC" },
442 { 0x0, "X2APIC" },
287 int num_cpus;
288 int num_cores;
289 int max_cpu_num;
290 int max_node_num;
291 int nodes_per_pkg;
292 int cores_per_node;
293 int threads_per_core;
294} topo;

--- 142 unchanged lines hidden (view full) ---

437 { 0x0, "Totl%C0" },
438 { 0x0, "Any%C0" },
439 { 0x0, "GFX%C0" },
440 { 0x0, "CPUGFX%" },
441 { 0x0, "Core" },
442 { 0x0, "CPU" },
443 { 0x0, "APIC" },
444 { 0x0, "X2APIC" },
445 { 0x0, "Die" },
443};
444
445#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
446#define BIC_USEC (1ULL << 0)
447#define BIC_TOD (1ULL << 1)
448#define BIC_Package (1ULL << 2)
449#define BIC_Node (1ULL << 3)
450#define BIC_Avg_MHz (1ULL << 4)

--- 37 unchanged lines hidden (view full) ---

488#define BIC_Totl_c0 (1ULL << 42)
489#define BIC_Any_c0 (1ULL << 43)
490#define BIC_GFX_c0 (1ULL << 44)
491#define BIC_CPUGFX (1ULL << 45)
492#define BIC_Core (1ULL << 46)
493#define BIC_CPU (1ULL << 47)
494#define BIC_APIC (1ULL << 48)
495#define BIC_X2APIC (1ULL << 49)
446};
447
448#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
449#define BIC_USEC (1ULL << 0)
450#define BIC_TOD (1ULL << 1)
451#define BIC_Package (1ULL << 2)
452#define BIC_Node (1ULL << 3)
453#define BIC_Avg_MHz (1ULL << 4)

--- 37 unchanged lines hidden (view full) ---

491#define BIC_Totl_c0 (1ULL << 42)
492#define BIC_Any_c0 (1ULL << 43)
493#define BIC_GFX_c0 (1ULL << 44)
494#define BIC_CPUGFX (1ULL << 45)
495#define BIC_Core (1ULL << 46)
496#define BIC_CPU (1ULL << 47)
497#define BIC_APIC (1ULL << 48)
498#define BIC_X2APIC (1ULL << 49)
499#define BIC_Die (1ULL << 50)
496
497#define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC)
498
499unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
500unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC;
501
502#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
503#define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)

--- 110 unchanged lines hidden (view full) ---

614 int printed = 0;
615
616 if (DO_BIC(BIC_USEC))
617 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
618 if (DO_BIC(BIC_TOD))
619 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
620 if (DO_BIC(BIC_Package))
621 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
500
501#define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC)
502
503unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
504unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC;
505
506#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
507#define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)

--- 110 unchanged lines hidden (view full) ---

618 int printed = 0;
619
620 if (DO_BIC(BIC_USEC))
621 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
622 if (DO_BIC(BIC_TOD))
623 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
624 if (DO_BIC(BIC_Package))
625 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
626 if (DO_BIC(BIC_Die))
627 outp += sprintf(outp, "%sDie", (printed++ ? delim : ""));
622 if (DO_BIC(BIC_Node))
623 outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
624 if (DO_BIC(BIC_Core))
625 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
626 if (DO_BIC(BIC_CPU))
627 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
628 if (DO_BIC(BIC_APIC))
629 outp += sprintf(outp, "%sAPIC", (printed++ ? delim : ""));

--- 267 unchanged lines hidden (view full) ---

897 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
898
899 tsc = t->tsc * tsc_tweak;
900
901 /* topo columns, print blanks on 1st (average) line */
902 if (t == &average.threads) {
903 if (DO_BIC(BIC_Package))
904 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
628 if (DO_BIC(BIC_Node))
629 outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
630 if (DO_BIC(BIC_Core))
631 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
632 if (DO_BIC(BIC_CPU))
633 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
634 if (DO_BIC(BIC_APIC))
635 outp += sprintf(outp, "%sAPIC", (printed++ ? delim : ""));

--- 267 unchanged lines hidden (view full) ---

903 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
904
905 tsc = t->tsc * tsc_tweak;
906
907 /* topo columns, print blanks on 1st (average) line */
908 if (t == &average.threads) {
909 if (DO_BIC(BIC_Package))
910 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
911 if (DO_BIC(BIC_Die))
912 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
905 if (DO_BIC(BIC_Node))
906 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
907 if (DO_BIC(BIC_Core))
908 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
909 if (DO_BIC(BIC_CPU))
910 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
911 if (DO_BIC(BIC_APIC))
912 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
913 if (DO_BIC(BIC_X2APIC))
914 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
915 } else {
916 if (DO_BIC(BIC_Package)) {
917 if (p)
918 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
919 else
920 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
921 }
913 if (DO_BIC(BIC_Node))
914 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
915 if (DO_BIC(BIC_Core))
916 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
917 if (DO_BIC(BIC_CPU))
918 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
919 if (DO_BIC(BIC_APIC))
920 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
921 if (DO_BIC(BIC_X2APIC))
922 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
923 } else {
924 if (DO_BIC(BIC_Package)) {
925 if (p)
926 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
927 else
928 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
929 }
930 if (DO_BIC(BIC_Die)) {
931 if (c)
932 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id);
933 else
934 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
935 }
922 if (DO_BIC(BIC_Node)) {
923 if (t)
924 outp += sprintf(outp, "%s%d",
925 (printed++ ? delim : ""),
926 cpus[t->cpu_id].physical_node_id);
927 else
928 outp += sprintf(outp, "%s-",
929 (printed++ ? delim : ""));

--- 1519 unchanged lines hidden (view full) ---

2449 CPU_FREE(cpus[i].put_ids);
2450 }
2451 free(cpus);
2452}
2453
2454
2455/*
2456 * Parse a file containing a single int.
936 if (DO_BIC(BIC_Node)) {
937 if (t)
938 outp += sprintf(outp, "%s%d",
939 (printed++ ? delim : ""),
940 cpus[t->cpu_id].physical_node_id);
941 else
942 outp += sprintf(outp, "%s-",
943 (printed++ ? delim : ""));

--- 1519 unchanged lines hidden (view full) ---

2463 CPU_FREE(cpus[i].put_ids);
2464 }
2465 free(cpus);
2466}
2467
2468
2469/*
2470 * Parse a file containing a single int.
2471 * Return 0 if file can not be opened
2472 * Exit if file can be opened, but can not be parsed
2457 */
2458int parse_int_file(const char *fmt, ...)
2459{
2460 va_list args;
2461 char path[PATH_MAX];
2462 FILE *filep;
2463 int value;
2464
2465 va_start(args, fmt);
2466 vsnprintf(path, sizeof(path), fmt, args);
2467 va_end(args);
2473 */
2474int parse_int_file(const char *fmt, ...)
2475{
2476 va_list args;
2477 char path[PATH_MAX];
2478 FILE *filep;
2479 int value;
2480
2481 va_start(args, fmt);
2482 vsnprintf(path, sizeof(path), fmt, args);
2483 va_end(args);
2468 filep = fopen_or_die(path, "r");
2484 filep = fopen(path, "r");
2485 if (!filep)
2486 return 0;
2469 if (fscanf(filep, "%d", &value) != 1)
2470 err(1, "%s: failed to parse number from file", path);
2471 fclose(filep);
2472 return value;
2473}
2474
2475/*
2476 * cpu_is_first_core_in_package(cpu)

--- 4 unchanged lines hidden (view full) ---

2481 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2482}
2483
2484int get_physical_package_id(int cpu)
2485{
2486 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2487}
2488
2487 if (fscanf(filep, "%d", &value) != 1)
2488 err(1, "%s: failed to parse number from file", path);
2489 fclose(filep);
2490 return value;
2491}
2492
2493/*
2494 * cpu_is_first_core_in_package(cpu)

--- 4 unchanged lines hidden (view full) ---

2499 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2500}
2501
2502int get_physical_package_id(int cpu)
2503{
2504 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2505}
2506
2507int get_die_id(int cpu)
2508{
2509 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu);
2510}
2511
2489int get_core_id(int cpu)
2490{
2491 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2492}
2493
2494void set_node_data(void)
2495{
2496 int pkg, node, lnode, cpu, cpux;

--- 2270 unchanged lines hidden (view full) ---

4767 return 0;
4768}
4769
4770void topology_probe()
4771{
4772 int i;
4773 int max_core_id = 0;
4774 int max_package_id = 0;
2512int get_core_id(int cpu)
2513{
2514 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2515}
2516
2517void set_node_data(void)
2518{
2519 int pkg, node, lnode, cpu, cpux;

--- 2270 unchanged lines hidden (view full) ---

4790 return 0;
4791}
4792
4793void topology_probe()
4794{
4795 int i;
4796 int max_core_id = 0;
4797 int max_package_id = 0;
4798 int max_die_id = 0;
4775 int max_siblings = 0;
4776
4777 /* Initialize num_cpus, max_cpu_num */
4778 set_max_cpu_num();
4779 topo.num_cpus = 0;
4780 for_all_proc_cpus(count_cpus);
4781 if (!summary_only && topo.num_cpus > 1)
4782 BIC_PRESENT(BIC_CPU);

--- 50 unchanged lines hidden (view full) ---

4833
4834 cpus[i].logical_cpu_id = i;
4835
4836 /* get package information */
4837 cpus[i].physical_package_id = get_physical_package_id(i);
4838 if (cpus[i].physical_package_id > max_package_id)
4839 max_package_id = cpus[i].physical_package_id;
4840
4799 int max_siblings = 0;
4800
4801 /* Initialize num_cpus, max_cpu_num */
4802 set_max_cpu_num();
4803 topo.num_cpus = 0;
4804 for_all_proc_cpus(count_cpus);
4805 if (!summary_only && topo.num_cpus > 1)
4806 BIC_PRESENT(BIC_CPU);

--- 50 unchanged lines hidden (view full) ---

4857
4858 cpus[i].logical_cpu_id = i;
4859
4860 /* get package information */
4861 cpus[i].physical_package_id = get_physical_package_id(i);
4862 if (cpus[i].physical_package_id > max_package_id)
4863 max_package_id = cpus[i].physical_package_id;
4864
4865 /* get die information */
4866 cpus[i].die_id = get_die_id(i);
4867 if (cpus[i].die_id > max_die_id)
4868 max_die_id = cpus[i].die_id;
4869
4841 /* get numa node information */
4842 cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
4843 if (cpus[i].physical_node_id > topo.max_node_num)
4844 topo.max_node_num = cpus[i].physical_node_id;
4845
4846 /* get core information */
4847 cpus[i].physical_core_id = get_core_id(i);
4848 if (cpus[i].physical_core_id > max_core_id)

--- 9 unchanged lines hidden (view full) ---

4858
4859 topo.cores_per_node = max_core_id + 1;
4860 if (debug > 1)
4861 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
4862 max_core_id, topo.cores_per_node);
4863 if (!summary_only && topo.cores_per_node > 1)
4864 BIC_PRESENT(BIC_Core);
4865
4870 /* get numa node information */
4871 cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
4872 if (cpus[i].physical_node_id > topo.max_node_num)
4873 topo.max_node_num = cpus[i].physical_node_id;
4874
4875 /* get core information */
4876 cpus[i].physical_core_id = get_core_id(i);
4877 if (cpus[i].physical_core_id > max_core_id)

--- 9 unchanged lines hidden (view full) ---

4887
4888 topo.cores_per_node = max_core_id + 1;
4889 if (debug > 1)
4890 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
4891 max_core_id, topo.cores_per_node);
4892 if (!summary_only && topo.cores_per_node > 1)
4893 BIC_PRESENT(BIC_Core);
4894
4895 topo.num_die = max_die_id + 1;
4896 if (debug > 1)
4897 fprintf(outf, "max_die_id %d, sizing for %d die\n",
4898 max_die_id, topo.num_die);
4899 if (!summary_only && topo.num_die > 1)
4900 BIC_PRESENT(BIC_Die);
4901
4866 topo.num_packages = max_package_id + 1;
4867 if (debug > 1)
4868 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
4869 max_package_id, topo.num_packages);
4870 if (!summary_only && topo.num_packages > 1)
4871 BIC_PRESENT(BIC_Package);
4872
4873 set_node_data();

--- 8 unchanged lines hidden (view full) ---

4882
4883 if (debug < 1)
4884 return;
4885
4886 for (i = 0; i <= topo.max_cpu_num; ++i) {
4887 if (cpu_is_not_present(i))
4888 continue;
4889 fprintf(outf,
4902 topo.num_packages = max_package_id + 1;
4903 if (debug > 1)
4904 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
4905 max_package_id, topo.num_packages);
4906 if (!summary_only && topo.num_packages > 1)
4907 BIC_PRESENT(BIC_Package);
4908
4909 set_node_data();

--- 8 unchanged lines hidden (view full) ---

4918
4919 if (debug < 1)
4920 return;
4921
4922 for (i = 0; i <= topo.max_cpu_num; ++i) {
4923 if (cpu_is_not_present(i))
4924 continue;
4925 fprintf(outf,
4890 "cpu %d pkg %d node %d lnode %d core %d thread %d\n",
4891 i, cpus[i].physical_package_id,
4926 "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n",
4927 i, cpus[i].physical_package_id, cpus[i].die_id,
4892 cpus[i].physical_node_id,
4893 cpus[i].logical_node_id,
4894 cpus[i].physical_core_id,
4895 cpus[i].thread_id);
4896 }
4897
4898}
4899

--- 711 unchanged lines hidden ---
4928 cpus[i].physical_node_id,
4929 cpus[i].logical_node_id,
4930 cpus[i].physical_core_id,
4931 cpus[i].thread_id);
4932 }
4933
4934}
4935

--- 711 unchanged lines hidden ---