[systemd-devel] bugid 35676

Richard Hitt rbh00 at netcom.com
Fri Mar 25 15:25:08 PDT 2011


Hi, as recommended by freenode:#systemd I'm emailing this info that 
amounts to the patch I just submitted.

Here are three sections:  1. the dialog on the irc channel, 2. the 
patch, 3. my standalone test program

-------- start dialog
<richtroye>  Hi, I have submitted a bug and patch for systemd at src/util.c:format_timespan() to change to this sort of message:
<richtroye>  systemd[1]: Startup finished in 1.541491s (kernel) + 1.507862s (initrd) + 3min 8.000672s (userspace) = 3min 11.050025s.
<richtroye>  https://bugs.freedesktop.org/show_bug.cgi?id=35676
<richtroye>  Here is the standalone code I used to test my changes:  http://fpaste.org/tMaD/
<richtroye>  Here are some sample outputs from that test program:  http://fpaste.org/Q0IV/
-------- end dialog


-------- start patch
--- util.c	2011-03-25 14:25:48.628685721 -0700
+++ FIXEDutil.c	2011-03-25 13:59:31.473809423 -0700
@@ -1948,13 +1948,11 @@
                  { "d", USEC_PER_DAY },
                  { "h", USEC_PER_HOUR },
                  { "min", USEC_PER_MINUTE },
-                { "s", USEC_PER_SEC },
-                { "ms", USEC_PER_MSEC },
-                { "us", 1 },
          };

          unsigned i;
          char *p = buf;
+        int k;

          assert(buf);
          assert(l>  0);
@@ -1971,7 +1969,6 @@
          /* The result of this function can be parsed with parse_usec */

          for (i = 0; i<  ELEMENTSOF(table); i++) {
-                int k;
                  size_t n;

                  if (t<  table[i].usec)
@@ -1989,6 +1986,10 @@
                  t %= table[i].usec;
          }

+        /* We have left a value between 0 and 59,999,999 (that is, 0s and 59.999999s) */
+        k = snprintf(p, l, "%s%llu.%06llus", p>  buf ? " " : "", (unsigned long long) (t / USEC_PER_SEC), (unsigned long long) (t % USEC_PER_SEC));
+        p += MIN((size_t) k, l);
+
          *p = 0;

          return buf;
-------- end patch

-------- start tester
[rbh00 at dell tmp]$ cat maketime.c
#include<stdio.h>
#include<string.h>

typedef unsigned long long uint64_t;
typedef uint64_t usec_t;
typedef unsigned int size_t;

#define USEC_PER_SEC  1000000ULL
#define USEC_PER_MSEC 1000ULL
#define NSEC_PER_USEC 1000ULL
#define USEC_PER_MINUTE (60ULL*USEC_PER_SEC)
#define USEC_PER_HOUR (60ULL*USEC_PER_MINUTE)
#define USEC_PER_DAY (24ULL*USEC_PER_HOUR)
#define USEC_PER_WEEK (7ULL*USEC_PER_DAY)
#define USEC_PER_MONTH (2629800ULL*USEC_PER_SEC)
#define USEC_PER_YEAR (31557600ULL*USEC_PER_SEC)
#define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
#define MIN(a,b) (a<b?a:b)

char *format_timespan(char *buf, size_t l, usec_t t) {
         static const struct {
                 const char *suffix;
                 usec_t usec;
         } table[] = {
                 { "w", USEC_PER_WEEK },
                 { "d", USEC_PER_DAY },
                 { "h", USEC_PER_HOUR },
                 { "min", USEC_PER_MINUTE },
#if 0
                 { "s", USEC_PER_SEC },
                 { "ms", USEC_PER_MSEC },
                 { "us", 1 },
#endif
         };

         unsigned i;
         char *p = buf;
         int k;

#if 0
         assert(buf);
         assert(l>  0);
#endif

         if (t == (usec_t) -1)
                 return NULL;

         if (t == 0) {
                 snprintf(p, l, "0");
                 p[l-1] = 0;
                 return p;
         }

         /* The result of this function can be parsed with parse_usec */

         for (i = 0; i<  ELEMENTSOF(table); i++) {
                 size_t n;

                 if (t<  table[i].usec)
                         continue;

                 if (l<= 1)
                         break;

                 k = snprintf(p, l, "%s%llu%s", p>  buf ? " " : "", (unsigned long long) (t / table[i].usec), table[i].suffix);
                 n = MIN((size_t) k, l);

                 l -= n;
                 p += n;

                 t %= table[i].usec;
         }

#if 0
#else
         /* We have left a value between 0 and 59,999,999 (that is, 0s and 59.999999s) */
         k = snprintf(p, l, "%s%llu.%06llus", p>  buf ? " " : "", (unsigned long long) (t / USEC_PER_SEC), (unsigned long long) (t % USEC_PER_SEC));
         p += MIN((size_t) k, l);
#endif

         *p = 0;

         return buf;
}

int
main(int argc, char *argv[])
{
	char buf[256];
	usec_t ut;

	ut = strtoull(argv[1]);
	format_timespan(buf, sizeof buf, ut);
	printf("Timespan for %llu is %s\n", ut, buf);
}
-------- end tester


Thanks.  Hope you like it.

Richard


More information about the systemd-devel mailing list