From f81bef2f53b477f49ea728ad732f8acf9b8ee6a9 Mon Sep 17 00:00:00 2001 From: Alexey Avramov Date: Tue, 26 Mar 2019 01:48:35 +0900 Subject: [PATCH] support re matching with realpath --- README.md | 1 + nohang | 35 ++++++++++++++++++++++++++++++++--- nohang.conf | 6 ++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 77d269e..48d809b 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ The tools listed above may work at the same time on one computer. - Impact on the badness of processes via matching their - names, - cgroups, + - realpathes, - cmdlines and - euids with specified regular expressions diff --git a/nohang b/nohang index f941384..0a8746d 100755 --- a/nohang +++ b/nohang @@ -798,6 +798,18 @@ def pid_to_badness(pid): if search(re_tup[1], cgroup) is not None: badness += int(re_tup[0]) + + if re_match_realpath: + realpath = pid_to_realpath(pid) + for re_tup in realpath_re_list: + if search(re_tup[1], realpath) is not None: + badness += int(re_tup[0]) + + + + + + if re_match_cmdline: cmdline = pid_to_cmdline(pid) for re_tup in cmdline_re_list: @@ -1508,6 +1520,7 @@ processname_re_list = [] cmdline_re_list = [] uid_re_list = [] cgroup_re_list = [] +realpath_re_list = [] @@ -1589,6 +1602,20 @@ try: valid_re(reg_exp) cgroup_re_list.append((badness_adj, reg_exp)) + if line.startswith('@REALPATH_RE'): + a = line.partition( + '@REALPATH_RE')[2].strip(' \n').partition('///') + badness_adj = a[0].strip(' ') + reg_exp = a[2].strip(' ') + valid_re(reg_exp) + realpath_re_list.append((badness_adj, reg_exp)) + + + + + + + except PermissionError: errprint('PermissionError', conf_err_mess) exit(1) @@ -1633,11 +1660,13 @@ regex_matching = conf_parse_bool('regex_matching') re_match_cmdline = conf_parse_bool('re_match_cmdline') re_match_uid = conf_parse_bool('re_match_uid') re_match_cgroup = conf_parse_bool('re_match_cgroup') +re_match_realpath = conf_parse_bool('re_match_realpath') -if regex_matching or re_match_cmdline or re_match_uid or re_match_cgroup: - from re import search - from sre_constants import error as invalid_re +# if regex_matching or re_match_cmdline or re_match_uid or re_match_cgroup +# or re_match_realpath: +# from re import search +# from sre_constants import error as invalid_re (mem_min_sigterm_kb, mem_min_sigterm_mb, mem_min_sigterm_percent ) = calculate_percent('mem_min_sigterm') diff --git a/nohang.conf b/nohang.conf index c6b1872..fc9060d 100644 --- a/nohang.conf +++ b/nohang.conf @@ -244,6 +244,12 @@ re_match_cgroup = False @CGROUP_RE 50 /// foo.service + 5.5 Matching realpath with RE patterns + +re_match_realpath = False + +@REALPATH_RE 20 /// ^/usr/bin + Note that you can control badness also via systemd units via OOMScoreAdjust, see https://www.freedesktop.org/software/systemd/man/systemd.exec.html#OOMScoreAdjust=