7 from __future__
import with_statement
9 __doc__ =
"log the vmem usage at each dso being dlopen'd"
10 __version__ =
"$Revision: 1.3 $"
11 __author__ =
"Sebastien Binet <binet@cern.ch>"
17 from os.path
import realpath
as _realpath
18 from os.path
import basename
as _basename
22 (
'fname', ctypes.c_char_p),
23 (
'step', ctypes.c_int),
29 """log Dso loading by calling-back into `cb_fct`
31 if `cb_fct` is None we use a default callback function which logs
35 self.
lib = ctypes.cdll.LoadLibrary (
"libAthDSoCallBacks.so")
40 self._cb_fct_type = ctypes.CFUNCTYPE(ctypes.c_int,
41 ctypes.POINTER(DsoEvent),
43 fct = self.lib.ath_dso_cbk_register
44 fct.argtypes = [self._cb_fct_type, ctypes.c_void_p]
45 fct.restype = ctypes.c_int
47 fct = self.lib.ath_dso_cbk_unregister
48 fct.argtypes = [self._cb_fct_type]
49 fct.restype = ctypes.c_int
51 from collections
import defaultdict
52 self._data = defaultdict(dict)
54 from AthenaCommon.Logging
import logging
55 self.msg = logging.getLogger (
"AthDsoLogger")
56 self.msg.setLevel (logging.INFO)
59 cb_fct = self.default_cb_fct
61 self.msg.info (
"creating the C-callback function")
62 self.cb_fct = self._cb_fct_type(cb_fct)
67 fname =
'vmem-dso.csv'
68 if os.path.exists (fname):
70 self._fd = open (fname,
'w')
71 self.out = csv.writer (self._fd, delimiter=
';')
72 map (self.out.writerow,
73 [ [
'master-pid', os.getpid()],
75 'vmem-start (kb)',
'vmem-stop (kb)',
'dvmem (kb)'],
79 self.msg.info (
"initializing the C-dlopen-logger")
87 if sys.platform !=
'darwin':
89 libname = evt.contents.fname
90 step = evt.contents.step
94 n = _basename (_realpath (libname))
98 from os
import sysconf
99 PAGE_SIZE = sysconf (
'SC_PAGE_SIZE')
101 from string
import split
as ssplit
103 with open(
'/proc/self/statm')
as f:
104 m =
int(ssplit (f.readlines()[0])[0])
108 self._data[pid][n] = [vmem(),
None]
110 data = self._data[pid][n]
112 vmem_start = data[0]/1024.
113 vmem_stop = data[1]/1024.
114 dvmem = vmem_stop - vmem_start
116 "[%d] loading lib: vmem=(%10.1f + %10.1f) kb [%s]",
117 pid, vmem_start, dvmem, n
119 self.out.writerow ([pid, n, vmem_start, vmem_stop, dvmem])
129 n = _basename (_realpath (libname))
133 from os
import sysconf
134 PAGE_SIZE = sysconf (
'SC_PAGE_SIZE')
138 self._data[pid][n] = [vmem(),
None]
140 data = self._data[pid][n]
142 vmem_start = data[0]/1024.
143 vmem_stop = data[1]/1024.
144 dvmem = vmem_stop - vmem_start
146 "[%d] loading lib: vmem=(%10.1f + %10.1f) kb [%s]",
147 pid, vmem_start, dvmem, n
149 self.out.writerow ([pid, n, vmem_start, vmem_stop, dvmem])