From 0146f46864a328cf10b92e985274bee1e3171fb4 Mon Sep 17 00:00:00 2001 From: Matheus Castanho Date: Tue, 12 Jul 2022 11:19:00 -0300 Subject: [PATCH] test: Add helper to read LPAR info from /proc Signed-off-by: Matheus Castanho --- test/test_utils.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++ test/test_utils.h | 10 ++++++++++ 2 files changed, 58 insertions(+) diff --git a/test/test_utils.c b/test/test_utils.c index 3db5525..d819da7 100644 --- a/test/test_utils.c +++ b/test/test_utils.c @@ -17,6 +17,8 @@ Byte ran_data[DATA_MAX_LEN]; +struct lpar_info lpar_info; + static char dict[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', @@ -67,6 +69,52 @@ int is_powervm(void) { return !access(dtpath, F_OK); } +int get_lpar_info(void) { + char *line = NULL; + FILE *f; + size_t n; + + const char* lparcfg = "/proc/ppc64/lparcfg"; + + const char shared_mode_key[] = "shared_processor_mode"; + const size_t shared_mode_len = sizeof(shared_mode_key)/sizeof(char) - 1; + bool shared_mode_found = false; + + const char active_procs_key[] = "partition_active_processors"; + const size_t active_procs_len = sizeof(active_procs_key)/sizeof(char) -1; + bool active_procs_found = false; + + f = fopen(lparcfg, "r"); + if (!f) { + fprintf(stderr, "Couldn't file %s!\n", lparcfg); + return -1; + } + + do { + ssize_t nchars = getline(&line, &n, f); + + if (nchars == 0 || nchars == -1) + break; + + if (!shared_mode_found && + !strncmp(shared_mode_key, line, shared_mode_len)) { + lpar_info.shared_proc_mode = line[nchars-2] == '1'; + shared_mode_found = true; + printf("Mode: %s\n", + lpar_info.shared_proc_mode ? "shared" : "dedicated"); + } else if (!active_procs_found && + !strncmp(active_procs_key, line, active_procs_len)) { + lpar_info.active_processors = atoi(&line[active_procs_len+1]); + active_procs_found = true; + printf("Number of active processors: %d\n", + lpar_info.active_processors); + } + } while(!active_procs_found || !shared_mode_found); + + free(line); + return 0; +} + int read_sysfs_entry(const char *path, int *val) { char buf[32]; diff --git a/test/test_utils.h b/test/test_utils.h index d5a4e91..8e2a978 100644 --- a/test/test_utils.h +++ b/test/test_utils.h @@ -2,6 +2,7 @@ #define TEST_UTILS_H_ #include +#include #define DATA_MAX_LEN (128*1024*1024) // 128M @@ -19,6 +20,12 @@ struct f_interval { struct timeval end; }; +struct lpar_info { + bool shared_proc_mode; + int active_processors; +}; + +extern struct lpar_info lpar_info; extern Byte ran_data[DATA_MAX_LEN]; extern void generate_random_data(int len); extern Byte* generate_allocated_random_data(unsigned int len); @@ -30,6 +37,9 @@ void zcheck_internal(int retval, int expected, char* file, int line); #define zcheck(val,exp) zcheck_internal((val),(exp),__FILE__,__LINE__) int is_powervm(void); + +int get_lpar_info(void); + int read_sysfs_entry(const char *path, int *val); int _test_nx_deflate(Byte* src, unsigned int src_len, Byte* compr,