util: optimize dumping instance timezone handling 02/191502/2
authorMichal Bloch <m.bloch@samsung.com>
Wed, 17 Oct 2018 12:08:39 +0000 (14:08 +0200)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Mon, 22 Oct 2018 03:01:40 +0000 (03:01 +0000)
  We can assume that such instances will not see a timezone
  change happen during their lifetime, and even if they do,
  it is probably best if they ignore it (a dump would ideally
  behave as if it was atomic). This means we can optimize the
  behaviour where printing the timestamp requires us to check
  whether the timezone changed, which involves calling stat()
  on /etc/localtime, which is not cheap on its own and made
  even worse when multiple symlinks are involves.

  glibc has a feature where setting the TZ environmental var
  will cache the timezone, achieving precisely what we want.

Change-Id: I2f5e26a4be81b8f846036282fc1a0bfe02b70b48
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/logutil/logutil.c

index 51e3bcb..834da44 100755 (executable)
@@ -465,6 +465,23 @@ int main(int argc, char **argv)
        if (validate_buffers(&enabled_buffers) < 0)
                return 1;
 
+       /** Optimisation for short-lived (i.e. dumping) instances.
+
+          We can assume that such instances will not see a timezone
+          change happen during their lifetime, and even if they do,
+          it is probably best if they ignore it (a dump would ideally
+          behave as if it was atomic). This means we can optimize the
+          behaviour where printing the timestamp requires us to check
+          whether the timezone changed, which involves calling stat()
+          on /etc/localtime, which is not cheap on its own and made
+          even worse when it has to go through multiple symlinks.
+
+          glibc has a feature where setting the TZ environmental var
+          will cache the timezone, achieving precisely what we want.
+        */
+       if (logs.dump != DUMP_CONTINUOUS && !getenv("TZ"))
+               putenv("TZ=:/etc/localtime");
+
        const char *const backend = log_config_get(&conf, "backend");
        if (!backend) {
                ERR("Error: backend not defined\n");