Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame^] | 1 | BUILDSTATS_BASE = "${TMPDIR}/buildstats/" |
| 2 | BUILDSTATS_BNFILE = "${BUILDSTATS_BASE}/.buildname" |
| 3 | BUILDSTATS_DEVFILE = "${BUILDSTATS_BASE}/.device" |
| 4 | |
| 5 | ################################################################################ |
| 6 | # Build statistics gathering. |
| 7 | # |
| 8 | # The CPU and Time gathering/tracking functions and bbevent inspiration |
| 9 | # were written by Christopher Larson and can be seen here: |
| 10 | # http://kergoth.pastey.net/142813 |
| 11 | # |
| 12 | ################################################################################ |
| 13 | |
| 14 | def get_process_cputime(pid): |
| 15 | with open("/proc/%d/stat" % pid, "r") as f: |
| 16 | fields = f.readline().rstrip().split() |
| 17 | # 13: utime, 14: stime, 15: cutime, 16: cstime |
| 18 | return sum(int(field) for field in fields[13:16]) |
| 19 | |
| 20 | def get_cputime(): |
| 21 | with open("/proc/stat", "r") as f: |
| 22 | fields = f.readline().rstrip().split()[1:] |
| 23 | return sum(int(field) for field in fields) |
| 24 | |
| 25 | def set_bn(e): |
| 26 | bn = e.getPkgs()[0] + "-" + e.data.getVar('MACHINE', True) |
| 27 | try: |
| 28 | os.remove(e.data.getVar('BUILDSTATS_BNFILE', True)) |
| 29 | except: |
| 30 | pass |
| 31 | with open(e.data.getVar('BUILDSTATS_BNFILE', True), "w") as f: |
| 32 | f.write(os.path.join(bn, e.data.getVar('BUILDNAME', True))) |
| 33 | |
| 34 | def get_bn(e): |
| 35 | with open(e.data.getVar('BUILDSTATS_BNFILE', True)) as f: |
| 36 | bn = f.readline() |
| 37 | return bn |
| 38 | |
| 39 | def set_device(e): |
| 40 | tmpdir = e.data.getVar('TMPDIR', True) |
| 41 | devfile = e.data.getVar('BUILDSTATS_DEVFILE', True) |
| 42 | try: |
| 43 | os.remove(devfile) |
| 44 | except: |
| 45 | pass |
| 46 | ############################################################################ |
| 47 | # We look for the volume TMPDIR lives on. To do all disks would make little |
| 48 | # sense and not give us any particularly useful data. In theory we could do |
| 49 | # something like stick DL_DIR on a different partition and this would |
| 50 | # throw stats gathering off. The same goes with SSTATE_DIR. However, let's |
| 51 | # get the basics in here and work on the cornercases later. |
| 52 | # A note. /proc/diskstats does not contain info on encryptfs, tmpfs, etc. |
| 53 | # If we end up hitting one of these fs, we'll just skip diskstats collection. |
| 54 | ############################################################################ |
| 55 | device = os.stat(tmpdir) |
| 56 | majordev = os.major(long(device.st_dev)) |
| 57 | minordev = os.minor(long(device.st_dev)) |
| 58 | ############################################################################ |
| 59 | # Bug 1700: |
| 60 | # Because tmpfs/encryptfs/ramfs etc inserts no entry in /proc/diskstats |
| 61 | # we set rdev to NoLogicalDevice and search for it later. If we find NLD |
| 62 | # we do not collect diskstats as the method to collect meaningful statistics |
| 63 | # for these fs types requires a bit more research. |
| 64 | ############################################################################ |
| 65 | rdev = "NoLogicalDevice" |
| 66 | try: |
| 67 | with open("/proc/diskstats", "r") as f: |
| 68 | for line in f: |
| 69 | if majordev == int(line.split()[0]) and minordev == int(line.split()[1]): |
| 70 | rdev = line.split()[2] |
| 71 | except: |
| 72 | pass |
| 73 | with open(devfile, "w") as f: |
| 74 | f.write(rdev) |
| 75 | |
| 76 | def get_device(e): |
| 77 | with open(e.data.getVar('BUILDSTATS_DEVFILE', True)) as f: |
| 78 | device = f.readline() |
| 79 | return device |
| 80 | |
| 81 | def get_diskstats(dev): |
| 82 | import itertools |
| 83 | ############################################################################ |
| 84 | # For info on what these are, see kernel doc file iostats.txt |
| 85 | ############################################################################ |
| 86 | DSTAT_KEYS = ['ReadsComp', 'ReadsMerged', 'SectRead', 'TimeReads', 'WritesComp', 'SectWrite', 'TimeWrite', 'IOinProgress', 'TimeIO', 'WTimeIO'] |
| 87 | try: |
| 88 | with open("/proc/diskstats", "r") as f: |
| 89 | for x in f: |
| 90 | if dev in x: |
| 91 | diskstats_val = x.rstrip().split()[4:] |
| 92 | except IOError as e: |
| 93 | return |
| 94 | diskstats = dict(itertools.izip(DSTAT_KEYS, diskstats_val)) |
| 95 | return diskstats |
| 96 | |
| 97 | def set_diskdata(var, dev, data): |
| 98 | data.setVar(var, get_diskstats(dev)) |
| 99 | |
| 100 | def get_diskdata(var, dev, data): |
| 101 | olddiskdata = data.getVar(var, False) |
| 102 | diskdata = {} |
| 103 | if olddiskdata is None: |
| 104 | return |
| 105 | newdiskdata = get_diskstats(dev) |
| 106 | for key in olddiskdata.iterkeys(): |
| 107 | diskdata["Start"+key] = str(int(olddiskdata[key])) |
| 108 | diskdata["End"+key] = str(int(newdiskdata[key])) |
| 109 | return diskdata |
| 110 | |
| 111 | def set_timedata(var, data, server_time=None): |
| 112 | import time |
| 113 | if server_time: |
| 114 | time = server_time |
| 115 | else: |
| 116 | time = time.time() |
| 117 | cputime = get_cputime() |
| 118 | proctime = get_process_cputime(os.getpid()) |
| 119 | data.setVar(var, (time, cputime, proctime)) |
| 120 | |
| 121 | def get_timedata(var, data, server_time=None): |
| 122 | import time |
| 123 | timedata = data.getVar(var, False) |
| 124 | if timedata is None: |
| 125 | return |
| 126 | oldtime, oldcpu, oldproc = timedata |
| 127 | procdiff = get_process_cputime(os.getpid()) - oldproc |
| 128 | cpudiff = get_cputime() - oldcpu |
| 129 | if server_time: |
| 130 | end_time = server_time |
| 131 | else: |
| 132 | end_time = time.time() |
| 133 | timediff = end_time - oldtime |
| 134 | if cpudiff > 0: |
| 135 | cpuperc = float(procdiff) * 100 / cpudiff |
| 136 | else: |
| 137 | cpuperc = None |
| 138 | return timediff, cpuperc |
| 139 | |
| 140 | def write_task_data(status, logfile, dev, e): |
| 141 | bn = get_bn(e) |
| 142 | bsdir = os.path.join(e.data.getVar('BUILDSTATS_BASE', True), bn) |
| 143 | with open(os.path.join(logfile), "a") as f: |
| 144 | timedata = get_timedata("__timedata_task", e.data, e.time) |
| 145 | if timedata: |
| 146 | elapsedtime, cpu = timedata |
| 147 | f.write(bb.data.expand("${PF}: %s: Elapsed time: %0.2f seconds \n" % |
| 148 | (e.task, elapsedtime), e.data)) |
| 149 | if cpu: |
| 150 | f.write("CPU usage: %0.1f%% \n" % cpu) |
| 151 | ############################################################################ |
| 152 | # Here we gather up disk data. In an effort to avoid lying with stats |
| 153 | # I do a bare minimum of analysis of collected data. |
| 154 | # The simple fact is, doing disk io collection on a per process basis |
| 155 | # without effecting build time would be difficult. |
| 156 | # For the best information, running things with BB_TOTAL_THREADS = "1" |
| 157 | # would return accurate per task results. |
| 158 | ############################################################################ |
| 159 | if dev != "NoLogicalDevice": |
| 160 | diskdata = get_diskdata("__diskdata_task", dev, e.data) |
| 161 | if diskdata: |
| 162 | for key in sorted(diskdata.iterkeys()): |
| 163 | f.write(key + ": " + diskdata[key] + "\n") |
| 164 | if status is "passed": |
| 165 | f.write("Status: PASSED \n") |
| 166 | else: |
| 167 | f.write("Status: FAILED \n") |
| 168 | f.write("Ended: %0.2f \n" % e.time) |
| 169 | |
| 170 | python run_buildstats () { |
| 171 | import bb.build |
| 172 | import bb.event |
| 173 | import bb.data |
| 174 | import time, subprocess, platform |
| 175 | |
| 176 | if isinstance(e, bb.event.BuildStarted): |
| 177 | ######################################################################## |
| 178 | # at first pass make the buildstats heriarchy and then |
| 179 | # set the buildname |
| 180 | ######################################################################## |
| 181 | bb.utils.mkdirhier(e.data.getVar('BUILDSTATS_BASE', True)) |
| 182 | set_bn(e) |
| 183 | bn = get_bn(e) |
| 184 | set_device(e) |
| 185 | device = get_device(e) |
| 186 | |
| 187 | bsdir = os.path.join(e.data.getVar('BUILDSTATS_BASE', True), bn) |
| 188 | bb.utils.mkdirhier(bsdir) |
| 189 | if device != "NoLogicalDevice": |
| 190 | set_diskdata("__diskdata_build", device, e.data) |
| 191 | set_timedata("__timedata_build", e.data) |
| 192 | build_time = os.path.join(bsdir, "build_stats") |
| 193 | # write start of build into build_time |
| 194 | with open(build_time, "a") as f: |
| 195 | host_info = platform.uname() |
| 196 | f.write("Host Info: ") |
| 197 | for x in host_info: |
| 198 | if x: |
| 199 | f.write(x + " ") |
| 200 | f.write("\n") |
| 201 | f.write("Build Started: %0.2f \n" % time.time()) |
| 202 | |
| 203 | elif isinstance(e, bb.event.BuildCompleted): |
| 204 | bn = get_bn(e) |
| 205 | device = get_device(e) |
| 206 | bsdir = os.path.join(e.data.getVar('BUILDSTATS_BASE', True), bn) |
| 207 | build_time = os.path.join(bsdir, "build_stats") |
| 208 | with open(build_time, "a") as f: |
| 209 | ######################################################################## |
| 210 | # Write build statistics for the build |
| 211 | ######################################################################## |
| 212 | timedata = get_timedata("__timedata_build", e.data) |
| 213 | if timedata: |
| 214 | time, cpu = timedata |
| 215 | # write end of build and cpu used into build_time |
| 216 | f.write("Elapsed time: %0.2f seconds \n" % (time)) |
| 217 | if cpu: |
| 218 | f.write("CPU usage: %0.1f%% \n" % cpu) |
| 219 | if device != "NoLogicalDevice": |
| 220 | diskio = get_diskdata("__diskdata_build", device, e.data) |
| 221 | if diskio: |
| 222 | for key in sorted(diskio.iterkeys()): |
| 223 | f.write(key + ": " + diskio[key] + "\n") |
| 224 | |
| 225 | if isinstance(e, bb.build.TaskStarted): |
| 226 | bn = get_bn(e) |
| 227 | device = get_device(e) |
| 228 | bsdir = os.path.join(e.data.getVar('BUILDSTATS_BASE', True), bn) |
| 229 | taskdir = os.path.join(bsdir, e.data.getVar('PF', True)) |
| 230 | if device != "NoLogicalDevice": |
| 231 | set_diskdata("__diskdata_task", device, e.data) |
| 232 | set_timedata("__timedata_task", e.data, e.time) |
| 233 | bb.utils.mkdirhier(taskdir) |
| 234 | # write into the task event file the name and start time |
| 235 | with open(os.path.join(taskdir, e.task), "a") as f: |
| 236 | f.write("Event: %s \n" % bb.event.getName(e)) |
| 237 | f.write("Started: %0.2f \n" % e.time) |
| 238 | |
| 239 | elif isinstance(e, bb.build.TaskSucceeded): |
| 240 | bn = get_bn(e) |
| 241 | device = get_device(e) |
| 242 | bsdir = os.path.join(e.data.getVar('BUILDSTATS_BASE', True), bn) |
| 243 | taskdir = os.path.join(bsdir, e.data.getVar('PF', True)) |
| 244 | write_task_data("passed", os.path.join(taskdir, e.task), device, e) |
| 245 | if e.task == "do_rootfs": |
| 246 | bs = os.path.join(bsdir, "build_stats") |
| 247 | with open(bs, "a") as f: |
| 248 | rootfs = e.data.getVar('IMAGE_ROOTFS', True) |
| 249 | rootfs_size = subprocess.Popen(["du", "-sh", rootfs], stdout=subprocess.PIPE).stdout.read() |
| 250 | f.write("Uncompressed Rootfs size: %s" % rootfs_size) |
| 251 | |
| 252 | elif isinstance(e, bb.build.TaskFailed): |
| 253 | bn = get_bn(e) |
| 254 | device = get_device(e) |
| 255 | bsdir = os.path.join(e.data.getVar('BUILDSTATS_BASE', True), bn) |
| 256 | taskdir = os.path.join(bsdir, e.data.getVar('PF', True)) |
| 257 | write_task_data("failed", os.path.join(taskdir, e.task), device, e) |
| 258 | ######################################################################## |
| 259 | # Lets make things easier and tell people where the build failed in |
| 260 | # build_status. We do this here because BuildCompleted triggers no |
| 261 | # matter what the status of the build actually is |
| 262 | ######################################################################## |
| 263 | build_status = os.path.join(bsdir, "build_stats") |
| 264 | with open(build_status, "a") as f: |
| 265 | f.write(e.data.expand("Failed at: ${PF} at task: %s \n" % e.task)) |
| 266 | } |
| 267 | |
| 268 | addhandler run_buildstats |
| 269 | run_buildstats[eventmask] = "bb.event.BuildStarted bb.event.BuildCompleted bb.build.TaskStarted bb.build.TaskSucceeded bb.build.TaskFailed" |
| 270 | |