static void nodeMeminfo(int node, uint64_t* totalMemory, uint64_t* freeMemory) { FILE *fp; bstring filename; bstring totalString = bformat("MemTotal:"); bstring freeString = bformat("MemFree:"); int i; filename = bformat("/sys/devices/system/node/node%d/meminfo", node); if (NULL != (fp = fopen (bdata(filename), "r"))) { bstring src = bread ((bNread) fread, fp); struct bstrList* tokens = bsplit(src,(char) '\n'); for (i=0;i<tokens->qty;i++) { if (binstr(tokens->entry[i],0,totalString) != BSTR_ERR) { bstring tmp = bmidstr (tokens->entry[i], 18, blength(tokens->entry[i])-18 ); bltrimws(tmp); struct bstrList* subtokens = bsplit(tmp,(char) ' '); *totalMemory = str2int(bdata(subtokens->entry[0])); } else if (binstr(tokens->entry[i],0,freeString) != BSTR_ERR) { bstring tmp = bmidstr (tokens->entry[i], 18, blength(tokens->entry[i])-18 ); bltrimws(tmp); struct bstrList* subtokens = bsplit(tmp,(char) ' '); *freeMemory = str2int(bdata(subtokens->entry[0])); } } } else { ERROR; } fclose(fp); }
static void define_handle(state_t* state, match_t* match, bool* reprocess) { // We need to parse this manually because we're interested in getting // the first word and then all of the content until a line that doesn't end // with "\". bstring name = bfromcstr(""); bstring word = bfromcstr(""); bstring definition = bfromcstr(""); bool getting_word = true; bool getting_definition = true; bool is_macro = false; match_t* new_match; struct replace_info* info; // Get the first word. while (getting_word) { char c = ppimpl_get_input(state); bconchar(word, c); if (!is_macro && c != '(') bconchar(name, c); bltrimws(word); // Handle termination. if (blength(word) > 0 && (c == ' ' || c == '\t') && !is_macro) { // End of word. btrimws(word); btrimws(name); getting_word = false; } else if (blength(word) > 0 && c == '(' && !is_macro) { // Start of macro. is_macro = true; } else if (blength(word) > 0 && c == '(' && is_macro) { // Second ( in a macro; error. dhalt(ERR_PP_MACRO_MALFORMED, ppimpl_get_location(state)); } else if (blength(word) > 0 && c == ')' && is_macro) { // End of macro name. btrimws(word); btrimws(name); getting_word = false; } else if (blength(word) == 0 && c == '\n') dhalt(ERR_PP_C_DEFINE_PARAMETERS_INCORRECT, ppimpl_get_location(state)); else if (blength(word) > 0 && c == '\n') { // End of word. btrimws(word); btrimws(name); getting_word = false; getting_definition = false; ppimpl_printf(state, "\n"); } } // Get the definition. while (getting_definition) { char c = ppimpl_get_input(state); bconchar(definition, c); bltrimws(definition); if (c == '\n') { if (blength(definition) > 1 && definition->data[blength(definition) - 2] == '\\') { // Remove the new slash. bdelete(definition, blength(definition) - 2, 1); ppimpl_oprintf(state, "\n"); } else { btrimws(definition); getting_definition = false; ppimpl_printf(state, "\n"); } } else if (c == '/' || c == '*') { if (blength(definition) > 1 && definition->data[blength(definition) - 2] == '/') { // a line or block comment ppimpl_iprintf(state, "/%c", c); // remove the slashes bdelete(definition, blength(definition) - 2, 2); btrimws(definition); getting_definition = false; } } } if (blength(definition) == 0 && !is_macro) bassigncstr(definition, "1"); // Create the new replacement handler. info = malloc(sizeof(struct replace_info)); info->full = word; info->replacement = definition; if (biseq(info->full, info->replacement)) { free(info); return; } new_match = malloc(sizeof(match_t)); new_match->text = bautofree(name); if (is_macro) new_match->handler = macro_handle; else new_match->handler = replace_handle; new_match->line_start_only = false; new_match->identifier_only = true; new_match->userdata = info; new_match->case_insensitive = false; ppimpl_register(state, new_match); *reprocess = true; }
uint64_t getFreeNodeMem(int nodeId) { FILE *fp; bstring filename; uint64_t free = 0; bstring freeString = bformat("MemFree:"); int i; filename = bformat("/sys/devices/system/node/node%d/meminfo", nodeId); if (NULL != (fp = fopen (bdata(filename), "r"))) { bstring src = bread ((bNread) fread, fp); struct bstrList* tokens = bsplit(src,(char) '\n'); for (i=0;i<tokens->qty;i++) { if (binstr(tokens->entry[i],0,freeString) != BSTR_ERR) { bstring tmp = bmidstr (tokens->entry[i], 18, blength(tokens->entry[i])-18 ); bltrimws(tmp); struct bstrList* subtokens = bsplit(tmp,(char) ' '); free = str2int(bdata(subtokens->entry[0])); bdestroy(tmp); bstrListDestroy(subtokens); } } bstrListDestroy(tokens); bdestroy(src); fclose(fp); } else if (!access("/proc/meminfo", R_OK)) { bdestroy(filename); filename = bfromcstr("/proc/meminfo"); if (NULL != (fp = fopen (bdata(filename), "r"))) { bstring src = bread ((bNread) fread, fp); struct bstrList* tokens = bsplit(src,(char) '\n'); for (i=0;i<tokens->qty;i++) { if (binstr(tokens->entry[i],0,freeString) != BSTR_ERR) { bstring tmp = bmidstr (tokens->entry[i], 10, blength(tokens->entry[i])-10 ); bltrimws(tmp); struct bstrList* subtokens = bsplit(tmp,(char) ' '); free = str2int(bdata(subtokens->entry[0])); bdestroy(tmp); bstrListDestroy(subtokens); } } bstrListDestroy(tokens); bdestroy(src); fclose(fp); } } else { bdestroy(freeString); bdestroy(filename); ERROR; } bdestroy(freeString); bdestroy(filename); return free; }
uint64_t getTotalNodeMem(int nodeId) { int i; FILE *fp; uint64_t total = 0; bstring totalString = bformat("MemTotal:"); bstring sysfilename = bformat("/sys/devices/system/node/node%d/meminfo", nodeId); bstring procfilename = bformat("/proc/meminfo"); char *sptr = bdata(procfilename); if (NULL != (fp = fopen (bdata(sysfilename), "r"))) { bstring src = bread ((bNread) fread, fp); struct bstrList* tokens = bsplit(src,(char) '\n'); for (i=0;i<tokens->qty;i++) { if (binstr(tokens->entry[i],0,totalString) != BSTR_ERR) { bstring tmp = bmidstr (tokens->entry[i], 18, blength(tokens->entry[i])-18 ); bltrimws(tmp); struct bstrList* subtokens = bsplit(tmp,(char) ' '); total = str2int(bdata(subtokens->entry[0])); bdestroy(tmp); bstrListDestroy(subtokens); } } bstrListDestroy(tokens); bdestroy(src); fclose(fp); } else if (!access(sptr, R_OK)) { if (NULL != (fp = fopen (bdata(procfilename), "r"))) { bstring src = bread ((bNread) fread, fp); struct bstrList* tokens = bsplit(src,(char) '\n'); for (i=0;i<tokens->qty;i++) { if (binstr(tokens->entry[i],0,totalString) != BSTR_ERR) { bstring tmp = bmidstr (tokens->entry[i], 10, blength(tokens->entry[i])-10 ); bltrimws(tmp); struct bstrList* subtokens = bsplit(tmp,(char) ' '); total = str2int(bdata(subtokens->entry[0])); bdestroy(tmp); bstrListDestroy(subtokens); } } bstrListDestroy(tokens); bdestroy(src); fclose(fp); } } else { bdestroy(totalString); bdestroy(sysfilename); bdestroy(procfilename); ERROR; } bdestroy(totalString); bdestroy(sysfilename); bdestroy(procfilename); return total; }