Beispiel #1
0
def RunMetrics(trace_processor_path, trace_file, metric_names,
               fetch_power_profile=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:
    power_profile_sql = binary_deps_manager.FetchDataFile(POWER_PROFILE_SQL)
    command_args[1:1] = ['--pre-metrics', power_profile_sql]

  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.warn("Metric not found in the output: %s", metric_name)
      continue
    elif 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)
      histograms.CreateHistogram(scoped_histogram_name, unit, samples)

  return histograms
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
Beispiel #3
0
  def testFetchDataFile(self):
    remote_path = 'remote/path/to/data'
    self.writeConfig(
        {'data_dep': {
            'remote_path': remote_path,
            'hash': '123',
        }})
    with mock.patch('py_utils.cloud_storage.Get') as get_patch:
      with mock.patch('py_utils.cloud_storage.CalculateHash') as hash_patch:
        hash_patch.return_value = '123'
        local_path = binary_deps_manager.FetchDataFile('data_dep')

    self.assertEqual(os.path.basename(local_path), 'data')
    get_patch.assert_called_once_with('chrome-telemetry', remote_path,
                                      local_path)