Skip to content

Commit

Permalink
[LibOS] Move arch-dependent proc cpuinfo display code into functions
Browse files Browse the repository at this point in the history
Move the code for generating the output of `/proc/cpuinfo` into
functions that each architecture has to implement.

Signed-off-by: Stefan Berger <[email protected]>
  • Loading branch information
stefanberger authored and Dmitrii Kuvaiskii committed May 30, 2022
1 parent b00af84 commit 3f2478b
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 37 deletions.
20 changes: 20 additions & 0 deletions LibOS/shim/include/shim_fs_proc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2014 Stony Brook University
* Copyright (C) 2022 IBM Corporation
*/

#ifndef SHIM_PROC_H_
#define SHIM_PROC_H_

int print_to_str(char** str, size_t off, size_t* size, const char* fmt, ...);

/* every architecture must implement the following 2 functions */
int proc_cpuinfo_display_cpu(char** str, size_t* size, size_t* max,
const struct pal_topo_info* topo,
const struct pal_cpu_info* cpu, size_t i,
struct pal_cpu_thread_info* thread);

int proc_cpuinfo_display_tail(char** str, size_t* size, size_t* max,
const struct pal_cpu_info* cpu);

#endif /* SHIM_PROC_H_ */
61 changes: 61 additions & 0 deletions LibOS/shim/src/arch/x86_64/fs_proc/info.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2014 Stony Brook University
* Copyright (C) 2022 IBM Corporation
*/

#include "api.h"
#include "pal.h"
#include "shim_fs_proc.h"

