File: trace_processor.py

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (401 lines) | stat: -rw-r--r-- 13,404 bytes parent folder | download | duplicates (4)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# Copyright 2019 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import csv
import json
import logging
import os
import subprocess
import tempfile
import threading

from collections import namedtuple

from core.perfetto_binary_roller import binary_deps_manager
from py_utils import tempfile_ext
from tracing.value import histogram_set
from tracing.value.histogram import Histogram

TP_BINARY_NAME = 'trace_processor_shell'
EXPORT_JSON_QUERY_TEMPLATE = 'select export_json(%s)\n'
METRICS_PATH = os.path.realpath(
    os.path.join(os.path.dirname(__file__), 'metrics'))
POWER_PROFILE_SQL = 'power_profile.sql'

MetricFiles = namedtuple('MetricFiles', ('sql', 'proto', 'internal_metric'))


class InvalidTraceProcessorOutput(Exception):
  pass


# These will be set to respective paths once the files have been fetched
# to avoid downloading several times during one Results Processor run.
_fetched_trace_processor = None
_fetched_power_profile = None
_fetch_lock = threading.Lock()


def _SqlString(s):
  """Produce a valid SQL string constant."""
  return "'%s'" % s.replace("'", "''")


def _EnsureTraceProcessor(trace_processor_path):
  global _fetched_trace_processor

  if trace_processor_path is None:
    with _fetch_lock:
      if not _fetched_trace_processor:
        _fetched_trace_processor = binary_deps_manager.FetchHostBinary(
            TP_BINARY_NAME)
        logging.info('Trace processor binary downloaded to %s',
                     _fetched_trace_processor)
    trace_processor_path = _fetched_trace_processor

  if not os.path.isfile(trace_processor_path):
    raise RuntimeError("Can't find trace processor executable at %s" %
                       trace_processor_path)
  return trace_processor_path


def _EnsurePowerProfile():
  global _fetched_power_profile
  with _fetch_lock:
    if not _fetched_power_profile:
      _fetched_power_profile = binary_deps_manager.FetchDataFile(
          POWER_PROFILE_SQL)
      logging.info('Device power profiles downloaded to %s',
                   _fetched_power_profile)
  return _fetched_power_profile


def _RunTraceProcessor(*args):
  """Run trace processor shell with given command line arguments."""
  # Unset environment variables that are not needed for current trace processor
  # use cases, but, if set, can interfere with its execution.
  custom_environment = os.environ.copy()
  custom_environment.pop('PERFETTO_BINARY_PATH', None)
  custom_environment.pop('PERFETTO_SYMBOLIZER_MODE', None)
  p = subprocess.Popen(args,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE,
                       env=custom_environment)
  stdout, stderr = p.communicate()
  stdout = stdout.decode('utf-8')
  stderr = stderr.decode('utf-8')
  if p.returncode == 0:
    return stdout
  raise RuntimeError(
      'Running trace processor failed. Command line:\n%s\nStderr:\n%s\n' %
      (' '.join(args), stderr))


def _CreateMetricFiles(metric_name):
  # Currently assuming all metric files live in tbmv3/metrics directory unless
  # the metrics are compiled into trace processor. We will revise this decision
  # later.
  sql_file = os.path.join(METRICS_PATH, metric_name + '.sql')
  proto_file = os.path.join(METRICS_PATH, metric_name + '.proto')
  internal_metric = False
  if not (os.path.isfile(sql_file) and os.path.isfile(proto_file)):
    # Metric files not found - metric may be compiled into trace processor.
    internal_metric = True
  return MetricFiles(sql=sql_file,
                     proto=proto_file,
                     internal_metric=internal_metric)


def _ScopedHistogramName(metric_name, histogram_name):
  """Returns scoped histogram name by preprending metric name.

  This is useful for avoiding histogram name collision. The '_metric' suffix of
  the metric name is dropped from scoped name. Example:
  _ScopedHistogramName("console_error_metric", "js_errors")
  => "console_error::js_errors"
  """
  metric_suffix = '_metric'
  suffix_length = len(metric_suffix)
  # TODO(crbug.com/40102479): Decide on whether metrics should always have
  # '_metric' suffix.
  if metric_name[-suffix_length:] == metric_suffix:
    scope = metric_name[:-suffix_length]
  else:
    scope = metric_name
  return '::'.join([scope, histogram_name])


