1 import os, sys, resource, time, socket, threading
3 class attrinfo(object):
4 def __init__(self, desc = None):
8 def __init__(self, *args, **kwargs):
9 super(perfobj, self).__init__()
11 def pdm_protocols(self):
14 class simpleattr(perfobj):
15 def __init__(self, func, info = None, *args, **kwargs):
16 super(simpleattr, self).__init__(*args, **kwargs)
28 def pdm_protocols(self):
29 return super(simpleattr, self).pdm_protocols() + ["attr"]
31 class valueattr(perfobj):
32 def __init__(self, init, info = None, *args, **kwargs):
33 super(valueattr, self).__init__(*args, **kwargs)
45 def pdm_protocols(self):
46 return super(valueattr, self).pdm_protocols() + ["attr"]
49 class eventobj(perfobj):
50 def __init__(self, *args, **kwargs):
51 super(eventobj, self).__init__(*args, **kwargs)
52 self.subscribers = set()
54 def subscribe(self, cb):
55 if cb in self.subscribers:
56 raise ValueError("Already subscribed")
57 self.subscribers.add(cb)
59 def unsubscribe(self, cb):
60 self.subscribers.remove(cb)
62 def notify(self, event):
63 for cb in self.subscribers:
68 def pdm_protocols(self):
69 return super(eventobj, self).pdm_protocols() + ["event"]
71 class staticdir(perfobj):
72 def __init__(self, *args, **kwargs):
73 super(staticdir, self).__init__(*args, **kwargs)
76 def __setitem__(self, name, ob):
79 def __delitem__(self, name):
82 def __getitem__(self, name):
85 def get(self, name, default = None):
86 return self.map.get(name, default)
89 return self.map.keys()
91 def lookup(self, name):
94 def pdm_protocols(self):
95 return super(staticdir, self).pdm_protocols() + ["dir"]
99 self.time = time.time()
101 idlock = threading.Lock()
103 class startevent(event):
105 super(startevent, self).__init__()
114 class finishevent(event):
115 def __init__(self, start, aborted):
116 super(finishevent, self).__init__()
118 self.aborted = aborted
122 ires = resource.getrusage(resource.RUSAGE_SELF)
124 ru = resource.getrusage(resource.RUSAGE_SELF)
125 return (ru.ru_utime - ires.ru_utime) + (ru.ru_stime - ires.ru_stime)
126 sysres["realtime"] = simpleattr(func = lambda: time.time() - itime)
127 sysres["cputime"] = simpleattr(func = ct)
128 sysres["utime"] = simpleattr(func = lambda: resource.getrusage(resource.RUSAGE_SELF).ru_utime - ires.ru_utime)
129 sysres["stime"] = simpleattr(func = lambda: resource.getrusage(resource.RUSAGE_SELF).ru_stime - ires.ru_stime)
130 sysres["maxrss"] = simpleattr(func = lambda: resource.getrusage(resource.RUSAGE_SELF).ru_maxrss)
131 sysres["rusage"] = simpleattr(func = lambda: resource.getrusage(resource.RUSAGE_SELF))
133 sysinfo = staticdir()
134 sysinfo["pid"] = simpleattr(func = os.getpid)
135 sysinfo["uname"] = simpleattr(func = os.uname)
136 sysinfo["hostname"] = simpleattr(func = socket.gethostname)
137 sysinfo["platform"] = valueattr(init = sys.platform)