def _accumulate_event_duration()

in utils/json_profiles_merger_lib.py [0:0]


def _accumulate_event_duration(event_list, accum_dict, only_phases=False):
  """Fill up accum_dict by accummulating durations of each event.

  Also create the entries for each phase by subtracting the build phase markers'
  ts attribute.
  Args:
    event_list: the list of event objects.
    accum_dict: the dict to be filled up with a mapping of the following format:
      { <name>: { name: ..., cat: ..., dur_list: [...]}, ...}
    only_phases: only collect entries from phase markers.
  """
  # A list of tuples of the form (marker, occurrence time in micro s)
  build_markers_ts_pairs = []
  max_ts = 0

  # Only collect events with a duration.
  # Special case: markers that indicates beginning/end of execution.
  for event in event_list:
    if 'ts' in event:
      max_ts = max(max_ts, event['ts'])

    if 'cat' in event and event['cat'] == 'build phase marker':
      build_markers_ts_pairs.append((event['name'], event['ts']))

    if 'dur' not in event:
      continue

    if not only_phases:
      if event['name'] not in accum_dict:
        accum_dict[event['name']] = {
            'name': event['name'],
            'cat': event['cat'],
            'dur_list': []
        }
      accum_dict[event['name']]['dur_list'].append(event['dur'])

  # Append an artificial marker that signifies the end of the run.
  # This is to determine the duration from the last marker to the actual end of
  # the run and will not end up in the final data.
  build_markers_ts_pairs.append((None, max_ts))

  # Fill in the markers.
  for i, marker_ts_pair in enumerate(build_markers_ts_pairs[:-1]):
    marker, ts = marker_ts_pair
    _, next_ts = build_markers_ts_pairs[i + 1]

    if marker not in accum_dict:
      accum_dict[marker] = {
          'name': marker,
          'cat': 'build phase marker',
          'dur_list': []
      }
    current_phase_duration_millis = (
        next_ts - ts) / 1000  # Convert from microseconds to milliseconds
    accum_dict[marker]['dur_list'].append(current_phase_duration_millis)