VirtualBox

Changeset 12942 in vbox for trunk/src/libs/xpcom18a4/python


Ignore:
Timestamp:
Oct 2, 2008 2:53:11 PM (16 years ago)
Author:
vboxsync
Message:

PerfAPI: Fixed the problem with passing null/empty arrays to PerfAPI methods.
Enabled attributes of type IUnknown in SOAP, JAX-WS, WSDL, CPP and Python style sheets.
Added PerfCollector python class to shellcommon.py to provide more 'pythonic' way to access metric data.
Added the reference to shellcommon.py to IDL docs as an example of PerfAPI usage in python.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/libs/xpcom18a4/python/sample/shellcommon.py

    r12753 r12942  
    22import sys
    33import pdb
     4
     5class 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
    480
    581g_hasreadline = 1
     
    78154   return s.split()
    79155   
    80 def startVm(mgr,vb,mach,type):
     156def startVm(mgr,vb,mach,type,perf):
    81157    session = mgr.getSessionObject(vb)
    82158    uuid = mach.id
     
    86162    rc = progress.resultCode
    87163    print "Completed:", completed, "rc:",rc
    88     if rc == 0:
     164    if int(rc) == 0:
     165        # @todo Nikolay, what is the point in ignoring exceptions?
    89166        try:
    90             vb.performanceCollector.setupMetrics(['*'], [mach], 10, 15)
     167            perf.setup(['*'], [mach], 10, 15)
    91168        except:
    92169            pass
     
    103180
    104181def 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']
    113184
    114185def cmdExistingVm(ctx,mach,cmd):
     
    217288    else:
    218289        type = "gui"
    219     startVm(ctx['mgr'], ctx['vb'], mach, type)
     290    startVm(ctx['mgr'], ctx['vb'], mach, type, ctx['perf'])
    220291    return 0
    221292
     
    291362      print "Processor #%d speed: %dMHz" %(i,host.getProcessorSpeed(i))
    292363               
    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']
    302366
    303367   return 0
     
    360424    # last 150 secs maximum, (sample every 10 secs and keep up to 15 samples)
    361425    try:
    362         vbox.performanceCollector.setupMetrics(['*'], [vbox.host], 10, 15)
     426        ctx['perf'].setup(['*'], [vbox.host], 10, 15)
    363427    except:
    364428        pass
     
    380444
    381445    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])
    383448    except:
    384449        pass
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette