Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2010,2011 Thomas Renninger <trenn@suse.de>, Novell Inc. |
| 3 | * |
| 4 | * Licensed under the terms of the GNU GPL License version 2. |
| 5 | * |
| 6 | * ToDo: Needs to be done more properly for AMD/Intel specifics |
| 7 | */ |
| 8 | |
| 9 | /* Helper struct for qsort, must be in sync with cpupower_topology.cpu_info */ |
| 10 | /* Be careful: Need to pass unsigned to the sort, so that offlined cores are |
| 11 | in the end, but double check for -1 for offlined cpus at other places */ |
| 12 | |
| 13 | #include <stdlib.h> |
| 14 | #include <stdio.h> |
| 15 | #include <unistd.h> |
| 16 | #include <errno.h> |
| 17 | #include <fcntl.h> |
| 18 | |
| 19 | #include <helpers/helpers.h> |
| 20 | #include <helpers/sysfs.h> |
| 21 | |
| 22 | /* returns -1 on failure, 0 on success */ |
Palmer Cox | 53d2000 | 2012-11-27 13:17:45 +0100 | [diff] [blame] | 23 | static int sysfs_topology_read_file(unsigned int cpu, const char *fname, int *result) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 24 | { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 25 | char linebuf[MAX_LINE_LEN]; |
| 26 | char *endp; |
| 27 | char path[SYSFS_PATH_MAX]; |
| 28 | |
| 29 | snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/topology/%s", |
| 30 | cpu, fname); |
Dominik Brodowski | 2cd005c | 2011-04-19 20:16:05 +0200 | [diff] [blame] | 31 | if (sysfs_read_file(path, linebuf, MAX_LINE_LEN) == 0) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 32 | return -1; |
Palmer Cox | 53d2000 | 2012-11-27 13:17:45 +0100 | [diff] [blame] | 33 | *result = strtol(linebuf, &endp, 0); |
Dominik Brodowski | 2cd005c | 2011-04-19 20:16:05 +0200 | [diff] [blame] | 34 | if (endp == linebuf || errno == ERANGE) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 35 | return -1; |
Palmer Cox | 53d2000 | 2012-11-27 13:17:45 +0100 | [diff] [blame] | 36 | return 0; |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 37 | } |
| 38 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 39 | static int __compare(const void *t1, const void *t2) |
| 40 | { |
| 41 | struct cpuid_core_info *top1 = (struct cpuid_core_info *)t1; |
| 42 | struct cpuid_core_info *top2 = (struct cpuid_core_info *)t2; |
| 43 | if (top1->pkg < top2->pkg) |
| 44 | return -1; |
| 45 | else if (top1->pkg > top2->pkg) |
| 46 | return 1; |
Palmer Cox | 35a1697 | 2012-11-27 13:17:46 +0100 | [diff] [blame] | 47 | else if (top1->core < top2->core) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 48 | return -1; |
Palmer Cox | 35a1697 | 2012-11-27 13:17:46 +0100 | [diff] [blame] | 49 | else if (top1->core > top2->core) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 50 | return 1; |
| 51 | else if (top1->cpu < top2->cpu) |
| 52 | return -1; |
| 53 | else if (top1->cpu > top2->cpu) |
| 54 | return 1; |
| 55 | else |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Returns amount of cpus, negative on error, cpu_top must be |
| 61 | * passed to cpu_topology_release to free resources |
| 62 | * |
| 63 | * Array is sorted after ->pkg, ->core, then ->cpu |
| 64 | */ |
| 65 | int get_cpu_topology(struct cpupower_topology *cpu_top) |
| 66 | { |
Palmer Cox | ea1021f | 2012-11-27 13:17:47 +0100 | [diff] [blame] | 67 | int cpu, last_pkg, cpus = sysconf(_SC_NPROCESSORS_CONF); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 68 | |
Palmer Cox | 35a1697 | 2012-11-27 13:17:46 +0100 | [diff] [blame] | 69 | cpu_top->core_info = malloc(sizeof(struct cpuid_core_info) * cpus); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 70 | if (cpu_top->core_info == NULL) |
| 71 | return -ENOMEM; |
| 72 | cpu_top->pkgs = cpu_top->cores = 0; |
| 73 | for (cpu = 0; cpu < cpus; cpu++) { |
Thomas Renninger | 7c74d2b | 2011-08-12 01:11:37 +0200 | [diff] [blame] | 74 | cpu_top->core_info[cpu].cpu = cpu; |
| 75 | cpu_top->core_info[cpu].is_online = sysfs_is_cpu_online(cpu); |
Shreyas B. Prabhu | 404c2db | 2015-08-03 11:46:00 +0530 | [diff] [blame] | 76 | if (!cpu_top->core_info[cpu].is_online) |
| 77 | continue; |
Palmer Cox | 53d2000 | 2012-11-27 13:17:45 +0100 | [diff] [blame] | 78 | if(sysfs_topology_read_file( |
| 79 | cpu, |
| 80 | "physical_package_id", |
| 81 | &(cpu_top->core_info[cpu].pkg)) < 0) |
| 82 | return -1; |
Palmer Cox | 53d2000 | 2012-11-27 13:17:45 +0100 | [diff] [blame] | 83 | if(sysfs_topology_read_file( |
| 84 | cpu, |
| 85 | "core_id", |
| 86 | &(cpu_top->core_info[cpu].core)) < 0) |
| 87 | return -1; |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 88 | } |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 89 | |
| 90 | qsort(cpu_top->core_info, cpus, sizeof(struct cpuid_core_info), |
| 91 | __compare); |
| 92 | |
Palmer Cox | ea1021f | 2012-11-27 13:17:47 +0100 | [diff] [blame] | 93 | /* Count the number of distinct pkgs values. This works |
| 94 | because the primary sort of the core_info struct was just |
| 95 | done by pkg value. */ |
| 96 | last_pkg = cpu_top->core_info[0].pkg; |
| 97 | for(cpu = 1; cpu < cpus; cpu++) { |
| 98 | if(cpu_top->core_info[cpu].pkg != last_pkg) { |
| 99 | last_pkg = cpu_top->core_info[cpu].pkg; |
| 100 | cpu_top->pkgs++; |
| 101 | } |
| 102 | } |
| 103 | cpu_top->pkgs++; |
| 104 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 105 | /* Intel's cores count is not consecutively numbered, there may |
| 106 | * be a core_id of 3, but none of 2. Assume there always is 0 |
| 107 | * Get amount of cores by counting duplicates in a package |
| 108 | for (cpu = 0; cpu_top->core_info[cpu].pkg = 0 && cpu < cpus; cpu++) { |
| 109 | if (cpu_top->core_info[cpu].core == 0) |
| 110 | cpu_top->cores++; |
| 111 | */ |
| 112 | return cpus; |
| 113 | } |
| 114 | |
| 115 | void cpu_topology_release(struct cpupower_topology cpu_top) |
| 116 | { |
| 117 | free(cpu_top.core_info); |
| 118 | } |