turbostat.c (843c57916dde0e260e500e4ae1a443a7d7fbe962) | turbostat.c (0e2d8f058f9924c373ee7061064936cd582bcbe7) |
---|---|
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 --- 144 unchanged lines hidden (view full) --- 153int backwards_count; 154char *progname; 155 156#define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */ 157cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset; 158size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size; 159#define MAX_ADDED_COUNTERS 8 160#define MAX_ADDED_THREAD_COUNTERS 24 | 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 --- 144 unchanged lines hidden (view full) --- 153int backwards_count; 154char *progname; 155 156#define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */ 157cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset; 158size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size; 159#define MAX_ADDED_COUNTERS 8 160#define MAX_ADDED_THREAD_COUNTERS 24 |
161#define BITMASK_SIZE 32 |
|
161 162struct thread_data { 163 struct timeval tv_begin; 164 struct timeval tv_end; 165 unsigned long long tsc; 166 unsigned long long aperf; 167 unsigned long long mperf; 168 unsigned long long c1; --- 82 unchanged lines hidden (view full) --- 251} sys; 252 253struct system_summary { 254 struct thread_data threads; 255 struct core_data cores; 256 struct pkg_data packages; 257} average; 258 | 162 163struct thread_data { 164 struct timeval tv_begin; 165 struct timeval tv_end; 166 unsigned long long tsc; 167 unsigned long long aperf; 168 unsigned long long mperf; 169 unsigned long long c1; --- 82 unchanged lines hidden (view full) --- 252} sys; 253 254struct system_summary { 255 struct thread_data threads; 256 struct core_data cores; 257 struct pkg_data packages; 258} average; 259 |
260struct cpu_topology { 261 int physical_package_id; 262 int logical_cpu_id; 263 int node_id; 264 int physical_core_id; 265 cpu_set_t *put_ids; /* Processing Unit/Thread IDs */ 266} *cpus; |
|
259 260struct topo_params { 261 int num_packages; 262 int num_cpus; 263 int num_cores; 264 int max_cpu_num; 265 int num_cores_per_pkg; 266 int num_threads_per_core; --- 1999 unchanged lines hidden (view full) --- 2266 close(fd_percpu[i]); 2267 } 2268 2269 free(fd_percpu); 2270} 2271 2272void free_all_buffers(void) 2273{ | 267 268struct topo_params { 269 int num_packages; 270 int num_cpus; 271 int num_cores; 272 int max_cpu_num; 273 int num_cores_per_pkg; 274 int num_threads_per_core; --- 1999 unchanged lines hidden (view full) --- 2274 close(fd_percpu[i]); 2275 } 2276 2277 free(fd_percpu); 2278} 2279 2280void free_all_buffers(void) 2281{ |
2282 int i; 2283 |
|
2274 CPU_FREE(cpu_present_set); 2275 cpu_present_set = NULL; 2276 cpu_present_setsize = 0; 2277 2278 CPU_FREE(cpu_affinity_set); 2279 cpu_affinity_set = NULL; 2280 cpu_affinity_setsize = 0; 2281 --- 16 unchanged lines hidden (view full) --- 2298 free(output_buffer); 2299 output_buffer = NULL; 2300 outp = NULL; 2301 2302 free_fd_percpu(); 2303 2304 free(irq_column_2_cpu); 2305 free(irqs_per_cpu); | 2284 CPU_FREE(cpu_present_set); 2285 cpu_present_set = NULL; 2286 cpu_present_setsize = 0; 2287 2288 CPU_FREE(cpu_affinity_set); 2289 cpu_affinity_set = NULL; 2290 cpu_affinity_setsize = 0; 2291 --- 16 unchanged lines hidden (view full) --- 2308 free(output_buffer); 2309 output_buffer = NULL; 2310 outp = NULL; 2311 2312 free_fd_percpu(); 2313 2314 free(irq_column_2_cpu); 2315 free(irqs_per_cpu); |
2316 2317 for (i = 0; i <= topo.max_cpu_num; ++i) { 2318 if (cpus[i].put_ids) 2319 CPU_FREE(cpus[i].put_ids); 2320 } 2321 free(cpus); |
|
2306} 2307 2308 2309/* 2310 * Parse a file containing a single int. 2311 */ 2312int parse_int_file(const char *fmt, ...) 2313{ --- 64 unchanged lines hidden (view full) --- 2378 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2379} 2380 2381int get_core_id(int cpu) 2382{ 2383 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2384} 2385 | 2322} 2323 2324 2325/* 2326 * Parse a file containing a single int. 2327 */ 2328int parse_int_file(const char *fmt, ...) 2329{ --- 64 unchanged lines hidden (view full) --- 2394 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2395} 2396 2397int get_core_id(int cpu) 2398{ 2399 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2400} 2401 |
2386int get_num_ht_siblings(int cpu) | 2402int get_node_id(struct cpu_topology *thiscpu) |
2387{ 2388 char path[80]; 2389 FILE *filep; | 2403{ 2404 char path[80]; 2405 FILE *filep; |
2390 int sib1; 2391 int matches = 0; 2392 char character; 2393 char str[100]; 2394 char *ch; | 2406 int i; 2407 int cpu = thiscpu->logical_cpu_id; |
2395 | 2408 |
2396 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 2397 filep = fopen_or_die(path, "r"); 2398 2399 /* 2400 * file format: 2401 * A ',' separated or '-' separated set of numbers 2402 * (eg 1-2 or 1,3,4,5) 2403 */ 2404 fscanf(filep, "%d%c\n", &sib1, &character); 2405 fseek(filep, 0, SEEK_SET); 2406 fgets(str, 100, filep); 2407 ch = strchr(str, character); 2408 while (ch != NULL) { 2409 matches++; 2410 ch = strchr(ch+1, character); | 2409 for (i = 0; i <= topo.max_cpu_num; i++) { 2410 sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", 2411 cpu, i); 2412 filep = fopen(path, "r"); 2413 if (!filep) 2414 continue; 2415 fclose(filep); 2416 return i; |
2411 } | 2417 } |
2418 return -1; 2419} |
|
2412 | 2420 |
2421int get_thread_siblings(struct cpu_topology *thiscpu) 2422{ 2423 char path[80], character; 2424 FILE *filep; 2425 unsigned long map; 2426 int shift, sib_core; 2427 int cpu = thiscpu->logical_cpu_id; 2428 int offset = topo.max_cpu_num + 1; 2429 size_t size; 2430 2431 thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1)); 2432 if (!thiscpu->put_ids) 2433 return -1; 2434 2435 size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 2436 CPU_ZERO_S(size, thiscpu->put_ids); 2437 2438 sprintf(path, 2439 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu); 2440 filep = fopen_or_die(path, "r"); 2441 do { 2442 offset -= BITMASK_SIZE; 2443 fscanf(filep, "%lx%c", &map, &character); 2444 for (shift = 0; shift < BITMASK_SIZE; shift++) { 2445 if ((map >> shift) & 0x1) { 2446 sib_core = get_core_id(shift + offset); 2447 if (sib_core == thiscpu->physical_core_id) 2448 CPU_SET_S(shift + offset, size, 2449 thiscpu->put_ids); 2450 } 2451 } 2452 } while (!strncmp(&character, ",", 1)); |
|
2413 fclose(filep); | 2453 fclose(filep); |
2414 return matches+1; | 2454 2455 return CPU_COUNT_S(size, thiscpu->put_ids); |
2415} 2416 2417/* 2418 * run func(thread, core, package) in topology order 2419 * skip non-present cpus 2420 */ 2421 2422int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, --- 78 unchanged lines hidden (view full) --- 2501 FILE *filep; 2502 unsigned long dummy; 2503 2504 topo.max_cpu_num = 0; 2505 filep = fopen_or_die( 2506 "/sys/devices/system/cpu/cpu0/topology/thread_siblings", 2507 "r"); 2508 while (fscanf(filep, "%lx,", &dummy) == 1) | 2456} 2457 2458/* 2459 * run func(thread, core, package) in topology order 2460 * skip non-present cpus 2461 */ 2462 2463int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, --- 78 unchanged lines hidden (view full) --- 2542 FILE *filep; 2543 unsigned long dummy; 2544 2545 topo.max_cpu_num = 0; 2546 filep = fopen_or_die( 2547 "/sys/devices/system/cpu/cpu0/topology/thread_siblings", 2548 "r"); 2549 while (fscanf(filep, "%lx,", &dummy) == 1) |
2509 topo.max_cpu_num += 32; | 2550 topo.max_cpu_num += BITMASK_SIZE; |
2510 fclose(filep); 2511 topo.max_cpu_num--; /* 0 based */ 2512} 2513 2514/* 2515 * count_cpus() 2516 * remember the last one seen, it will be the max 2517 */ --- 2046 unchanged lines hidden (view full) --- 4564} 4565 4566void topology_probe() 4567{ 4568 int i; 4569 int max_core_id = 0; 4570 int max_package_id = 0; 4571 int max_siblings = 0; | 2551 fclose(filep); 2552 topo.max_cpu_num--; /* 0 based */ 2553} 2554 2555/* 2556 * count_cpus() 2557 * remember the last one seen, it will be the max 2558 */ --- 2046 unchanged lines hidden (view full) --- 4605} 4606 4607void topology_probe() 4608{ 4609 int i; 4610 int max_core_id = 0; 4611 int max_package_id = 0; 4612 int max_siblings = 0; |
4572 struct cpu_topology { 4573 int core_id; 4574 int physical_package_id; 4575 } *cpus; | |
4576 4577 /* Initialize num_cpus, max_cpu_num */ 4578 set_max_cpu_num(); 4579 topo.num_cpus = 0; 4580 for_all_proc_cpus(count_cpus); 4581 if (!summary_only && topo.num_cpus > 1) 4582 BIC_PRESENT(BIC_CPU); 4583 --- 40 unchanged lines hidden (view full) --- 4624 for (i = 0; i <= topo.max_cpu_num; ++i) { 4625 int siblings; 4626 4627 if (cpu_is_not_present(i)) { 4628 if (debug > 1) 4629 fprintf(outf, "cpu%d NOT PRESENT\n", i); 4630 continue; 4631 } | 4613 4614 /* Initialize num_cpus, max_cpu_num */ 4615 set_max_cpu_num(); 4616 topo.num_cpus = 0; 4617 for_all_proc_cpus(count_cpus); 4618 if (!summary_only && topo.num_cpus > 1) 4619 BIC_PRESENT(BIC_CPU); 4620 --- 40 unchanged lines hidden (view full) --- 4661 for (i = 0; i <= topo.max_cpu_num; ++i) { 4662 int siblings; 4663 4664 if (cpu_is_not_present(i)) { 4665 if (debug > 1) 4666 fprintf(outf, "cpu%d NOT PRESENT\n", i); 4667 continue; 4668 } |
4632 cpus[i].core_id = get_core_id(i); 4633 if (cpus[i].core_id > max_core_id) 4634 max_core_id = cpus[i].core_id; | |
4635 | 4669 |
4670 cpus[i].logical_cpu_id = i; 4671 4672 /* get package information */ |
|
4636 cpus[i].physical_package_id = get_physical_package_id(i); 4637 if (cpus[i].physical_package_id > max_package_id) 4638 max_package_id = cpus[i].physical_package_id; 4639 | 4673 cpus[i].physical_package_id = get_physical_package_id(i); 4674 if (cpus[i].physical_package_id > max_package_id) 4675 max_package_id = cpus[i].physical_package_id; 4676 |
4640 siblings = get_num_ht_siblings(i); | 4677 /* get numa node information */ 4678 cpus[i].node_id = get_node_id(&cpus[i]); 4679 4680 /* get core information */ 4681 cpus[i].physical_core_id = get_core_id(i); 4682 if (cpus[i].physical_core_id > max_core_id) 4683 max_core_id = cpus[i].physical_core_id; 4684 4685 /* get thread information */ 4686 siblings = get_thread_siblings(&cpus[i]); |
4641 if (siblings > max_siblings) 4642 max_siblings = siblings; | 4687 if (siblings > max_siblings) 4688 max_siblings = siblings; |
4689 |
|
4643 if (debug > 1) | 4690 if (debug > 1) |
4644 fprintf(outf, "cpu %d pkg %d core %d\n", 4645 i, cpus[i].physical_package_id, cpus[i].core_id); | 4691 fprintf(outf, "cpu %d pkg %d node %d core %d\n", 4692 i, cpus[i].physical_package_id, 4693 cpus[i].node_id, 4694 cpus[i].physical_core_id); |
4646 } 4647 topo.num_cores_per_pkg = max_core_id + 1; 4648 if (debug > 1) 4649 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", 4650 max_core_id, topo.num_cores_per_pkg); 4651 if (!summary_only && topo.num_cores_per_pkg > 1) 4652 BIC_PRESENT(BIC_Core); 4653 4654 topo.num_packages = max_package_id + 1; 4655 if (debug > 1) 4656 fprintf(outf, "max_package_id %d, sizing for %d packages\n", 4657 max_package_id, topo.num_packages); 4658 if (!summary_only && topo.num_packages > 1) 4659 BIC_PRESENT(BIC_Package); 4660 4661 topo.num_threads_per_core = max_siblings; 4662 if (debug > 1) 4663 fprintf(outf, "max_siblings %d\n", max_siblings); | 4695 } 4696 topo.num_cores_per_pkg = max_core_id + 1; 4697 if (debug > 1) 4698 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", 4699 max_core_id, topo.num_cores_per_pkg); 4700 if (!summary_only && topo.num_cores_per_pkg > 1) 4701 BIC_PRESENT(BIC_Core); 4702 4703 topo.num_packages = max_package_id + 1; 4704 if (debug > 1) 4705 fprintf(outf, "max_package_id %d, sizing for %d packages\n", 4706 max_package_id, topo.num_packages); 4707 if (!summary_only && topo.num_packages > 1) 4708 BIC_PRESENT(BIC_Package); 4709 4710 topo.num_threads_per_core = max_siblings; 4711 if (debug > 1) 4712 fprintf(outf, "max_siblings %d\n", max_siblings); |
4664 4665 free(cpus); | |
4666} 4667 4668void 4669allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 4670{ 4671 int i; 4672 4673 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg * --- 705 unchanged lines hidden --- | 4713} 4714 4715void 4716allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 4717{ 4718 int i; 4719 4720 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg * --- 705 unchanged lines hidden --- |