#define ADD_INFO(fmt, ...) \
do { \
int ret = print_to_str(str, *size, max, fmt, ##__VA_ARGS__); \
if (ret < 0) { \
return ret; \
} \
*size += ret; \
} while (0)

int proc_cpuinfo_display_cpu(char** str, size_t* size, size_t* max,
const struct pal_topo_info* topo,
const struct pal_cpu_info* cpu, size_t i,
struct pal_cpu_thread_info* thread) {
struct pal_cpu_core_info* core = &topo->cores[thread->core_id];
/* Below strings must match exactly the strings retrieved from /proc/cpuinfo
* (see Linux's arch/x86/kernel/cpu/proc.c) */
ADD_INFO("processor\t: %lu\n", i);
ADD_INFO("vendor_id\t: %s\n", cpu->cpu_vendor);
ADD_INFO("cpu family\t: %lu\n", cpu->cpu_family);
ADD_INFO("model\t\t: %lu\n", cpu->cpu_model);
ADD_INFO("model name\t: %s\n", cpu->cpu_brand);
ADD_INFO("stepping\t: %lu\n", cpu->cpu_stepping);
ADD_INFO("physical id\t: %zu\n", core->socket_id);

/* Linux keeps this numbering socket-local, but we can use a different one, and it's
* documented as "hardware platform's identifier (rather than the kernel's)" anyways. */
ADD_INFO("core id\t\t: %lu\n", thread->core_id);

size_t cores_in_socket = 0;
for (size_t j = 0; j < topo->cores_cnt; j++) { // slow, but shouldn't matter
if (topo->cores[j].socket_id == core->socket_id)
cores_in_socket++;
}
ADD_INFO("cpu cores\t: %zu\n", cores_in_socket);
double bogomips = cpu->cpu_bogomips;
// Apparently Gramine snprintf cannot into floats.
ADD_INFO("bogomips\t: %lu.%02lu\n", (unsigned long)bogomips,
(unsigned long)(bogomips * 100.0 + 0.5) % 100);
ADD_INFO("\n");

return 0;
}

int proc_cpuinfo_display_tail(char** str, size_t* size, size_t* max,
const struct pal_cpu_info* cpu) {
__UNUSED(str);
__UNUSED(size);
__UNUSED(max);
__UNUSED(cpu);

return 0;
}
1 change: 1 addition & 0 deletions LibOS/shim/src/arch/x86_64/meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
libos_sources_arch_list = {
'fs_proc/info.c': {},
'shim_arch_prctl.c': {},
'shim_elf_entry.nasm': { 'type': 'nasm' },
'shim_context.c': {},
Expand Down
67 changes: 30 additions & 37 deletions LibOS/shim/src/fs/proc/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

#include "shim_fs_pseudo.h"
#include "shim_fs_proc.h"

int proc_meminfo_load(struct shim_dentry* dent, char** out_data, size_t* out_size) {
__UNUSED(dent);
Expand Down Expand Up @@ -93,7 +94,7 @@ static void* realloc_size(void* ptr, size_t old_size, size_t new_size) {
return tmp;
}

static int print_to_str(char** str, size_t off, size_t* size, const char* fmt, ...) {
int print_to_str(char** str, size_t off, size_t* size, const char* fmt, ...) {
int ret;
va_list ap;

Expand All @@ -119,19 +120,10 @@ static int print_to_str(char** str, size_t off, size_t* size, const char* fmt, .
return ret;
}

#define ADD_INFO(fmt, ...) \
do { \
int ret = print_to_str(&str, size, &max, fmt, ##__VA_ARGS__); \
if (ret < 0) { \
free(str); \
return ret; \
} \
size += ret; \
} while (0)

int proc_cpuinfo_load(struct shim_dentry* dent, char** out_data, size_t* out_size) {
__UNUSED(dent);

int ret;
size_t size = 0;
size_t max = 128;
char* str = malloc(max);
Expand All @@ -146,39 +138,40 @@ int proc_cpuinfo_load(struct shim_dentry* dent, char** out_data, size_t* out_siz
/* Offline cores are skipped in cpuinfo, with gaps in numbering. */
continue;
}
struct pal_cpu_core_info* core = &topo->cores[thread->core_id];
/* Below strings must match exactly the strings retrieved from /proc/cpuinfo
* (see Linux's arch/x86/kernel/cpu/proc.c) */
ADD_INFO("processor\t: %lu\n", i);
ADD_INFO("vendor_id\t: %s\n", cpu->cpu_vendor);
ADD_INFO("cpu family\t: %lu\n", cpu->cpu_family);
ADD_INFO("model\t\t: %lu\n", cpu->cpu_model);
ADD_INFO("model name\t: %s\n", cpu->cpu_brand);
ADD_INFO("stepping\t: %lu\n", cpu->cpu_stepping);
ADD_INFO("physical id\t: %zu\n", core->socket_id);

/* Linux keeps this numbering socket-local, but we can use a different one, and it's
* documented as "hardware platform's identifier (rather than the kernel's)" anyways. */
ADD_INFO("core id\t\t: %lu\n", thread->core_id);

size_t cores_in_socket = 0;
for (size_t j = 0; j < topo->cores_cnt; j++) { // slow, but shouldn't matter
if (topo->cores[j].socket_id == core->socket_id)
cores_in_socket++;
ret = proc_cpuinfo_display_cpu(&str, &size, &max, topo, cpu, i, thread);
if (ret < 0) {
goto exit;
}
ADD_INFO("cpu cores\t: %zu\n", cores_in_socket);
double bogomips = cpu->cpu_bogomips;
// Apparently Gramine snprintf cannot into floats.
ADD_INFO("bogomips\t: %lu.%02lu\n", (unsigned long)bogomips,
(unsigned long)(bogomips * 100.0 + 0.5) % 100);
ADD_INFO("\n");
}

ret = proc_cpuinfo_display_tail(&str, &size, &max, cpu);
if (ret < 0) {
goto exit;
}

*out_data = str;
*out_size = size;
return 0;

ret = 0;

exit:
if (ret < 0) {
free(str);
}

return ret;
}

#define ADD_INFO(fmt, ...) \
do { \
int ret = print_to_str(&str, size, &max, fmt, ##__VA_ARGS__); \
if (ret < 0) { \
free(str); \
return ret; \
} \
size += ret; \
} while (0)

int proc_stat_load(struct shim_dentry* dent, char** out_data, size_t* out_size) {
__UNUSED(dent);

Expand Down

0 comments on commit 3f2478b

Please sign in to comment.