Skip to contentSkip to conversation

rt-tests: hwlatdetect: Gracefully handle lack of /dev/cpu_dma_latency

PATCH In review Punit Agrawal 2 participants 3 messages Sep 29, 2021

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 rt-tests: hwlatdetect: Gracefully handle lack of /dev/cpu_dma_latency

From: Punit Agrawal <[email protected]>

On systems where cpu idle is disabled to reduce latencies, "/dev/cpu_dma_latency" does not exist and leads to the following exception report from python when using hwlatdetect.py -

FileNotFoundError: [Errno 2] No such file or directory: '/dev/cpu_dma_latency

Update hwlatdetect to check whether the file exists before attemping to write values to it. Also, make the related debug output conditional to c-states support being enabled in the kernel.

While we are touching this part of the code, also address a couple of minor issues in the code such as -
* correct typos
* reflow comment to fit 80-char column limit

---
v2 -> v3:
* Address issues in comment
* Initialise self.dma_latency_handle in init()
* Make debug() statements conditional to /dev/cpu_dma_latency

---
 src/hwlatdetect/hwlatdetect.py | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
src/hwlatdetect/hwlatdetect.py +10 -6
index 12228f45f852..56fd2af8641d 100755
@@ -200,6 +200,7 @@ class Detector(object):
         if os.path.exists('/usr/sbin/rdmsr'):
             self.have_msr = True
             self.initsmi = self.getsmicounts()
+        self.dma_latency_handle = None
 
     def getsmicounts(self):
         counts = []
@@ -210,20 +211,23 @@ class Detector(object):
         return counts
 
     # methods for preventing/enabling c-state transitions
-    # openinging /dev/cpu_dma_latency and writeing a 32-bit zero to that file will prevent
-    # c-state transitions while the file descriptor is open.
+    #
+    # opening /dev/cpu_dma_latency and writing a 32-bit zero to that file will
+    # prevent c-state transitions while the file descriptor is open.
+    #
     # use c_states_off() to disable c-state transitions
     # use c_states_on() to close the file descriptor and re-enable c-states
     #
     def c_states_off(self):
-        self.dma_latency_handle = os.open("/dev/cpu_dma_latency", os.O_WRONLY)
-        os.write(self.dma_latency_handle, b'\x00\x00\x00\x00')
-        debug("c-states disabled")
+        if os.path.exists("/dev/cpu_dma_latency"):
+            self.dma_latency_handle = os.open("/dev/cpu_dma_latency", os.O_WRONLY)
+            os.write(self.dma_latency_handle, b'\x00\x00\x00\x00')
+            debug("c-states disabled")
 
     def c_states_on(self):
         if self.dma_latency_handle:
             os.close(self.dma_latency_handle)
-        debug("c-states enabled")
+            debug("c-states enabled")
 
     def cleanup(self):
         raise RuntimeError("must override base method 'cleanup'!")
Signature · 1 line
2.32.0
#1 lore raw
Quoted 74 lines · Punit Agrawal

From: Punit Agrawal <[email protected]>

On systems where cpu idle is disabled to reduce latencies, "/dev/cpu_dma_latency" does not exist and leads to the following exception report from python when using hwlatdetect.py -

FileNotFoundError: [Errno 2] No such file or directory: '/dev/cpu_dma_latency

Update hwlatdetect to check whether the file exists before attemping to write values to it. Also, make the related debug output conditional to c-states support being enabled in the kernel.

While we are touching this part of the code, also address a couple of minor issues in the code such as -
* correct typos
* reflow comment to fit 80-char column limit

---
v2 -> v3:
* Address issues in comment
* Initialise self.dma_latency_handle in init()
* Make debug() statements conditional to /dev/cpu_dma_latency

---
 src/hwlatdetect/hwlatdetect.py | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
src/hwlatdetect/hwlatdetect.py +10 -6
index 12228f45f852..56fd2af8641d 100755
@@ -200,6 +200,7 @@ class Detector(object):
         if os.path.exists('/usr/sbin/rdmsr'):
             self.have_msr = True
             self.initsmi = self.getsmicounts()
+        self.dma_latency_handle = None
 
     def getsmicounts(self):
         counts = []
