Skip to content

Commit

Permalink
test: Add helper to read LPAR info from /proc
Browse files Browse the repository at this point in the history
Signed-off-by: Matheus Castanho <msc@linux.ibm.com>
  • Loading branch information
mscastanho committed Jul 14, 2022
1 parent 0de0e18 commit 0146f46
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
48 changes: 48 additions & 0 deletions test/test_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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];
Expand Down
10 changes: 10 additions & 0 deletions test/test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define TEST_UTILS_H_

#include <sys/time.h>
#include <stdbool.h>

#define DATA_MAX_LEN (128*1024*1024) // 128M

Expand All @@ -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);
Expand All @@ -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,
Expand Down

0 comments on commit 0146f46

Please sign in to comment.