mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
nodeinfo: drop static variable
We were wasting time to malloc a copy of a constant string, then copy it into static storage, for every call to nodeGetInfo. At least we were lucky that it was a constant source, and thus not subject to even worse issues with one thread clobbering the static storage while another was using it. This gets rid of the waste, by passing the string through the stack instead, as well as renaming internal functions to better match our conventions. * src/nodeinfo.c (sysfs_path): Delete. (get_cpu_value, count_thread_siblings, parse_socket): Add parameter, and rename... (virNodeGetCpuValue, virNodeCountThreadSiblings) (virNodeParseSocket): ... into a common namespace. (cpu_online, parse_core): Inline into callers. (linuxNodeInfoCPUPopulate): Update caller. (nodeGetInfo): Drop a useless malloc.
This commit is contained in:
parent
5f89c86004
commit
88f12a3665
@ -81,14 +81,14 @@ static int linuxNodeGetMemoryStats(FILE *meminfo,
|
|||||||
virNodeMemoryStatsPtr params,
|
virNodeMemoryStatsPtr params,
|
||||||
int *nparams);
|
int *nparams);
|
||||||
|
|
||||||
static char sysfs_path[1024];
|
|
||||||
/* Return the positive decimal contents of the given
|
/* Return the positive decimal contents of the given
|
||||||
* (*sysfs_path)/cpu%u/FILE, or -1 on error. If MISSING_OK and the
|
* DIR/cpu%u/FILE, or -1 on error. If MISSING_OK and the
|
||||||
* file could not be found, return 1 instead of an error; this is
|
* file could not be found, return 1 instead of an error; this is
|
||||||
* because some machines cannot hot-unplug cpu0, or because
|
* because some machines cannot hot-unplug cpu0, or because
|
||||||
* hot-unplugging is disabled. */
|
* hot-unplugging is disabled. */
|
||||||
static int
|
static int
|
||||||
get_cpu_value(unsigned int cpu, const char *file, bool missing_ok)
|
virNodeGetCpuValue(const char *dir, unsigned int cpu, const char *file,
|
||||||
|
bool missing_ok)
|
||||||
{
|
{
|
||||||
char *path;
|
char *path;
|
||||||
FILE *pathfp;
|
FILE *pathfp;
|
||||||
@ -96,7 +96,7 @@ get_cpu_value(unsigned int cpu, const char *file, bool missing_ok)
|
|||||||
char value_str[INT_BUFSIZE_BOUND(value)];
|
char value_str[INT_BUFSIZE_BOUND(value)];
|
||||||
char *tmp;
|
char *tmp;
|
||||||
|
|
||||||
if (virAsprintf(&path, "%s/cpu%u/%s", sysfs_path, cpu, file) < 0) {
|
if (virAsprintf(&path, "%s/cpu%u/%s", dir, cpu, file) < 0) {
|
||||||
virReportOOMError();
|
virReportOOMError();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -128,15 +128,8 @@ cleanup:
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check if CPU is online via sysfs_path/cpu%u/online. Return 1 if online,
|
static unsigned long
|
||||||
0 if offline, and -1 on error. */
|
virNodeCountThreadSiblings(const char *dir, unsigned int cpu)
|
||||||
static int
|
|
||||||
cpu_online(unsigned int cpu)
|
|
||||||
{
|
|
||||||
return get_cpu_value(cpu, "online", true);
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned long count_thread_siblings(unsigned int cpu)
|
|
||||||
{
|
{
|
||||||
unsigned long ret = 0;
|
unsigned long ret = 0;
|
||||||
char *path;
|
char *path;
|
||||||
@ -145,7 +138,7 @@ static unsigned long count_thread_siblings(unsigned int cpu)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (virAsprintf(&path, "%s/cpu%u/topology/thread_siblings",
|
if (virAsprintf(&path, "%s/cpu%u/topology/thread_siblings",
|
||||||
sysfs_path, cpu) < 0) {
|
dir, cpu) < 0) {
|
||||||
virReportOOMError();
|
virReportOOMError();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -180,9 +173,11 @@ cleanup:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int parse_socket(unsigned int cpu)
|
static int
|
||||||
|
virNodeParseSocket(const char *dir, unsigned int cpu)
|
||||||
{
|
{
|
||||||
int ret = get_cpu_value(cpu, "topology/physical_package_id", false);
|
int ret = virNodeGetCpuValue(dir, cpu, "topology/physical_package_id",
|
||||||
|
false);
|
||||||
# if defined(__powerpc__) || \
|
# if defined(__powerpc__) || \
|
||||||
defined(__powerpc64__) || \
|
defined(__powerpc64__) || \
|
||||||
defined(__s390__) || \
|
defined(__s390__) || \
|
||||||
@ -194,11 +189,6 @@ static int parse_socket(unsigned int cpu)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int parse_core(unsigned int cpu)
|
|
||||||
{
|
|
||||||
return get_cpu_value(cpu, "topology/core_id", false);
|
|
||||||
}
|
|
||||||
|
|
||||||
int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
|
int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
|
||||||
const char *sysfs_cpudir,
|
const char *sysfs_cpudir,
|
||||||
virNodeInfoPtr nodeinfo)
|
virNodeInfoPtr nodeinfo)
|
||||||
@ -222,10 +212,6 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
|
|||||||
nodeinfo->nodes = numa_max_node() + 1;
|
nodeinfo->nodes = numa_max_node() + 1;
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
if (!virStrcpyStatic(sysfs_path, sysfs_cpudir)) {
|
|
||||||
virReportSystemError(errno, _("cannot copy %s"), sysfs_cpudir);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
/* NB: It is impossible to fill our nodes, since cpuinfo
|
/* NB: It is impossible to fill our nodes, since cpuinfo
|
||||||
* has no knowledge of NUMA nodes */
|
* has no knowledge of NUMA nodes */
|
||||||
|
|
||||||
@ -296,7 +282,7 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
|
|||||||
if (sscanf(cpudirent->d_name, "cpu%u", &cpu) != 1)
|
if (sscanf(cpudirent->d_name, "cpu%u", &cpu) != 1)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
online = cpu_online(cpu);
|
online = virNodeGetCpuValue(sysfs_cpudir, cpu, "online", true);
|
||||||
if (online < 0) {
|
if (online < 0) {
|
||||||
closedir(cpudir);
|
closedir(cpudir);
|
||||||
return -1;
|
return -1;
|
||||||
@ -306,20 +292,20 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
|
|||||||
nodeinfo->cpus++;
|
nodeinfo->cpus++;
|
||||||
|
|
||||||
/* Parse core */
|
/* Parse core */
|
||||||
core = parse_core(cpu);
|
core = virNodeGetCpuValue(sysfs_cpudir, cpu, "topology/core_id", false);
|
||||||
if (!CPU_ISSET(core, &core_mask)) {
|
if (!CPU_ISSET(core, &core_mask)) {
|
||||||
CPU_SET(core, &core_mask);
|
CPU_SET(core, &core_mask);
|
||||||
nodeinfo->cores++;
|
nodeinfo->cores++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parse socket */
|
/* Parse socket */
|
||||||
sock = parse_socket(cpu);
|
sock = virNodeParseSocket(sysfs_cpudir, cpu);
|
||||||
if (!CPU_ISSET(sock, &socket_mask)) {
|
if (!CPU_ISSET(sock, &socket_mask)) {
|
||||||
CPU_SET(sock, &socket_mask);
|
CPU_SET(sock, &socket_mask);
|
||||||
nodeinfo->sockets++;
|
nodeinfo->sockets++;
|
||||||
}
|
}
|
||||||
|
|
||||||
cur_threads = count_thread_siblings(cpu);
|
cur_threads = virNodeCountThreadSiblings(sysfs_cpudir, cpu);
|
||||||
if (cur_threads == 0) {
|
if (cur_threads == 0) {
|
||||||
closedir(cpudir);
|
closedir(cpudir);
|
||||||
return -1;
|
return -1;
|
||||||
@ -329,7 +315,7 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
|
|||||||
}
|
}
|
||||||
if (errno) {
|
if (errno) {
|
||||||
virReportSystemError(errno,
|
virReportSystemError(errno,
|
||||||
_("problem reading %s"), sysfs_path);
|
_("problem reading %s"), sysfs_cpudir);
|
||||||
closedir(cpudir);
|
closedir(cpudir);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -627,7 +613,6 @@ int nodeGetInfo(virConnectPtr conn ATTRIBUTE_UNUSED, virNodeInfoPtr nodeinfo) {
|
|||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
{
|
{
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
char *sysfs_cpuinfo = NULL;
|
|
||||||
FILE *cpuinfo = fopen(CPUINFO_PATH, "r");
|
FILE *cpuinfo = fopen(CPUINFO_PATH, "r");
|
||||||
if (!cpuinfo) {
|
if (!cpuinfo) {
|
||||||
virReportSystemError(errno,
|
virReportSystemError(errno,
|
||||||
@ -635,12 +620,7 @@ int nodeGetInfo(virConnectPtr conn ATTRIBUTE_UNUSED, virNodeInfoPtr nodeinfo) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (virAsprintf(&sysfs_cpuinfo, CPU_SYS_PATH) < 0) {
|
ret = linuxNodeInfoCPUPopulate(cpuinfo, CPU_SYS_PATH, nodeinfo);
|
||||||
virReportOOMError();
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = linuxNodeInfoCPUPopulate(cpuinfo, sysfs_cpuinfo, nodeinfo);
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
@ -649,7 +629,6 @@ int nodeGetInfo(virConnectPtr conn ATTRIBUTE_UNUSED, virNodeInfoPtr nodeinfo) {
|
|||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
VIR_FORCE_FCLOSE(cpuinfo);
|
VIR_FORCE_FCLOSE(cpuinfo);
|
||||||
VIR_FREE(sysfs_cpuinfo);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
Loading…
Reference in New Issue
Block a user