fix: stats with 0 requests
[deliverable/lttng-analyses.git] / linuxautomaton / linuxautomaton / mem.py
CommitLineData
4ed24f86
JD
1#!/usr/bin/env python3
2#
3# The MIT License (MIT)
4#
a3fa57c0 5# Copyright (C) 2015 - Julien Desfossez <jdesfossez@efficios.com>
39fec005 6# 2015 - Antoine Busque <abusque@efficios.com>
4ed24f86
JD
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24# SOFTWARE.
25
4e99adc5
JD
26from linuxautomaton import sp
27
28
29class MemStateProvider(sp.StateProvider):
30 def __init__(self, state):
4e99adc5
JD
31 cbs = {
32 'mm_page_alloc': self._process_mm_page_alloc,
994541c3 33 'mm_page_free': self._process_mm_page_free
4e99adc5 34 }
ec0bf342
AB
35
36 self._state = state
4e99adc5
JD
37 self._register_cbs(cbs)
38
39 def process_event(self, ev):
40 self._process_event_cb(ev)
41
42 def _get_current_proc(self, event):
3f52a605 43 cpu_id = event['cpu_id']
ec0bf342 44 if cpu_id not in self._state.cpus:
4e99adc5 45 return None
994541c3 46
ec0bf342 47 cpu = self._state.cpus[cpu_id]
2b4a3c12 48 if cpu.current_tid is None:
4e99adc5 49 return None
994541c3 50
ec0bf342 51 return self._state.tids[cpu.current_tid]
4e99adc5
JD
52
53 def _process_mm_page_alloc(self, event):
ec0bf342 54 self._state.mm.page_count += 1
994541c3
AB
55
56 # Increment the number of pages allocated during the execution
e379eca1 57 # of all currently syscall io requests
ec0bf342 58 for process in self._state.tids.values():
e379eca1 59 if process.current_syscall is None:
4e99adc5 60 continue
994541c3 61
e379eca1
AB
62 if process.current_syscall.io_rq:
63 process.current_syscall.io_rq.pages_allocated += 1
994541c3
AB
64
65 current_process = self._get_current_proc(event)
66 if current_process is None:
4e99adc5 67 return
994541c3 68
ec0bf342
AB
69 self._state.send_notification_cb('tid_page_alloc',
70 proc=current_process)
4e99adc5
JD
71
72 def _process_mm_page_free(self, event):
ec0bf342 73 if self._state.mm.page_count == 0:
4e99adc5 74 return
4e99adc5 75
ec0bf342 76 self._state.mm.page_count -= 1
994541c3 77
994541c3
AB
78 current_process = self._get_current_proc(event)
79 if current_process is None:
4e99adc5 80 return
994541c3 81
ec0bf342 82 self._state.send_notification_cb('tid_page_free', proc=current_process)
This page took 0.028772 seconds and 5 git commands to generate.