Home | History | Annotate | Download | only in mem_tests
      1 #! /usr/bin/python
      2 """Groups memory by allocation sizes.
      3 
      4 Takes a log entry and sorts sorts everything into groups based on what size
      5 chunks the memory has been allocated in. groups is an array that contains the
      6 divisions (in bytes).
      7 
      8 The output format is:
      9 
     10 timestamp, percent of memory in chunks < groups[0], percent between groups[0]
     11 and groups[1], etc.
     12 
     13 """
     14 
     15 import argparse
     16 from cros_utils import compute_total_diff
     17 from datetime import datetime
     18 
     19 pretty_print = True
     20 
     21 parser = argparse.ArgumentParser()
     22 parser.add_argument('filename')
     23 args = parser.parse_args()
     24 
     25 my_file = open(args.filename)
     26 output_file = open('groups.csv', 'a')
     27 
     28 # The cutoffs for each group in the output (in bytes)
     29 groups = [1024, 8192, 65536, 524288, 4194304]
     30 
     31 base_time = datetime(2014, 6, 11, 0, 0)
     32 prev_line = ''
     33 half_entry = (None, None)
     34 
     35 for 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