Skip to content

Commit

Permalink
Use json to describe dmprof policies.
Browse files Browse the repository at this point in the history
BUG=123758


Review URL: https://chromiumcodereview.appspot.com/10824104

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@150528 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
dmikurube@chromium.org committed Aug 8, 2012
1 parent 25c78cf commit 752ad23
Show file tree
Hide file tree
Showing 5 changed files with 851 additions and 14 deletions.
59 changes: 48 additions & 11 deletions tools/deep_memory_profiler/dmprof
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ POLICY_DEEP_1 = 'POLICY_DEEP_1'
# mmap regions are distincted w/ the allocation_type column.
POLICY_DEEP_2 = 'POLICY_DEEP_2'

# POLICY_DEEP_3 is in JSON format.
POLICY_DEEP_3 = 'POLICY_DEEP_3'


class EmptyDumpException(Exception):
def __init__(self, value):
Expand Down Expand Up @@ -133,10 +136,10 @@ class DelayedStaticSymbols(object):
class Rule(object):
"""Represents one matching rule in a policy file."""

def __init__(self, name, mmap, pattern):
def __init__(self, name, mmap, stacktrace_pattern):
self.name = name
self.mmap = mmap
self.condition = re.compile(pattern + r'\Z')
self.stacktrace_pattern = re.compile(stacktrace_pattern + r'\Z')


class Policy(object):
Expand Down Expand Up @@ -170,7 +173,7 @@ def get_component(rule_list, bucket, symbols):
stacktrace = ''.join(symbols[a] + ' ' for a in bucket.stacktrace).strip()

for rule in rule_list:
if bucket.mmap == rule.mmap and rule.condition.match(stacktrace):
if bucket.mmap == rule.mmap and rule.stacktrace_pattern.match(stacktrace):
bucket.component_cache = rule.name
return rule.name

