Skip to content

Commit

Permalink
Switch to runtime check for /proc/self/stat
Browse files Browse the repository at this point in the history
Removes a compile-time check that may have affected cross-compilation.

Work on fish-shell#1067.
  • Loading branch information
zanchey committed Apr 29, 2019
1 parent d8ac051 commit 636af89
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 30 deletions.
3 changes: 0 additions & 3 deletions cmake/ConfigureChecks.cmake
Expand Up @@ -128,9 +128,6 @@ IF(STRUCT_WINSIZE GREATER -1 AND HAVE_TIOCGWINSZ EQUAL 1)
ENDIF()
CMAKE_POP_CHECK_STATE()

IF(EXISTS "/proc/self/stat")
SET(HAVE__PROC_SELF_STAT 1)
ENDIF()
CHECK_TYPE_SIZE("wchar_t[8]" WCHAR_T_BITS LANGUAGE CXX)

# Solaris, NetBSD and X/Open-conforming systems have a fixed-args tparm
Expand Down
3 changes: 0 additions & 3 deletions config_cmake.h.in
Expand Up @@ -127,9 +127,6 @@
/* Define to 1 if the _nl_msg_cat_cntr symbol is exported. */
#cmakedefine HAVE__NL_MSG_CAT_CNTR 1

/* Define to 1 if you have the file `/proc/self/stat'. */
#cmakedefine HAVE__PROC_SELF_STAT 1

/* Define to 1 if the _sys_errs array is available. */
#cmakedefine HAVE__SYS__ERRS 1

Expand Down
3 changes: 0 additions & 3 deletions src/builtin_eval.cpp
Expand Up @@ -3,9 +3,6 @@

#include <errno.h>
#include <stddef.h>
#ifdef HAVE__PROC_SELF_STAT
#include <sys/time.h>
#endif

#include "builtin.h"
#include "common.h"
Expand Down
17 changes: 7 additions & 10 deletions src/builtin_jobs.cpp
Expand Up @@ -3,9 +3,7 @@

#include <errno.h>
#include <stddef.h>
#ifdef HAVE__PROC_SELF_STAT
#include <sys/time.h>
#endif

#include "builtin.h"
#include "common.h"
Expand All @@ -26,7 +24,6 @@ enum {
JOBS_PRINT_NOTHING, // print nothing (exit status only)
};

#ifdef HAVE__PROC_SELF_STAT
/// Calculates the cpu usage (in percent) of the specified job.
static int cpu_use(const job_t *j) {
double u = 0;
Expand All @@ -45,7 +42,6 @@ static int cpu_use(const job_t *j) {
}
return u * 1000000;
}
#endif

/// Print information about the specified job.
static void builtin_jobs_print(const job_t *j, int mode, int header, io_streams_t &streams) {
Expand All @@ -57,17 +53,18 @@ static void builtin_jobs_print(const job_t *j, int mode, int header, io_streams_
if (header) {
// Print table header before first job.
streams.out.append(_(L"Job\tGroup\t"));
#ifdef HAVE__PROC_SELF_STAT
streams.out.append(_(L"CPU\t"));
#endif
if (have_proc_stat) {
streams.out.append(_(L"CPU\t"));
}
streams.out.append(_(L"State\tCommand\n"));
}

streams.out.append_format(L"%d\t%d\t", j->job_id, j->pgid);

#ifdef HAVE__PROC_SELF_STAT
streams.out.append_format(L"%d%%\t", cpu_use(j));
#endif
if (have_proc_stat) {
streams.out.append_format(L"%d%%\t", cpu_use(j));
}

streams.out.append(j->is_stopped() ? _(L"stopped") : _(L"running"));
streams.out.append(L"\t");
streams.out.append(j->command_wcstr());
Expand Down
4 changes: 4 additions & 0 deletions src/env.cpp
Expand Up @@ -560,6 +560,10 @@ void misc_init() {
fflush(stdout);
setvbuf(stdout, NULL, _IONBF, 0);
}
// Check for /proc/self/stat to see if we are running with Linux-style procfs
if (access("/proc/self/stat", R_OK) == 0) {
have_proc_stat = true;
}
}

/// Ensure the content of the magic path env vars is reasonable. Specifically, that empty path
Expand Down
6 changes: 2 additions & 4 deletions src/proc.cpp
Expand Up @@ -72,6 +72,7 @@ bool is_login = false;
int is_event = 0;
job_control_t job_control_mode = job_control_t::interactive;
int no_exec = 0;
bool have_proc_stat = false;

static int is_interactive = -1;

Expand Down Expand Up @@ -617,13 +618,12 @@ bool job_reap(bool allow_interactive) {
return found;
}

#ifdef HAVE__PROC_SELF_STAT

/// Maximum length of a /proc/[PID]/stat filename.
#define FN_SIZE 256

/// 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;

wchar_t fn[FN_SIZE];
Expand Down Expand Up @@ -666,8 +666,6 @@ void proc_update_jiffies() {
}
}

#endif

// Return control of the terminal to a job's process group. restore_attrs is true if we are restoring
// a previously-stopped job, in which case we need to restore terminal attributes.
bool terminal_give_to_job(const job_t *j, bool restore_attrs) {
Expand Down
6 changes: 2 additions & 4 deletions src/proc.h
Expand Up @@ -228,12 +228,10 @@ class process_t {
bool stopped{false};
/// Reported status value.
proc_status_t status{};
#ifdef HAVE__PROC_SELF_STAT
/// Last time of cpu time check.
struct timeval last_time {};
/// Number of jiffies spent in process at last cpu time check.
unsigned long last_jiffies{0};
#endif
};

typedef std::unique_ptr<process_t> process_ptr_t;
Expand Down Expand Up @@ -492,15 +490,13 @@ bool job_reap(bool interactive);
/// Mark a process as failed to execute (and therefore completed).
void job_mark_process_as_failed(const std::shared_ptr<job_t> &job, const process_t *p);

#ifdef HAVE__PROC_SELF_STAT
/// Use the procfs filesystem to look up how many jiffies of cpu time was used by this process. This
/// function is only available on systems with the procfs file entry 'stat', i.e. Linux.
unsigned long proc_get_jiffies(process_t *p);

/// Update process time usage for all processes by calling the proc_get_jiffies function for every
/// process of every job.
void proc_update_jiffies();
#endif

/// Perform a set of simple sanity checks on the job list. This includes making sure that only one
/// job is in the foreground, that every process is in a valid state, etc.
Expand Down Expand Up @@ -555,4 +551,6 @@ void add_disowned_pgid(pid_t pgid);
/// function
enum { INVALID_PID = -2 };

extern bool have_proc_stat;

#endif
6 changes: 3 additions & 3 deletions src/reader.cpp
Expand Up @@ -1976,9 +1976,9 @@ void reader_run_command(parser_t &parser, const wcstring &cmd) {
// For compatibility with fish 2.0's $_, now replaced with `status current-command`
parser.vars().set_one(L"_", ENV_GLOBAL, program_name);

#ifdef HAVE__PROC_SELF_STAT
proc_update_jiffies();
#endif
if (have_proc_stat) {
proc_update_jiffies();
}
}

parser_test_error_bits_t reader_shell_test(const wcstring &b) {
Expand Down

0 comments on commit 636af89

Please sign in to comment.