buildstats.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. #
  4. # Implements system state sampling. Called by buildstats.bbclass.
  5. # Because it is a real Python module, it can hold persistent state,
  6. # like open log files and the time of the last sampling.
  7. import time
  8. import re
  9. import bb.event
  10. class SystemStats:
  11. def __init__(self, d):
  12. bn = d.getVar('BUILDNAME')
  13. bsdir = os.path.join(d.getVar('BUILDSTATS_BASE'), bn)
  14. bb.utils.mkdirhier(bsdir)
  15. self.proc_files = []
  16. for filename, handler in (
  17. ('diskstats', self._reduce_diskstats),
  18. ('meminfo', self._reduce_meminfo),
  19. ('stat', self._reduce_stat),
  20. ):
  21. # The corresponding /proc files might not exist on the host.
  22. # For example, /proc/diskstats is not available in virtualized
  23. # environments like Linux-VServer. Silently skip collecting
  24. # the data.
  25. if os.path.exists(os.path.join('/proc', filename)):
  26. # In practice, this class gets instantiated only once in
  27. # the bitbake cooker process. Therefore 'append' mode is
  28. # not strictly necessary, but using it makes the class
  29. # more robust should two processes ever write
  30. # concurrently.
  31. destfile = os.path.join(bsdir, '%sproc_%s.log' % ('reduced_' if handler else '', filename))
  32. self.proc_files.append((filename, open(destfile, 'ab'), handler))
  33. self.monitor_disk = open(os.path.join(bsdir, 'monitor_disk.log'), 'ab')
  34. # Last time that we sampled /proc data resp. recorded disk monitoring data.
  35. self.last_proc = 0
  36. self.last_disk_monitor = 0
  37. # Minimum number of seconds between recording a sample. This
  38. # becames relevant when we get called very often while many
  39. # short tasks get started. Sampling during quiet periods
  40. # depends on the heartbeat event, which fires less often.
  41. self.min_seconds = 1
  42. self.meminfo_regex = re.compile(b'^(MemTotal|MemFree|Buffers|Cached|SwapTotal|SwapFree):\s*(\d+)')
  43. self.diskstats_regex = re.compile(b'^([hsv]d.|mtdblock\d|mmcblk\d|cciss/c\d+d\d+.*)$')
  44. self.diskstats_ltime = None
  45. self.diskstats_data = None
  46. self.stat_ltimes = None
  47. def close(self):
  48. self.monitor_disk.close()
  49. for _, output, _ in self.proc_files:
  50. output.close()
  51. def _reduce_meminfo(self, time, data):
  52. """
  53. Extracts 'MemTotal', 'MemFree', 'Buffers', 'Cached', 'SwapTotal', 'SwapFree'
  54. and writes their values into a single line, in that order.
  55. """
  56. values = {}
  57. for line in data.split(b'\n'):
  58. m = self.meminfo_regex.match(line)
  59. if m:
  60. values[m.group(1)] = m.group(2)
  61. if len(values) == 6:
  62. return (time,
  63. b' '.join([values[x] for x in
  64. (b'MemTotal', b'MemFree', b'Buffers', b'Cached', b'SwapTotal', b'SwapFree')]) + b'\n')
  65. def _diskstats_is_relevant_line(self, linetokens):
  66. if len(linetokens) != 14:
  67. return False
  68. disk = linetokens[2]
  69. return self.diskstats_regex.match(disk)
  70. def _reduce_diskstats(self, time, data):
  71. relevant_tokens = filter(self._diskstats_is_relevant_line, map(lambda x: x.split(), data.split(b'\n')))
  72. diskdata = [0] * 3
  73. reduced = None
  74. for tokens in relevant_tokens:
  75. # rsect
  76. diskdata[0] += int(tokens[5])
  77. # wsect
  78. diskdata[1] += int(tokens[9])
  79. # use
  80. diskdata[2] += int(tokens[12])
  81. if self.diskstats_ltime:
  82. # We need to compute information about the time interval
  83. # since the last sampling and record the result as sample
  84. # for that point in the past.
  85. interval = time - self.diskstats_ltime
  86. if interval > 0:
  87. sums = [ a - b for a, b in zip(diskdata, self.diskstats_data) ]
  88. readTput = sums[0] / 2.0 * 100.0 / interval
  89. writeTput = sums[1] / 2.0 * 100.0 / interval
  90. util = float( sums[2] ) / 10 / interval
  91. util = max(0.0, min(1.0, util))
  92. reduced = (self.diskstats_ltime, (readTput, writeTput, util))
  93. self.diskstats_ltime = time
  94. self.diskstats_data = diskdata
  95. return reduced
  96. def _reduce_nop(self, time, data):
  97. return (time, data)
  98. def _reduce_stat(self, time, data):
  99. if not data:
  100. return None
  101. # CPU times {user, nice, system, idle, io_wait, irq, softirq} from first line
  102. tokens = data.split(b'\n', 1)[0].split()
  103. times = [ int(token) for token in tokens[1:] ]
  104. reduced = None
  105. if self.stat_ltimes:
  106. user = float((times[0] + times[1]) - (self.stat_ltimes[0] + self.stat_ltimes[1]))
  107. system = float((times[2] + times[5] + times[6]) - (self.stat_ltimes[2] + self.stat_ltimes[5] + self.stat_ltimes[6]))
  108. idle = float(times[3] - self.stat_ltimes[3])
  109. iowait = float(times[4] - self.stat_ltimes[4])
  110. aSum = max(user + system + idle + iowait, 1)
  111. reduced = (time, (user/aSum, system/aSum, iowait/aSum))
  112. self.stat_ltimes = times
  113. return reduced
  114. def sample(self, event, force):
  115. now = time.time()
  116. if (now - self.last_proc > self.min_seconds) or force:
  117. for filename, output, handler in self.proc_files:
  118. with open(os.path.join('/proc', filename), 'rb') as input:
  119. data = input.read()
  120. if handler:
  121. reduced = handler(now, data)
  122. else:
  123. reduced = (now, data)
  124. if reduced:
  125. if isinstance(reduced[1], bytes):
  126. # Use as it is.
  127. data = reduced[1]
  128. else:
  129. # Convert to a single line.
  130. data = (' '.join([str(x) for x in reduced[1]]) + '\n').encode('ascii')
  131. # Unbuffered raw write, less overhead and useful
  132. # in case that we end up with concurrent writes.
  133. os.write(output.fileno(),
  134. ('%.0f\n' % reduced[0]).encode('ascii') +
  135. data +
  136. b'\n')
  137. self.last_proc = now
  138. if isinstance(event, bb.event.MonitorDiskEvent) and \
  139. ((now - self.last_disk_monitor > self.min_seconds) or force):
  140. os.write(self.monitor_disk.fileno(),
  141. ('%.0f\n' % now).encode('ascii') +
  142. ''.join(['%s: %d\n' % (dev, sample.total_bytes - sample.free_bytes)
  143. for dev, sample in event.disk_usage.items()]).encode('ascii') +
  144. b'\n')
  145. self.last_disk_monitor = now