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