Commit | Line | Data |
---|---|---|
4ed24f86 JD |
1 | # The MIT License (MIT) |
2 | # | |
39fec005 | 3 | # Copyright (C) 2015 - Antoine Busque <abusque@efficios.com> |
4ed24f86 JD |
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 | ||
6f89f629 | 23 | from . import stats |
4e99adc5 JD |
24 | from .analysis import Analysis |
25 | ||
26 | ||
27 | class Memtop(Analysis): | |
b6d9132b | 28 | def __init__(self, state, conf): |
39fec005 AB |
29 | notification_cbs = { |
30 | 'tid_page_alloc': self._process_tid_page_alloc, | |
31 | 'tid_page_free': self._process_tid_page_free | |
32 | } | |
33 | ||
b6d9132b | 34 | super().__init__(state, conf) |
39fec005 | 35 | self._state.register_notification_cbs(notification_cbs) |
4e99adc5 | 36 | |
b6d9132b | 37 | self.tids = {} |
b5db5706 AB |
38 | |
39 | def reset(self): | |
39fec005 AB |
40 | for tid in self.tids: |
41 | self.tids[tid].reset() | |
42 | ||
43 | def _process_tid_page_alloc(self, **kwargs): | |
a621ba35 | 44 | cpu_id = kwargs['cpu_id'] |
39fec005 | 45 | proc = kwargs['proc'] |
a621ba35 | 46 | |
43b66dd6 AB |
47 | if not self._filter_process(proc): |
48 | return | |
a621ba35 AB |
49 | if not self._filter_cpu(cpu_id): |
50 | return | |
43b66dd6 | 51 | |
39fec005 AB |
52 | tid = proc.tid |
53 | if tid not in self.tids: | |
54 | self.tids[tid] = ProcessMemStats.new_from_process(proc) | |
55 | ||
56 | self.tids[tid].allocated_pages += 1 | |
57 | ||
58 | def _process_tid_page_free(self, **kwargs): | |
a621ba35 | 59 | cpu_id = kwargs['cpu_id'] |
39fec005 | 60 | proc = kwargs['proc'] |
a621ba35 | 61 | |
43b66dd6 AB |
62 | if not self._filter_process(proc): |
63 | return | |
a621ba35 AB |
64 | if not self._filter_cpu(cpu_id): |
65 | return | |
43b66dd6 | 66 | |
39fec005 AB |
67 | tid = proc.tid |
68 | if tid not in self.tids: | |
69 | self.tids[tid] = ProcessMemStats.new_from_process(proc) | |
70 | ||
71 | self.tids[tid].freed_pages += 1 | |
72 | ||
4e3662f1 | 73 | |
6f89f629 | 74 | class ProcessMemStats(stats.Process): |
39fec005 | 75 | def __init__(self, pid, tid, comm): |
6f89f629 AB |
76 | super().__init__(pid, tid, comm) |
77 | ||
39fec005 AB |
78 | self.allocated_pages = 0 |
79 | self.freed_pages = 0 | |
80 | ||
39fec005 AB |
81 | def reset(self): |
82 | self.allocated_pages = 0 | |
83 | self.freed_pages = 0 |