• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Copyright (C) 2020 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import argparse
17
18from perfetto.trace_processor import TraceProcessor
19
20
21def main():
22  # Parse arguments passed from command line
23  parser = argparse.ArgumentParser()
24  parser.add_argument(
25      "-a",
26      "--address",
27      help="Address at which trace_processor is being run, e.g. localhost:9001",
28      type=str)
29  parser.add_argument(
30      "-b",
31      "--binary",
32      help="Absolute path to a trace processor binary",
33      type=str)
34  parser.add_argument("-f", "--file", help="Absolute path to trace", type=str)
35  args = parser.parse_args()
36
37  # Pass arguments into api to construct the trace processor and load the trace
38  if args.address is None and args.file is None:
39    raise Exception("You must specify an address or a file path to trace")
40  elif args.address is None:
41    tp = TraceProcessor(file_path=args.file, bin_path=args.binary)
42  elif args.file is None:
43    tp = TraceProcessor(addr=args.address)
44  else:
45    tp = TraceProcessor(
46        addr=args.address, file_path=args.file, bin_path=args.binary)
47
48  # Iterate through QueryResultIterator
49  res_it = tp.query('select * from slice limit 10')
50  for row in res_it:
51    print(row.name)
52
53  # Convert QueryResultIterator into a pandas dataframe + iterate. This yields
54  # the same results as the function above.
55  try:
56    res_df = tp.query('select * from slice limit 10').as_pandas_dataframe()
57    for index, row in res_df.iterrows():
58      print(row['name'])
59  except Exception:
60    pass
61
62  # Call another function on the loaded trace
63  am_metrics = tp.metric(['android_mem'])
64  tp.close()
65
66
67if __name__ == "__main__":
68  main()
69