]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/plr/tools/tsar/tsar_events.py
f94d3fcb036be453ecfbc1e905aaaaf8df991e41
[l4.git] / l4 / pkg / plr / tools / tsar / tsar_events.py
1 #!/usr/bin/python
2
3 """TSAR Event type definitions"""
4
5 import struct
6 import sys
7
8 #pylint: disable=C0103,R0903
9
10
11 class Event:
12     """Event base class"""
13
14     HEADSIZE = 13
15     EVENTSIZE = 32
16
17     SYSCALL_TYPE = 1
18     PAGEFAULT_TYPE = 2
19     SWIFI_TYPE = 3
20     FOO_TYPE = 4
21     TRAP_TYPE = 5
22     THREAD_START_TYPE = 6
23     THREAD_STOP_TYPE = 7
24     LOCKING_TYPE = 8
25     SHMLOCKING_TYPE = 9
26     BARNES_TYPE = 10
27
28     @staticmethod
29     def eventname(event):
30         """Get name for event type"""
31         syscall_names = ["INV", "SYS", "PF",
32                          "SWIFI", "FOO", "TRAP",
33                          "START", "STOP", "LOCK", "SHML"]
34         return syscall_names[event]
35
36     def __init__(self, time=0, typ=0, utcb=0, uid=None):
37         self.timestamp = time
38         self.type = typ
39         self.utcb = utcb
40         if uid is None:
41             (self.id2, self.id1) = ("", self.utcb)
42         else:
43             (self.id2, self.id1) = uid.split(":")
44
45     def uid(self):
46         """Unique event source ID (NOT unique event ID!)"""
47         return "%s:%s" % (self.id1, self.id2)
48
49     def __repr__(self):
50         try:
51             return "%d [%8x|%5s] (%s,%s) " % (self.timestamp, self.utcb,
52                                               Event.eventname(self.type),
53                                               str(self.id1),
54                                               str(self.id2))
55         except TypeError:
56             print self.timestamp
57             sys.exit(1)
58
59     def pretty(self):
60         """Pretty-print event"""
61         return " %d" % self.type
62
63
64 class SyscallEvent(Event):
65     """Romain system call event"""
66     def __init__(self, raw, time=0, utcb=0, uid=None):
67         Event.__init__(self, time, Event.SYSCALL_TYPE, utcb, uid)
68         (self.eip, self.label, ) = struct.unpack_from("II",
69                                                       raw[Event.HEADSIZE:])
70
71     def __repr__(self):
72         return Event.__repr__(self) + " SYSCALL %08x, ret to %08x" % \
73             (self.label, self.eip)
74
75     def pretty(self):
76         return ["SYSCALL %08x" % (self.label),
77                 " ret -> %08x" % (self.eip)]
78
79
80 class PagefaultEvent(Event):
81     """Page fault event"""
82     def __init__(self, raw, time=0, utcb=0, uid=None):
83         Event.__init__(self, time, Event.PAGEFAULT_TYPE, utcb, uid)
84         (self.writepf, ) = struct.unpack_from("B", raw[Event.HEADSIZE:])
85         (self.pfa,
86          self.local,
87          self.remote) = struct.unpack_from("III", raw[Event.HEADSIZE + 1:])
88         #print hex(self.pfa)
89
90     def __repr__(self):
91         res = Event.__repr__(self)
92         if (self.writepf):
93             res += " w"
94         res += "r pf @ %08x -> %08x" % (self.local, self.remote)
95         return res
96
97     def pretty(self):
98         res = []
99         if self.writepf:
100             res += ["wr pf @ 0x%x" % self.pfa]
101         else:
102             res += ["r pf @ 0x%x" % self.pfa]
103         res += ["%x -> %x" % (self.local, self.remote)]
104         return res
105
106
107 class SwifiEvent(Event):
108     """FI event"""
109     def __init__(self, raw, time=0, utcb=0, uid=None):
110         Event.__init__(self, time, Event.SWIFI_TYPE, utcb, uid)
111
112     def pretty(self):
113         return ["SWIFI"]
114
115
116 class FooEvent(Event):
117     """Foo test event"""
118     def __init__(self, raw, time=0, utcb=0, uid=None):
119         Event.__init__(self, time, Event.FOO_TYPE, utcb, uid)
120         (self.is_start, ) = struct.unpack_from("I", raw[Event.HEADSIZE:])
121
122     def __repr__(self):
123         res = Event.__repr__(self)
124         if self.is_start == 0:
125             res += " STOP"
126         else:
127             res += " START"
128         return res
129
130     def pretty(self):
131         return ["FOO"]
132
133
134 class TrapEvent(Event):
135     """Generic trap event"""
136     counters = {}
137
138     def __init__(self, raw, time=0, utcb=0, uid=None):
139         Event.__init__(self, time, Event.TRAP_TYPE, utcb, uid)
140         (self.is_start, ) = struct.unpack_from("B", raw[Event.HEADSIZE:])
141         (self.trapaddr, self.trapno, ) = \
142             struct.unpack_from("II", raw[Event.HEADSIZE + 1:])
143
144         #print "S %d T %d" % (self.is_start, self.trapno)
145
146     def __repr__(self):
147         res = Event.__repr__(self)
148         if self.is_start == 1:
149             res += " start, trapno %x" % self.trapno
150         else:
151             res += " done"
152         return res
153
154     def pretty(self):
155         if self.is_start:
156             return ["\033[33mTRAP %x @ %08x\033[0m" %
157                     (self.trapno, self.trapaddr)]
158         else:
159             return ["--- TRAP END ---"]
160
161
162 class ThreadStartEvent(Event):
163     """Thread start event"""
164
165     def __init__(self, raw, time=0, utcb=0, uid=None):
166         Event.__init__(self, time, Event.THREAD_START_TYPE, utcb, uid)
167
168     def __repr__(self):
169         res = Event.__repr__(self)
170         res += "Thread::Start"
171         return res
172
173     def pretty(self):
174         return ["Thread::Start"]
175
176
177 class ThreadStopEvent(Event):
178     """Thread end event"""
179
180     def __init__(self, raw, time=0, utcb=0, uid=None):
181         Event.__init__(self, time, Event.THREAD_STOP_TYPE, utcb, uid)
182
183     def __repr__(self):
184         res = Event.__repr__(self)
185         res += "Thread::Exit"
186         return res
187
188     def pretty(self):
189         return ["Thread::Exit"]
190
191
192 class LockingEvent(Event):
193     """Lock interception event"""
194     def __init__(self, raw, time=0, utcb=0, uid=None):
195         Event.__init__(self, time, Event.LOCKING_TYPE, utcb, uid)
196         (self.locktype, ) = \
197             struct.unpack_from("I", raw[Event.HEADSIZE:])
198
199         self.typenames = ["__lock", "__unlock", "mtx_lock", "mtx_unlock"]
200
201     def __repr__(self):
202         res = Event.__repr__(self)
203         res += "LOCK(%d)" % self.locktype
204         return res
205
206     def pretty(self):
207         return ["Lock(%s)" % self.typenames[self.locktype]]
208
209
210 class SHMLockingEvent(Event):
211     """SHM locking event"""
212     def __init__(self, raw, time=0, utcb=0, uid=None):
213         Event.__init__(self, time, Event.SHMLOCKING_TYPE, utcb, uid)
214         (self.lockid, self.epoch, self.owner, self.evtype) = \
215             struct.unpack_from("IIII", raw[Event.HEADSIZE:])
216
217     def __repr__(self):
218         res = Event.__repr__(self)
219         res += "SHM %x, %d, %x" % (self.lockid, self.epoch, self.evtype)
220         return res
221
222     def pretty(self):
223
224         st1 = ["down1", "down2", "up1", "up2"][self.evtype - 2]
225         st1 += "(%x)" % self.lockid
226
227         st2 = "  ID %x, EP %x" % (self.utcb, self.epoch)
228         st3 = ""
229
230         if self.evtype in [2, 5]:
231             if self.owner == 0xffffffff:
232                 st3 = "(owner: %x)" % self.owner
233             else:
234                 st3 = "(owner: \033[31;1m%x\033[0m)" % self.owner
235
236         if self.evtype == 2:
237             return ["\033[34;1m==========\033[0m", st1, st2, st3]
238         if self.evtype == 5:
239             return [st1, st3, "\033[34m==========\033[0m"]
240
241         return [st1]
242
243
244 class BarnesEvent(Event):
245     """SPLASH2::Barnes runtime event"""
246     def __init__(self, raw, time=0, utcb=0, uid=None):
247         Event.__init__(self, time, Event.SHMLOCKING_TYPE, utcb, uid)
248         (self.ptr, self.num, self.type) = \
249             struct.unpack_from("III", raw[Event.HEADSIZE:])
250
251     def __repr__(self):
252         return "Barnes(%d) ptr %x num %d" % (self.type, self.ptr, self.num)
253
254     def pretty(self):
255         return ["\033[32mbarnes(%d)\033[0m" % self.type,
256                 "%x %d\033[0m" % (self.ptr, self.num)]