summaryrefslogtreecommitdiff
path: root/site_scons/site_tools/build_metrics/__init__.py
blob: c0786ddef84444eb8dc2c712da3cd30743603575 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright 2020 MongoDB Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
"""Configure the build to track build performance."""

import atexit
import json
import os
import sys
import time
from timeit import default_timer as timer

from jsonschema import validate
import psutil

from .util import add_meta_data, get_build_metric_dict, CaptureAtexits
from .memory import MemoryMonitor
from .per_action_metrics import PerActionMetrics
from .artifacts import CollectArtifacts
from .scons import SConsStats
from .cache_dir import CacheDirCollector, CacheDirValidateWithMetrics
from .libdeps import LibdepsCollector

_SEC_TO_NANOSEC_FACTOR = 1000000000.0
_METRICS_COLLECTORS = []


def finalize_build_metrics(env):
    metrics = get_build_metric_dict()
    metrics['end_time'] = time.time_ns()
    for m in _METRICS_COLLECTORS:
        start_time = timer()
        sys.stdout.write(f"Processing {m.get_name()}...")
        sys.stdout.flush()
        key, value = m.finalize()
        sys.stdout.write(f" {round(timer() - start_time, 2)}s\n")
        metrics[key] = value

    with open(os.path.join(os.path.dirname(__file__), "build_metrics_format.schema")) as f:
        validate(metrics, json.load(f))

    build_metrics_file = env.GetOption('build-metrics')
    if build_metrics_file == '-':
        json.dump(metrics, sys.stdout, indent=4, sort_keys=True)
    else:
        with open(build_metrics_file, 'w') as f:
            json.dump(metrics, f, indent=4, sort_keys=True)
        with open(f"{os.path.splitext(build_metrics_file)[0]}-chrome-tracer.json", 'w') as f:
            json.dump(generate_chrome_tracer_json(metrics), f, indent=4)


def generate_chrome_tracer_json(metrics):
    tracer_json = {"traceEvents": []}
    job_slots = []
    task_stack = sorted(metrics['build_tasks'], reverse=True, key=lambda x: x['start_time'])

    # Chrome trace organizes tasks per pids, so if we want to have a clean layout which
    # clearly shows concurrent processes, we are creating job slots by comparing start and
    # end times, and using "pid" as the job slot identifier. job_slots are a list of chronologically
    # in order tasks. We keep a list of job slots and always check at the end of the job slot to
    # compare the lowest end time that will accommodate the next task start time. If there are no
    # job slots which can accommodate the next task, we create a new job slot. Note the job slots
    # ordering is similar to how the OS process scheduler would organize and start the processes
    # from the build, however we are reproducing this retroactively and simplistically and it
    # is not guaranteed to match exactly.
    while task_stack:
        task = task_stack.pop()
        candidates = [
            job_slot for job_slot in job_slots if job_slot[-1]['end_time'] < task['start_time']
        ]
        if candidates:
            # We need to find the best job_slot to add this next task too, so we look at the
            # end_times, the one with the lowest would have been the first one available. We just
            # arbitrarily guess the first one will be the best, then iterate to find out which
            # one is the best. We then add to the existing job_slot which best_candidate points to.
            min_end = candidates[0][-1]['end_time']
            best_candidate = candidates[0]
            for candidate in candidates:
                if candidate[-1]['end_time'] < min_end:
                    best_candidate = candidate
                    min_end = candidate[-1]['end_time']

            best_candidate.append(task)
        else:
            # None of the current job slots were available to accommodate the new task so we
            # make a new one.
            job_slots.append([task])

    for i, job_slot in enumerate(job_slots):
        for build_task in job_slot:

            tracer_json['traceEvents'].append({
                'name':
                    build_task['outputs'][0] if build_task['outputs'] else build_task['builder'],
                'cat':
                    build_task['builder'],
                'ph':
                    'X',
                'ts':
                    build_task['start_time'] / 1000.0,
                'dur': (build_task['end_time'] - build_task['start_time']) / 1000.0,
                'pid':
                    i,
                'args': {
                    "cpu": build_task['cpu_time'],
                    "mem": build_task['mem_usage'],
                },
            })

    return tracer_json


def generate(env, **kwargs):
    global _METRICS_COLLECTORS

    # This will force our at exit to the of the stack ensuring
    # that it is the last thing called when exiting.
    c = CaptureAtexits()
    atexit.unregister(c)
    for func in c.captured:
        atexit.unregister(func)
    atexit.register(finalize_build_metrics, env)
    for func in c.captured:
        atexit.register(func)

    env.AddMethod(get_build_metric_dict, "GetBuildMetricDictionary")
    env.AddMethod(add_meta_data, "AddBuildMetricsMetaData")

    metrics = get_build_metric_dict()
    p = psutil.Process(os.getpid())

    metrics['start_time'] = int(p.create_time() * _SEC_TO_NANOSEC_FACTOR)
    metrics['scons_command'] = " ".join([sys.executable] + sys.argv)

    _METRICS_COLLECTORS = [
        MemoryMonitor(psutil.Process().memory_info().vms),
        PerActionMetrics(),
        CollectArtifacts(env),
        SConsStats(),
        CacheDirCollector(),
        LibdepsCollector(env)
    ]

    env['CACHEDIR_CLASS'] = CacheDirValidateWithMetrics


def exists(env):
    return True


def options(opts):
    """
    Add command line Variables for build metrics tool.
    """
    opts.AddVariables(
        ("BUILD_METRICS_ARTIFACTS_DIR", "Path to scan for artifacts after the build has stopped."),
        ("BUILD_METRICS_BLOATY", "Path to the bloaty bin"),
    )