buildstats.bbclass 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. BUILDSTATS_BASE = "${TMPDIR}/buildstats/"
  2. ################################################################################
  3. # Build statistics gathering.
  4. #
  5. # The CPU and Time gathering/tracking functions and bbevent inspiration
  6. # were written by Christopher Larson.
  7. #
  8. ################################################################################
  9. def get_buildprocess_cputime(pid):
  10. with open("/proc/%d/stat" % pid, "r") as f:
  11. fields = f.readline().rstrip().split()
  12. # 13: utime, 14: stime, 15: cutime, 16: cstime
  13. return sum(int(field) for field in fields[13:16])
  14. def get_process_cputime(pid):
  15. import resource
  16. with open("/proc/%d/stat" % pid, "r") as f:
  17. fields = f.readline().rstrip().split()
  18. stats = {
  19. 'utime' : fields[13],
  20. 'stime' : fields[14],
  21. 'cutime' : fields[15],
  22. 'cstime' : fields[16],
  23. }
  24. iostats = {}
  25. if os.path.isfile("/proc/%d/io" % pid):
  26. with open("/proc/%d/io" % pid, "r") as f:
  27. while True:
  28. i = f.readline().strip()
  29. if not i:
  30. break
  31. if not ":" in i:
  32. # one more extra line is appended (empty or containing "0")
  33. # most probably due to race condition in kernel while
  34. # updating IO stats
  35. break
  36. i = i.split(": ")
  37. iostats[i[0]] = i[1]
  38. resources = resource.getrusage(resource.RUSAGE_SELF)
  39. childres = resource.getrusage(resource.RUSAGE_CHILDREN)
  40. return stats, iostats, resources, childres
  41. def get_cputime():
  42. with open("/proc/stat", "r") as f:
  43. fields = f.readline().rstrip().split()[1:]
  44. return sum(int(field) for field in fields)
  45. def set_timedata(var, d, server_time):
  46. d.setVar(var, server_time)
  47. def get_timedata(var, d, end_time):
  48. oldtime = d.getVar(var, False)
  49. if oldtime is None:
  50. return
  51. return end_time - oldtime
  52. def set_buildtimedata(var, d):
  53. import time
  54. time = time.time()
  55. cputime = get_cputime()
  56. proctime = get_buildprocess_cputime(os.getpid())
  57. d.setVar(var, (time, cputime, proctime))
  58. def get_buildtimedata(var, d):
  59. import time
  60. timedata = d.getVar(var, False)
  61. if timedata is None:
  62. return
  63. oldtime, oldcpu, oldproc = timedata
  64. procdiff = get_buildprocess_cputime(os.getpid()) - oldproc
  65. cpudiff = get_cputime() - oldcpu
  66. end_time = time.time()
  67. timediff = end_time - oldtime
  68. if cpudiff > 0:
  69. cpuperc = float(procdiff) * 100 / cpudiff
  70. else:
  71. cpuperc = None
  72. return timediff, cpuperc
  73. def write_task_data(status, logfile, e, d):
  74. with open(os.path.join(logfile), "a") as f:
  75. elapsedtime = get_timedata("__timedata_task", d, e.time)
  76. if elapsedtime:
  77. f.write(d.expand("${PF}: %s\n" % e.task))
  78. f.write(d.expand("Elapsed time: %0.2f seconds\n" % elapsedtime))
  79. cpu, iostats, resources, childres = get_process_cputime(os.getpid())
  80. if cpu:
  81. f.write("utime: %s\n" % cpu['utime'])
  82. f.write("stime: %s\n" % cpu['stime'])
  83. f.write("cutime: %s\n" % cpu['cutime'])
  84. f.write("cstime: %s\n" % cpu['cstime'])
  85. for i in iostats:
  86. f.write("IO %s: %s\n" % (i, iostats[i]))
  87. rusages = ["ru_utime", "ru_stime", "ru_maxrss", "ru_minflt", "ru_majflt", "ru_inblock", "ru_oublock", "ru_nvcsw", "ru_nivcsw"]
  88. for i in rusages:
  89. f.write("rusage %s: %s\n" % (i, getattr(resources, i)))
  90. for i in rusages:
  91. f.write("Child rusage %s: %s\n" % (i, getattr(childres, i)))
  92. if status == "passed":
  93. f.write("Status: PASSED \n")
  94. else:
  95. f.write("Status: FAILED \n")
  96. f.write("Ended: %0.2f \n" % e.time)
  97. def write_host_data(logfile, e, d, type):
  98. import subprocess, os, datetime
  99. # minimum time allowed for each command to run, in seconds
  100. time_threshold = 0.5
  101. limit = 10
  102. # the total number of commands
  103. num_cmds = 0
  104. msg = ""
  105. if type == "interval":
  106. # interval at which data will be logged
  107. interval = d.getVar("BB_HEARTBEAT_EVENT", False)
  108. if interval is None:
  109. bb.warn("buildstats: Collecting host data at intervals failed. Set BB_HEARTBEAT_EVENT=\"<interval>\" in conf/local.conf for the interval at which host data will be logged.")
  110. d.setVar("BB_LOG_HOST_STAT_ON_INTERVAL", "0")
  111. return
  112. interval = int(interval)
  113. cmds = d.getVar('BB_LOG_HOST_STAT_CMDS_INTERVAL')
  114. msg = "Host Stats: Collecting data at %d second intervals.\n" % interval
  115. if cmds is None:
  116. d.setVar("BB_LOG_HOST_STAT_ON_INTERVAL", "0")
  117. bb.warn("buildstats: Collecting host data at intervals failed. Set BB_LOG_HOST_STAT_CMDS_INTERVAL=\"command1 ; command2 ; ... \" in conf/local.conf.")
  118. return
  119. if type == "failure":
  120. cmds = d.getVar('BB_LOG_HOST_STAT_CMDS_FAILURE')
  121. msg = "Host Stats: Collecting data on failure.\n"
  122. msg += "Failed at task: " + e.task + "\n"
  123. if cmds is None:
  124. d.setVar("BB_LOG_HOST_STAT_ON_FAILURE", "0")
  125. bb.warn("buildstats: Collecting host data on failure failed. Set BB_LOG_HOST_STAT_CMDS_FAILURE=\"command1 ; command2 ; ... \" in conf/local.conf.")
  126. return
  127. c_san = []
  128. for cmd in cmds.split(";"):
  129. if len(cmd) == 0:
  130. continue
  131. num_cmds += 1
  132. c_san.append(cmd)
  133. if num_cmds == 0:
  134. if type == "interval":
  135. d.setVar("BB_LOG_HOST_STAT_ON_INTERVAL", "0")
  136. if type == "failure":
  137. d.setVar("BB_LOG_HOST_STAT_ON_FAILURE", "0")
  138. return
  139. # return if the interval is not enough to run all commands within the specified BB_HEARTBEAT_EVENT interval
  140. if type == "interval":
  141. limit = interval / num_cmds
  142. if limit <= time_threshold:
  143. d.setVar("BB_LOG_HOST_STAT_ON_INTERVAL", "0")
  144. bb.warn("buildstats: Collecting host data failed. BB_HEARTBEAT_EVENT interval not enough to run the specified commands. Increase value of BB_HEARTBEAT_EVENT in conf/local.conf.")
  145. return
  146. # set the environment variables
  147. path = d.getVar("PATH")
  148. opath = d.getVar("BB_ORIGENV", False).getVar("PATH")
  149. ospath = os.environ['PATH']
  150. os.environ['PATH'] = path + ":" + opath + ":" + ospath
  151. with open(logfile, "a") as f:
  152. f.write("Event Time: %f\nDate: %s\n" % (e.time, datetime.datetime.now()))
  153. f.write("%s" % msg)
  154. for c in c_san:
  155. try:
  156. output = subprocess.check_output(c.split(), stderr=subprocess.STDOUT, timeout=limit).decode('utf-8')
  157. except (subprocess.CalledProcessError, subprocess.TimeoutExpired, FileNotFoundError) as err:
  158. output = "Error running command: %s\n%s\n" % (c, err)
  159. f.write("%s\n%s\n" % (c, output))
  160. # reset the environment
  161. os.environ['PATH'] = ospath
  162. python run_buildstats () {
  163. import bb.build
  164. import bb.event
  165. import time, subprocess, platform
  166. bn = d.getVar('BUILDNAME')
  167. ########################################################################
  168. # bitbake fires HeartbeatEvent even before a build has been
  169. # triggered, causing BUILDNAME to be None
  170. ########################################################################
  171. if bn is not None:
  172. bsdir = os.path.join(d.getVar('BUILDSTATS_BASE'), bn)
  173. taskdir = os.path.join(bsdir, d.getVar('PF'))
  174. if isinstance(e, bb.event.HeartbeatEvent) and bb.utils.to_boolean(d.getVar("BB_LOG_HOST_STAT_ON_INTERVAL")):
  175. bb.utils.mkdirhier(bsdir)
  176. write_host_data(os.path.join(bsdir, "host_stats_interval"), e, d, "interval")
  177. if isinstance(e, bb.event.BuildStarted):
  178. ########################################################################
  179. # If the kernel was not configured to provide I/O statistics, issue
  180. # a one time warning.
  181. ########################################################################
  182. if not os.path.isfile("/proc/%d/io" % os.getpid()):
  183. bb.warn("The Linux kernel on your build host was not configured to provide process I/O statistics. (CONFIG_TASK_IO_ACCOUNTING is not set)")
  184. ########################################################################
  185. # at first pass make the buildstats hierarchy and then
  186. # set the buildname
  187. ########################################################################
  188. bb.utils.mkdirhier(bsdir)
  189. set_buildtimedata("__timedata_build", d)
  190. build_time = os.path.join(bsdir, "build_stats")
  191. # write start of build into build_time
  192. with open(build_time, "a") as f:
  193. host_info = platform.uname()
  194. f.write("Host Info: ")
  195. for x in host_info:
  196. if x:
  197. f.write(x + " ")
  198. f.write("\n")
  199. f.write("Build Started: %0.2f \n" % d.getVar('__timedata_build', False)[0])
  200. elif isinstance(e, bb.event.BuildCompleted):
  201. build_time = os.path.join(bsdir, "build_stats")
  202. with open(build_time, "a") as f:
  203. ########################################################################
  204. # Write build statistics for the build
  205. ########################################################################
  206. timedata = get_buildtimedata("__timedata_build", d)
  207. if timedata:
  208. time, cpu = timedata
  209. # write end of build and cpu used into build_time
  210. f.write("Elapsed time: %0.2f seconds \n" % (time))
  211. if cpu:
  212. f.write("CPU usage: %0.1f%% \n" % cpu)
  213. if isinstance(e, bb.build.TaskStarted):
  214. set_timedata("__timedata_task", d, e.time)
  215. bb.utils.mkdirhier(taskdir)
  216. # write into the task event file the name and start time
  217. with open(os.path.join(taskdir, e.task), "a") as f:
  218. f.write("Event: %s \n" % bb.event.getName(e))
  219. f.write("Started: %0.2f \n" % e.time)
  220. elif isinstance(e, bb.build.TaskSucceeded):
  221. write_task_data("passed", os.path.join(taskdir, e.task), e, d)
  222. if e.task == "do_rootfs":
  223. bs = os.path.join(bsdir, "build_stats")
  224. with open(bs, "a") as f:
  225. rootfs = d.getVar('IMAGE_ROOTFS')
  226. if os.path.isdir(rootfs):
  227. try:
  228. rootfs_size = subprocess.check_output(["du", "-sh", rootfs],
  229. stderr=subprocess.STDOUT).decode('utf-8')
  230. f.write("Uncompressed Rootfs size: %s" % rootfs_size)
  231. except subprocess.CalledProcessError as err:
  232. bb.warn("Failed to get rootfs size: %s" % err.output.decode('utf-8'))
  233. elif isinstance(e, bb.build.TaskFailed):
  234. # Can have a failure before TaskStarted so need to mkdir here too
  235. bb.utils.mkdirhier(taskdir)
  236. write_task_data("failed", os.path.join(taskdir, e.task), e, d)
  237. ########################################################################
  238. # Lets make things easier and tell people where the build failed in
  239. # build_status. We do this here because BuildCompleted triggers no
  240. # matter what the status of the build actually is
  241. ########################################################################
  242. build_status = os.path.join(bsdir, "build_stats")
  243. with open(build_status, "a") as f:
  244. f.write(d.expand("Failed at: ${PF} at task: %s \n" % e.task))
  245. if bb.utils.to_boolean(d.getVar("BB_LOG_HOST_STAT_ON_FAILURE")):
  246. write_host_data(os.path.join(bsdir, "host_stats_%s_failure" % e.task), e, d, "failure")
  247. }
  248. addhandler run_buildstats
  249. run_buildstats[eventmask] = "bb.event.BuildStarted bb.event.BuildCompleted bb.event.HeartbeatEvent bb.build.TaskStarted bb.build.TaskSucceeded bb.build.TaskFailed"
  250. python runqueue_stats () {
  251. import buildstats
  252. from bb import event, runqueue
  253. # We should not record any samples before the first task has started,
  254. # because that's the first activity shown in the process chart.
  255. # Besides, at that point we are sure that the build variables
  256. # are available that we need to find the output directory.
  257. # The persistent SystemStats is stored in the datastore and
  258. # closed when the build is done.
  259. system_stats = d.getVar('_buildstats_system_stats', False)
  260. if not system_stats and isinstance(e, (bb.runqueue.sceneQueueTaskStarted, bb.runqueue.runQueueTaskStarted)):
  261. system_stats = buildstats.SystemStats(d)
  262. d.setVar('_buildstats_system_stats', system_stats)
  263. if system_stats:
  264. # Ensure that we sample at important events.
  265. done = isinstance(e, bb.event.BuildCompleted)
  266. system_stats.sample(e, force=done)
  267. if done:
  268. system_stats.close()
  269. d.delVar('_buildstats_system_stats')
  270. }
  271. addhandler runqueue_stats
  272. runqueue_stats[eventmask] = "bb.runqueue.sceneQueueTaskStarted bb.runqueue.runQueueTaskStarted bb.event.HeartbeatEvent bb.event.BuildCompleted bb.event.MonitorDiskEvent"