runqueue: Rework process_possible_migrations() to improve performance
PATCH 1/5 runqueue: Rework process_possible_migrations() to improve performance
The looping over multiple changed hashes causes many calls to get_taskhash and get_unihash which are potentially slow and then overwritten.
Instead, batch up all the tasks which have changed unihashes and then do one big loop over the changed tasks rather than each in turn.
This makes worlds of difference to the performance graphs and should speed up build where many tasks are being rehashed.
--- lib/bb/runqueue.py | 103 ++++++++++++++++++++++++--------------------- 1 file changed, 56 insertions(+), 47 deletions(-)
lib/bb/runqueue.py +56 -47
@@ -2248,6 +2248,7 @@ class RunQueueExecute: def process_possible_migrations(self): changed = set() + toprocess = set() for tid, unihash in self.updated_taskhash_queue.copy(): if tid in self.runq_running and tid not in self.runq_complete: continue
@@ -2258,53 +2259,61 @@ class RunQueueExecute: logger.info("Task %s unihash changed to %s" % (tid, unihash)) self.rqdata.runtaskentries[tid].unihash = unihash bb.parse.siggen.set_unihash(tid, unihash) - - # Work out all tasks which depend on this one - total = set() - next = set(self.rqdata.runtaskentries[tid].revdeps) - while next: - current = next.copy() - total = total |next - next = set() - for ntid in current: - next |= self.rqdata.runtaskentries[ntid].revdeps - next.difference_update(total) - - # Now iterate those tasks in dependency order to regenerate their taskhash/unihash - done = set() - next = set(self.rqdata.runtaskentries[tid].revdeps) - while next: - current = next.copy() - next = set() - for tid in current: - if not self.rqdata.runtaskentries[tid].depends.isdisjoint(total): - continue - procdep = [] - for dep in self.rqdata.runtaskentries[tid].depends: - procdep.append(dep) - orighash = self.rqdata.runtaskentries[tid].hash - newhash = bb.parse.siggen.get_taskhash(tid, procdep, self.rqdata.dataCaches[mc_from_tid(tid)]) - origuni = self.rqdata.runtaskentries[tid].unihash - newuni = bb.parse.siggen.get_unihash(tid) - # FIXME, need to check it can come from sstate at all for determinism? - remapped = False - if newuni == origuni: - # Nothing to do, we match, skip code below - remapped = True - elif tid in self.scenequeue_covered or tid in self.sq_live: - # Already ran this setscene task or it running. Report the new taskhash - remapped = bb.parse.siggen.report_unihash_equiv(tid, newhash, origuni, newuni, self.rqdata.dataCaches) - logger.info("Already covered setscene for %s so ignoring rehash (remap)" % (tid)) - - if not remapped: - logger.debug(1, "Task %s hash changes: %s->%s %s->%s" % (tid, orighash, newhash, origuni, newuni)) - self.rqdata.runtaskentries[tid].hash = newhash - self.rqdata.runtaskentries[tid].unihash = newuni - changed.add(tid) - - next |= self.rqdata.runtaskentries[tid].revdeps - total.remove(tid) - next.intersection_update(total) + toprocess.add(tid) + + # Work out all tasks which depend upon these + total = set() + for p in toprocess: + next = set(self.rqdata.runtaskentries[p].revdeps) + while next: + current = next.copy() + total = total | next + next = set() + for ntid in current: + next |= self.rqdata.runtaskentries[ntid].revdeps + next.difference_update(total) + + # Now iterate those tasks in dependency order to regenerate their taskhash/unihash + next = set() + for p in total: + if len(self.rqdata.runtaskentries[p].depends) == 0: + next.add(p) + elif self.rqdata.runtaskentries[p].depends.isdisjoint(total): + next.add(p) + + # When an item doesn't have dependencies in total, we can process it. Drop items from total when handled + while next: + current = next.copy() + next = set() + for tid in current: + if not self.rqdata.runtaskentries[tid].depends.isdisjoint(total): + continue + procdep = [] + for dep in self.rqdata.runtaskentries[tid].depends: + procdep.append(dep) + orighash = self.rqdata.runtaskentries[tid].hash + newhash = bb.parse.siggen.get_taskhash(tid, procdep, self.rqdata.dataCaches[mc_from_tid(tid)]) + origuni = self.rqdata.runtaskentries[tid].unihash + newuni = bb.parse.siggen.get_unihash(tid) + # FIXME, need to check it can come from sstate at all for determinism? + remapped = False + if newuni == origuni: + # Nothing to do, we match, skip code below + remapped = True + elif tid in self.scenequeue_covered or tid in self.sq_live: + # Already ran this setscene task or it running. Report the new taskhash + remapped = bb.parse.siggen.report_unihash_equiv(tid, newhash, origuni, newuni, self.rqdata.dataCaches) + logger.info("Already covered setscene for %s so ignoring rehash (remap)" % (tid)) + + if not remapped: + #logger.debug(1, "Task %s hash changes: %s->%s %s->%s" % (tid, orighash, newhash, origuni, newuni)) + self.rqdata.runtaskentries[tid].hash = newhash + self.rqdata.runtaskentries[tid].unihash = newuni + changed.add(tid) + + next |= self.rqdata.runtaskentries[tid].revdeps + total.remove(tid) + next.intersection_update(total) if changed: for mc in self.rq.worker:
Signature · 1 line
2.20.1
PATCH 2/5 runqueue: Fix task mismatch failures from incorrect logic
The "no dependencies" task case was not being correctly considered in this code and seemed to be the cause of occasionaly task hash mismatch errors that were being seen as the dependencies were never accounted for properly.
--- lib/bb/runqueue.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/bb/runqueue.py +1 -1
@@ -2286,7 +2286,7 @@ class RunQueueExecute: current = next.copy() next = set() for tid in current: - if not self.rqdata.runtaskentries[tid].depends.isdisjoint(total): + if len(self.rqdata.runtaskentries[p].depends) and not self.rqdata.runtaskentries[tid].depends.isdisjoint(total): continue procdep = [] for dep in self.rqdata.runtaskentries[tid].depends:
Signature · 1 line
2.20.1
PATCH 3/5 siggen: Split get_tashhash for performance
In reply to Richard PurdieThere are two operations happening in get_taskhash, the building of the underlying data and the calculation of the hash.
Split these into two funtions since the preparation part doesn't need to rerun when unihash changes, only the calculation does.
This split allows sigificant performance improvements for hashequiv in builds where many hashes are equivalent and many hashes are changing.
--- lib/bb/runqueue.py | 1 + lib/bb/siggen.py | 33 ++++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-)
lib/bb/runqueue.py +1 -0
@@ -1185,6 +1185,7 @@ class RunQueueData: procdep = [] for dep in self.runtaskentries[tid].depends: procdep.append(dep) + bb.parse.siggen.prep_taskhash(tid, procdep, self.dataCaches[mc_from_tid(tid)]) self.runtaskentries[tid].hash = bb.parse.siggen.get_taskhash(tid, procdep, self.dataCaches[mc_from_tid(tid)]) self.runtaskentries[tid].unihash = bb.parse.siggen.get_unihash(tid)
lib/bb/siggen.py +24 -9
@@ -52,6 +52,9 @@ class SignatureGenerator(object): def get_unihash(self, tid): return self.taskhash[tid] + def prep_taskhash(self, tid, deps, dataCache): + return + def get_taskhash(self, tid, deps, dataCache): self.taskhash[tid] = hashlib.sha256(tid.encode("utf-8")).hexdigest() return self.taskhash[tid]
@@ -198,12 +201,11 @@ class SignatureGeneratorBasic(SignatureGenerator): pass return taint - def get_taskhash(self, tid, deps, dataCache): + def prep_taskhash(self, tid, deps, dataCache): (mc, _, task, fn) = bb.runqueue.split_tid_mcfn(tid) - data = dataCache.basetaskhash[tid] - self.basehash[tid] = data + self.basehash[tid] = dataCache.basetaskhash[tid] self.runtaskdeps[tid] = [] self.file_checksum_values[tid] = [] recipename = dataCache.pkg_fn[fn]
@@ -216,7 +218,6 @@ class SignatureGeneratorBasic(SignatureGenerator): continue if dep not in self.taskhash: bb.fatal("%s is not in taskhash, caller isn't calling in dependency order?" % dep) - data = data + self.get_unihash(dep) self.runtaskdeps[tid].append(dep) if task in dataCache.file_checksums[fn]:
@@ -226,27 +227,41 @@ class SignatureGeneratorBasic(SignatureGenerator): checksums = bb.fetch2.get_file_checksums(dataCache.file_checksums[fn][task], recipename) for (f,cs) in checksums: self.file_checksum_values[tid].append((f,cs)) - if cs: - data = data + cs taskdep = dataCache.task_deps[fn] if 'nostamp' in taskdep and task in taskdep['nostamp']: # Nostamp tasks need an implicit taint so that they force any dependent tasks to run if tid in self.taints and self.taints[tid].startswith("nostamp:"): # Don't reset taint value upon every call - data = data + self.taints[tid][8:] + pass else: import uuid taint = str(uuid.uuid4()) - data = data + taint self.taints[tid] = "nostamp:" + taint taint = self.read_taint(fn, task, dataCache.stamp[fn]) if taint: - data = data + taint self.taints[tid] = taint logger.warning("%s is tainted from a forced run" % tid) + return + + def get_taskhash(self, tid, deps, dataCache): + + data = self.basehash[tid] + for dep in self.runtaskdeps[tid]: + data = data + self.get_unihash(dep) + + for (f, cs) in self.file_checksum_values[tid]: + if cs: + data = data + cs + + if tid in self.taints: + if self.taints[tid].startswith("nostamp:"): + data = data + self.taints[tid][8:] + else: + data = data + self.taints[tid] + h = hashlib.sha256(data.encode("utf-8")).hexdigest() self.taskhash[tid] = h #d.setVar("BB_TASKHASH_task-%s" % task, taskhash[task])
Signature · 1 line
2.20.1
PATCH 4/5 runqueue: Fix sstate task iteration performance
In reply to Richard PurdieCreating a new sorted list of sstate tasks each iteration through runqueue is extremely ineffecient and was compounded by the recent change from a list to set.
Create one sorted list instead of recreating it each time.
--- lib/bb/runqueue.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
lib/bb/runqueue.py +5 -1
@@ -1965,10 +1965,14 @@ class RunQueueExecute: self.rq.read_workers() self.process_possible_migrations() + if not hasattr(self, "sorted_setscene_tids"): + # Don't want to sort this set every execution + self.sorted_setscene_tids = sorted(self.rqdata.runq_setscene_tids) + task = None if not self.sqdone and self.can_start_task(): # Find the next setscene to run - for nexttask in sorted(self.rqdata.runq_setscene_tids): + for nexttask in self.sorted_setscene_tids: if nexttask in self.sq_buildable and nexttask not in self.sq_running and self.sqdata.stamps[nexttask] not in self.build_stamps.values(): if nexttask not in self.sqdata.unskippable and len(self.sqdata.sq_revdeps[nexttask]) > 0 and self.sqdata.sq_revdeps[nexttask].issubset(self.scenequeue_covered) and self.check_dependencies(nexttask, self.sqdata.sq_revdeps[nexttask]): if nexttask not in self.rqdata.target_tids:
Signature · 1 line
2.20.1
PATCH 5/5 runqueue: Optimise task migration code slightly
In reply to Richard PurdieMove the calls to difference_update out a code level which improves efficiency significantly.
Also further combine the outer loop for efficiency too.
These two changes remove a bottleneck from the performance charts.
--- lib/bb/runqueue.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
lib/bb/runqueue.py +9 -8
@@ -2268,15 +2268,16 @@ class RunQueueExecute: # Work out all tasks which depend upon these total = set() + next = set() for p in toprocess: - next = set(self.rqdata.runtaskentries[p].revdeps) - while next: - current = next.copy() - total = total | next - next = set() - for ntid in current: - next |= self.rqdata.runtaskentries[ntid].revdeps - next.difference_update(total) + next |= self.rqdata.runtaskentries[p].revdeps + while next: + current = next.copy() + total = total | next + next = set() + for ntid in current: + next |= self.rqdata.runtaskentries[ntid].revdeps + next.difference_update(total) # Now iterate those tasks in dependency order to regenerate their taskhash/unihash next = set()
Signature · 1 line
2.20.1