astobj2: Record lock usage to refs log when DEBUG_THREADS is enabled.

When DEBUG_THREADS is enabled we can know if the astobj2 mutex / rwlock
was ever used, so it can be recorded in the REF_DEBUG destructor entry.

Create contrib/scripts/reflocks.py to process locking used by
allocator.  This can be used to identify places where
AO2_ALLOC_OPT_LOCK_NOLOCK should be used to reduce memory usage.

Change-Id: I2e3cd23336a97df2692b545f548fd79b14b53bf4
This commit is contained in:
Corey Farrell
2018-10-01 00:11:44 -04:00
parent 5a9230eacb
commit 693e00eee6
3 changed files with 157 additions and 2 deletions

View File

@@ -108,6 +108,10 @@ def process_file(options):
else:
current_objects[obj]['curcount'] += int(parsed_line['delta'])
if 'destructor' in parsed_line['state']:
# refcounter.py doesn't care about lock-state.
parsed_line['state'] = '**destructor**'
current_objects[obj]['log'].append(
"[%s] %s:%s %s: %s %s - [%s]" % (
parsed_line['thread_id'],

126
contrib/scripts/reflocks.py Executable file
View File

@@ -0,0 +1,126 @@
#!/usr/bin/env python
"""Process a ref debug log for lock usage
This file will process a log file created by Asterisk
that was compiled with REF_DEBUG and DEBUG_THREADS.
See http://www.asterisk.org for more information about
the Asterisk project. Please do not directly contact
any of the maintainers of this project for assistance;
the project provides a web site, mailing lists and IRC
channels for your use.
This program is free software, distributed under the terms of
the GNU General Public License Version 2. See the LICENSE file
at the top of the source tree.
Copyright (C) 2018, CFWare, LLC
Corey Farrell <git@cfware.com>
"""
from __future__ import print_function
import sys
import os
from optparse import OptionParser
def process_file(options):
"""The routine that kicks off processing a ref file"""
object_types = {}
objects = {}
filename = options.filepath
with open(filename, 'r') as ref_file:
for line in ref_file:
if 'constructor' not in line and 'destructor' not in line:
continue
# The line format is:
# addr,delta,thread_id,file,line,function,state,tag
# Only addr, file, line, function, state are used by reflocks.py
tokens = line.strip().split(',', 7)
addr = tokens[0]
state = tokens[6]
if 'constructor' in state:
obj_type = '%s:%s:%s' % (tokens[3], tokens[4], tokens[5])
if obj_type not in object_types:
object_types[obj_type] = {
'used': 0,
'unused': 0,
'lockobj': 0,
'none': 0
}
objects[addr] = obj_type
elif 'destructor' in state:
if addr not in objects:
# This error would be reported by refcounter.py.
continue
obj_type = objects[addr]
del objects[addr]
if '**lock-state:unused**' in state:
object_types[obj_type]['unused'] += 1
elif '**lock-state:used**' in state:
object_types[obj_type]['used'] += 1
elif '**lock-state:lockobj**' in state:
object_types[obj_type]['lockobj'] += 1
elif '**lock-state:none**' in state:
object_types[obj_type]['none'] += 1
for (allocator, info) in object_types.items():
stats = [];
if info['used'] > 0:
if not options.used:
continue
stats.append("%d used" % info['used'])
if info['unused'] > 0:
stats.append("%d unused" % info['unused'])
if info['lockobj'] > 0 and options.lockobj:
stats.append("%d lockobj" % info['lockobj'])
if info['none'] > 0 and options.none:
stats.append("%d none" % info['none'])
if len(stats) == 0:
continue
print("%s: %s" % (allocator, ', '.join(stats)))
def main(argv=None):
"""Main entry point for the script"""
ret_code = 0
if argv is None:
argv = sys.argv
parser = OptionParser()
parser.add_option("-f", "--file", action="store", type="string",
dest="filepath", default="/var/log/asterisk/refs",
help="The full path to the refs file to process")
parser.add_option("-u", "--suppress-used", action="store_false",
dest="used", default=True,
help="Don't output types that have used locks.")
parser.add_option("-n", "--show-none", action="store_true",
dest="none", default=False,
help="Show counts of objects with no locking.")
parser.add_option("-o", "--show-lockobj", action="store_true",
dest="lockobj", default=False,
help="Show counts of objects with a lockobj.")
(options, args) = parser.parse_args(argv)
if not os.path.isfile(options.filepath):
print("File not found: %s" % options.filepath, file=sys.stderr)
return -1
try:
process_file(options)
except (KeyboardInterrupt, SystemExit, IOError):
print("File processing cancelled", file=sys.stderr)
return -1
return ret_code
if __name__ == "__main__":
sys.exit(main(sys.argv))