Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP DNM] linux/sysfs: use opendir() to list cpu caches #629

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 18 additions & 9 deletions hwloc/topology-linux.c
Expand Up @@ -5032,11 +5032,18 @@ look_sysfscpu(struct hwloc_topology *topology,

/* look at the caches */
if (topology->want_some_cpu_caches) {
for(j=0; j<10; j++) {
DIR *cachesdir;
struct dirent *dirent;
sprintf(str, "/sys/devices/system/cpu/cpu%d/cache/", i);
cachesdir = hwloc_opendir(str, data->root_fd);
if (cachesdir) {
while ((dirent = readdir(cachesdir)) != NULL) {
char str2[20]; /* enough for a level number (one digit) or a type (Data/Instruction/Unified) */
hwloc_bitmap_t cacheset;

sprintf(str, "/sys/devices/system/cpu/cpu%d/cache/index%d/shared_cpu_map", i, j);
if (strncmp(dirent->d_name, "index", 5))
continue;
sprintf(str, "/sys/devices/system/cpu/cpu%d/cache/%s/shared_cpu_map", i, dirent->d_name);
cacheset = hwloc__alloc_read_path_as_cpumask(str, data->root_fd);
if (cacheset) {
if (hwloc_bitmap_iszero(cacheset)) {
Expand Down Expand Up @@ -5066,14 +5073,14 @@ look_sysfscpu(struct hwloc_topology *topology,
struct hwloc_obj *cache;

/* get the cache level depth */
sprintf(str, "/sys/devices/system/cpu/cpu%d/cache/index%d/level", i, j); /* contains %u at least up to 4.19 */
sprintf(str, "/sys/devices/system/cpu/cpu%d/cache/%s/level", i, dirent->d_name); /* contains %u at least up to 4.19 */
if (hwloc_read_path_as_uint(str, &depth, data->root_fd) < 0) {
hwloc_bitmap_free(cacheset);
continue;
}

/* cache type */
sprintf(str, "/sys/devices/system/cpu/cpu%d/cache/index%d/type", i, j);
sprintf(str, "/sys/devices/system/cpu/cpu%d/cache/%s/type", i, dirent->d_name);
if (hwloc_read_path_by_length(str, str2, sizeof(str2), data->root_fd) > 0) {
if (!strncmp(str2, "Data", 4))
ctype = HWLOC_OBJ_CACHE_DATA;
Expand All @@ -5084,7 +5091,7 @@ look_sysfscpu(struct hwloc_topology *topology,
}

/* cache id */
sprintf(str, "/sys/devices/system/cpu/cpu%d/cache/index%d/id", i, j);
sprintf(str, "/sys/devices/system/cpu/cpu%d/cache/%s/id", i, dirent->d_name);
hwloc_read_path_as_uint(str, &id, data->root_fd);

otype = hwloc_cache_type_by_depth_type(depth, ctype);
Expand All @@ -5099,7 +5106,7 @@ look_sysfscpu(struct hwloc_topology *topology,

/* get the cache size */
kB = 0;
sprintf(str, "/sys/devices/system/cpu/cpu%d/cache/index%d/size", i, j); /* contains %uK at least up to 4.19 */
sprintf(str, "/sys/devices/system/cpu/cpu%d/cache/%s/size", i, dirent->d_name); /* contains %uK at least up to 4.19 */
hwloc_read_path_as_uint(str, &kB, data->root_fd);
/* KNL reports L3 with size=0 and full cpuset in cpuid.
* Let hwloc_linux_try_add_knl_mcdram_cache() detect it better.
Expand All @@ -5111,19 +5118,19 @@ look_sysfscpu(struct hwloc_topology *topology,

/* get the line size */
linesize = 0;
sprintf(str, "/sys/devices/system/cpu/cpu%d/cache/index%d/coherency_line_size", i, j); /* contains %u at least up to 4.19 */
sprintf(str, "/sys/devices/system/cpu/cpu%d/cache/%s/coherency_line_size", i, dirent->d_name); /* contains %u at least up to 4.19 */
hwloc_read_path_as_uint(str, &linesize, data->root_fd);

/* get the number of sets and lines per tag.
* don't take the associativity directly in "ways_of_associativity" because
* some archs (ia64, ppc) put 0 there when fully-associative, while others (x86) put something like -1 there.
*/
sets = 0;
sprintf(str, "/sys/devices/system/cpu/cpu%d/cache/index%d/number_of_sets", i, j); /* contains %u at least up to 4.19 */
sprintf(str, "/sys/devices/system/cpu/cpu%d/cache/%s/number_of_sets", i, dirent->d_name); /* contains %u at least up to 4.19 */
hwloc_read_path_as_uint(str, &sets, data->root_fd);

lines_per_tag = 1;
sprintf(str, "/sys/devices/system/cpu/cpu%d/cache/index%d/physical_line_partition", i, j); /* contains %u at least up to 4.19 */
sprintf(str, "/sys/devices/system/cpu/cpu%d/cache/%s/physical_line_partition", i, dirent->d_name); /* contains %u at least up to 4.19 */
hwloc_read_path_as_uint(str, &lines_per_tag, data->root_fd);

/* first cpu in this cache, add the cache */
Expand All @@ -5147,6 +5154,8 @@ look_sysfscpu(struct hwloc_topology *topology,
}
hwloc_bitmap_free(cacheset);
}
closedir(cachesdir);
}
}

} hwloc_bitmap_foreach_end();
Expand Down
11 changes: 9 additions & 2 deletions tests/hwloc/linux/gather/test-gather-topology.sh.in
Expand Up @@ -62,7 +62,11 @@ export HWLOC_FSROOT=//

echo "Saving current system topology to XML..."
# ignore DAXDevice info attr because it won't appear in save2.xml unless we pass --io to gather below
if ! "$lstopo" --no-io -.xml | grep -v DAXDevice > "$tmpdir/save1.xml" ; then
if ! "$lstopo" --no-io -.xml \
| sed -e 's/ id="obj[0-9]*"//' \
| sed -e 's/ gp_index="[0-9]*"//' \
| grep -v DAXDevice \
> "$tmpdir/save1.xml" ; then
error "Failed"
exit 1
fi
Expand All @@ -85,7 +89,10 @@ export HWLOC_FSROOT="$tmpdir/save"
rm -f "$tmpdir/save/proc/hwloc-nofile-info"

echo "Saving tarball topology to XML..."
if ! "$lstopo" --no-io "$tmpdir/save2.xml" ; then
if ! "$lstopo" --no-io -.xml \
| sed -e 's/ id="obj[0-9]*"//' \
| sed -e 's/ gp_index="[0-9]*"//' \
> "$tmpdir/save2.xml" ; then
error "Failed"
exit 1
fi
Expand Down