From cf4d3b5f4c370b3cf30a3ea8a9a3eaf770d7cd10 Mon Sep 17 00:00:00 2001 From: Douglas Raillard Date: Mon, 17 Jul 2023 12:07:58 +0100 Subject: [PATCH] collector/dmesg: Avoid unnecessary dmesg command Only run the minimal amount of commands, as executing a command can be costly. In the sequence reset() -> start(), we only need to get the output of dmesg upon start() to know what part of the log will be ignored (everything before the call to start()). There is no need to perform that upon reset() since the sequence: reset() -> start() -> stop() -> start() -> stop() \______1________/ \______2________/ is anyway equivalent to: reset() -> start() -> stop() \______2________/ So reset() can essentially be a no-op and the actual reset logic lives in start(). --- devlib/collector/dmesg.py | 43 ++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/devlib/collector/dmesg.py b/devlib/collector/dmesg.py index faf9682..72529f5 100644 --- a/devlib/collector/dmesg.py +++ b/devlib/collector/dmesg.py @@ -234,27 +234,7 @@ class DmesgCollector(CollectorBase): if entry.timestamp > timestamp ] - def reset(self): - # If the buffer is emptied on start(), it does not matter as we will - # not end up with entries dating from before start() - if self.empty_buffer: - # Empty the dmesg ring buffer. This requires root in all cases - self.target.execute('dmesg -c', as_root=True) - else: - self.stop() - try: - entry = self.entries[-1] - except IndexError: - pass - else: - self._begin_timestamp = entry.timestamp - - self._dmesg_out = None - - def start(self): - self.reset() - - def stop(self): + def _get_output(self): levels_list = list(takewhile( lambda level: level != self.level, self.LOG_LEVELS @@ -270,6 +250,27 @@ class DmesgCollector(CollectorBase): self._dmesg_out = self.target.execute(cmd, as_root=self.needs_root) + def reset(self): + self._dmesg_out = None + + def start(self): + # If the buffer is emptied on start(), it does not matter as we will + # not end up with entries dating from before start() + if self.empty_buffer: + # Empty the dmesg ring buffer. This requires root in all cases + self.target.execute('dmesg -c', as_root=True) + else: + self._get_output() + try: + entry = self.entries[-1] + except IndexError: + pass + else: + self._begin_timestamp = entry.timestamp + + def stop(self): + self._get_output() + def set_output(self, output_path): self.output_path = output_path