Asterisk - The Open Source Telephony Project  18.5.0
reflocks.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 """Process a ref debug log for lock usage
3 
4  This file will process a log file created by Asterisk
5  that was compiled with REF_DEBUG and DEBUG_THREADS.
6 
7  See http://www.asterisk.org for more information about
8  the Asterisk project. Please do not directly contact
9  any of the maintainers of this project for assistance;
10  the project provides a web site, mailing lists and IRC
11  channels for your use.
12 
13  This program is free software, distributed under the terms of
14  the GNU General Public License Version 2. See the LICENSE file
15  at the top of the source tree.
16 
17  Copyright (C) 2018, CFWare, LLC
18  Corey Farrell <[email protected]>
19 """
20 
21 from __future__ import print_function
22 import sys
23 import os
24 
25 from optparse import OptionParser
26 
27 
28 def process_file(options):
29  """The routine that kicks off processing a ref file"""
30 
31  object_types = {}
32  objects = {}
33  filename = options.filepath
34 
35  with open(filename, 'r') as ref_file:
36  for line in ref_file:
37  if 'constructor' not in line and 'destructor' not in line:
38  continue
39  # The line format is:
40  # addr,delta,thread_id,file,line,function,state,tag
41  # Only addr, file, line, function, state are used by reflocks.py
42  tokens = line.strip().split(',', 7)
43  addr = tokens[0]
44  state = tokens[6]
45  if 'constructor' in state:
46  obj_type = '%s:%s:%s' % (tokens[3], tokens[4], tokens[5])
47  if obj_type not in object_types:
48  object_types[obj_type] = {
49  'used': 0,
50  'unused': 0,
51  'none': 0
52  }
53  objects[addr] = obj_type
54  elif 'destructor' in state:
55  if addr not in objects:
56  # This error would be reported by refcounter.py.
57  continue
58  obj_type = objects[addr]
59  del objects[addr]
60  if '**lock-state:unused**' in state:
61  object_types[obj_type]['unused'] += 1
62  elif '**lock-state:used**' in state:
63  object_types[obj_type]['used'] += 1
64  elif '**lock-state:none**' in state:
65  object_types[obj_type]['none'] += 1
66 
67  for (allocator, info) in object_types.items():
68  stats = [];
69  if info['used'] > 0:
70  if not options.used:
71  continue
72  stats.append("%d used" % info['used'])
73  if info['unused'] > 0:
74  stats.append("%d unused" % info['unused'])
75  if info['none'] > 0 and options.none:
76  stats.append("%d none" % info['none'])
77  if len(stats) == 0:
78  continue
79  print("%s: %s" % (allocator, ', '.join(stats)))
80 
81 
82 def main(argv=None):
83  """Main entry point for the script"""
84 
85  ret_code = 0
86 
87  if argv is None:
88  argv = sys.argv
89 
90  parser = OptionParser()
91 
92  parser.add_option("-f", "--file", action="store", type="string",
93  dest="filepath", default="/var/log/asterisk/refs",
94  help="The full path to the refs file to process")
95  parser.add_option("-u", "--suppress-used", action="store_false",
96  dest="used", default=True,
97  help="Don't output types that have used locks.")
98  parser.add_option("-n", "--show-none", action="store_true",
99  dest="none", default=False,
100  help="Show counts of objects with no locking.")
101 
102  (options, args) = parser.parse_args(argv)
103 
104  if not os.path.isfile(options.filepath):
105  print("File not found: %s" % options.filepath, file=sys.stderr)
106  return -1
107 
108  try:
109  process_file(options)
110  except (KeyboardInterrupt, SystemExit, IOError):
111  print("File processing cancelled", file=sys.stderr)
112  return -1
113 
114  return ret_code
115 
116 
117 if __name__ == "__main__":
118  sys.exit(main(sys.argv))
def process_file(options)
Definition: reflocks.py:28
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
def main(argv=None)
Definition: reflocks.py:82