Skip to contentSkip to conversation

hwlatdetect: Add bounds checking and improve code readability

PATCH New John Kacur 1 participant 3 messages Feb 19

lore
Share & tools
lore

John Kacur

author
#0 lore raw

PATCH 4/5 hwlatdetect: Add bounds checking and improve code readability

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 <[email protected]>
---
 src/hwlatdetect/hwlatdetect.py | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
src/hwlatdetect/hwlatdetect.py +15 -1
index 865f62196c26..38671724f3e3 100755
@@ -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)
Signature · 1 line
2.53.0
author
#1 lore raw

PATCH 1/5 hwlatdetect: Make count field backward compatible

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 <[email protected]>
---
 src/hwlatdetect/hwlatdetect.py | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
src/hwlatdetect/hwlatdetect.py +8 -3
index af9852cc95e8..c128f60bc8db 100755
@@ -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:
Signature · 1 line
2.53.0
author
#2 lore raw

PATCH 5/5 rt-tests: cyclictest: Remove duplicate option in getopt string

In reply to John Kacur

Remove the duplicate -D in the getopt string

---
 src/cyclictest/cyclictest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/cyclictest/cyclictest.c +1 -1
index b4bce8915b43..191945e1e0db 100644
@@ -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;
Signature · 1 line
2.53.0