CMS 3D CMS Logo

dqmMemoryStats.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 import sys
4 
5 from DQMServices.FileIO.DQM import DQMReader
6 from collections import namedtuple
7 
8 HistogramEntry = namedtuple('HistogramEntry', ['type', 'bin_size', 'bin_count', 'extra', 'total_bytes'])
9 
11  BIN_SIZE = {
12  'TH1S': 2, 'TH2S': 2,
13  'TH1F': 4, 'TH2F': 4,
14  'TH1D': 8, 'TH2D': 8,
15  'TH3F': 4,
16  'TProfile': 8, 'TProfile2D': 8,
17  }
18 
19  def __init__(self):
20  self._all = {}
21 
22  def analyze(self, fn, obj):
23  name = fn.split("/")[-1]
24 
25  if hasattr(obj, 'ClassName'):
26  # this is a root type
27  t = str(obj.ClassName())
28  bin_size = self.BIN_SIZE.get(t, None)
29  if bin_size is None:
30  sys.stderr.write("warning: unknown root type: %s\n" % t)
31  sys.stderr.flush()
32  bin_size = 8
33 
34  bin_count = obj.GetNcells()
35  extra = len(fn)
36  total_bytes = bin_count * bin_size + extra
37 
38  self._all[fn] = HistogramEntry(t, bin_size, bin_count, extra, total_bytes)
39  else:
40  t = str(type(obj))
41  bin_count, bin_size, extra = 0, 0, len(str(obj)) + len(fn)
42  total_bytes = bin_count * bin_size + extra
43 
44  self._all[fn] = HistogramEntry(t, bin_size, bin_count, extra, total_bytes)
45 
46  def group(self, level):
47  group_stats = {}
48 
49  for k, v in self._all.items():
50  group_key = "/".join(k.split("/")[:level])
51 
52  current = group_stats.get(group_key, 0)
53  group_stats[group_key] = current + v.total_bytes
54 
55  return group_stats
56 
57  def difference(self, ref):
58  results = HistogramAnalyzer()
59  results._all = dict(self._all)
60 
61  zero = HistogramEntry("null", 0, 0, 0, 0)
62  def cmp(a, b):
63  return HistogramEntry(b.type, b.bin_size,
64  a.bin_count - b.bin_count,
65  a.extra - b.extra,
66  a.total_bytes - b.total_bytes )
67 
68  for k, refv in ref._all.items():
69  results._all[k] = cmp(self._all.get(k, zero), refv)
70 
71  return results
72 
73 
74 def kibisize(num):
75  for prefix in ['KiB','MiB','GiB']:
76  num /= 1024.0
77 
78  if num < 1024.0:
79  return "%.2f %s" % (num, prefix)
80 
81 def displayDirectoryStatistics(stats, args):
82  group_stats = stats.group(args.depth)
83 
84  cutoff, display = args.cutoff * 1024, args.display
85 
86  as_list = [(v, k, ) for (k, v) in group_stats.items()]
87  as_list.sort(reverse=True, key=lambda (v, k): abs(v))
88 
89  if cutoff is not None:
90  as_list = filter(lambda (v, k): abs(v) > cutoff, as_list)
91 
92  if display is not None:
93  as_list = as_list[:display]
94 
95  if args.human:
96  print "*" * 80
97  print (" DQM level %d folder breakdown " % args.depth).center(80, "*")
98  if cutoff:
99  print ("* Size cutoff: %s" % kibisize(cutoff)).ljust(79) + "*"
100  if display:
101  print ("* Showing top %d entries." % display).ljust(79) + "*"
102  print "*" * 80
103 
104  for v, k in as_list:
105  if args.human:
106  print kibisize(v).ljust(16, " "), k
107  else:
108  print v, k
109 
110 if __name__ == '__main__':
111  import argparse
112 
113  parser = argparse.ArgumentParser()
114  parser.add_argument("-i", "--input", help = "Input DQM ROOT file")
115  parser.add_argument("-r", "--ref", help = "Reference DQM ROOT file (to diff)")
116  parser.add_argument("--summary", help = "Dump summary", action = "store_true")
117  parser.add_argument("-x", "--human", help = "Human readable output.", action = "store_true")
118  parser.add_argument("-n", "--display", help = "Max entries to display in --summary.", type = int, default = None)
119  parser.add_argument("-c", "--cutoff", help = "Max cutoff to display in --summary.", type = float, default = 512, metavar="KiB")
120  parser.add_argument("-d", "--depth", help = "Folder depth in --summary.", type = int, default = 2)
121 
122  args = parser.parse_args()
123 
124  stats = HistogramAnalyzer()
125  reader = DQMReader(args.input)
126  for (fn, v) in reader.read_objects():
127  stats.analyze(fn, v)
128  reader.close()
129 
130  if args.ref:
131  reader = DQMReader(args.ref)
132  ref_stats = HistogramAnalyzer()
133  for (fn, v) in reader.read_objects():
134  ref_stats.analyze(fn, v)
135  reader.close()
136 
137  stats = stats.difference(ref_stats)
138 
139  if args.summary:
140  displayDirectoryStatistics(stats, args)
141 
142  total = stats.group(0)
143  if args.human:
144  print "Total bytes: %s" % kibisize(total[""])
145  else:
146  print total[""]
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
def kibisize(num)
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def displayDirectoryStatistics(stats, args)