Skip to content

Commit

Permalink
Pass poll_loop_args_t by reference
Browse files Browse the repository at this point in the history
Found by lgtm.com:

  This parameter of type const poll_loop_args_t is 72
  bytes - consider passing a const pointer/reference instead.
  • Loading branch information
rfjakob committed Mar 22, 2020
1 parent ab670ac commit 2f98f75
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
22 changes: 11 additions & 11 deletions kill.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ static void maybe_notify(char* notif_command, char* notif_args)
* Send the selected signal to "pid" and wait for the process to exit
* (max 10 seconds)
*/
int kill_wait(const poll_loop_args_t args, pid_t pid, int sig)
int kill_wait(const poll_loop_args_t *args, pid_t pid, int sig)
{
if (args.dryrun && sig != 0) {
if (args->dryrun && sig != 0) {
warn("dryrun, not actually sending any signal\n");
return 0;
}
Expand All @@ -78,7 +78,7 @@ int kill_wait(const poll_loop_args_t args, pid_t pid, int sig)
if (sig != SIGKILL) {
m = parse_meminfo();
print_mem_stats(debug, m);
if (m.MemAvailablePercent <= args.mem_kill_percent && m.SwapFreePercent <= args.swap_kill_percent) {
if (m.MemAvailablePercent <= args->mem_kill_percent && m.SwapFreePercent <= args->swap_kill_percent) {
sig = SIGKILL;
res = kill(pid, sig);
// kill first, print after
Expand All @@ -104,7 +104,7 @@ int kill_wait(const poll_loop_args_t args, pid_t pid, int sig)
/*
* Find the process with the largest oom_score and kill it.
*/
void kill_largest_process(const poll_loop_args_t args, int sig)
void kill_largest_process(const poll_loop_args_t *args, int sig)
{
struct procinfo victim = { 0 };
struct timespec t0 = { 0 }, t1 = { 0 };
Expand Down Expand Up @@ -153,7 +153,7 @@ void kill_largest_process(const poll_loop_args_t args, int sig)
}
cur.badness = res;
}
if (args.ignore_oom_score_adj) {
if (args->ignore_oom_score_adj) {
int oom_score_adj = 0;
int res = get_oom_score_adj(cur.pid, &oom_score_adj);
if (res < 0) {
Expand All @@ -165,16 +165,16 @@ void kill_largest_process(const poll_loop_args_t args, int sig)
}
}

if ((args.prefer_regex || args.avoid_regex)) {
if ((args->prefer_regex || args->avoid_regex)) {
int res = get_comm(cur.pid, cur.name, sizeof(cur.name));
if (res < 0) {
debug(" error reading process name: %s\n", strerror(-res));
continue;
}
if (args.prefer_regex && regexec(args.prefer_regex, cur.name, (size_t)0, NULL, 0) == 0) {
if (args->prefer_regex && regexec(args->prefer_regex, cur.name, (size_t)0, NULL, 0) == 0) {
cur.badness += BADNESS_PREFER;
}
if (args.avoid_regex && regexec(args.avoid_regex, cur.name, (size_t)0, NULL, 0) == 0) {
if (args->avoid_regex && regexec(args->avoid_regex, cur.name, (size_t)0, NULL, 0) == 0) {
cur.badness += BADNESS_AVOID;
}
}
Expand Down Expand Up @@ -234,7 +234,7 @@ void kill_largest_process(const poll_loop_args_t args, int sig)

if (victim.pid <= 0) {
warn("Could not find a process to kill. Sleeping 1 second.\n");
maybe_notify(args.notif_command,
maybe_notify(args->notif_command,
"-i dialog-error 'earlyoom' 'Error: Could not find a process to kill. Sleeping 1 second.'");
sleep(1);
return;
Expand Down Expand Up @@ -271,7 +271,7 @@ void kill_largest_process(const poll_loop_args_t args, int sig)
sanitize(victim.name);
snprintf(notif_args, sizeof(notif_args),
"-i dialog-warning 'earlyoom' 'Low memory! Killing process %d %s'", victim.pid, victim.name);
maybe_notify(args.notif_command, notif_args);
maybe_notify(args->notif_command, notif_args);
}

if (sig == 0) {
Expand All @@ -280,7 +280,7 @@ void kill_largest_process(const poll_loop_args_t args, int sig)

if (res != 0) {
warn("kill failed: %s\n", strerror(saved_errno));
maybe_notify(args.notif_command,
maybe_notify(args->notif_command,
"-i dialog-error 'earlyoom' 'Error: Failed to kill process'");
// Killing the process may have failed because we are not running as root.
// In that case, trying again in 100ms will just yield the same error.
Expand Down
2 changes: 1 addition & 1 deletion kill.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ typedef struct {
bool dryrun;
} poll_loop_args_t;

void kill_largest_process(poll_loop_args_t args, int sig);
void kill_largest_process(const poll_loop_args_t *args, int sig);

#endif
22 changes: 11 additions & 11 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ enum {
};

static int set_oom_score_adj(int);
static void poll_loop(const poll_loop_args_t args);
static void poll_loop(const poll_loop_args_t *args);

// Prevent Golang / Cgo name collision when the test suite runs -
// Cgo generates it's own main function.
Expand Down Expand Up @@ -294,7 +294,7 @@ int main(int argc, char* argv[])
* calling mlockall()
*/
debug("dry-running kill_largest_process()...\n");
kill_largest_process(args, 0);
kill_largest_process(&args, 0);

int err = mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT);
// kernels older than 4.4 don't support MCL_ONFAULT. Retry without it.
Expand All @@ -306,7 +306,7 @@ int main(int argc, char* argv[])
}

// Jump into main poll loop
poll_loop(args);
poll_loop(&args);
return 0;
}

Expand Down Expand Up @@ -368,7 +368,7 @@ static unsigned sleep_time_ms(const poll_loop_args_t* args, const meminfo_t* m)
return (unsigned)ms;
}

static void poll_loop(const poll_loop_args_t args)
static void poll_loop(const poll_loop_args_t *args)
{
// Print a a memory report when this reaches zero. We start at zero so
// we print the first report immediately.
Expand All @@ -377,24 +377,24 @@ static void poll_loop(const poll_loop_args_t args)
while (1) {
int sig = 0;
meminfo_t m = parse_meminfo();
if (m.MemAvailablePercent <= args.mem_kill_percent && m.SwapFreePercent <= args.swap_kill_percent) {
if (m.MemAvailablePercent <= args->mem_kill_percent && m.SwapFreePercent <= args->swap_kill_percent) {
print_mem_stats(warn, m);
warn("low memory! at or below SIGKILL limits: mem " PRIPCT ", swap " PRIPCT "\n",
args.mem_kill_percent, args.swap_kill_percent);
args->mem_kill_percent, args->swap_kill_percent);
sig = SIGKILL;
} else if (m.MemAvailablePercent <= args.mem_term_percent && m.SwapFreePercent <= args.swap_term_percent) {
} else if (m.MemAvailablePercent <= args->mem_term_percent && m.SwapFreePercent <= args->swap_term_percent) {
print_mem_stats(warn, m);
warn("low memory! at or below SIGTERM limits: mem " PRIPCT ", swap " PRIPCT "\n",
args.mem_term_percent, args.swap_term_percent);
args->mem_term_percent, args->swap_term_percent);
sig = SIGTERM;
}
if (sig) {
kill_largest_process(args, sig);
} else if (args.report_interval_ms && report_countdown_ms <= 0) {
} else if (args->report_interval_ms && report_countdown_ms <= 0) {
print_mem_stats(printf, m);
report_countdown_ms = args.report_interval_ms;
report_countdown_ms = args->report_interval_ms;
}
unsigned sleep_ms = sleep_time_ms(&args, &m);
unsigned sleep_ms = sleep_time_ms(args, &m);
debug("adaptive sleep time: %d ms\n", sleep_ms);
usleep(sleep_ms * 1000);
report_countdown_ms -= (int)sleep_ms;
Expand Down
2 changes: 1 addition & 1 deletion testsuite_c_wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func parse_meminfo() C.meminfo_t {

func kill_largest_process() {
var args C.poll_loop_args_t
C.kill_largest_process(args, 0)
C.kill_largest_process(&args, 0)
}

func get_oom_score(pid int) int {
Expand Down

0 comments on commit 2f98f75

Please sign in to comment.