* [PATCH 4/5] hwlatdetect: Add bounds checking and improve code readability
@ 2026-02-19 21:21 John Kacur
2026-02-19 21:21 ` [PATCH 1/5] hwlatdetect: Make count field backward compatible John Kacur
2026-02-19 21:21 ` [PATCH 5/5] rt-tests: cyclictest: Remove duplicate option in getopt string John Kacur
0 siblings, 2 replies; 3+ messages in thread
From: John Kacur @ 2026-02-19 21:21 UTC (permalink / raw)
To: linux-rt-users; +Cc: Clark Williams, John Kacur, Claude Sonnet 4.5
Add defensive checks to the Sample.__init__ method to prevent
IndexError on malformed trace input:
- Verify fields array has at least 8 elements
- Check cpu_field is non-empty before accessing first character
- Add comments explaining the two different trace formats
- Improve code readability by using a named variable for cpu_field
Assisted-by: Claude Sonnet 4.5 <noreply@anthropic.com>
Signed-off-by: John Kacur <jkacur@redhat.com>
---
src/hwlatdetect/hwlatdetect.py | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/src/hwlatdetect/hwlatdetect.py b/src/hwlatdetect/hwlatdetect.py
index 865f62196c26..38671724f3e3 100755
--- a/src/hwlatdetect/hwlatdetect.py
+++ b/src/hwlatdetect/hwlatdetect.py
@@ -257,8 +257,22 @@ class Tracer(Detector):
def __init__(self, line):
fields = line.split()
+ if len(fields) < 8:
+ raise ValueError(f"Unexpected trace format: {line}")
+
kv = key_values(fields)
- self.cpu = int(fields[1][1:-1]) if fields[1][0] == '[' else int(fields[1][:-5])
+
+ # Parse CPU number from either [NNN] or NNd.... format
+ cpu_field = fields[1]
+ if not cpu_field:
+ raise ValueError(f"Empty CPU field in: {line}")
+
+ if cpu_field[0] == '[':
+ # nolatency-format: [007]
+ self.cpu = int(cpu_field[1:-1])
+ else:
+ # latency-format: 13d....
+ self.cpu = int(cpu_field[:-5])
i, o = fields[6].split('/')
ts = fields[7][3:]
self.timestamp = str(ts)
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 1/5] hwlatdetect: Make count field backward compatible
2026-02-19 21:21 [PATCH 4/5] hwlatdetect: Add bounds checking and improve code readability John Kacur
@ 2026-02-19 21:21 ` John Kacur
2026-02-19 21:21 ` [PATCH 5/5] rt-tests: cyclictest: Remove duplicate option in getopt string John Kacur
1 sibling, 0 replies; 3+ messages in thread
From: John Kacur @ 2026-02-19 21:21 UTC (permalink / raw)
To: linux-rt-users; +Cc: Clark Williams, John Kacur, Claude Sonnet 4.5
The count field was added to hwlat trace output in kernel v5.10
(commit b396bfdebffc). Make this field optional to maintain backward
compatibility with older kernels:
- Set count to None when the field is not present in trace output
- Only include count in __str__ output when it exists
- Fall back to len(samples) in get("count") for older kernels
This allows hwlatdetect to work correctly on both kernels with and
without the count field.
Assisted-by: Claude Sonnet 4.5 <noreply@anthropic.com>
Signed-off-by: John Kacur <jkacur@redhat.com>
---
src/hwlatdetect/hwlatdetect.py | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/src/hwlatdetect/hwlatdetect.py b/src/hwlatdetect/hwlatdetect.py
index af9852cc95e8..c128f60bc8db 100755
--- a/src/hwlatdetect/hwlatdetect.py
+++ b/src/hwlatdetect/hwlatdetect.py
@@ -264,10 +264,12 @@ class Tracer(Detector):
self.timestamp = str(ts)
self.inner = int(i)
self.outer = int(o)
- self.count = int(kv["count"])
+ self.count = int(kv["count"]) if "count" in kv else None
def __str__(self):
- return f"ts: {self.timestamp}, inner:{self.inner}, outer:{self.outer}, cpu:{self.cpu}, count:{self.count}"
+ if self.count is not None:
+ return f"ts: {self.timestamp}, inner:{self.inner}, outer:{self.outer}, cpu:{self.cpu}, count:{self.count}"
+ return f"ts: {self.timestamp}, inner:{self.inner}, outer:{self.outer}, cpu:{self.cpu}"
def display(self):
""" convert object to string and print """
@@ -299,7 +301,10 @@ class Tracer(Detector):
def get(self, field):
if field == "count":
- return sum(s.count for s in self.samples)
+ # Use new count field if available, otherwise fall back to sample count
+ if self.samples and self.samples[0].count is not None:
+ return sum(s.count for s in self.samples)
+ return len(self.samples)
if field == "max":
max = 0
for values in self.samples:
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 5/5] rt-tests: cyclictest: Remove duplicate option in getopt string
2026-02-19 21:21 [PATCH 4/5] hwlatdetect: Add bounds checking and improve code readability John Kacur
2026-02-19 21:21 ` [PATCH 1/5] hwlatdetect: Make count field backward compatible John Kacur
@ 2026-02-19 21:21 ` John Kacur
1 sibling, 0 replies; 3+ messages in thread
From: John Kacur @ 2026-02-19 21:21 UTC (permalink / raw)
To: linux-rt-users; +Cc: Clark Williams, John Kacur, Costa Shulyupin
Remove the duplicate -D in the getopt string
Reported-by: Costa Shulyupin <costa.shul@redhat.com>
Signed-off-by: John Kacur <jkacur@redhat.com>
---
src/cyclictest/cyclictest.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/cyclictest/cyclictest.c b/src/cyclictest/cyclictest.c
index b4bce8915b43..191945e1e0db 100644
--- a/src/cyclictest/cyclictest.c
+++ b/src/cyclictest/cyclictest.c
@@ -1147,7 +1147,7 @@ static void process_options(int argc, char *argv[], int max_cpus)
{"deepest-idle-state", required_argument, NULL, OPT_DEEPEST_IDLE_STATE },
{NULL, 0, NULL, 0 },
};
- int c = getopt_long(argc, argv, "a::A::b:c:d:D:F:h:H:i:l:MNo:p:mqrRsSt::uvD:x",
+ int c = getopt_long(argc, argv, "a::A::b:c:d:D:F:h:H:i:l:MNo:p:mqrRsSt::uvx",
long_options, &option_index);
if (c == -1)
break;
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-02-19 21:21 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-19 21:21 [PATCH 4/5] hwlatdetect: Add bounds checking and improve code readability John Kacur
2026-02-19 21:21 ` [PATCH 1/5] hwlatdetect: Make count field backward compatible John Kacur
2026-02-19 21:21 ` [PATCH 5/5] rt-tests: cyclictest: Remove duplicate option in getopt string John Kacur
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox