mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-07 17:28:15 +00:00
virhostcpu.c: refactor virHostCPUParseCountLinux()
This function reads the string in sysfspath/cpu/present and parses it manually to retrieve the number of present CPUs. virHostCPUGetPresentBitmap() reads and parses the same file, using a more robust parser via virBitmapParseUnlimited(), but returns a bitmap. Let's drop all the manual parsing done here and simply return the size of the resulting bitmap from virHostCPUGetPresentBitmap(). Given that no more parsing is being done manually in the function, rename it to virHostCPUCountLinux(). Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com> Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
parent
9d31433483
commit
bc07020511
@ -856,33 +856,17 @@ virHostCPUGetStatsLinux(FILE *procstat,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Determine the number of CPUs (maximum CPU id + 1) from a file containing
|
/* Determine the number of CPUs (maximum CPU id + 1) present in
|
||||||
* a list of CPU ids, like the Linux sysfs cpu/present file */
|
* the host. */
|
||||||
static int
|
static int
|
||||||
virHostCPUParseCountLinux(void)
|
virHostCPUCountLinux(void)
|
||||||
{
|
{
|
||||||
char *str = NULL;
|
g_autoptr(virBitmap) present = virHostCPUGetPresentBitmap();
|
||||||
char *tmp;
|
|
||||||
int ret = -1;
|
|
||||||
|
|
||||||
if (virFileReadValueString(&str, "%s/cpu/present", SYSFS_SYSTEM_PATH) < 0)
|
if (!present)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
tmp = str;
|
return virBitmapSize(present);
|
||||||
do {
|
|
||||||
if (virStrToLong_i(tmp, &tmp, 10, &ret) < 0 ||
|
|
||||||
!strchr(",-", *tmp)) {
|
|
||||||
virReportError(VIR_ERR_NO_SUPPORT,
|
|
||||||
_("failed to parse %s"), str);
|
|
||||||
ret = -1;
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
} while (*tmp++ && *tmp);
|
|
||||||
ret++;
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
VIR_FREE(str);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -1031,7 +1015,7 @@ int
|
|||||||
virHostCPUGetCount(void)
|
virHostCPUGetCount(void)
|
||||||
{
|
{
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
return virHostCPUParseCountLinux();
|
return virHostCPUCountLinux();
|
||||||
#elif defined(__FreeBSD__) || defined(__APPLE__)
|
#elif defined(__FreeBSD__) || defined(__APPLE__)
|
||||||
return virHostCPUGetCountAppleFreeBSD();
|
return virHostCPUGetCountAppleFreeBSD();
|
||||||
#else
|
#else
|
||||||
|
Loading…
x
Reference in New Issue
Block a user