Commit | Line | Data |
---|---|---|
c72a561f JG |
1 | # The MIT License (MIT) |
2 | # | |
3 | # Copyright (c) 2013-2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
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 | |
21 | # THE SOFTWARE. | |
22 | ||
23 | import bt2 | |
24 | import itertools | |
25 | from babeltrace import reader_event_declaration | |
26 | ||
27 | ||
28 | class TraceHandle: | |
29 | """ | |
30 | A :class:`TraceHandle` is a handle allowing the user to manipulate | |
31 | a specific trace directly. It is a unique identifier representing a | |
32 | trace, and is not meant to be instantiated by the user. | |
33 | """ | |
34 | ||
35 | def __init__(self): | |
36 | raise NotImplementedError("TraceHandle cannot be instantiated") | |
37 | ||
38 | def __repr__(self): | |
39 | # TODO print an id or some information about component / query result? | |
40 | return "Babeltrace TraceHandle: trace_id('{0}')".format(self._id) | |
41 | ||
42 | def __hash__(self): | |
43 | return hash((self.path, self.id)) | |
44 | ||
45 | def __eq__(self, other): | |
46 | if type(other) is not type(self): | |
47 | return False | |
48 | ||
49 | return (self.path, self.id) == (other.path, other.id) | |
50 | ||
51 | @property | |
52 | def id(self): | |
53 | """ | |
54 | Numeric ID of this trace handle. | |
55 | """ | |
56 | ||
57 | return self._id | |
58 | ||
59 | @property | |
60 | def path(self): | |
61 | """ | |
62 | Path of the underlying trace. | |
63 | """ | |
64 | ||
65 | return self._path | |
66 | ||
67 | def _query_trace_info(self): | |
68 | try: | |
69 | result = bt2.QueryExecutor().query(self._trace_collection._fs_comp_cls, | |
70 | 'trace-info', {'path': self._path}) | |
71 | except: | |
72 | raise ValueError | |
73 | ||
74 | assert(len(result) == 1) | |
75 | return result | |
76 | ||
77 | @property | |
78 | def timestamp_begin(self): | |
79 | """ | |
80 | Buffers creation timestamp (nanoseconds since Epoch) of the | |
81 | underlying trace. | |
82 | """ | |
83 | ||
84 | result = self._query_trace_info() | |
85 | ||
86 | try: | |
87 | return int(result[0]['range-ns']['begin']) | |
88 | except: | |
89 | raise ValueError | |
90 | ||
91 | @property | |
92 | def timestamp_end(self): | |
93 | """ | |
94 | Buffers destruction timestamp (nanoseconds since Epoch) of the | |
95 | underlying trace. | |
96 | """ | |
97 | ||
98 | result = self._query_trace_info() | |
99 | ||
100 | try: | |
101 | return int(result[0]['range-ns']['end']) | |
102 | except: | |
103 | raise ValueError | |
104 | ||
105 | @property | |
106 | def _has_intersection(self): | |
107 | result = self._query_trace_info() | |
108 | ||
109 | try: | |
110 | return 'intersection-range-ns' in result[0] | |
111 | except: | |
112 | raise ValueError | |
113 | ||
114 | def _get_event_declarations(self): | |
115 | notif_iter = bt2.TraceCollectionNotificationIterator([ | |
3d60267b | 116 | bt2.ComponentSpec('ctf', 'fs', self._path) |
c72a561f JG |
117 | ]) |
118 | ||
119 | # raises if the trace contains no streams | |
120 | first_notif = next(notif_iter) | |
121 | assert(type(first_notif) is bt2.StreamBeginningNotification) | |
122 | trace = first_notif.stream.stream_class.trace | |
123 | ec_iters = [sc.values() for sc in trace.values()] | |
124 | return map(reader_event_declaration._create_event_declaration, | |
125 | itertools.chain(*ec_iters)) | |
126 | ||
127 | @property | |
128 | def events(self): | |
129 | """ | |
130 | Generates all the :class:`EventDeclaration` objects of the | |
131 | underlying trace. | |
132 | """ | |
133 | ||
134 | try: | |
135 | return self._get_event_declarations() | |
136 | except: | |
137 | return |