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  # assume constant size for strings
43  bin_count, bin_size, extra = 0, 0, 10 + len(fn)
44  total_bytes = bin_count * bin_size + extra
45 
46  self._all[fn] = HistogramEntry(t, bin_size, bin_count, extra, total_bytes)
47 
48  def group(self, level, countObjects):
49  group_stats = {}
50 
51  for k, v in self._all.items():
52  group_key = "/".join(k.split("/")[:level])
53 
54  current = group_stats.get(group_key, 0)
55  group_stats[group_key] = current + (1 if countObjects else v.total_bytes)
56 
57  return group_stats
58 
59  def difference(self, ref):
60  results = HistogramAnalyzer()
61  results._all = dict(self._all)
62 
63  zero = HistogramEntry("null", 0, 0, 0, 0)
64  def cmp(a, b):
65  return HistogramEntry(b.type, b.bin_size,
66  a.bin_count - b.bin_count,
67  a.extra - b.extra,
68  a.total_bytes - b.total_bytes )
69 
70  for k, refv in ref._all.items():
71  results._all[k] = cmp(self._all.get(k, zero), refv)
72 
73  return results
74 
75 
76 def kibisize(num,args):
77  if args.count:
78  return str(num)
79  pStr="%."+str(args.precision)+"f %s"
80  for prefix in ['KiB','MiB','GiB']:
81  num /= 1024.0
82 
83  if num < 1024.0 or args.units == prefix:
84  return pStr % (num, prefix)
85  return pStr % (num, prefix)
86 
87 def displayDirectoryStatistics(stats, args):
88  group_stats = stats.group(args.depth, args.count)
89 
90  cutoff, display = args.cutoff * 1024, args.display
91 
92  as_list = [(v, k, ) for (k, v) in group_stats.items()]
93  as_list.sort(reverse=True, key=lambda v_k1: abs(v_k1[0]))
94 
95  if cutoff is not None:
96  as_list = [v_k for v_k in as_list if abs(v_k[0]) > cutoff]
97 
98  if display is not None:
99  as_list = as_list[:display]
100 
101  if args.human:
102  print "*" * 80
103  print (" DQM level %d folder breakdown " % args.depth).center(80, "*")
104  if cutoff:
105  print ("* Size cutoff: %s" % kibisize(cutoff,args)).ljust(79) + "*"
106  if display:
107  print ("* Showing top %d entries." % display).ljust(79) + "*"
108  print "*" * 80
109 
110  for v, k in as_list:
111  if args.human:
112  print kibisize(v,args).ljust(16, " "), k
113  else:
114  print v, k
115 
116 if __name__ == '__main__':
117  import argparse
118 
119  parser = argparse.ArgumentParser()
120  parser.add_argument("-i", "--input", help = "Input DQM ROOT file")
121  parser.add_argument("-r", "--ref", help = "Reference DQM ROOT file (to diff)")
122  parser.add_argument("--summary", help = "Dump summary", action = "store_true")
123  parser.add_argument("--count", help = "Count Histograms", action = "store_true")
124  parser.add_argument("-x", "--human", help = "Human readable output.", action = "store_true")
125  parser.add_argument("-n", "--display", help = "Max entries to display in --summary.", type = int, default = None)
126  parser.add_argument("-c", "--cutoff", help = "Max cutoff to display in --summary.", type = float, default = 512, metavar="KiB")
127  parser.add_argument("-d", "--depth", help = "Folder depth in --summary.", type = int, default = 2)
128  parser.add_argument("-u", "--units", help = "Memory units to use (KiB,MiB,GiB) if fixed output desired", type = str, default = "None")
129  parser.add_argument("-p", "--precision", help = "Places after decimal to display.", type = int, default = 2)
130 
131  args = parser.parse_args()
132 
133  stats = HistogramAnalyzer()
134  reader = DQMReader(args.input)
135  for (fn, v) in reader.read_objects():
136  stats.analyze(fn, v)
137  reader.close()
138 
139  if args.ref:
140  reader = DQMReader(args.ref)
141  ref_stats = HistogramAnalyzer()
142  for (fn, v) in reader.read_objects():
143  ref_stats.analyze(fn, v)
144  reader.close()
145 
146  stats = stats.difference(ref_stats)
147 
148  if args.summary:
149  displayDirectoryStatistics(stats, args)
150 
151  total = stats.group(0, args.count)
152  if args.human:
153  print "Total bytes: %s" % kibisize(total[""],args)
154  else:
155  print total[""]
def group(self, level, countObjects)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def kibisize(num, args)
def displayDirectoryStatistics(stats, args)
#define str(s)