1# Copyright (c) 2012 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""The Deep Memory Profiler analyzer script.
6
7See http://dev.chromium.org/developers/deep-memory-profiler for details.
8"""
9
10import logging
11import sys
12
13from lib.exceptions import ParsingException
14import subcommands
15
16
17LOGGER = logging.getLogger('dmprof')
18
19
20def main():
21  COMMANDS = {
22    'buckets': subcommands.BucketsCommand,
23    'cat': subcommands.CatCommand,
24    'csv': subcommands.CSVCommand,
25    'expand': subcommands.ExpandCommand,
26    'json': subcommands.JSONCommand,
27    'list': subcommands.ListCommand,
28    'map': subcommands.MapCommand,
29    'pprof': subcommands.PProfCommand,
30    'stacktrace': subcommands.StacktraceCommand,
31    'upload': subcommands.UploadCommand,
32  }
33
34  if len(sys.argv) < 2 or (not sys.argv[1] in COMMANDS):
35    sys.stderr.write("""Usage: dmprof <command> [options] [<args>]
36
37Commands:
38   buckets      Dump a bucket list with resolving symbols
39   cat          Categorize memory usage (under development)
40   csv          Classify memory usage in CSV
41   expand       Show all stacktraces contained in the specified component
42   json         Classify memory usage in JSON
43   list         Classify memory usage in simple listing format
44   map          Show history of mapped regions
45   pprof        Format the profile dump so that it can be processed by pprof
46   stacktrace   Convert runtime addresses to symbol names
47   upload       Upload dumped files
48
49Quick Reference:
50   dmprof buckets <first-dump>
51   dmprof cat <first-dump>
52   dmprof csv [-p POLICY] <first-dump>
53   dmprof expand <dump> <policy> <component> <depth>
54   dmprof json [-p POLICY] <first-dump>
55   dmprof list [-p POLICY] <first-dump>
56   dmprof map <first-dump> <policy>
57   dmprof pprof [-c COMPONENT] <dump> <policy>
58   dmprof stacktrace <dump>
59   dmprof upload [--gsutil path/to/gsutil] <first-dump> <destination-gs-path>
60""")
61    sys.exit(1)
62  action = sys.argv.pop(1)
63
64  LOGGER.setLevel(logging.DEBUG)
65  handler = logging.StreamHandler()
66  handler.setLevel(logging.INFO)
67  formatter = logging.Formatter('%(message)s')
68  handler.setFormatter(formatter)
69  LOGGER.addHandler(handler)
70
71  try:
72    errorcode = COMMANDS[action]().do(sys.argv)
73  except ParsingException, e:
74    errorcode = 1
75    sys.stderr.write('Exit by parsing error: %s\n' % e)
76
77  return errorcode
78
79
80if __name__ == '__main__':
81  sys.exit(main())
82