LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
citizen.py
Go to the documentation of this file.
1 import re
2 import lsst.daf.base as dafBase
3 
4 def setCallbacks(new=None, delete=None, both=False):
5  """Set the callback IDs for the Citizen; if both is true, set both new and delete to the same value
6 
7 You probably want to chant the following to gdb:
8  break defaultNewCallback
9  break defaultDeleteCallback
10 
11 You might want to put this in your .gdbinit file.
12 
13 You can retrieve a citizen's signature from python with obj.repr()
14  """
15 
16  if both:
17  if new:
18  if delete and new != delete:
19  raise RuntimeError, "You may not specify new, delete, and both"
20  delete = new
21  else:
22  new = delete
23 
24  if new:
25  dafBase.Citizen.setNewCallbackId(new)
26  if delete:
27  dafBase.Citizen.setDeleteCallbackId(delete)
28 
29 def mortal(memId0=0, nleakPrintMax=20, first=True, showTypes=None):
30  """Print leaked memory blocks
31  @param memId0 Only consider blocks allocated after this memId
32  @param nleakPrintMax Maximum number of leaks to print; <= 0 means unlimited
33  @param first Print the first nleakPrintMax blocks; if False print the last blocks.
34  @param showTypes Only print objects matching this regex (if starts with !, print objects that don't match)
35 
36  If you want finer control than nleakPrintMax/first provide, use
37  dafBase.Citizen.census() to get the entire list
38 
39 You can get the next memId to be allocated with mortal("set"), e.g.
40  memId0 = mortal("set")
41  # work work work
42  mortal(memId0)
43  """
44 
45  if memId0 == 'set':
46  return dafBase.Citizen.getNextMemId()
47 
48  nleak = dafBase.Citizen.census(0, memId0)
49  if nleak != 0:
50  print "%d Objects leaked" % dafBase.Citizen.census(0, memId0)
51 
52  census = dafBase.Citizen.census()
53  census = [census[i].repr() for i in range(len(census))] # using [i] for some swiggy reason
54  if showTypes:
55  if showTypes[0] == '!':
56  invert = True # invert the matching logic
57  showTypes = showTypes[1:]
58  else:
59  invert = False
60 
61  _census, census = census, []
62  for c in _census:
63  memId, addr, dtype = c.split()
64  memId = int(memId[:-1])
65 
66  if \
67  (not invert and re.search(showTypes, dtype)) or \
68  (invert and not re.search(showTypes, dtype)):
69  census.append(c)
70 
71  nleak = len(census)
72  print "%d leaked objects match" % nleak
73 
74  if nleakPrintMax <= 0 or nleak <= nleakPrintMax:
75  for c in census:
76  memId, addr, type = c.split()
77  memId = int(memId[:-1])
78  if memId >= memId0:
79  print c
80  else:
81  print "..."
82  for i in range(nleakPrintMax - 1, -1, -1):
83  print census[i]