summaryrefslogtreecommitdiffstats
path: root/meta/lib/buildstats.py
diff options
context:
space:
mode:
authorAryaman Gupta <aryaman.gupta@windriver.com>2022-06-22 15:21:03 -0400
committerRichard Purdie <richard.purdie@linuxfoundation.org>2022-06-29 16:16:56 +0100
commitac162116b365900925bd753ae264847f751ce799 (patch)
tree15ad4cad296ba4c48202fd3db63d8e7cd0da3940 /meta/lib/buildstats.py
parentba6160f2e0e6cade77ec9075f7d913db3099cbdb (diff)
downloadpoky-ac162116b365900925bd753ae264847f751ce799.tar.gz
buildstats.py: enable collection of /proc/pressure data
The Linux pressure monitoring system helps determine when system resources are being overutilized by measuring how contended the CPU, IO and memory are. This information can be found under /proc/pressure/ which contains 3 files - cpu, memory and io. In each of the files, the format is as follows: some avg10=70.24 avg60=68.52 avg300=69.91 total=3559632828 full avg10=57.59 avg60=58.06 avg300=60.38 total=3300487258 The "some" state of a given resource represents when one or more tasks are delayed on that resource whereas the "full" state represents when all the tasks are delayed. Currently, we only collect data from the "some" state but the "full" data can simply be appended to the log files if neccessary. The "avg10", "avg60" and "avg300" fields represent the average percentage of time runnable tasks were delayed in the last 10, 60 or 300 seconds respectively. The "total" field represents the total time, in microseconds, that some runnable task was delayed on a resource. More information can be found at: https://www.kernel.org/doc/html/latest/accounting/psi.html and in the source code under kernel/sched/psi.c This commit adds functionality to collect and log the "some" CPU, memory and IO pressure. The "avg10", "avg60" and "avg300" fields are logged without change. In place of the "total" field, the difference between the current "total" and the previous sample's "total" is logged, allowing the measurement of pressure in between each polling interval, as was done for /proc/stat data. The log files are stored in: <build_name>/tmp/buildstats/<build_time>/reduced_proc_pressure/{cpu,io,memory}.log mirroring the directory structure of /proc/pressure. If the /proc/pressure directory does not exist or the resource files can't be read/opened, the reduced_proc_pressure directory is not created. (From OE-Core rev: 061931520b8baa7f3a03bf466aa9ec8bf995bc14) Signed-off-by: Aryaman Gupta <aryaman.gupta@windriver.com> Signed-off-by: Randy MacLeod <randy.macleod@windriver.com> Signed-off-by: Luca Ceresoli <luca.ceresoli@bootlin.com> Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/lib/buildstats.py')
-rw-r--r--meta/lib/buildstats.py57
1 files changed, 47 insertions, 10 deletions
diff --git a/meta/lib/buildstats.py b/meta/lib/buildstats.py
index c52b6c3b72..64ad3ef40e 100644
--- a/meta/lib/buildstats.py
+++ b/meta/lib/buildstats.py
@@ -14,13 +14,27 @@ class SystemStats:
14 bn = d.getVar('BUILDNAME') 14 bn = d.getVar('BUILDNAME')
15 bsdir = os.path.join(d.getVar('BUILDSTATS_BASE'), bn) 15 bsdir = os.path.join(d.getVar('BUILDSTATS_BASE'), bn)
16 bb.utils.mkdirhier(bsdir) 16 bb.utils.mkdirhier(bsdir)
17 file_handlers = [('diskstats', self._reduce_diskstats),
18 ('meminfo', self._reduce_meminfo),
19 ('stat', self._reduce_stat)]
20
21 # Some hosts like openSUSE have readable /proc/pressure files
22 # but throw errors when these files are opened. Catch these error
23 # and ensure that the reduce_proc_pressure directory is not created.
24 if os.path.exists("/proc/pressure"):
25 try:
26 source = open('/proc/pressure/cpu', 'rb')
27 source.read()
28 pressuredir = os.path.join(bsdir, 'reduced_proc_pressure')
29 bb.utils.mkdirhier(pressuredir)
30 file_handlers.extend([('pressure/cpu', self._reduce_pressure),
31 ('pressure/io', self._reduce_pressure),
32 ('pressure/memory', self._reduce_pressure)])
33 except Exception:
34 pass
17 35
18 self.proc_files = [] 36 self.proc_files = []
19 for filename, handler in ( 37 for filename, handler in (file_handlers):
20 ('diskstats', self._reduce_diskstats),
21 ('meminfo', self._reduce_meminfo),
22 ('stat', self._reduce_stat),
23 ):
24 # The corresponding /proc files might not exist on the host. 38 # The corresponding /proc files might not exist on the host.
25 # For example, /proc/diskstats is not available in virtualized 39 # For example, /proc/diskstats is not available in virtualized
26 # environments like Linux-VServer. Silently skip collecting 40 # environments like Linux-VServer. Silently skip collecting
@@ -48,13 +62,15 @@ class SystemStats:
48 self.diskstats_ltime = None 62 self.diskstats_ltime = None
49 self.diskstats_data = None 63 self.diskstats_data = None
50 self.stat_ltimes = None 64 self.stat_ltimes = None
65 # Last time we sampled /proc/pressure. All resources stored in a single dict with the key as filename
66 self.last_pressure = {"pressure/cpu": None, "pressure/io": None, "pressure/memory": None}
51 67
52 def close(self): 68 def close(self):
53 self.monitor_disk.close() 69 self.monitor_disk.close()
54 for _, output, _ in self.proc_files: 70 for _, output, _ in self.proc_files:
55 output.close() 71 output.close()
56 72
57 def _reduce_meminfo(self, time, data): 73 def _reduce_meminfo(self, time, data, filename):
58 """ 74 """
59 Extracts 'MemTotal', 'MemFree', 'Buffers', 'Cached', 'SwapTotal', 'SwapFree' 75 Extracts 'MemTotal', 'MemFree', 'Buffers', 'Cached', 'SwapTotal', 'SwapFree'
60 and writes their values into a single line, in that order. 76 and writes their values into a single line, in that order.
@@ -75,7 +91,7 @@ class SystemStats:
75 disk = linetokens[2] 91 disk = linetokens[2]
76 return self.diskstats_regex.match(disk) 92 return self.diskstats_regex.match(disk)
77 93
78 def _reduce_diskstats(self, time, data): 94 def _reduce_diskstats(self, time, data, filename):
79 relevant_tokens = filter(self._diskstats_is_relevant_line, map(lambda x: x.split(), data.split(b'\n'))) 95 relevant_tokens = filter(self._diskstats_is_relevant_line, map(lambda x: x.split(), data.split(b'\n')))
80 diskdata = [0] * 3 96 diskdata = [0] * 3
81 reduced = None 97 reduced = None
@@ -104,10 +120,10 @@ class SystemStats:
104 return reduced 120 return reduced
105 121
106 122
107 def _reduce_nop(self, time, data): 123 def _reduce_nop(self, time, data, filename):
108 return (time, data) 124 return (time, data)
109 125
110 def _reduce_stat(self, time, data): 126 def _reduce_stat(self, time, data, filename):
111 if not data: 127 if not data:
112 return None 128 return None
113 # CPU times {user, nice, system, idle, io_wait, irq, softirq} from first line 129 # CPU times {user, nice, system, idle, io_wait, irq, softirq} from first line
@@ -126,6 +142,27 @@ class SystemStats:
126 self.stat_ltimes = times 142 self.stat_ltimes = times
127 return reduced 143 return reduced
128 144
145 def _reduce_pressure(self, time, data, filename):
146 """
147 Return reduced pressure: {avg10, avg60, avg300} and delta total compared to the previous sample
148 for the cpu, io and memory resources. A common function is used for all 3 resources since the
149 format of the /proc/pressure file is the same in each case.
150 """
151 if not data:
152 return None
153 tokens = data.split(b'\n', 1)[0].split()
154 avg10 = float(tokens[1].split(b'=')[1])
155 avg60 = float(tokens[2].split(b'=')[1])
156 avg300 = float(tokens[3].split(b'=')[1])
157 total = int(tokens[4].split(b'=')[1])
158
159 reduced = None
160 if self.last_pressure[filename]:
161 delta = total - self.last_pressure[filename]
162 reduced = (time, (avg10, avg60, avg300, delta))
163 self.last_pressure[filename] = total
164 return reduced
165
129 def sample(self, event, force): 166 def sample(self, event, force):
130 now = time.time() 167 now = time.time()
131 if (now - self.last_proc > self.min_seconds) or force: 168 if (now - self.last_proc > self.min_seconds) or force:
@@ -133,7 +170,7 @@ class SystemStats:
133 with open(os.path.join('/proc', filename), 'rb') as input: 170 with open(os.path.join('/proc', filename), 'rb') as input:
134 data = input.read() 171 data = input.read()
135 if handler: 172 if handler:
136 reduced = handler(now, data) 173 reduced = handler(now, data, filename)
137 else: 174 else:
138 reduced = (now, data) 175 reduced = (now, data)
139 if reduced: 176 if reduced: