turbostat.c (7ae383be81781c5e1347f71c3eb0d53ce5188200) turbostat.c (e275b3885dffd31095984ed2476ed0447fa7309a)
1/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable 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

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

1376 filep = fopen_or_die(path, "r");
1377 if (fscanf(filep, "%d", &value) != 1)
1378 err(1, "%s: failed to parse number from file", path);
1379 fclose(filep);
1380 return value;
1381}
1382
1383/*
1/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable 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

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

1376 filep = fopen_or_die(path, "r");
1377 if (fscanf(filep, "%d", &value) != 1)
1378 err(1, "%s: failed to parse number from file", path);
1379 fclose(filep);
1380 return value;
1381}
1382
1383/*
1384 * cpu_is_first_sibling_in_core(cpu)
1385 * return 1 if given CPU is 1st HT sibling in the core
1384 * get_cpu_position_in_core(cpu)
1385 * return the position of the CPU among its HT siblings in the core
1386 * return -1 if the sibling is not in list
1386 */
1387 */
1387int cpu_is_first_sibling_in_core(int cpu)
1388int get_cpu_position_in_core(int cpu)
1388{
1389{
1389 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1390 char path[64];
1391 FILE *filep;
1392 int this_cpu;
1393 char character;
1394 int i;
1395
1396 sprintf(path,
1397 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1398 cpu);
1399 filep = fopen(path, "r");
1400 if (filep == NULL) {
1401 perror(path);
1402 exit(1);
1403 }
1404
1405 for (i = 0; i < topo.num_threads_per_core; i++) {
1406 fscanf(filep, "%d", &this_cpu);
1407 if (this_cpu == cpu) {
1408 fclose(filep);
1409 return i;
1410 }
1411
1412 /* Account for no separator after last thread*/
1413 if (i != (topo.num_threads_per_core - 1))
1414 fscanf(filep, "%c", &character);
1415 }
1416
1417 fclose(filep);
1418 return -1;
1390}
1391
1392/*
1393 * cpu_is_first_core_in_package(cpu)
1394 * return 1 if given CPU is 1st core in package
1395 */
1396int cpu_is_first_core_in_package(int cpu)
1397{

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

1407{
1408 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1409}
1410
1411int get_num_ht_siblings(int cpu)
1412{
1413 char path[80];
1414 FILE *filep;
1419}
1420
1421/*
1422 * cpu_is_first_core_in_package(cpu)
1423 * return 1 if given CPU is 1st core in package
1424 */
1425int cpu_is_first_core_in_package(int cpu)
1426{

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

1436{
1437 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1438}
1439
1440int get_num_ht_siblings(int cpu)
1441{
1442 char path[80];
1443 FILE *filep;
1415 int sib1, sib2;
1416 int matches;
1444 int sib1;
1445 int matches = 0;
1417 char character;
1446 char character;
1447 char str[100];
1448 char *ch;
1418
1419 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1420 filep = fopen_or_die(path, "r");
1449
1450 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1451 filep = fopen_or_die(path, "r");
1452
1421 /*
1422 * file format:
1453 /*
1454 * file format:
1423 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1424 * otherwinse 1 sibling (self).
1455 * A ',' separated or '-' separated set of numbers
1456 * (eg 1-2 or 1,3,4,5)
1425 */
1457 */
1426 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1458 fscanf(filep, "%d%c\n", &sib1, &character);
1459 fseek(filep, 0, SEEK_SET);
1460 fgets(str, 100, filep);
1461 ch = strchr(str, character);
1462 while (ch != NULL) {
1463 matches++;
1464 ch = strchr(ch+1, character);
1465 }
1427
1428 fclose(filep);
1466
1467 fclose(filep);
1429
1430 if (matches == 3)
1431 return 2;
1432 else
1433 return 1;
1468 return matches+1;
1434}
1435
1436/*
1437 * run func(thread, core, package) in topology order
1438 * skip non-present cpus
1439 */
1440
1441int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,

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

2750
2751
2752int initialize_counters(int cpu_id)
2753{
2754 int my_thread_id, my_core_id, my_package_id;
2755
2756 my_package_id = get_physical_package_id(cpu_id);
2757 my_core_id = get_core_id(cpu_id);
1469}
1470
1471/*
1472 * run func(thread, core, package) in topology order
1473 * skip non-present cpus
1474 */
1475
1476int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,

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

2785
2786
2787int initialize_counters(int cpu_id)
2788{
2789 int my_thread_id, my_core_id, my_package_id;
2790
2791 my_package_id = get_physical_package_id(cpu_id);
2792 my_core_id = get_core_id(cpu_id);
2758
2759 if (cpu_is_first_sibling_in_core(cpu_id)) {
2760 my_thread_id = 0;
2793 my_thread_id = get_cpu_position_in_core(cpu_id);
2794 if (!my_thread_id)
2761 topo.num_cores++;
2795 topo.num_cores++;
2762 } else {
2763 my_thread_id = 1;
2764 }
2765
2766 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2767 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2768 return 0;
2769}
2770
2771void allocate_output_buffer()
2772{

--- 202 unchanged lines hidden ---
2796
2797 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2798 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2799 return 0;
2800}
2801
2802void allocate_output_buffer()
2803{

--- 202 unchanged lines hidden ---