From 6de58f5fd0c9d47a65e24d3617e465b9fa8d8872 Mon Sep 17 00:00:00 2001 From: Nathan Skrzypczak Date: Mon, 24 Jan 2022 17:10:41 +0100 Subject: cnat: maglev fixes & improvements This fixes the maglev logic which previously included a wrong simplication. It moves the maglev logic to its own file, and adds a test function in the debug cli. Type: improvement Change-Id: I2790ae2a26fc1c5739ff02f41d436bfcafd5b380 Signed-off-by: Nathan Skrzypczak --- src/vppinfra/format.h | 1 + src/vppinfra/std-formats.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) (limited to 'src/vppinfra') diff --git a/src/vppinfra/format.h b/src/vppinfra/format.h index d143ecdb6e9..ee47a2099c2 100644 --- a/src/vppinfra/format.h +++ b/src/vppinfra/format.h @@ -98,6 +98,7 @@ _(format_hex_bytes_no_wrap); _(format_white_space); _(format_f64); _(format_time_interval); +_ (format_duration); #ifdef CLIB_UNIX /* Unix specific formats. */ diff --git a/src/vppinfra/std-formats.c b/src/vppinfra/std-formats.c index 421cb3dda99..99ea0c1a713 100644 --- a/src/vppinfra/std-formats.c +++ b/src/vppinfra/std-formats.c @@ -134,6 +134,52 @@ format_white_space (u8 * s, va_list * va) return s; } +u8 * +format_duration (u8 *s, va_list *args) +{ + f64 t = va_arg (*args, f64); + s = format (s, ""); + + const f64 seconds_per_minute = 60; + const f64 seconds_per_hour = 60 * seconds_per_minute; + const f64 seconds_per_day = 24 * seconds_per_hour; + uword days, hours, minutes, secs, msecs, usecs; + + days = t / seconds_per_day; + t -= days * seconds_per_day; + + hours = t / seconds_per_hour; + t -= hours * seconds_per_hour; + + minutes = t / seconds_per_minute; + t -= minutes * seconds_per_minute; + + secs = t; + t -= secs; + + msecs = 1e3 * t; + + usecs = 1e6 * t; + usecs = usecs % 1000; + + if (t == 0.) + s = format (s, "0"); + if (days) + s = format (s, "%ddays ", days); + if (hours) + s = format (s, "%dh ", hours); + if (minutes) + s = format (s, "%dmin ", minutes); + if (secs) + s = format (s, "%ds ", secs); + if (msecs) + s = format (s, "%dms ", msecs); + if (usecs) + s = format (s, "%dus", usecs); + + return (s); +} + u8 * format_time_interval (u8 * s, va_list * args) { -- cgit 1.2.3-korg