1# Copyright 2014 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5 6class ProcessStatisticTimelineData(object): 7 """Holds value of a stat for one or more processes. 8 9 This object can hold a value for more than one pid by adding another 10 object.""" 11 12 def __init__(self, pid, value): 13 super(ProcessStatisticTimelineData, self).__init__() 14 assert value >= 0 15 self._value_by_pid = {pid: value} 16 17 def __sub__(self, other): 18 """The results of subtraction is an object holding only the pids contained 19 in |self|. 20 21 The motivation is that some processes may have died between two consecutive 22 measurements. The desired behavior is to only make calculations based on 23 the processes that are alive at the end of the second measurement.""" 24 # pylint: disable=protected-access 25 ret = self.__class__(0, 0) 26 my_dict = self._value_by_pid 27 28 ret._value_by_pid = ( 29 {k: my_dict[k] - other._value_by_pid.get(k, 0) for 30 k in my_dict.keys()}) 31 return ret 32 33 def __add__(self, other): 34 """The result contains pids from both |self| and |other|, if duplicate 35 pids are found between objects, an error will occur. """ 36 # pylint: disable=protected-access 37 intersecting_pids = (set(self._value_by_pid.keys()) & 38 set(other._value_by_pid.keys())) 39 assert len(intersecting_pids) == 0 40 41 ret = self.__class__(0, 0) 42 ret._value_by_pid = {} 43 ret._value_by_pid.update(self._value_by_pid) 44 ret._value_by_pid.update(other._value_by_pid) 45 return ret 46 47 @property 48 def value_by_pid(self): 49 return self._value_by_pid 50 51 def total_sum(self): 52 """Returns the sum of all values contained by this object. """ 53 return sum(self._value_by_pid.values()) 54 55 56class IdleWakeupTimelineData(ProcessStatisticTimelineData): 57 """A ProcessStatisticTimelineData to hold idle wakeups.""" 58 pass 59