@@ -210,20 +211,23 @@ class Detector(object):
         return counts
 
     # methods for preventing/enabling c-state transitions
-    # openinging /dev/cpu_dma_latency and writeing a 32-bit zero to that file will prevent
-    # c-state transitions while the file descriptor is open.
+    #
+    # opening /dev/cpu_dma_latency and writing a 32-bit zero to that file will
+    # prevent c-state transitions while the file descriptor is open.
+    #
     # use c_states_off() to disable c-state transitions
     # use c_states_on() to close the file descriptor and re-enable c-states
     #
     def c_states_off(self):
-        self.dma_latency_handle = os.open("/dev/cpu_dma_latency", os.O_WRONLY)
-        os.write(self.dma_latency_handle, b'\x00\x00\x00\x00')
-        debug("c-states disabled")
+        if os.path.exists("/dev/cpu_dma_latency"):
+            self.dma_latency_handle = os.open("/dev/cpu_dma_latency", os.O_WRONLY)
+            os.write(self.dma_latency_handle, b'\x00\x00\x00\x00')
+            debug("c-states disabled")
 
     def c_states_on(self):
         if self.dma_latency_handle:
             os.close(self.dma_latency_handle)
-        debug("c-states enabled")
+            debug("c-states enabled")
 
     def cleanup(self):
         raise RuntimeError("must override base method 'cleanup'!")
Signature · 1 line
2.32.0
#2 lore raw
Quoted 70 lines · John Kacur
Quoted 68 lines · Punit Agrawal

From: Punit Agrawal <[email protected]>

On systems where cpu idle is disabled to reduce latencies, "/dev/cpu_dma_latency" does not exist and leads to the following exception report from python when using hwlatdetect.py -

FileNotFoundError: [Errno 2] No such file or directory: '/dev/cpu_dma_latency

Update hwlatdetect to check whether the file exists before attemping to write values to it. Also, make the related debug output conditional to c-states support being enabled in the kernel.

While we are touching this part of the code, also address a couple of minor issues in the code such as -
* correct typos
* reflow comment to fit 80-char column limit

---
v2 -> v3:
* Address issues in comment
* Initialise self.dma_latency_handle in init()
* Make debug() statements conditional to /dev/cpu_dma_latency

---
 src/hwlatdetect/hwlatdetect.py | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
src/hwlatdetect/hwlatdetect.py +10 -6
index 12228f45f852..56fd2af8641d 100755
@@ -200,6 +200,7 @@ class Detector(object):
         if os.path.exists('/usr/sbin/rdmsr'):
             self.have_msr = True
             self.initsmi = self.getsmicounts()
+        self.dma_latency_handle = None
 
     def getsmicounts(self):
         counts = []
@@ -210,20 +211,23 @@ class Detector(object):
         return counts
 
     # methods for preventing/enabling c-state transitions
-    # openinging /dev/cpu_dma_latency and writeing a 32-bit zero to that file will prevent
-    # c-state transitions while the file descriptor is open.
+    #
+    # opening /dev/cpu_dma_latency and writing a 32-bit zero to that file will
+    # prevent c-state transitions while the file descriptor is open.
+    #
     # use c_states_off() to disable c-state transitions
     # use c_states_on() to close the file descriptor and re-enable c-states
     #
     def c_states_off(self):
-        self.dma_latency_handle = os.open("/dev/cpu_dma_latency", os.O_WRONLY)
-        os.write(self.dma_latency_handle, b'\x00\x00\x00\x00')
-        debug("c-states disabled")
+        if os.path.exists("/dev/cpu_dma_latency"):
+            self.dma_latency_handle = os.open("/dev/cpu_dma_latency", os.O_WRONLY)
+            os.write(self.dma_latency_handle, b'\x00\x00\x00\x00')
+            debug("c-states disabled")
 
     def c_states_on(self):
         if self.dma_latency_handle:
             os.close(self.dma_latency_handle)
-        debug("c-states enabled")
+            debug("c-states enabled")
 
Quoted 8 lines
Quoted 6 lines
src/hwlatdetect/hwlatdetect.py +0 -0
     def cleanup(self):
         raise RuntimeError("must override base method 'cleanup'!")
Signature · 1 line
2.32.0

Thanks for applying the patch!