]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/choom.c
choom: new command to adjust OOM-killer score value
[thirdparty/util-linux.git] / sys-utils / choom.c
1 /*
2 * choom - Change OOM score setting
3 *
4 * Copyright (C) 2018 Karel Zak <kzak@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <getopt.h>
25 #include <errno.h>
26
27 #include <linux/oom.h>
28
29 #include "nls.h"
30 #include "c.h"
31 #include "path.h"
32 #include "strutils.h"
33 #include "closestream.h"
34
35 static void __attribute__((__noreturn__)) usage(void)
36 {
37 FILE *out = stdout;
38 fputs(USAGE_HEADER, out);
39 fprintf(out,
40 _(" %1$s [options] -p pid\n"
41 " %1$s [options] -n number -p pid\n"
42 " %1$s [options] -n number command [args...]]\n"),
43 program_invocation_short_name);
44
45 fputs(USAGE_SEPARATOR, out);
46 fputs(_("Display and adjust OOM-killer score.\n"), out);
47
48 fputs(USAGE_OPTIONS, out);
49 fputs(_(" -n, --adjust <num> specify the adjust score value\n"), out);
50 fputs(_(" -p, --pid <num> process ID\n"), out);
51 fputs(USAGE_SEPARATOR, out);
52 printf(USAGE_HELP_OPTIONS(24));
53 printf(USAGE_MAN_TAIL("choom(1)"));
54 exit(EXIT_SUCCESS);
55 }
56
57 static int get_score(const pid_t pid)
58 {
59 return path_read_s32("/proc/%d/oom_score", (int) pid);
60 }
61
62 static int get_score_adj(const pid_t pid)
63 {
64 return path_read_s32("/proc/%d/oom_score_adj", (int) pid);
65 }
66
67 static int set_score_adj(const pid_t pid, int adj)
68 {
69 char buf[sizeof(stringify_value(OOM_SCORE_ADJ_MIN))];
70
71 snprintf(buf, sizeof(buf), "%d", adj);
72
73 if (path_write_str(buf, "/proc/%d/oom_score_adj", (int) pid) < 0)
74 return -1;
75 return 0;
76 }
77
78 int main(int argc, char **argv)
79 {
80 pid_t pid = 0;
81 int c, adj = 0, has_adj = 0;
82
83 static const struct option longopts[] = {
84 { "adjust", required_argument, NULL, 'n' },
85 { "pid", required_argument, NULL, 'p' },
86 { "help", no_argument, NULL, 'h' },
87 { "version", no_argument, NULL, 'V' },
88 { NULL, 0, NULL, 0 }
89 };
90
91 setlocale(LC_ALL, "");
92 bindtextdomain(PACKAGE, LOCALEDIR);
93 textdomain(PACKAGE);
94 atexit(close_stdout);
95
96 while ((c = getopt_long(argc, argv, "hn:p:V", longopts, NULL)) != -1) {
97 switch (c) {
98 case 'p':
99 pid = strtos32_or_err(optarg, _("invalid PID argument"));
100 break;
101 case 'n':
102 adj = strtos32_or_err(optarg, _("invalid adjust argument"));
103 has_adj = 1;
104 break;
105 case 'V':
106 printf(UTIL_LINUX_VERSION);
107 return EXIT_SUCCESS;
108 case 'h':
109 usage();
110 default:
111 errtryhelp(EXIT_FAILURE);
112 }
113 }
114
115 if (optind < argc && pid) {
116 warnx(_("invalid argument: %s"), argv[optind]);
117 errtryhelp(EXIT_FAILURE);
118 }
119 if (!pid && argc - optind < 1) {
120 warnx(_("no PID or COMMAND specified"));
121 errtryhelp(EXIT_FAILURE);
122 }
123 if (optind < argc && !has_adj) {
124 warnx(_("no OOM score adjust value specified"));
125 errtryhelp(EXIT_FAILURE);
126 }
127
128 /* Show */
129 if (!has_adj) {
130 printf(_("pid %d's current OOM score: %d\n"), pid, get_score(pid));
131 printf(_("pid %d's current OOM score adjust value: %d\n"), pid, get_score_adj(pid));
132
133 /* Change */
134 } else if (pid) {
135 int old = get_score_adj(pid);
136
137 if (set_score_adj(pid, adj))
138 err(EXIT_FAILURE, _("failed to set score adjust value"));
139
140 printf(_("pid %d's OOM score adjust value changed from %d to %d\n"), pid, old, adj);
141
142 /* Start new process */
143 } else {
144 argv += optind;
145 execvp(argv[0], argv);
146 errexec(argv[0]);
147 }
148
149 return EXIT_SUCCESS;
150 }