]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
analyze: Add "timespan" command to dump time span in usec
authorChris Down <chris@chrisdown.name>
Mon, 15 Oct 2018 11:55:35 +0000 (12:55 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 23 Oct 2018 12:26:51 +0000 (14:26 +0200)
This is useful for a couple of cases, I'm mostly interested in case #1:

1. Verifying "reasonable" values in a trivially scriptable way
2. Debugging unexpected time span parsing directly

Test Plan:

```
% build/systemd-analyze timespan 20
Original: 20
      μs: 20
   Human: 20us
% build/systemd-analyze timespan 20ms
Original: 20ms
      μs: 20000
   Human: 20ms
% build/systemd-analyze timespan 20z
Failed to parse time span '20z': Invalid argument
```

man/systemd-analyze.xml
man/systemd.time.xml
src/analyze/analyze.c
src/basic/locale-util.c
src/basic/locale-util.h
src/test/test-locale-util.c

index 7aa10fc68e29e934b0d6b68a97b1e7d046329e75..15e95b0c8f5be9788c25dc5dc3292533614bd3f7 100644 (file)
       <arg choice="plain">service-watchdogs</arg>
       <arg choice="opt"><replaceable>BOOL</replaceable></arg>
     </cmdsynopsis>
+    <cmdsynopsis>
+      <command>systemd-analyze</command>
+      <arg choice="opt" rep="repeat">OPTIONS</arg>
+      <arg choice="plain">timespan</arg>
+      <arg choice="plain" rep="repeat"><replaceable>SPAN</replaceable></arg>
+    </cmdsynopsis>
   </refsynopsisdiv>
 
   <refsect1>
@@ -253,6 +259,10 @@ NAutoVTs=8
     <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
     The hardware watchdog is not affected by this setting.</para>
 
+    <para><command>systemd-analyze timespan</command> parses a time span and outputs the equivalent value in microseconds, and as a reformatted timespan.
+    The time span should adhere to the same syntax documented in <citerefentry><refentrytitle>systemd.time</refentrytitle><manvolnum>7</manvolnum></citerefentry>.
+    Values without associated magnitudes are parsed as seconds.</para>
+
     <para>If no command is passed, <command>systemd-analyze
     time</command> is implied.</para>
 
index 15e1680b8dbaf3e111e2a728549a401d7c2b75be..24df5ab9429ed7a6362685e9012515b8eccc5693 100644 (file)
 1y 12month
 55s500ms
 300ms20s 5day</programlisting>
+
+    <para>One can use the <command>timespan</command> command of
+    <citerefentry><refentrytitle>systemd-analyze</refentrytitle><manvolnum>1</manvolnum></citerefentry>
+    to normalise a textual time span for testing and validation purposes.</para>
   </refsect1>
 
   <refsect1>
index 144f18e9c72f079c47178565f34a33dc5468dad6..d381740dfb44d718dd9ec3d92184c4a140ff3dab 100644 (file)
@@ -4,6 +4,7 @@
 ***/
 
 #include <getopt.h>
+#include <inttypes.h>
 #include <locale.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -33,6 +34,7 @@
 #include "special.h"
 #include "strv.h"
 #include "strxcpyx.h"
+#include "time-util.h"
 #include "terminal-util.h"
 #include "unit-name.h"
 #include "util.h"
@@ -1551,6 +1553,29 @@ static int dump_syscall_filters(int argc, char *argv[], void *userdata) {
 }
 #endif
 
+static int dump_timespan(int argc, char *argv[], void *userdata) {
+        char **input_timespan;
+
+        STRV_FOREACH(input_timespan, strv_skip(argv, 1)) {
+                int r;
+                usec_t usec_magnitude = 1, output_usecs;
+                char ft_buf[FORMAT_TIMESPAN_MAX];
+
+                r = parse_time(*input_timespan, &output_usecs, USEC_PER_SEC);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to parse time span '%s': %m", *input_timespan);
+
+                printf("Original: %s\n", *input_timespan);
+                printf("      %ss: %" PRIu64 "\n", special_glyph(MU), output_usecs);
+                printf("   Human: %s\n", format_timespan(ft_buf, sizeof(ft_buf), output_usecs, usec_magnitude));
+
+                if (input_timespan[1])
+                        putchar('\n');
+        }
+
+        return EXIT_SUCCESS;
+}
+
 static int test_calendar(int argc, char *argv[], void *userdata) {
         int ret = 0, r;
         char **p;
@@ -1710,6 +1735,7 @@ static int help(int argc, char *argv[], void *userdata) {
                "  verify FILE...           Check unit files for correctness\n"
                "  calendar SPEC...         Validate repetitive calendar time events\n"
                "  service-watchdogs [BOOL] Get/set service watchdog state\n"
+               "  timespan SPAN...         Validate a time span\n"
                "\nSee the %s for details.\n"
                , program_invocation_short_name
                , link
@@ -1900,6 +1926,7 @@ int main(int argc, char *argv[]) {
                 { "verify",            2,        VERB_ANY, 0,            do_verify              },
                 { "calendar",          2,        VERB_ANY, 0,            test_calendar          },
                 { "service-watchdogs", VERB_ANY, 2,        0,            service_watchdogs      },
+                { "timespan",          2,        VERB_ANY, 0,            dump_timespan          },
                 {}
         };
 
index 3ad352f22f3fde9ce62009e8250648720f2b56b8..8b89bd002448841333a51b1bfc520e0a7e025d7c 100644 (file)
@@ -367,7 +367,8 @@ const char *special_glyph(SpecialGlyph code) {
                         [BLACK_CIRCLE]       = "*",
                         [ARROW]              = "->",
                         [MDASH]              = "-",
-                        [ELLIPSIS]           = "..."
+                        [ELLIPSIS]           = "...",
+                        [MU]                 = "u",
                 },
 
                 /* UTF-8 */
@@ -381,6 +382,7 @@ const char *special_glyph(SpecialGlyph code) {
                         [ARROW]              = "\342\206\222",             /* → */
                         [MDASH]              = "\342\200\223",             /* – */
                         [ELLIPSIS]           = "\342\200\246",             /* … */
+                        [MU]                 = "\316\274",                 /* μ */
                 },
         };
 
index 775fe8bc728af79e9497547e618feaecf306d4c7..7762254940da8749b335d696454c7c3b3c83825b 100644 (file)
@@ -48,6 +48,7 @@ typedef enum {
         ARROW,
         MDASH,
         ELLIPSIS,
+        MU,
         _SPECIAL_GLYPH_MAX
 } SpecialGlyph;
 
index 8ffae8ca03d08bd7305b75b2a7bfe7a09a4abae2..3634534782dc2986ea2b56e62e68f59d69e807c2 100644 (file)
@@ -65,7 +65,7 @@ static void test_keymaps(void) {
 
 #define dump_glyph(x) log_info(STRINGIFY(x) ": %s", special_glyph(x))
 static void dump_special_glyphs(void) {
-        assert_cc(ELLIPSIS + 1 == _SPECIAL_GLYPH_MAX);
+        assert_cc(MU + 1 == _SPECIAL_GLYPH_MAX);
 
         log_info("/* %s */", __func__);