1#!/usr/bin/python3
2
3# Read and analyse stdout of footprint.cc
4# $ sudo ./footprint 100000 | ./footprint.py
5
6from collections import OrderedDict
7import re, sys
8
9slabs = {}
10
11sections = []
12
13section = None
14
15for line in sys.stdin:
16    if m := re.match('===== (.*) =====', line):
17        section_name = m.group(1)
18        # print(section_name)
19        if (section):
20            sections.append(section)
21        section = (section_name, OrderedDict(), OrderedDict())
22        meminfo = True
23        continue
24    if re.match('slabinfo -', line):
25        meminfo = False
26        continue
27    if meminfo:
28        if m := re.match('(.*): *(\\d+) kB', line):
29            section[1][m.group(1)] = int(m.group(2))
30    else:
31        if line[0] == '#':
32            continue
33        (slab, active, total, objsize) = line.split()[:4]
34        slabs[slab] = int(objsize)
35        section[2][slab] = int(active)
36
37
38sections.append(section)
39
40for i in range(1, len(sections)):
41    print('=====', sections[i][0])
42    meminfo = sections[i][1]
43    old = sections[i-1][1]
44    for key in meminfo:
45        diff = meminfo[key]-old[key]
46        if diff:
47            print(key, meminfo[key], diff)
48
49    print('-----')
50    slab = sections[i][2]
51    old = sections[i-1][2]
52    for key in slab:
53        diff = slab[key]-old[key]
54        if diff:
55            print(key, slabs[key], slab[key], diff)
56
57