--- src/libplpa/plpa_map.c | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --quilt old/src/libplpa/plpa_map.c new/src/libplpa/plpa_map.c --- old/src/libplpa/plpa_map.c +++ new/src/libplpa/plpa_map.c @@ -120,10 +120,11 @@ #include #include #include #include #include +#include #include #include #include typedef struct tuple_t_ { @@ -173,28 +174,56 @@ static void load_cache(const char *sysfs { int i, j, k, invalid_entry, fd; char path[PATH_MAX], buf[8]; PLPA_NAME(cpu_set_t) *cores_on_sockets; int found; + DIR *dir; + struct dirent dentry, *dentryp = NULL; /* Check for the parent directory */ sprintf(path, "%s/devices/system/cpu", sysfs_mount); if (access(path, R_OK|X_OK)) { return; } - /* Go through and find the max processor ID */ - for (num_processors = max_processor_id = i = 0; - i < PLPA_BITMASK_CPU_MAX; ++i) { - sprintf(path, "%s/devices/system/cpu/cpu%d", sysfs_mount, i); - if (0 != access(path, (R_OK | X_OK))) { - max_processor_id = i - 1; - break; - } - ++num_processors; + dir = opendir(path); + if (NULL == dir) { + return; } + /* Catch all entries of format "cpu%d", count them and maintain + max_processor_id */ + num_processors = 0; + do { + int ret = readdir_r(dir, &dentry, &dentryp); + if (0 != ret) { + closedir(dir); + clear_cache(); + return; + } + + if (dentryp) { + int cpuid; + + ret = sscanf(dentryp->d_name, "cpu%d", &cpuid); + if (1 != ret) { + continue; + } + + ++num_processors; + if (cpuid >= PLPA_BITMASK_CPU_MAX) { + closedir(dir); + clear_cache(); + return; + } else if (cpuid > max_processor_id) { + max_processor_id = cpuid; + } + } + + } while (NULL != dentryp); + closedir(dir); + /* If we found no processors, then we have no topology info */ if (0 == num_processors) { clear_cache(); return; }