6 __author__ =
"Sebastien Binet <binet@cern.ch>"
7 __version__ =
"$Revision: 1.3 $"
8 __doc__ =
"""python module holding some general and/or miscellaneous tools, functions and classes to ease gathering monitoring data
18 Helper method to crawl through collections in a StoreGateSvc instance and
19 assess how much memory they are wasting b/c they 'forgot' to reserve
20 enough space before doing 'push_back'.
22 This task is performed by just comparing what Klass::size() and
23 Klass::capacity() are returning as a value.
25 @param `sgname` the fully qualified Gaudi name of the StoreGateSvc instance
26 @return a dictionary of { <sgkey> : (size, capacity, clid) }
30 from AthenaPython
import PyAthena
35 sg = PyAthena.py_svc (sgname)
36 cl = PyAthena.py_svc (
'ClassIDSvc')
39 proxies = sg.proxies()
49 klass =
"%s" % cl.typename(clid)
56 data = sg.retrieve(klass, k)
65 if not hasattr(data,
'size')
or \
66 not hasattr(data,
'capacity'):
73 rt = sz/
float(cp)*100.
77 wasted[k] = (sz, cp, clid)
83 if not (fname
is None):
87 for z
in open(
'/proc/%d/smaps'%os.getpid()):
89 if not (fname
is None):
95 if not (fname
is None):
99 pat = re.compile(
r'(?P<addr_beg>\w*?)\-(?P<addr_end>\w*?)\s'\
100 r'(?P<perm>.{4})\s(?P<offset>\w*?)\s'\
101 r'(?P<devmajor>\d{2}):(?P<devminor>\d{2})\s'\
102 r'(?P<inode>\d*?)\s(?P<libname>.*)')
104 _realpath = os.path.realpath
107 for line
in open(
'/proc/%s/smaps'%pid):
109 res = re.match(pat,z)
112 libname =
g(
'libname').strip()
113 libs.add(_realpath(libname))
114 libs =
sorted([z
for z
in libs], reverse=
True)
117 print(libname, file=o)
121 if sys.platform ==
'darwin':
123 from resource
import getrusage, RUSAGE_SELF
124 cpu = getrusage(RUSAGE_SELF)
125 cpu = (cpu.ru_utime+cpu.ru_stime) * 1e3
131 from resource
import getpagesize
132 from os
import popen, getpid
133 pageSize = getpagesize()
134 vsz =
int(popen(
'ps -p %d -o %s | tail -1' % (getpid(),
"vsz")).
read())
135 rss =
int(popen(
'ps -p %d -o %s | tail -1' % (getpid(),
"rss")).
read())
136 vmem = vsz*pageSize / 1024.0 / 1024.0
141 from os
import getpid,sysconf
142 from resource
import getrusage, RUSAGE_SELF
143 cpu = getrusage(RUSAGE_SELF)
144 cpu = (cpu.ru_utime+cpu.ru_stime) * 1e3
145 pageSize = sysconf(
'SC_PAGE_SIZE')/Units.MB
146 mem =
open(
'/proc/%d/statm'%getpid(),
'r')
147 mem = mem.readlines()[0].
split()
148 vmem =
int(mem[0])*pageSize
149 rss =
int(mem[1])*pageSize
153 import socket,commands
154 hostname =
'<unknown>'
156 hostname = socket.gethostname()
159 sc,out = commands.getstatusoutput(
'which lshosts')
161 return (
'no lshosts command',0.)
163 sc,out = commands.getstatusoutput(
"%s %s"%(cmd,hostname))
165 return (
'host not in db', 0.)
168 title,data = out.splitlines()
169 title = title.split()
173 for i,k
in enumerate(title[:-1]):
174 cpu_infos[k] = data[i]
175 except Exception
as err:
177 return (
'ok', cpu_infos)