[systemd-devel] [PATCH] RD_TIMESTAMP: support the value being set from /proc/uptime

Tom Gundersen teg at jklm.no
Thu Aug 25 13:59:48 PDT 2011


This is useful to be able to use systemd-analyze with initrd's that don't
have systemd support. In particular, Arch's initrd exports RD_TIMESTAMP
on this format. I also believe dracut falls back to this when systemd-timestamp
is not present.

The downsides of using /proc/uptime rather than systemd-timestamp are:

* It is significantly less accurate compared to the native
  tool (+/- 5ms) [but more than accurate enough for most uses]
* Only the monotonic time is set, not the realtime (which is set
  to zero). [this does not make a difference for systemd-analyze]

To make this work we change the semantics of dual_timestamp_is_set
to return true also when monotonic is set, but realtime is not.
---

 src/main.c |   11 +++++++++--
 src/util.h |    2 +-
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/main.c b/src/main.c
index 40d03ca..96227f4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -949,14 +949,21 @@ fail:
 static struct dual_timestamp* parse_initrd_timestamp(struct dual_timestamp *t) {
         const char *e;
         unsigned long long a, b;
+	float c, d;
 
         assert(t);
 
         if (!(e = getenv("RD_TIMESTAMP")))
                 return NULL;
 
-        if (sscanf(e, "%llu %llu", &a, &b) != 2)
-                return NULL;
+        if (sscanf(e, "%llu %llu", &a, &b) != 2) {
+		/* allow timestamp to be set from /proc/uptime */
+		if(sscanf(e, "%f %f", &c, &d) != 2)
+                        return NULL;
+
+		a = 0;
+		b = (unsigned long long) (c * USEC_PER_SEC);
+	}
 
         t->realtime = (usec_t) a;
         t->monotonic = (usec_t) b;
diff --git a/src/util.h b/src/util.h
index bf5703c..0c82080 100644
--- a/src/util.h
+++ b/src/util.h
@@ -77,7 +77,7 @@ usec_t now(clockid_t clock);
 dual_timestamp* dual_timestamp_get(dual_timestamp *ts);
 dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u);
 
-#define dual_timestamp_is_set(ts) ((ts)->realtime > 0)
+#define dual_timestamp_is_set(ts) ((ts)->realtime > 0 || (ts)->monotonic > 0)
 
 usec_t timespec_load(const struct timespec *ts);
 struct timespec *timespec_store(struct timespec *ts, usec_t u);
-- 
1.7.6.1



More information about the systemd-devel mailing list