bool cpu_frequency::get_frequency(cpu_frequency_type& type,unsigned long& khz, unsigned long& khzUpper, unsigned long& khzLower) { if(!valid) return false; std::string gov; if(!get_string_from_file(path + "scaling_governor",gov)) { return false; } cpu_frequency_type t = get_governor_type(gov); if(t == Invalid) { return false; } if(!get_number_from_file(path + "cpuinfo_cur_freq",khz)) { return false; } if(!get_number_from_file(path + "scaling_max_freq",khzUpper)) { return false; } if(!get_number_from_file(path + "scaling_min_freq",khzLower)) { return false; } type = t; last_error = PBSE_NONE; return true; }
cpu_frequency::cpu_frequency(int cpu_number) { path = base_cpu_path; path += "cpu"; std::stringstream cnv; cnv << cpu_number; path += cnv.str(); path += "/cpufreq/"; last_error = PBSE_NODE_CANT_MANAGE_FREQUENCY; if(!get_number_from_file(path + "cpuinfo_min_freq",cpu_min_frequency)) { valid = false; return; } if(!get_number_from_file(path + "cpuinfo_max_freq",cpu_max_frequency)) { valid = false; return; } std::vector<std::string> govs; if(!get_strings_from_file(path + "scaling_available_governors",govs)) { valid = false; return; } for(std::vector<std::string>::iterator i = govs.begin();i != govs.end();i++) { cpu_frequency_type t = get_governor_type(*i); if(t != Invalid) { available_governors.push_back(t); } } std::string gov; if(!get_string_from_file(path + "scaling_governor",gov)) { valid = false; return; } governor = get_governor_type(gov); if(governor == Invalid) { valid = false; return; } if(!get_numbers_from_file(path + "scaling_available_frequencies",available_frequencies)) { valid = false; return; } std::sort(available_frequencies.begin(),available_frequencies.end(),descending); valid = true; last_error = PBSE_NONE; }
static int abuf_json_sysdata(struct autobuf *abuf, const char* key, const char* syspath) { int ret = -1; char buf[256]; *buf = 0; ret = get_string_from_file(syspath, buf, 256); if (*buf != 0) abuf_json_string(abuf, key, buf); return ret; }
int main(int argc, char *argv[]) { debg_ofp = fopen("balanced_parenthsis.log", "w"); int max_len; char *inp_str; int inp_choice; int ds_choice; unsigned int pos; int status; if (argc < 5) { fprintf(stderr, "Invalid Arguments\n"); exit(EXIT_FAILURE); } max_len = atoi(argv[2]); inp_str = argv[1]; inp_choice = atoi(argv[3]); ds_choice = atoi(argv[4]); if( inp_choice > 0 ) { inp_str = get_string_from_file(inp_str, max_len); } fprintf(debg_ofp, "Input String: %s, Max_len: %d Input Choice: %s\n", inp_str, max_len, (inp_choice > 0 ? "File" : "Command line")); switch(ds_choice) { case 1: status = is_balance_ll(inp_str, &pos); break; case 2: status = is_balance_arr(inp_str, max_len, &pos); break; default: printf("Wrong choice\n"); } if ( status == 0 ) { printf("TRUE\n"); } else { printf("FALSE %u\n", pos); } if( inp_choice > 0 ) { free(inp_str); } }
/* * get_all_cpu_cache_details() * Obtain information on all cpus caches on the system. * * Returns: dynamically-allocated cpus_t object, or NULL on error. */ cpus_t * get_all_cpu_cache_details(void) { uint32_t i; int ret; glob_t globbuf; char **results; cpus_t *cpus = NULL; size_t cpu_count; (void)memset(&globbuf, 0, sizeof(globbuf)); ret = file_exists(SYS_CPU_PREFIX); if (!ret) { pr_err("%s does not exist\n", SYS_CPU_PREFIX); return NULL; } if (ret != S_IFDIR) { pr_err("file %s is not a directory\n", SYS_CPU_PREFIX); return NULL; } ret = glob(GLOB_PATTERN, SHIM_GLOB_ONLYDIR, NULL, &globbuf); if (ret != 0) { pr_err("glob on regex \"%s\" failed: %d\n", GLOB_PATTERN, ret); return NULL; } results = globbuf.gl_pathv; cpu_count = globbuf.gl_pathc; if (!cpu_count) { /* Maybe we should check this? */ pr_err("no CPUs found - is /sys mounted?\n"); goto out; } cpus = calloc(1, sizeof(cpus_t)); if (!cpus) goto out; cpus->cpus = calloc(cpu_count, sizeof(cpu_t)); if (!cpus->cpus) { free(cpus); cpus = NULL; goto out; } cpus->count = cpu_count; for (i = 0; i < cpus->count; i++) { char *contents = NULL; cpu_t *cpu; cpu = &cpus->cpus[i]; cpu->num = i; if (i == 0) { /* 1st CPU cannot be taken offline */ cpu->online = 1; } else { const size_t len = strlen(results[i]); char path[len + 32]; (void)memset(path, 0, sizeof(path)); (void)strncpy(path, results[i], len); mk_path(path, len, "/online"); contents = get_string_from_file(path); if (!contents) goto out; cpu->online = atoi(contents); free(contents); } ret = get_cpu_cache_details(&cpus->cpus[i], results[i]); if (ret != EXIT_SUCCESS) { free(cpus->cpus); free(cpus); cpus = NULL; goto out; } } out: globfree(&globbuf); return cpus; }
/* * add_cpu_cache_detail() * @cache: cpu_cache_t pointer. * @index_path: full /sys path to the particular cpu cache which is to * be represented by @cache. * Populate the specified @cache based on the given cache index. * * Returns: EXIT_FAILURE or EXIT_SUCCESS. */ static int add_cpu_cache_detail(cpu_cache_t *cache, const char *index_path) { const size_t index_posn = index_path ? strlen(index_path) : 0; const size_t path_len = index_posn + 32; char path[path_len]; char *contents = NULL; int ret = EXIT_FAILURE; (void)memset(path, 0, sizeof(path)); if (!cache) { pr_dbg("%s: invalid cache specified\n", __func__); goto out; } if (!index_path) { pr_dbg("%s: invalid index specified\n", __func__); goto out; } (void)strncpy(path, index_path, index_posn + 1); mk_path(path, index_posn, "/type"); contents = get_string_from_file(path); if (!contents) goto out; cache->type = (cache_type_t)get_cache_type(contents); if (cache->type == CACHE_TYPE_UNKNOWN) goto out; free(contents); mk_path(path, index_posn, "/size"); contents = get_string_from_file(path); if (!contents) goto out; cache->size = size_to_bytes(contents); free(contents); mk_path(path, index_posn, "/level"); contents = get_string_from_file(path); if (!contents) goto out; cache->level = (uint16_t)atoi(contents); free(contents); mk_path(path, index_posn, "/coherency_line_size"); contents = get_string_from_file(path); if (!contents) goto out; cache->line_size = (uint32_t)atoi(contents); free(contents); mk_path(path, index_posn, "/ways_of_associativity"); contents = get_string_from_file(path); /* Don't error if file is not readable: cache may not be * way-based. */ cache->ways = contents ? atoi(contents) : 0; ret = EXIT_SUCCESS; out: if (contents) free(contents); return ret; }