Commit | Line | Data |
---|---|---|
82497c22 AB |
1 | # The MIT License (MIT) |
2 | # | |
3 | # Copyright (C) 2015 - Antoine Busque <abusque@efficios.com> | |
4 | # | |
5 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | # of this software and associated documentation files (the "Software"), to deal | |
7 | # in the Software without restriction, including without limitation the rights | |
8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | # copies of the Software, and to permit persons to whom the Software is | |
10 | # furnished to do so, subject to the following conditions: | |
11 | # | |
12 | # The above copyright notice and this permission notice shall be included in | |
13 | # all copies or substantial portions of the Software. | |
14 | # | |
15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | # SOFTWARE. | |
22 | ||
ff833baa AB |
23 | from collections import namedtuple |
24 | ||
25 | ||
26 | PrioEvent = namedtuple('PrioEvent', ['timestamp', 'prio']) | |
27 | ||
28 | ||
82497c22 AB |
29 | class Stats(): |
30 | def reset(self): | |
31 | raise NotImplementedError() | |
32 | ||
33 | ||
34 | class Process(Stats): | |
35 | def __init__(self, pid, tid, comm): | |
36 | self.pid = pid | |
37 | self.tid = tid | |
38 | self.comm = comm | |
ff833baa | 39 | self.prio_list = [] |
82497c22 AB |
40 | |
41 | @classmethod | |
42 | def new_from_process(cls, proc): | |
43 | return cls(proc.pid, proc.tid, proc.comm) | |
44 | ||
ff833baa AB |
45 | def update_prio(self, timestamp, prio): |
46 | self.prio_list.append(PrioEvent(timestamp, prio)) | |
47 | ||
48 | def reset(self): | |
49 | if self.prio_list: | |
50 | # Keep the last prio as the first for the next period | |
51 | self.prio_list = self.prio_list[-1:] | |
52 | ||
82497c22 AB |
53 | |
54 | class IO(Stats): | |
55 | def __init__(self): | |
56 | # Number of bytes read or written | |
57 | self.read = 0 | |
58 | self.write = 0 | |
59 | ||
60 | def reset(self): | |
61 | self.read = 0 | |
62 | self.write = 0 | |
63 | ||
64 | def __iadd__(self, other): | |
65 | self.read += other.read | |
774a02a3 | 66 | self.write += other.write |
82497c22 | 67 | return self |