class ProtoFieldInfo(object):

  def __init__(self, name, parent, repeated, field_options):
    self.name = name
    self.parent = parent
    self.repeated = repeated
    self.field_options = field_options

  @property
  def path_from_root(self):
    if self.parent is None:
      return [self]
    return self.parent.path_from_root + [self]

  def __repr__(self):
    return 'ProtoFieldInfo("%s", repeated=%s)' % (self.name, self.repeated)


def _LeafFieldAnnotations(annotations, parent=None):
  """Yields leaf fields in the annotations tree, yielding a proto field info

  each time. Given the following annotation:
  __annotations: {
    a: {
      __field_options: { },
      b: { __field_options: { unit: "count" }, __repeated: True },
      c: { __field_options: { unit: "ms" } }
    }
  }
  It yields:
  ProtoFieldInfo(name="b", parent=FieldInfoForA,
                 repeated=True, field_options={unit: "count"})
  ProtoFieldInfo(name="c", parent=FieldInfoForA,
                 repeated=False, field_options={unit: "ms"})

  """
  for (name, field_value) in annotations.items():
    if name[:2] == '__':
      continue  # internal fields.
    current_field = ProtoFieldInfo(
        name=name,
        parent=parent,
        repeated=field_value.get('__repeated', False),
        field_options=field_value.get('__field_options', {}))
    has_no_descendants = True
    for descendant in _LeafFieldAnnotations(field_value, current_field):
      has_no_descendants = False
      yield descendant
    if has_no_descendants:
      yield current_field


def _PluckField(json_dict, field_path):
  """Returns the values of fields matching field_path from json dict.

  Field path is a sequence of ProtoFieldInfo starting from the root dict. Arrays
  are flattened along the way. For exampe, consider the following json dict:
  {
    a: {
      b: [ { c: 24 }, { c: 25 } ],
      d: 42,
    }
  }
  Field path (a, d) returns [42]. Field_path (a, b, c) returns [24, 25].
  """
  if len(field_path) == 0:
    return [json_dict]
  path_head = field_path[0]
  path_tail = field_path[1:]

  if path_head.repeated:
    field_values = json_dict[path_head.name]
    if not isinstance(field_values, list):
      raise InvalidTraceProcessorOutput(
          'Field marked as repeated but json value is not list')
    output = []
    for field_value in field_values:
      output.extend(_PluckField(field_value, path_tail))
    return output
  field_value = json_dict[path_head.name]
  if isinstance(field_value, list):
    raise InvalidTraceProcessorOutput(
        'Field not marked as repeated but json value is list')
  return _PluckField(field_value, path_tail)


def RunQuery(trace_processor_path, trace_file, sql_command):
  """Run SQL query on trace using trace processor and return result.

  Args:
    trace_processor_path: path to the trace_processor executable.
    trace_file: path to the trace file.
    sql_command: string SQL command

  Returns:
    SQL query output table when executed on the proto trace as a
    list of dictionaries. Each item in the list represents a row
    in the output table. All values in the dictionary are
    represented as strings. Null is represented as None.
    Booleans are represented as '0' and '1'. Empty queries
    or rows return [].

    For example, for a SQL output table that looks like this:
      | "string_col" | "long_col" | "double_col" | "bool_col" | "maybe_null_col"
      | "StringVal1" |  123       | 12.34        | true       | "[NULL]"
      | "StringVal2" |  124       | 34.56        | false      |  25
      | "StringVal3" |  125       | 68.92        | false      | "[NULL]"

    The list of dictionaries result will look like this:
      [{
        'string_col': 'StringVal1',
        'long_col': '123',
        'double_col': '12.34',
        'bool_col': '1',
        'maybe_null_col': None,
      }, {
        'string_col': 'StringVal2',
        'long_col': '124',
        'double_col': '34.56',
        'bool_col': '0',
        'maybe_null_col': '25',
      }, {
        'string_col': 'StringVal3',
        'long_col': '125',
        'double_col': '68.92',
        'bool_col': '0',
        'maybe_null_col': None,
      }]
  """
  trace_processor_path = _EnsureTraceProcessor(trace_processor_path)

  # Write query to temporary file because trace processor accepts
  # SQL query in a file.
  tp_output = None
  with tempfile_ext.NamedTemporaryFile(mode='w+') as sql_file:
    sql_file.write(sql_command)
    sql_file.close()
    # Run Trace Processor
    command_args = [
        trace_processor_path,
        '--query-file',
        sql_file.name,
        trace_file,
    ]
    tp_output = _RunTraceProcessor(*command_args)

  # Trace Processor returns output string in csv format. Write
  # string to temporary file because reader accepts csv files.
  # Parse csv file into list of dictionaries because DictReader
  # object inconveniently requires open csv file to access data.
  csv_output = []
  # tempfile creates and opens the file
  with tempfile.NamedTemporaryFile(mode='w+') as csv_file:
    csv_file.write(tp_output)
    csv_file.flush()
    csv_file.seek(0)
    csv_reader = csv.DictReader(csv_file)
    for row in csv_reader:
      # CSV file represents null values as the string '[NULL]'.
      # Parse these null values to None type.
      row_parsed = dict(row)
      for key, val in row_parsed.items():
        if val == '[NULL]':
          row_parsed[key] = None
      csv_output.append(row_parsed)

  return csv_output


