Subject: | [PATCH] Adding support for /proc/$$/oom_score and oom_adj (Linux) |
[PATCH] Adding support for /proc/$$/oom_score and oom_adj (Linux)
Hello and thanks for Proc::ProcessTable,
I thought I'd share my patch for adding support for oom_score and
oom_adj. It needs some polishing (docs) and field testing, in
particular on older kernels. It may be better suited as a make time
option, with the default being off.
Cheers,
Peter (Stig) Edwards
Proc-ProcessTable-0.45/os/Linux.c#1 - Proc-ProcessTable-0.45/os/Linux.c
====
***************
*** 469,475 ****
--- 469,531 ----
obstack_free(mem_pool, status_text);
}
+ static void get_oom_score(char *pid, char *format_str, struct
procstat* prs,
+ struct obstack *mem_pool)
+ {
+ char *oom_score_text, *loc;
+ off_t oom_score_len;
+ int result;
+ bool read_ok = true;
+
+ if ((oom_score_text = read_file(pid, "oom_score", &oom_score_len,
mem_pool)) == NULL)
+ return;
+
+ loc = oom_score_text;
+
+ result = sscanf(loc, "%d", &prs->oom_score);
+
+ /* 1 item in scanf's list... It's all or nothing baby */
+ if (result != 1) {
+ read_ok = false;
+ goto done;
+ }
+
+ /* enable fields; F_OOM_SCORE is not the range */
+ field_enable_range(format_str, F_OOM_SCORE, F_OOM_SCORE);
+
+ done:
+ obstack_free(mem_pool, oom_score_text);
+ }
+
+ static void get_oom_adj(char *pid, char *format_str, struct procstat*
prs,
+ struct obstack *mem_pool)
+ {
+ char *oom_adj_text, *loc;
+ off_t oom_adj_len;
+ int result;
+ bool read_ok = true;
+
+ if ((oom_adj_text = read_file(pid, "oom_adj", &oom_adj_len,
mem_pool)) == NULL)
+ return;
+
+ loc = oom_adj_text;
+
+ result = sscanf(loc, "%d", &prs->oom_adj);
+ /* 1 item in scanf's list... It's all or nothing baby */
+ if (result != 1) {
+ read_ok = false;
+ goto done;
+ }
+
+ /* enable fields; F_OOM_ADJ is not the range */
+ field_enable_range(format_str, F_OOM_ADJ, F_OOM_ADJ);
+
+ done:
+ obstack_free(mem_pool, oom_adj_text);
+ }
+
+
/* fixup_stat_values()
*
* Correct, calculate, covert values to user expected values.
***************
*** 647,652 ****
--- 703,714 ----
/* scapre from /proc/{$pid}/status */
get_proc_status(dir_result->d_name, format_str, prs,
&mem_pool);
+ /* scapre from /proc/{$pid}/oom_score */
+ get_oom_score(dir_result->d_name, format_str, prs, &mem_pool);
+
+ /* scapre from /proc/{$pid}/oom_adj */
+ get_oom_adj(dir_result->d_name, format_str, prs, &mem_pool);
+
/* calculate precent cpu & mem values */
calc_prec(format_str, prs, &mem_pool);
***************
*** 687,693 ****
prs->pctmem,
prs->cmndline,
prs->exec,
! prs->cwd
);
/* we want a new prs, for the next itteration */
--- 749,757 ----
prs->pctmem,
prs->cmndline,
prs->exec,
! prs->cwd,
! prs->oom_score,
! prs->oom_adj
);
/* we want a new prs, for the next itteration */
==== Proc-ProcessTable-0.45/os/Linux.h#1 - Proc-ProcessTable-
0.45/os/Linux.h ====
***************
*** 44,49 ****
--- 44,51 ----
/* other values */
char pctcpu[sizeof("100.00")]; /*
precent cpu, without '%' char */
char pctmem[sizeof("100.00")]; /*
precent memory, without '%' char */
+ int oom_score;
+ int oom_adj;
};
***************
*** 105,112 ****
"cmndline\0"
"exec\0"
"cwd\0"
/* format string */
! "IIISIIIILLLLLJJJJIJLLLJJSIIIIIISSSSS\0"
};
/* I generated this array with a perl script processing the above
char array,
--- 107,116 ----
"cmndline\0"
"exec\0"
"cwd\0"
+ "oom_score\0"
+ "oom_adj\0"
/* format string */
! "IIISIIIILLLLLJJJJIJLLLJJSIIIIIISSSSSII\0"
};
/* I generated this array with a perl script processing the above
char array,
***************
*** 156,163 ****
270,
279,
284,
/* default format string (pre lower casing) */
! 288
};
enum string_name {
--- 160,169 ----
270,
279,
284,
+ 288,
+ 298,
/* default format string (pre lower casing) */
! 306
};
enum string_name {
***************
*** 203,208 ****
--- 209,216 ----
STR_FIELD_CMNDLINE,
STR_FIELD_EXEC,
STR_FIELD_CWD,
+ STR_FIELD_OOM_SCORE,
+ STR_FIELD_OOM_ADJ,
/* format string */
STR_DEFAULT_FORMAT
};
***************
*** 245,251 ****
F_PCTMEM,
F_CMNDLINE,
F_EXEC,
! F_CWD
};
static const char* const field_names[] =
--- 253,261 ----
F_PCTMEM,
F_CMNDLINE,
F_EXEC,
! F_CWD,
! F_OOM_SCORE,
! F_OOM_ADJ
};
static const char* const field_names[] =
***************
*** 285,290 ****
strings + 263,
strings + 270,
strings + 279,
! strings + 284
};
--- 295,302 ----
strings + 263,
strings + 270,
strings + 279,
! strings + 284,
! strings + 288,
! strings + 298
};