mem_groups.py revision a8af9a7a2462b00e72deff99327bdb452a715277
1#! /usr/bin/python
2"""Groups memory by allocation sizes.
3
4Takes a log entry and sorts sorts everything into groups based on what size
5chunks the memory has been allocated in. groups is an array that contains the
6divisions (in bytes).
7
8The output format is:
9
10timestamp, percent of memory in chunks < groups[0], percent between groups[0]
11and groups[1], etc.
12
13"""
14
15import argparse
16from cros_utils import compute_total_diff
17from datetime import datetime
18
19pretty_print = True
20
21parser = argparse.ArgumentParser()
22parser.add_argument('filename')
23args = parser.parse_args()
24
25my_file = open(args.filename)
26output_file = open('groups.csv', 'a')
27
28# The cutoffs for each group in the output (in bytes)
29groups = [1024, 8192, 65536, 524288, 4194304]
30
31base_time = datetime(2014, 6, 11, 0, 0)
32prev_line = ''
33half_entry = (None, None)
34
35for line in my_file:
36  if 'heap profile:' in line:
37    if half_entry[0] is not None:
38      group_totals = half_entry[1]
39      total = sum(group_totals) * 1.0
40      to_join = [half_entry[0]] + [value / total for value in group_totals]
41      to_output = ','.join([str(elem) for elem in to_join])
42      output_file.write(to_output)
43    total_diff = compute_total_diff(line, base_time)
44    half_entry = (total_diff, [0] * (len(groups) + 1))
45  if '] @ ' in line and 'heap profile:' not in line:
46    mem_samples = line.strip().split('[')[0]
47    num_samples, total_mem = map(int, mem_samples.strip().split(':'))
48    mem_per_sample = total_mem // num_samples
49    group_totals = half_entry[1]
50    for cutoff_index in range(len(groups)):
51      if mem_per_sample <= groups[cutoff_index]:
52        group_totals[cutoff_index] += total_mem
53        break
54    if mem_per_sample > groups[-1]:
55      group_totals[-1] += total_mem
56