summaryrefslogtreecommitdiffstats
path: root/generic
diff options
context:
space:
mode:
authorNathan Scott <nathans@redhat.com>2021-03-30 15:55:48 +1100
committerSohaib <sohaib.amhmd@gmail.com>2021-04-05 23:41:07 +0200
commit356488aa53e8c0bedeb7641685d931c8900098c5 (patch)
treec09f2df8264cc4075b72a55409ae261c021cda55 /generic
parent421bdeec603b4fb1a4edec0e802c437fbe47fca0 (diff)
Request the realtime and monotonic clock times once per sample
Refactor the sample time code to make one call to gettimeofday (aka the realtime clock in clock_gettime, when available) and one to the monotonic clock. Stores each in more appropriately named ProcessList fields for ready access when needed. Every platform gets the opportunity to provide their own clock code, and the existing Mac OS X specific code is moved below darwin instead of in Compat. A couple of leftover time(2) calls are converted to use these ProcessList fields as well, instead of yet again sampling the system clock. Related to https://github.com/htop-dev/htop/pull/574
Diffstat (limited to 'generic')
-rw-r--r--generic/gettime.c57
-rw-r--r--generic/gettime.h19
2 files changed, 76 insertions, 0 deletions
diff --git a/generic/gettime.c b/generic/gettime.c
new file mode 100644
index 00000000..ef7d2290
--- /dev/null
+++ b/generic/gettime.c
@@ -0,0 +1,57 @@
+/*
+htop - generic/gettime.c
+(C) 2021 htop dev team
+Released under the GNU GPLv2, see the COPYING file
+in the source distribution for its full text.
+*/
+#include "config.h" // IWYU pragma: keep
+
+#include <string.h>
+#include <time.h>
+
+#include "generic/gettime.h"
+
+
+void Generic_gettime_realtime(struct timeval* tvp, uint64_t* msec) {
+
+#if defined(HAVE_CLOCK_GETTIME)
+
+ struct timespec ts;
+ if (clock_gettime(CLOCK_REALTIME, &ts) == 0) {
+ tvp->tv_sec = ts.tv_sec;
+ tvp->tv_usec = ts.tv_nsec / 1000;
+ *msec = ((uint64_t)ts.tv_sec * 1000) + ((uint64_t)ts.tv_nsec / 1000000);
+ } else {
+ memset(tvp, 0, sizeof(struct timeval));
+ *msec = 0;
+ }
+
+#else /* lower resolution gettimeofday(2) is always available */
+
+ struct timeval tv;
+ if (gettimeofday(&tv, NULL) == 0) {
+ *tsp = tv; /* struct copy */
+ *msec = ((uint64_t)tv.tv_sec * 1000) + ((uint64_t)tv.tv_usec / 1000);
+ } else {
+ memset(tvp, 0, sizeof(struct timeval));
+ *msec = 0;
+ }
+
+#endif
+}
+
+void Generic_gettime_monotonic(uint64_t* msec) {
+#if defined(HAVE_CLOCK_GETTIME)
+
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
+ *msec = ((uint64_t)ts.tv_sec * 1000) + ((uint64_t)ts.tv_nsec / 1000000);
+ else
+ *msec = 0;
+
+#else
+
+# error "No monotonic clock available"
+
+#endif
+}
diff --git a/generic/gettime.h b/generic/gettime.h
new file mode 100644
index 00000000..fab33da1
--- /dev/null
+++ b/generic/gettime.h
@@ -0,0 +1,19 @@
+#ifndef HEADER_gettime
+#define HEADER_gettime
+/*
+htop - generic/gettime.h
+(C) 2021 htop dev team
+Released under the GNU GPLv2, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include <stdint.h>
+#include <sys/time.h>
+#include <sys/types.h>
+
+
+void Generic_gettime_realtime(struct timeval* ts, uint64_t* msec);
+
+void Generic_gettime_monotonic(uint64_t* msec);
+
+#endif

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