Skip to content

Commit

Permalink
[LibOS] Add shared_cpu_list file to sysfs cache info
Browse files Browse the repository at this point in the history
This is e.g. required by the gemm-common Rust crate, see
`gemm-common/src/cache.rs`. Without this file, the crate logic
incorrectly calculates shared-cpu count as zero and leads to a
division-by-zero exception.

Signed-off-by: Dmitrii Kuvaiskii <[email protected]>
  • Loading branch information
Dmitrii Kuvaiskii committed Jul 24, 2024
1 parent cd68a46 commit 7e44993
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions libos/include/libos_fs_pseudo.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ struct libos_dev_ops {
* Note: Used to allocate on stack; increase with caution or use malloc instead. */
#define PAL_SYSFS_MAP_FILESZ 256

/* Used to represent cpu ranges (lists) like "0-31,64-95,97".
* Note: Used to allocate on stack; increase with caution or use malloc instead. */
#define PAL_SYSFS_RANGES_FILESZ 512

/*
* A node of the pseudo filesystem. A single node can describe either a single file, or a family of
* files:
Expand Down
14 changes: 13 additions & 1 deletion libos/src/fs/sys/cache_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,26 @@ int sys_cache_load(struct libos_dentry* dent, char** out_data, size_t* out_size)
const struct pal_topo_info* topo = &g_pal_public_state->topo_info;
size_t cache_idx = topo->threads[thread_id].ids_of_caches[cache_class];
const struct pal_cache_info* cache = &topo->caches[cache_idx];
char str[PAL_SYSFS_MAP_FILESZ] = {'\0'};

/* str buffer of the same size is used for all below pseudo-files; we pick the largest size for
* this buffer (which is presumably the size required for the "shared_cpu_list" file) */
static_assert(PAL_SYSFS_RANGES_FILESZ >= PAL_SYSFS_MAP_FILESZ, "sysfs file size too small");
char str[PAL_SYSFS_RANGES_FILESZ] = {'\0'};

if (strcmp(name, "shared_cpu_map") == 0) {
struct callback_arg callback_arg = {
.cache_id_to_match = cache_idx,
.cache_class = cache_class,
};
ret = sys_print_as_bitmask(str, sizeof(str), topo->threads_cnt,
is_same_cache, &callback_arg);
} else if (strcmp(name, "shared_cpu_list") == 0) {
struct callback_arg callback_arg = {
.cache_id_to_match = cache_idx,
.cache_class = cache_class,
};
ret = sys_print_as_ranges(str, sizeof(str), topo->threads_cnt,
is_same_cache, &callback_arg);
} else if (strcmp(name, "level") == 0) {
ret = snprintf(str, sizeof(str), "%zu\n", cache->level);
} else if (strcmp(name, "type") == 0) {
Expand Down
2 changes: 1 addition & 1 deletion libos/src/fs/sys/cpu_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int sys_cpu_general_load(struct libos_dentry* dent, char** out_data, size_t* out
int ret;
const struct pal_topo_info* topo = &g_pal_public_state->topo_info;
const char* name = dent->name;
char str[PAL_SYSFS_BUF_FILESZ];
char str[PAL_SYSFS_RANGES_FILESZ];

if (strcmp(name, "online") == 0) {
ret = sys_print_as_ranges(str, sizeof(str), topo->threads_cnt, is_online, NULL);
Expand Down
1 change: 1 addition & 0 deletions libos/src/fs/sys/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ static void init_cpu_dir(struct pseudo_node* cpu) {
indexX->list_names = &sys_resource_list_names;

pseudo_add_str(indexX, "shared_cpu_map", &sys_cache_load);
pseudo_add_str(indexX, "shared_cpu_list", &sys_cache_load);
pseudo_add_str(indexX, "level", &sys_cache_load);
pseudo_add_str(indexX, "type", &sys_cache_load);
pseudo_add_str(indexX, "size", &sys_cache_load);
Expand Down
2 changes: 1 addition & 1 deletion libos/src/fs/sys/node_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int sys_node_general_load(struct libos_dentry* dent, char** out_data, size_t* ou
int ret;
const struct pal_topo_info* topo = &g_pal_public_state->topo_info;
const char* name = dent->name;
char str[PAL_SYSFS_BUF_FILESZ];
char str[PAL_SYSFS_RANGES_FILESZ];
if (strcmp(name, "online") == 0) {
ret = sys_print_as_ranges(str, sizeof(str), topo->numa_nodes_cnt, is_online, NULL);
} else if (strcmp(name, "possible") == 0) {
Expand Down

0 comments on commit 7e44993

Please sign in to comment.