1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7# Summarize sstate usage at the end of the build
8python buildstats_summary () {
9    import collections
10    import os.path
11
12    bsdir = e.data.expand("${BUILDSTATS_BASE}/${BUILDNAME}")
13    if not os.path.exists(bsdir):
14        return
15
16    sstatetasks = (e.data.getVar('SSTATETASKS') or '').split()
17    built = collections.defaultdict(lambda: [set(), set()])
18    for pf in os.listdir(bsdir):
19        taskdir = os.path.join(bsdir, pf)
20        if not os.path.isdir(taskdir):
21            continue
22
23        tasks = os.listdir(taskdir)
24        for t in sstatetasks:
25            no_sstate, sstate = built[t]
26            if t in tasks:
27                no_sstate.add(pf)
28            elif t + '_setscene' in tasks:
29                sstate.add(pf)
30
31    header_printed = False
32    for t in sstatetasks:
33        no_sstate, sstate = built[t]
34        if no_sstate | sstate:
35            if not header_printed:
36                header_printed = True
37                bb.note("Build completion summary:")
38
39            sstate_count = len(sstate)
40            no_sstate_count = len(no_sstate)
41            total_count = sstate_count + no_sstate_count
42            bb.note("  {0}: {1:.1f}% sstate reuse({2} setscene, {3} scratch)".format(
43                t, round(100 * sstate_count / total_count, 1), sstate_count, no_sstate_count))
44}
45addhandler buildstats_summary
46buildstats_summary[eventmask] = "bb.event.BuildCompleted"
47