5 from __future__
import print_function
7 __author__ =
"Sebastien Binet <binet@cern.ch>"
8 __doc__ =
"get the inclusive and exclusive vmem sizes of a library"
16 from PyUtils.Decorators
import forking
as forking
17 from PerfMonComps.PyMonUtils
import loaded_libs, pymon
24 return ctypes.cdll.LoadLibrary(libname)
31 libs = [l
for l
in loaded_libs()
32 if not os.path.basename(l)
in _veto_libs
and
33 os.access(l, os.R_OK)]
34 return (vmem0, vmem1, libs)
38 """retrieve the list of dependencies of a given library
39 both direct and indirect dependencies
41 bkg_libs = [os.path.basename(l)
for l
in loaded_libs()]
42 bkg_libs = [l
for l
in bkg_libs
if l
not in _veto_libs]
45 libs = [os.path.basename(l)
for l
in libs]
46 return [l
for l
in libs
if l
not in bkg_libs
and l!=libname]
50 bkg_libs = loaded_libs()
52 vmem0,vmem1,libs = load_lib (libname)
53 dVmemOffset = vmem1-vmem0
55 linked_libs = [os.path.basename(lib)
57 if (
not (lib
in bkg_libs)
and
58 os.path.basename(lib) != os.path.basename(libname)
59 and os.access(lib, os.R_OK))]
63 def load_linked_in_libs(linked_libs):
69 except Exception
as err:
70 print (
"** problem loading [%s]\n%s" % (l,err))
76 for _
in range(NRETRY):
77 ok = load_linked_in_libs(linked_libs)
81 print (
"** err: could not reliably load all libs (after %s retries)"%NRETRY)
85 vmem0,vmem1,libs = load_lib (libname)
87 dVmemLib = vmem1-vmem0
88 return (dVmemLib, dVmemOffset, len(linked_libs), len(bkg_libs))
93 _print = display.append
95 for libname
in libnames:
97 bname = os.path.basename(libname)
98 _print(
":: inspecting library: %s" % bname)
99 stats = analyze (libname)
100 _print(
":: nbr linked: %s" % stats[2])
101 _print(
":: dVmem-self: %8.3f Mb" % stats[0])
102 _print(
":: dVmem-all : %8.3f Mb" % stats[1])
104 lib_stats[bname] = dict(
107 nbr_linked= stats[2],
110 import operator
as _op
112 dep_stats = dict((d,
analyze(d)[:2])
for d
in deps)
113 dep_vmem = dep_stats.items()
114 dep_vmem.sort(key=_op.itemgetter(1), reverse=
True)
115 _print(
":: deps: lib-self | lib-all:")
117 _print(
":: --> [%8.3f |%8.3f Mb] (%s)" % (v[0], v[1], k))
119 return display,lib_stats
123 fname =
"vmem-stats-sz.csv"
124 print (
":: saving vmem statistics in [%s]..."%fname)
126 if os.path.exists (fname):
128 o = csv.writer (
open(fname,
"w"), delimiter=
';')
129 o.writerow ([
'nbr libraries', len(lib_stats)])
130 o.writerow ([
'lib name',
'dvmem-self (Mb)',
'dvmem-all (Mb)',
'nbr linked-libs'])
131 for k,v
in lib_stats.items():
132 o.writerow ([os.path.basename(k), v[
'vmem_self'], v[
'vmem_all'], v[
'nbr_linked']])
134 print (
":: saving vmem statistics in [%s]... [done]"%fname)
138 parser = argparse.ArgumentParser(
139 description=
'get the inclusive and exclusive vmem sizes of a library'
141 _add = parser.add_argument
145 help=
'list of library names to be inspected')
149 help=
'enable detailed output')
150 args = parser.parse_args()
152 libnames = args.libnames
153 doDetailed = args.detailed
155 print (
":: inspecting libraries: %s" % libnames)
156 display,lib_stats = analyze_libraries (libnames, doDetailed)
160 save_stats (lib_stats)
164 if __name__ ==
'__main__':