Changeset 12942 in vbox for trunk/src/libs/xpcom18a4/python
- Timestamp:
- Oct 2, 2008 2:53:11 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/libs/xpcom18a4/python/sample/shellcommon.py
r12753 r12942 2 2 import sys 3 3 import pdb 4 5 class PerfCollector: 6 """ This class provides a wrapper over IPerformanceCollector in order to 7 get more 'pythonic' interface. 8 9 To begin collection of metrics use setup() method. 10 11 To get collected data use query() method. 12 13 It is possible to disable metric collection without changing collection 14 parameters with disable() method. The enable() method resumes metric 15 collection. 16 """ 17 18 def __init__(self, vb): 19 """ Initializes the instance. 20 21 Pass an instance of IVirtualBox as parameter. 22 """ 23 self.collector = vb.performanceCollector 24 25 def _update_metric_params(self): 26 metrics = self.collector.getMetrics(['*'], None) 27 self.metrics = {} 28 for m in metrics: 29 self.metrics[str(m.object) + "/" + str(m.metricName)] = m 30 31 def setup(self, names, objects, period, nsamples): 32 """ Discards all previously collected values for the specified 33 metrics, sets the period of collection and the number of retained 34 samples, enables collection. 35 """ 36 self.collector.setupMetrics(names, objects, period, nsamples) 37 self._update_metric_params() 38 39 def enable(self, names, objects): 40 """ Resumes metric collection for the specified metrics. 41 """ 42 self.collector.enableMetrics(names, objects) 43 44 def disable(self, names, objects): 45 """ Suspends metric collection for the specified metrics. 46 """ 47 self.collector.disableMetrics(names, objects) 48 49 def query(self, names, objects): 50 """ Retrieves collected metric values as well as some auxiliary 51 information. Returns an array of dictionaries, one dictionary per 52 metric. Each dictionary contains the following entries: 53 'name': metric name 54 'object': managed object this metric associated with 55 'unit': unit of measurement 56 'scale': divide 'values' by this number to get float numbers 57 'values': collected data 58 'values_as_string': pre-processed values ready for 'print' statement 59 """ 60 (values, names_out, objects_out, indices, lengths) = self.collector.queryMetricsData(names, objects) 61 out = [] 62 for i in xrange(0, len(names_out)): 63 metric = self.metrics[str(objects_out[i]) + "/" + str(names_out[i])] 64 unit = str(metric.getUnit()) 65 if unit == '%': 66 scale = 1000. 67 fmt = '%.2f%s' 68 else: 69 scale = 1 70 fmt = '%d %s' 71 out.append({ 72 'name':str(names_out[i]), 73 'object':str(objects_out[i]), 74 'unit':str(metric.getUnit()), 75 'scale':scale, 76 'values':[int(values[j]) for j in xrange(int(indices[i]), int(indices[i])+int(lengths[i]))], 77 'values_as_string':'['+', '.join([fmt % (int(values[j])/scale, unit) for j in xrange(int(indices[i]), int(indices[i])+int(lengths[i]))])+']' 78 }) 79 return out 4 80 5 81 g_hasreadline = 1 … … 78 154 return s.split() 79 155 80 def startVm(mgr,vb,mach,type ):156 def startVm(mgr,vb,mach,type,perf): 81 157 session = mgr.getSessionObject(vb) 82 158 uuid = mach.id … … 86 162 rc = progress.resultCode 87 163 print "Completed:", completed, "rc:",rc 88 if rc == 0: 164 if int(rc) == 0: 165 # @todo Nikolay, what is the point in ignoring exceptions? 89 166 try: 90 vb.performanceCollector.setupMetrics(['*'], [mach], 10, 15)167 perf.setup(['*'], [mach], 10, 15) 91 168 except: 92 169 pass … … 103 180 104 181 def guestStats(ctx,mach): 105 collector = ctx['vb'].performanceCollector 106 (vals, names, objs, idxs, lens) = collector.queryMetricsData(["*"], [mach]) 107 for i in range(0,len(names)): 108 valsStr = '[ ' 109 for j in range(0, lens[i]): 110 valsStr += str(vals[int(idxs[i])+j])+' ' 111 valsStr += ']' 112 print names[i],valsStr 182 for metric in ctx['perf'].query(["*"], [mach]): 183 print metric['name'], metric['values_as_string'] 113 184 114 185 def cmdExistingVm(ctx,mach,cmd): … … 217 288 else: 218 289 type = "gui" 219 startVm(ctx['mgr'], ctx['vb'], mach, type )290 startVm(ctx['mgr'], ctx['vb'], mach, type, ctx['perf']) 220 291 return 0 221 292 … … 291 362 print "Processor #%d speed: %dMHz" %(i,host.getProcessorSpeed(i)) 292 363 293 collector = ctx['vb'].performanceCollector 294 295 (vals, names, objs, idxs, lens) = collector.queryMetricsData(["*"], [host]) 296 for i in range(0,len(names)): 297 valsStr = '[ ' 298 for j in range(0, lens[i]): 299 valsStr += str(vals[int(idxs[i])+j])+' ' 300 valsStr += ']' 301 print names[i],valsStr 364 for metric in ctx['perf'].query(["*"], [host]): 365 print metric['name'], metric['values_as_string'] 302 366 303 367 return 0 … … 360 424 # last 150 secs maximum, (sample every 10 secs and keep up to 15 samples) 361 425 try: 362 vbox.performanceCollector.setupMetrics(['*'], [vbox.host], 10, 15)426 ctx['perf'].setup(['*'], [vbox.host], 10, 15) 363 427 except: 364 428 pass … … 380 444 381 445 try: 382 vbox.performanceCollector.disableMetrics(['*'], [vbox.host]) 446 # There is no need to disable metric collection. This is just an example. 447 ctx['perf'].disable(['*'], [vbox.host]) 383 448 except: 384 449 pass
Note:
See TracChangeset
for help on using the changeset viewer.