public inbox for linux-rt-users@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] rt-tests: hwlatdetect: Add --time-format argument
@ 2026-03-23 16:03 Costa Shulyupin
  2026-04-15 19:27 ` John Kacur
  0 siblings, 1 reply; 2+ messages in thread
From: Costa Shulyupin @ 2026-03-23 16:03 UTC (permalink / raw)
  To: linux-rt-users; +Cc: John Kacur, Costa Shulyupin, Luis Claudio R. Goncalves

Add --time-format argument to customize timestamp output format using
strftime directives. By default raw timestamps are output for
backward compatibility.

Add %n directive processing for nanoseconds (9 digits) since strftime
doesn't support it.

Signed-off-by: Costa Shulyupin <costa.shul@redhat.com>
---
 src/hwlatdetect/hwlatdetect.py | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/hwlatdetect/hwlatdetect.py b/src/hwlatdetect/hwlatdetect.py
index 18ca930487f0..34fe5af4844a 100755
--- a/src/hwlatdetect/hwlatdetect.py
+++ b/src/hwlatdetect/hwlatdetect.py
@@ -14,6 +14,7 @@
 import subprocess
 import sys
 import time
+from datetime import datetime
 
 version = "0.8"
 debugging = False
@@ -255,6 +256,7 @@     def detect(self):
         'private class for tracer sample data'
         __slots__ = 'cpu', 'timestamp', 'delta', 'inner', 'outer', 'count'
         prev = float('nan')
+        time_format = None
 
         def __init__(self, line):
             fields = line.split()
@@ -284,8 +286,17 @@         def __init__(self, line):
             self.outer = int(o)
             self.count = int(kv["count"]) if "count" in kv else None
 
+        def format_timestamp(self):
+            """Format timestamp with %n (nanoseconds) support.
+            Float precision is safe for microseconds until year 2242 (2^33 seconds)."""
+            if not self.time_format:
+                return self.timestamp
+            t, ns = self.timestamp.split('.')
+            return datetime.fromtimestamp(float(f"{t}.{ns[:6]}")).strftime(self.time_format.replace('%n', ns))
+
         def __str__(self):
-            s = f"ts: {self.timestamp}, delta:{self.delta:.6f}, inner:{self.inner}, outer:{self.outer}, cpu:{self.cpu}"
+            s = f"ts: {self.format_timestamp()}"
+            s += f", delta:{self.delta:.6f}, inner:{self.inner}, outer:{self.outer}, cpu:{self.cpu}"
             return s if self.count is None else s + f", count:{self.count}"
 
         def display(self):
@@ -472,6 +483,9 @@     def cleanup(self):
                         dest="watch",
                         help="print sample data to stdout as it arrives")
 
+    parser.add_argument("--time-format",
+                        help="strftime format for timestamps (e.g. %%H:%%M:%%S.%%n)")
+
     args = parser.parse_args()
 
     # need these before creating detector instance
@@ -525,6 +539,8 @@     def cleanup(self):
     if args.watch:
         watch = True
 
+    Tracer.Sample.time_format = args.time_format
+
     reportfile = args.report
 
     if args.cpulist:
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v1] rt-tests: hwlatdetect: Add --time-format argument
  2026-03-23 16:03 [PATCH v1] rt-tests: hwlatdetect: Add --time-format argument Costa Shulyupin
@ 2026-04-15 19:27 ` John Kacur
  0 siblings, 0 replies; 2+ messages in thread
From: John Kacur @ 2026-04-15 19:27 UTC (permalink / raw)
  To: Costa Shulyupin; +Cc: linux-rt-users, Luis Claudio R . Goncalves

On Mon, 23 Mar 2026 at 18:03:46 +0200, Costa Shulyupin wrote:
> Add --time-format argument to customize timestamp output format using
> strftime directives. By default raw timestamps are output for
> backward compatibility.

Hi Costa,

Thanks for this patch. The --time-format feature is useful, but I have a few
concerns about the implementation:

1. The format_timestamp() method assumes the timestamp always contains a
   decimal point:

       t, ns = self.timestamp.split('.')

   This will fail with ValueError if the timestamp doesn't have a '.' for
   any reason. Please add error handling.

2. There's no try/except around strftime(). If a user provides an invalid
   format string, the code will crash. This should be handled gracefully.

3. The use of a class variable for time_format is unconventional:

       Tracer.Sample.time_format = args.time_format

   While it works, this makes all Sample instances share state in a way that
   could be surprising. Consider passing the format through instance
   initialization or as a method parameter instead.

4. Minor: The comment about "Float precision is safe for microseconds until
   year 2242" is misleading. The issue is that datetime only supports
   microsecond precision, not nanoseconds, which is why the custom %n
   handling is needed.

Could you please send a v2 with these improvements?

Thanks,
John

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-04-15 19:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-23 16:03 [PATCH v1] rt-tests: hwlatdetect: Add --time-format argument Costa Shulyupin
2026-04-15 19:27 ` John Kacur

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox