summaryrefslogtreecommitdiffstats
path: root/linux
diff options
context:
space:
mode:
authorNathan Scott <nathans@redhat.com>2020-12-14 11:19:54 +1100
committerNathan Scott <nathans@redhat.com>2020-12-14 11:56:13 +1100
commita3db2da4a7eac5287d347529dd8fe7780e57d6c9 (patch)
treea277b6ce342b4c915d63baf4bd13262cd2a753ac /linux
parentcf982f2928c874f78b38ce1a715f355923df3f52 (diff)
Cleanup initialization of jiffies on the Linux platform
Small cleanups - add error handling, remove a local static variable and refactor LinuxProcess_adjustTime (also rename it, as its in LinuxProcessList.c not LinuxProcess.c) - and while there, move the related 'btime' global variable into LinuxProcessList.c so it can be made static. Resolves https://github.com/htop-dev/htop/issues/384
Diffstat (limited to 'linux')
-rw-r--r--linux/LinuxProcess.c1
-rw-r--r--linux/LinuxProcess.h2
-rw-r--r--linux/LinuxProcessList.c30
3 files changed, 15 insertions, 18 deletions
diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c
index 44c4b2d9..d1cad976 100644
--- a/linux/LinuxProcess.c
+++ b/linux/LinuxProcess.c
@@ -24,7 +24,6 @@ in the source distribution for its full text.
/* semi-global */
-long long btime;
int pageSize;
int pageSizeKB;
diff --git a/linux/LinuxProcess.h b/linux/LinuxProcess.h
index 3c1e7e75..c9e095d8 100644
--- a/linux/LinuxProcess.h
+++ b/linux/LinuxProcess.h
@@ -191,8 +191,6 @@ static inline bool Process_isUserlandThread(const Process* this) {
return this->pid != this->tgid;
}
-extern long long btime;
-
extern int pageSize;
extern int pageSizeKB;
diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c
index 5298cd3e..969c4e73 100644
--- a/linux/LinuxProcessList.c
+++ b/linux/LinuxProcessList.c
@@ -63,6 +63,10 @@ in the source distribution for its full text.
# define O_PATH 010000000
#endif
+static long long btime;
+
+static long jiffy;
+
static FILE* fopenat(openat_arg_t openatArg, const char* pathname, const char* mode) {
assert(String_eq(mode, "r")); /* only currently supported mode */
@@ -214,6 +218,11 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, ui
CRT_fatalError("Cannot get pagesize by sysconf(_SC_PAGESIZE)");
pageSizeKB = pageSize / ONE_K;
+ // Initialize clock ticks
+ jiffy = sysconf(_SC_CLK_TCK);
+ if (jiffy == -1)
+ CRT_fatalError("Cannot get clock ticks by sysconf(_SC_CLK_TCK)");
+
// Test /proc/PID/smaps_rollup availability (faster to parse, Linux 4.14+)
this->haveSmapsRollup = (access(PROCDIR "/self/smaps_rollup", R_OK) == 0);
@@ -273,16 +282,7 @@ void ProcessList_delete(ProcessList* pl) {
free(this);
}
-static inline unsigned long long LinuxProcess_adjustTime(unsigned long long t) {
- static long jiffy = -1;
- if (jiffy == -1) {
- errno = 0;
- jiffy = sysconf(_SC_CLK_TCK);
- if (errno || -1 == jiffy) {
- jiffy = -1;
- return t; // Assume 100Hz clock
- }
- }
+static inline unsigned long long LinuxProcessList_adjustTime(unsigned long long t) {
return t * 100 / jiffy;
}
@@ -335,13 +335,13 @@ static bool LinuxProcessList_readStatFile(Process* process, openat_arg_t procFd,
location += 1;
lp->cmajflt = strtoull(location, &location, 10);
location += 1;
- lp->utime = LinuxProcess_adjustTime(strtoull(location, &location, 10));
+ lp->utime = LinuxProcessList_adjustTime(strtoull(location, &location, 10));
location += 1;
- lp->stime = LinuxProcess_adjustTime(strtoull(location, &location, 10));
+ lp->stime = LinuxProcessList_adjustTime(strtoull(location, &location, 10));
location += 1;
- lp->cutime = LinuxProcess_adjustTime(strtoull(location, &location, 10));
+ lp->cutime = LinuxProcessList_adjustTime(strtoull(location, &location, 10));
location += 1;
- lp->cstime = LinuxProcess_adjustTime(strtoull(location, &location, 10));
+ lp->cstime = LinuxProcessList_adjustTime(strtoull(location, &location, 10));
location += 1;
process->priority = strtol(location, &location, 10);
location += 1;
@@ -351,7 +351,7 @@ static bool LinuxProcessList_readStatFile(Process* process, openat_arg_t procFd,
location += 1;
location = strchr(location, ' ') + 1;
if (process->starttime_ctime == 0) {
- process->starttime_ctime = btime + LinuxProcess_adjustTime(strtoll(location, &location, 10)) / 100;
+ process->starttime_ctime = btime + LinuxProcessList_adjustTime(strtoll(location, &location, 10)) / 100;
} else {
location = strchr(location, ' ') + 1;
}

© 2014-2024 Faster IT GmbH | imprint | privacy policy