Refactor in a single package with subpackages
[deliverable/lttng-analyses.git] / lttnganalyses / linuxautomaton / irq.py
1 #!/usr/bin/env python3
2 #
3 # The MIT License (MIT)
4 #
5 # Copyright (C) 2015 - Julien Desfossez <jdesfossez@efficios.com>
6 # 2015 - Antoine Busque <abusque@efficios.com>
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
26 from . import sp, sv
27
28
29 class IrqStateProvider(sp.StateProvider):
30 def __init__(self, state):
31 cbs = {
32 'irq_handler_entry': self._process_irq_handler_entry,
33 'irq_handler_exit': self._process_irq_handler_exit,
34 'softirq_raise': self._process_softirq_raise,
35 'softirq_entry': self._process_softirq_entry,
36 'softirq_exit': self._process_softirq_exit
37 }
38
39 self._state = state
40 self._register_cbs(cbs)
41
42 def process_event(self, ev):
43 self._process_event_cb(ev)
44
45 def _get_cpu(self, cpu_id):
46 if cpu_id not in self._state.cpus:
47 self._state.cpus[cpu_id] = sv.CPU(cpu_id)
48
49 return self._state.cpus[cpu_id]
50
51 # Hard IRQs
52 def _process_irq_handler_entry(self, event):
53 cpu = self._get_cpu(event['cpu_id'])
54 irq = sv.HardIRQ.new_from_irq_handler_entry(event)
55 cpu.current_hard_irq = irq
56
57 self._state.send_notification_cb('irq_handler_entry',
58 id=irq.id,
59 irq_name=event['name'])
60
61 def _process_irq_handler_exit(self, event):
62 cpu = self._get_cpu(event['cpu_id'])
63 if cpu.current_hard_irq is None or \
64 cpu.current_hard_irq.id != event['irq']:
65 cpu.current_hard_irq = None
66 return
67
68 cpu.current_hard_irq.end_ts = event.timestamp
69 cpu.current_hard_irq.ret = event['ret']
70
71 self._state.send_notification_cb('irq_handler_exit',
72 hard_irq=cpu.current_hard_irq)
73 cpu.current_hard_irq = None
74
75 # SoftIRQs
76 def _process_softirq_raise(self, event):
77 cpu = self._get_cpu(event['cpu_id'])
78 vec = event['vec']
79
80 if vec not in cpu.current_softirqs:
81 cpu.current_softirqs[vec] = []
82
83 # Don't append a SoftIRQ object if one has already been raised,
84 # because they are level-triggered. The only exception to this
85 # is if the first SoftIRQ object already had a begin_ts which
86 # means this raise was triggered after its entry, and will be
87 # handled in the following softirq_entry
88 if cpu.current_softirqs[vec] and \
89 cpu.current_softirqs[vec][0].begin_ts is None:
90 return
91
92 irq = sv.SoftIRQ.new_from_softirq_raise(event)
93 cpu.current_softirqs[vec].append(irq)
94
95 def _process_softirq_entry(self, event):
96 cpu = self._get_cpu(event['cpu_id'])
97 vec = event['vec']
98
99 if cpu.current_softirqs[vec]:
100 cpu.current_softirqs[vec][0].begin_ts = event.timestamp
101 else:
102 # SoftIRQ entry without a corresponding raise
103 irq = sv.SoftIRQ.new_from_softirq_entry(event)
104 cpu.current_softirqs[vec].append(irq)
105
106 def _process_softirq_exit(self, event):
107 cpu = self._get_cpu(event['cpu_id'])
108 vec = event['vec']
109
110 if not cpu.current_softirqs[vec]:
111 return
112
113 cpu.current_softirqs[vec][0].end_ts = event.timestamp
114 self._state.send_notification_cb('softirq_exit',
115 softirq=cpu.current_softirqs[vec][0])
116 del cpu.current_softirqs[vec][0]
This page took 0.033782 seconds and 5 git commands to generate.