Cleanup license (MIT)
[deliverable/lttng-analyses.git] / linuxautomaton / linuxautomaton / mem.py
1 #!/usr/bin/env python3
2 #
3 # The MIT License (MIT)
4 #
5 # Copyright (C) 2015 - Julien Desfossez <jdesfosez@efficios.com>
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 # SOFTWARE.
24
25 from linuxautomaton import sp
26
27
28 class MemStateProvider(sp.StateProvider):
29 def __init__(self, state):
30 self.state = state
31 self.mm = state.mm
32 self.cpus = state.cpus
33 self.tids = state.tids
34 self.dirty_pages = state.dirty_pages
35 self.mm["allocated_pages"] = 0
36 self.mm["freed_pages"] = 0
37 self.mm["count"] = 0
38 self.mm["dirty"] = 0
39 self.dirty_pages["pages"] = []
40 self.dirty_pages["global_nr_dirty"] = -1
41 self.dirty_pages["base_nr_dirty"] = -1
42 cbs = {
43 'mm_page_alloc': self._process_mm_page_alloc,
44 'mm_page_free': self._process_mm_page_free,
45 'block_dirty_buffer': self._process_block_dirty_buffer,
46 'writeback_global_dirty_state':
47 self._process_writeback_global_dirty_state,
48 }
49 self._register_cbs(cbs)
50
51 def process_event(self, ev):
52 self._process_event_cb(ev)
53
54 def _get_current_proc(self, event):
55 cpu_id = event["cpu_id"]
56 if cpu_id not in self.cpus:
57 return None
58 c = self.cpus[cpu_id]
59 if c.current_tid == -1:
60 return None
61 return self.tids[c.current_tid]
62
63 def _process_mm_page_alloc(self, event):
64 self.mm["count"] += 1
65 self.mm["allocated_pages"] += 1
66 for p in self.tids.values():
67 if len(p.current_syscall.keys()) == 0:
68 continue
69 if "alloc" not in p.current_syscall.keys():
70 p.current_syscall["alloc"] = 1
71 else:
72 p.current_syscall["alloc"] += 1
73 t = self._get_current_proc(event)
74 if t is None:
75 return
76 t.allocated_pages += 1
77
78 def _process_mm_page_free(self, event):
79 self.mm["freed_pages"] += 1
80 if self.mm["count"] == 0:
81 return
82 self.mm["count"] -= 1
83 t = self._get_current_proc(event)
84 if t is None:
85 return
86 t.freed_pages += 1
87
88 def _process_block_dirty_buffer(self, event):
89 self.mm["dirty"] += 1
90 if event["cpu_id"] not in self.cpus.keys():
91 return
92 c = self.cpus[event["cpu_id"]]
93 if c.current_tid <= 0:
94 return
95 p = self.tids[c.current_tid]
96 current_syscall = self.tids[c.current_tid].current_syscall
97 if len(current_syscall.keys()) == 0:
98 return
99 if self.dirty_pages is None:
100 return
101 if "fd" in current_syscall.keys():
102 self.dirty_pages["pages"].append((p, current_syscall["name"],
103 current_syscall["fd"].filename,
104 current_syscall["fd"].fd))
105 return
106
107 def _process_writeback_global_dirty_state(self, event):
108 # print("%s count : %d, count dirty : %d, nr_dirty : %d, "
109 # "nr_writeback : %d, nr_dirtied : %d, nr_written : %d" %
110 # (common.ns_to_hour_nsec(event.timestamp), self.mm["count"],
111 # self.mm["dirty"], event["nr_dirty"],
112 # event["nr_writeback"], event["nr_dirtied"],
113 # event["nr_written"]))
114 self.mm["dirty"] = 0
This page took 0.034119 seconds and 5 git commands to generate.