提交 ea1021ff 编写于 作者: P Palmer Cox 提交者: Rafael J. Wysocki

cpupower tools: Fix warning and a bug with the cpu package count

The pkgs member of cpupower_topology is being used as the number of
cpu packages. As the comment in get_cpu_topology notes, the package ids
are not guaranteed to be contiguous. So, simply setting pkgs to the value
of the highest physical_package_id doesn't actually provide a count of
the number of cpu packages. Instead, calculate pkgs by setting it to
the number of distinct physical_packge_id values which is pretty easy
to do after the core_info structs are sorted. Calculating pkgs this
way also has the nice benefit of getting rid of a sign comparison warning
that GCC 4.6 was reporting.
Signed-off-by: NPalmer Cox <p@lmercox.com>
Signed-off-by: NThomas Renninger <trenn@suse.de>
Signed-off-by: NRafael J. Wysocki <rafael.j.wysocki@intel.com>
上级 35a16973
...@@ -64,7 +64,7 @@ static int __compare(const void *t1, const void *t2) ...@@ -64,7 +64,7 @@ static int __compare(const void *t1, const void *t2)
*/ */
int get_cpu_topology(struct cpupower_topology *cpu_top) int get_cpu_topology(struct cpupower_topology *cpu_top)
{ {
int cpu, cpus = sysconf(_SC_NPROCESSORS_CONF); int cpu, last_pkg, cpus = sysconf(_SC_NPROCESSORS_CONF);
cpu_top->core_info = malloc(sizeof(struct cpuid_core_info) * cpus); cpu_top->core_info = malloc(sizeof(struct cpuid_core_info) * cpus);
if (cpu_top->core_info == NULL) if (cpu_top->core_info == NULL)
...@@ -78,20 +78,28 @@ int get_cpu_topology(struct cpupower_topology *cpu_top) ...@@ -78,20 +78,28 @@ int get_cpu_topology(struct cpupower_topology *cpu_top)
"physical_package_id", "physical_package_id",
&(cpu_top->core_info[cpu].pkg)) < 0) &(cpu_top->core_info[cpu].pkg)) < 0)
return -1; return -1;
if ((int)cpu_top->core_info[cpu].pkg != -1 &&
cpu_top->core_info[cpu].pkg > cpu_top->pkgs)
cpu_top->pkgs = cpu_top->core_info[cpu].pkg;
if(sysfs_topology_read_file( if(sysfs_topology_read_file(
cpu, cpu,
"core_id", "core_id",
&(cpu_top->core_info[cpu].core)) < 0) &(cpu_top->core_info[cpu].core)) < 0)
return -1; return -1;
} }
cpu_top->pkgs++;
qsort(cpu_top->core_info, cpus, sizeof(struct cpuid_core_info), qsort(cpu_top->core_info, cpus, sizeof(struct cpuid_core_info),
__compare); __compare);
/* Count the number of distinct pkgs values. This works
because the primary sort of the core_info struct was just
done by pkg value. */
last_pkg = cpu_top->core_info[0].pkg;
for(cpu = 1; cpu < cpus; cpu++) {
if(cpu_top->core_info[cpu].pkg != last_pkg) {
last_pkg = cpu_top->core_info[cpu].pkg;
cpu_top->pkgs++;
}
}
cpu_top->pkgs++;
/* Intel's cores count is not consecutively numbered, there may /* Intel's cores count is not consecutively numbered, there may
* be a core_id of 3, but none of 2. Assume there always is 0 * be a core_id of 3, but none of 2. Assume there always is 0
* Get amount of cores by counting duplicates in a package * Get amount of cores by counting duplicates in a package
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册