Пример #1
0
def TracerMain_(log, storage_path, backend_name, device_id, pid, interval,
    count, trace_native_heap):
  """Entry point for the background periodic tracer task."""
  # Initialize storage.
  storage = file_storage.Storage(storage_path)

  # Initialize the backend.
  backend = backends.GetBackend(backend_name)
  for k, v in storage.LoadSettings(backend_name).iteritems():
    backend.settings[k] = v

  # Initialize the device.
  device = backends.GetDevice(backend_name, device_id)
  for k, v in storage.LoadSettings(device_id).iteritems():
    device.settings[k] = v

  # Start periodic tracing.
  process = device.GetProcess(pid)
  log.put((1, 'Starting trace (%d dumps x %s s.). Device: %s, process: %s' % (
      count, interval, device.name, process.name)))
  datetime_str = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M')
  archive_name = '%s - %s - %s' % (datetime_str, device.name, process.name)
  archive = storage.OpenArchive(archive_name, create=True)
  heaps_to_symbolize = []

  for i in xrange(1, count + 1):  # [1, count] range is easier to handle.
    process = device.GetProcess(pid)
    if not process:
      log.put((100, 'Process %d died.' % pid))
      return 1
    # Calculate the completion rate proportionally to 80%. We keep the remaining
    # 20% for the final symbolization step (just an approximate estimation).
    completion = 80 * i / count
    log.put((completion, 'Dumping trace %d of %d' % (i, count)))
    archive.StartNewSnapshot()
    mmaps = process.DumpMemoryMaps()
    log.put((completion, 'Dumped %d memory maps' % len(mmaps)))
    archive.StoreMemMaps(mmaps)
    if trace_native_heap:
      nheap = process.DumpNativeHeap()
      log.put((completion, 'Dumped %d native allocs' % len(nheap.allocations)))
      archive.StoreNativeHeap(nheap)
      heaps_to_symbolize += [nheap]

    if i < count:
      time.sleep(interval)

  log.put((90, 'Symbolizing'))
  symbols = backend.ExtractSymbols(heaps_to_symbolize,
                                   device.settings['native_symbol_paths'] or '')

  expected_symbols_count = len(set.union(
      *[set(x.stack_frames.iterkeys()) for x in heaps_to_symbolize]))
  log.put((99, 'Symbolization complete. Got %d symbols (%.1f%%).' % (
      len(symbols), 100.0 * len(symbols) / expected_symbols_count)))

  archive.StoreSymbols(symbols)

  log.put((100, 'Trace complete.'))
  return 0
Пример #2
0
def _SetDeviceOrBackendSettings(args, req_vars):  # pylint: disable=W0613
    backend = backends.GetBackend(args[0])
    if not backend:
        return _HTTP_GONE, [], 'Backend not found'
    if args[1]:
        device = _GetDevice(args)
        if not device:
            return _HTTP_GONE, [], 'Device not found'
        settings = device.settings
        storage_name = device.id
    else:
        settings = backend.settings
        storage_name = backend.name

    for key in req_vars.iterkeys():
        settings[key] = req_vars[key]
    _persistent_storage.StoreSettings(storage_name, settings.values)
    return _HTTP_OK, [], ''
Пример #3
0
def _GetDeviceOrBackendSettings(args, req_vars):  # pylint: disable=W0613
    backend = backends.GetBackend(args[0])
    if not backend:
        return _HTTP_GONE, [], 'Backend not found'
    if args[1]:
        device = _GetDevice(args)
        if not device:
            return _HTTP_GONE, [], 'Device not found'
        settings = device.settings
    else:
        settings = backend.settings

    assert (isinstance(settings, backends.Settings))
    resp = {}
    for key in settings.expected_keys:
        resp[key] = {
            'description': settings.expected_keys[key],
            'value': settings.values[key]
        }
    return _HTTP_OK, [], resp