analyze: Add "timespan" command to dump time span in usec

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
```
This commit is contained in:
Chris Down 2018-10-15 12:55:35 +01:00 committed by Lennart Poettering
parent f402ce827d
commit 3f1c1287a9
6 changed files with 46 additions and 2 deletions

View File

@ -106,6 +106,12 @@
<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>

View File

@ -74,6 +74,10 @@
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>

View 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 },
{}
};

View 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", /* μ */
},
};

View File

@ -48,6 +48,7 @@ typedef enum {
ARROW,
MDASH,
ELLIPSIS,
MU,
_SPECIAL_GLYPH_MAX
} SpecialGlyph;

View 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__);