def RunMetrics(trace_processor_path,
               trace_file,
               metric_names,
               fetch_power_profile=False,
               retain_all_samples=False):
  """Run TBMv3 metrics using trace processor.

  Args:
    trace_processor_path: path to the trace_processor executable.
    trace_file: path to the trace file.
    metric_names: a list of metric names (the corresponding files must exist in
      tbmv3/metrics directory).

  Returns:
    A HistogramSet with metric results.
  """
  trace_processor_path = _EnsureTraceProcessor(trace_processor_path)
  metric_name_args = []
  for metric_name in metric_names:
    metric_files = _CreateMetricFiles(metric_name)
    if metric_files.internal_metric:
      metric_name_args.append(metric_name)
    else:
      metric_name_args.append(metric_files.sql)
  command_args = [
      trace_processor_path,
      '--run-metrics',
      ','.join(metric_name_args),
      '--metrics-output',
      'json',
      trace_file,
  ]
  if fetch_power_profile:
    command_args[1:1] = ['--pre-metrics', _EnsurePowerProfile()]

  output = _RunTraceProcessor(*command_args)
  measurements = json.loads(output)

  histograms = histogram_set.HistogramSet()
  root_annotations = measurements.get('__annotations', {})
  for metric_name in metric_names:
    full_metric_name = 'perfetto.protos.' + metric_name
    annotations = root_annotations.get(full_metric_name, None)
    metric_proto = measurements.get(full_metric_name, None)
    if metric_proto is None:
      logging.warning('Metric not found in the output: %s', metric_name)
      continue
    if annotations is None:
      logging.info('Skipping metric %s because it has no field with unit.',
                   metric_name)
      continue

    for field in _LeafFieldAnnotations(annotations):
      unit = field.field_options.get('unit', None)
      if unit is None:
        logging.debug('Skipping field %s to histograms because it has no unit',
                      field.name)
        continue
      histogram_name = ':'.join([field.name for field in field.path_from_root])
      samples = _PluckField(metric_proto, field.path_from_root)
      scoped_histogram_name = _ScopedHistogramName(metric_name, histogram_name)
      hist = Histogram(scoped_histogram_name, unit)
      if retain_all_samples:
        hist.max_num_sample_values = float('inf')
      for sample in samples:
        hist.AddSample(sample)
      histograms.AddHistogram(hist)
  return histograms


def RunMetric(trace_processor_path,
              trace_file,
              metric_name,
              fetch_power_profile=False,
              retain_all_samples=False):
  return RunMetrics(trace_processor_path,
                    trace_file, [metric_name],
                    fetch_power_profile=fetch_power_profile,
                    retain_all_samples=retain_all_samples)


def ConvertProtoTraceToJson(trace_processor_path, proto_file, json_path):
  """Convert proto trace to json using trace processor.

  Args:
    trace_processor_path: path to the trace_processor executable.
    proto_file: path to the proto trace file.
    json_path: path to the output file.

  Returns:
    Output path.
  """
  trace_processor_path = _EnsureTraceProcessor(trace_processor_path)
  with tempfile_ext.NamedTemporaryFile(mode='w+') as query_file:
    query_file.write(EXPORT_JSON_QUERY_TEMPLATE % _SqlString(json_path))
    query_file.close()
    _RunTraceProcessor(
        trace_processor_path,
        '-q',
        query_file.name,
        proto_file,
    )

  return json_path