Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * PAPR Energy attributes sniff test
0004  * This checks if the papr folders and contents are populated relating to
0005  * the energy and frequency attributes
0006  *
0007  * Copyright 2022, Pratik Rajesh Sampat, IBM Corp.
0008  */
0009 
0010 #include <errno.h>
0011 #include <stdio.h>
0012 #include <string.h>
0013 #include <dirent.h>
0014 #include <sys/types.h>
0015 #include <sys/stat.h>
0016 #include <unistd.h>
0017 #include <stdlib.h>
0018 
0019 #include "utils.h"
0020 
0021 enum energy_freq_attrs {
0022     POWER_PERFORMANCE_MODE = 1,
0023     IDLE_POWER_SAVER_STATUS = 2,
0024     MIN_FREQ = 3,
0025     STAT_FREQ = 4,
0026     MAX_FREQ = 6,
0027     PROC_FOLDING_STATUS = 8
0028 };
0029 
0030 enum type {
0031     INVALID,
0032     STR_VAL,
0033     NUM_VAL
0034 };
0035 
0036 static int value_type(int id)
0037 {
0038     int val_type;
0039 
0040     switch (id) {
0041     case POWER_PERFORMANCE_MODE:
0042     case IDLE_POWER_SAVER_STATUS:
0043         val_type = STR_VAL;
0044         break;
0045     case MIN_FREQ:
0046     case STAT_FREQ:
0047     case MAX_FREQ:
0048     case PROC_FOLDING_STATUS:
0049         val_type = NUM_VAL;
0050         break;
0051     default:
0052         val_type = INVALID;
0053     }
0054 
0055     return val_type;
0056 }
0057 
0058 static int verify_energy_info(void)
0059 {
0060     const char *path = "/sys/firmware/papr/energy_scale_info";
0061     struct dirent *entry;
0062     struct stat s;
0063     DIR *dirp;
0064 
0065     errno = 0;
0066     if (stat(path, &s)) {
0067         SKIP_IF(errno == ENOENT);
0068         FAIL_IF(errno);
0069     }
0070 
0071     FAIL_IF(!S_ISDIR(s.st_mode));
0072 
0073     dirp = opendir(path);
0074 
0075     while ((entry = readdir(dirp)) != NULL) {
0076         char file_name[64];
0077         int id, attr_type;
0078         FILE *f;
0079 
0080         if (strcmp(entry->d_name, ".") == 0 ||
0081             strcmp(entry->d_name, "..") == 0)
0082             continue;
0083 
0084         id = atoi(entry->d_name);
0085         attr_type = value_type(id);
0086         FAIL_IF(attr_type == INVALID);
0087 
0088         /* Check if the files exist and have data in them */
0089         sprintf(file_name, "%s/%d/desc", path, id);
0090         f = fopen(file_name, "r");
0091         FAIL_IF(!f);
0092         FAIL_IF(fgetc(f) == EOF);
0093 
0094         sprintf(file_name, "%s/%d/value", path, id);
0095         f = fopen(file_name, "r");
0096         FAIL_IF(!f);
0097         FAIL_IF(fgetc(f) == EOF);
0098 
0099         if (attr_type == STR_VAL) {
0100             sprintf(file_name, "%s/%d/value_desc", path, id);
0101             f = fopen(file_name, "r");
0102             FAIL_IF(!f);
0103             FAIL_IF(fgetc(f) == EOF);
0104         }
0105     }
0106 
0107     return 0;
0108 }
0109 
0110 int main(void)
0111 {
0112     return test_harness(verify_energy_info, "papr_attributes");
0113 }