From 7d537eefbb7e4e9ba0b2b205c7375e7a87ac4094 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Fri, 27 Aug 2021 13:03:01 -0700 Subject: [PATCH] proc_get_jiffies to accept pid directly No need to accept the mutable proc here. --- src/builtin_jobs.cpp | 2 +- src/proc.cpp | 9 ++++----- src/proc.h | 4 ++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/builtin_jobs.cpp b/src/builtin_jobs.cpp index 556597e6b..8b12f4e8d 100644 --- a/src/builtin_jobs.cpp +++ b/src/builtin_jobs.cpp @@ -34,7 +34,7 @@ static int cpu_use(const job_t *j) { struct timeval t; unsigned long jiffies; gettimeofday(&t, nullptr); - jiffies = proc_get_jiffies(p.get()); + jiffies = proc_get_jiffies(p->pid); double t1 = 1000000.0 * p->last_time.tv_sec + p->last_time.tv_usec; double t2 = 1000000.0 * t.tv_sec + t.tv_usec; diff --git a/src/proc.cpp b/src/proc.cpp index 22bb68d55..3bc902210 100644 --- a/src/proc.cpp +++ b/src/proc.cpp @@ -724,9 +724,8 @@ bool job_reap(parser_t &parser, bool allow_interactive) { } /// Get the CPU time for the specified process. -unsigned long proc_get_jiffies(process_t *p) { - if (!have_proc_stat()) return 0; - if (p->pid <= 0) return 0; +unsigned long proc_get_jiffies(pid_t inpid) { + if (inpid <= 0 || !have_proc_stat()) return 0; char state; int pid, ppid, pgrp, session, tty_nr, tpgid, exit_signal, processor; @@ -739,7 +738,7 @@ unsigned long proc_get_jiffies(process_t *p) { /// Maximum length of a /proc/[PID]/stat filename. constexpr size_t FN_SIZE = 256; char fn[FN_SIZE]; - std::snprintf(fn, FN_SIZE, "/proc/%d/stat", p->pid); + std::snprintf(fn, FN_SIZE, "/proc/%d/stat", inpid); // Don't use autoclose_fd here, we will fdopen() and then fclose() instead. int fd = open_cloexec(fn, O_RDONLY); if (fd < 0) return 0; @@ -767,7 +766,7 @@ void proc_update_jiffies(parser_t &parser) { for (const auto &job : parser.jobs()) { for (process_ptr_t &p : job->processes) { gettimeofday(&p->last_time, nullptr); - p->last_jiffies = proc_get_jiffies(p.get()); + p->last_jiffies = proc_get_jiffies(p->pid); } } } diff --git a/src/proc.h b/src/proc.h index cfd6bcc95..7d977c13e 100644 --- a/src/proc.h +++ b/src/proc.h @@ -509,9 +509,9 @@ job_list_t jobs_requiring_warning_on_exit(const parser_t &parser); /// jobs_requiring_warning_on_exit(). void print_exit_warning_for_jobs(const job_list_t &jobs); -/// Use the procfs filesystem to look up how many jiffies of cpu time was used by this process. This +/// Use the procfs filesystem to look up how many jiffies of cpu time was used by a given pid. This /// function is only available on systems with the procfs file entry 'stat', i.e. Linux. -unsigned long proc_get_jiffies(process_t *p); +unsigned long proc_get_jiffies(pid_t inpid); /// Update process time usage for all processes by calling the proc_get_jiffies function for every /// process of every job.