Expand Down Expand Up @@ -653,18 +656,20 @@ def update_symbols(
sys.stderr.write(' All symbols resolved.\n')


def parse_policy(policy_path):
"""Parses policy file.
def parse_policy_text(policy_path):
"""Parses policy file in text format.
A policy file contains component's names and their
stacktrace pattern written in regular expression.
Those patterns are matched against each symbols of
each stacktraces in the order written in the policy file
TODO(dmikurube): Deprecate this function after a while.
Args:
policy_path: A path for a policy file.
Returns:
A list containing component's name and its regex object
A loaded policy object.
"""
with open(policy_path, mode='r') as policy_f:
policy_lines = policy_f.readlines()
Expand Down Expand Up @@ -700,7 +705,30 @@ def parse_policy(policy_path):
sys.stderr.write(' invalid heap profile policy version: %s\n' % (
policy_version))

return rule_list, policy_version, components
return Policy(rule_list, policy_version, components)


def parse_policy_json(policy_path):
"""Parses policy file in json format.
A policy file contains component's names and their
stacktrace pattern written in regular expression.
Those patterns are matched against each symbols of
each stacktraces in the order written in the policy file
Args:
policy_path: A path for a policy file.
Returns:
A loaded policy object.
"""
with open(policy_path, mode='r') as f:
policy = json.load(f)

rules = []
for rule in policy['rules']:
rules.append(Rule(
rule['name'], rule['allocator'] == 'mmap', rule['stacktrace']))
return Policy(rules, policy['version'], policy['components'])


def find_prefix(path):
Expand Down Expand Up @@ -800,18 +828,27 @@ def load_default_policies():

def load_policy(policies_dict, policy_label):
policy_file = policies_dict[policy_label]['file']
policy_format = policies_dict[policy_label]['format']
policy_path = os.path.join(os.path.dirname(__file__), policy_file)
rule_list, policy_version, components = parse_policy(policy_path)
policy = None
if policy_format == 'json':
policy = parse_policy_json(policy_path)
elif policy_format == 'text':
policy = parse_policy_text(policy_path)
else:
return None
sys.stderr.write(' %s: %s (version: %s)\n' %
(policy_label, policy_path, policy_version))
return Policy(rule_list, policy_version, components)
(policy_label, policy_path, policy.version))
return policy


def load_policies_dict(policies_dict):
sys.stderr.write('Loading policy files.\n')
policies = {}
for policy_label in policies_dict:
policies[policy_label] = load_policy(policies_dict, policy_label)
loaded_policy = load_policy(policies_dict, policy_label)
if loaded_policy:
policies[policy_label] = loaded_policy
return policies


Expand Down
9 changes: 6 additions & 3 deletions tools/deep_memory_profiler/policies.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"l0": {
"file": "policy.l0.txt"
"file": "policy.l0.json",
"format": "json"
},
"l1": {
"file": "policy.l1.txt"
"file": "policy.l1.json",
"format": "json"
},
"l2": {
"file": "policy.l2.txt"
"file": "policy.l2.json",
"format": "json"
}
}
155 changes: 155 additions & 0 deletions tools/deep_memory_profiler/policy.l0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
{
"components": [
"second",
"mmap-profiler",
"mmap-allocated-type",
"mmap-tcmalloc",
"FROM_HERE_FOR_TOTAL",
"mustbezero",
"nonprofiled-absent",
"nonprofiled-anonymous",
"nonprofiled-file-exec",
"nonprofiled-file-nonexec",
"nonprofiled-stack",
"nonprofiled-other",
"no-bucket",
"mmap-v8",
"mmap-catch-all",
"tc-used-all",
"tc-unused",
"UNTIL_HERE_FOR_TOTAL",
"total-exclude-profiler",
"total",
"anonymous",
"file-exec",
"file-nonexec",
"stack",
"other",
"mmap-total-log",
"mmap-no-log",
"mmap-total-record",
"other-total-log",
"tc-total-log",
"tc-no-log",
"tc-total-record",
"tc-total"
],
"rules": [
{
"name": "second",
"stacktrace": "optional",
"allocator": "optional"
},
{
"name": "mmap-profiler",
"stacktrace": ".*(ProfilerMalloc|MemoryRegionMap::).*",
"allocator": "mmap"
},
{
"name": "mmap-allocated-type",
"stacktrace": ".*(AllocatedTypeMalloc).*",
"allocator": "mmap"
},
{
"name": "mmap-tcmalloc",
"stacktrace": ".*(DoAllocWithArena|SbrkSysAllocator::Alloc|MmapSysAllocator::Alloc|LowLevelAlloc::Alloc|LowLevelAlloc::AllocWithArena).*",
"allocator": "mmap"
},
{
"name": "FROM_HERE_FOR_TOTAL",
"stacktrace": "optional",
"allocator": "optional"
},
{
"name": "mustbezero",
"stacktrace": "optional",
"allocator": "optional"
},
{
"name": "nonprofiled-absent",
"stacktrace": "optional",
"allocator": "optional"
},
{
"name": "nonprofiled-anonymous",
"stacktrace": "optional",
"allocator": "optional"
},
{
"name": "nonprofiled-file-exec",
"stacktrace": "optional",
"allocator": "optional"
},
{
"name": "nonprofiled-file-nonexec",
"stacktrace": "optional",
"allocator": "optional"
},
{
"name": "nonprofiled-stack",
"stacktrace": "optional",
"allocator": "optional"
},
{
"name": "nonprofiled-other",
"stacktrace": "optional",
"allocator": "optional"
},
{
"name": "mmap-v8",
"stacktrace": ".*v8::.*",
"allocator": "mmap"
},
{
"name": "mmap-catch-all",
"stacktrace": ".*",
"allocator": "mmap"
},
{
"name": "tc-used-all",
"stacktrace": ".*",
"allocator": "malloc"
},
{
"name": "UNTIL_HERE_FOR_TOTAL",
"stacktrace": "optional",
"allocator": "optional"
},
{
"name": "total-exclude-profiler",
"stacktrace": "optional",
"allocator": "optional"
},
{
"name": "total",
"stacktrace": "optional",
"allocator": "optional"
},
{
"name": "anonymous",
"stacktrace": "optional",
"allocator": "optional"
},
{
"name": "file-exec",
"stacktrace": "optional",
"allocator": "optional"
},
{
"name": "file-nonexec",
"stacktrace": "optional",
"allocator": "optional"
},
{
"name": "stack",
"stacktrace": "optional",
"allocator": "optional"
},
{
"name": "other",
"stacktrace": "optional",
"allocator": "optional"
}
],
"version": "POLICY_DEEP_3"
}
Loading

0 comments on commit 752ad23

Please sign in to comment.