Fix signedness in some field extraction
[lttv.git] / lttv / lttv / traceset-process.c
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
3 * Copyright (C) 2012 Yannick Brosseau
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License Version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
17 * MA 02111-1307, USA.
18 */
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <lttv/traceset-process.h>
24 #include <lttv/traceset.h>
25 #include <lttv/event.h>
26 #include <babeltrace/context.h>
27 #include <babeltrace/iterator.h>
28 #include <babeltrace/trace-handle.h>
29 #include <babeltrace/ctf/events.h>
30 #include <babeltrace/ctf/iterator.h>
31
32 void lttv_process_traceset_begin(LttvTraceset *traceset,
33 LttvHooks *before_traceset,
34 LttvHooks *before_trace,
35 LttvHooks *event)
36 {
37 struct bt_iter_pos begin_pos;
38 /* simply add hooks in context. _before hooks are called by add_hooks. */
39 /* It calls all before_traceset, before_trace, and before_tracefile hooks. */
40 lttv_traceset_add_hooks(traceset,
41 before_traceset,
42 before_trace,
43 event);
44
45
46
47 begin_pos.type = BT_SEEK_BEGIN;
48
49 if(!traceset->iter) {
50 traceset->iter = bt_ctf_iter_create(lttv_traceset_get_context(traceset),
51 &begin_pos,
52 NULL);
53 }
54 }
55
56 guint lttv_process_traceset_middle(LttvTraceset *traceset,
57 LttTime end,
58 gulong nb_events,
59 const LttvTracesetPosition *end_position)
60 {
61 unsigned count = 0;
62 gint last_ret = 0;
63 LttvTracesetPosition *currentPos;
64
65 struct bt_ctf_event *bt_event;
66
67 LttvEvent event;
68
69 while(TRUE) {
70
71 if(last_ret == TRUE || ((count >= nb_events) && (nb_events != G_MAXULONG))) {
72 break;
73 }
74
75 if((bt_event = bt_ctf_iter_read_event(traceset->iter)) != NULL) {
76
77 LttTime time = ltt_time_from_uint64(bt_ctf_get_timestamp_raw(bt_event));
78 if(ltt_time_compare(end, time) <= 0) {
79 break;
80 }
81
82 currentPos = lttv_traceset_create_current_position(traceset);
83 if(lttv_traceset_position_compare(currentPos,end_position ) == 0){
84 lttv_traceset_destroy_position(currentPos);
85 break;
86 }
87 lttv_traceset_destroy_position(currentPos);
88 count++;
89
90 event.bt_event = bt_event;
91
92 /* Retreive the associated state */
93 event.state = g_ptr_array_index(traceset->state_trace_handle_index,
94 bt_ctf_event_get_handle_id(bt_event));
95
96 last_ret = lttv_hooks_call(traceset->event_hooks, &event);
97
98 if(bt_iter_next(bt_ctf_get_iter(traceset->iter)) < 0) {
99 printf("ERROR NEXT\n");
100 break;
101 }
102 } else {
103 /* READ FAILED */
104
105 break;
106
107 }
108 }
109 return count;
110 }
111
112 void lttv_process_traceset_end(LttvTraceset *traceset,
113 LttvHooks *after_traceset,
114 LttvHooks *after_trace,
115 LttvHooks *event)
116 {
117 /* Remove hooks from context. _after hooks are called by remove_hooks. */
118 /* It calls all after_traceset, after_trace, and after_tracefile hooks. */
119 lttv_traceset_remove_hooks(traceset,
120 after_traceset,
121 after_trace,
122 event);
123
124 }
125
126
127 void lttv_traceset_add_hooks(LttvTraceset *traceset,
128 LttvHooks *before_traceset,
129 LttvHooks *before_trace,
130 LttvHooks *event)
131 {
132 guint i, nb_trace;
133
134 LttvTrace *trace;
135
136 lttv_hooks_call(before_traceset, traceset);
137
138 lttv_hooks_add_list(traceset->event_hooks, event);
139
140 nb_trace = lttv_traceset_number(traceset);
141
142 for(i = 0 ; i < nb_trace ; i++) {
143 trace = (LttvTrace *)g_ptr_array_index(traceset->traces,i);
144 lttv_trace_add_hooks(trace,
145 before_trace,
146 event
147 );
148 }
149 }
150 void lttv_traceset_remove_hooks(LttvTraceset *traceset,
151 LttvHooks *after_traceset,
152 LttvHooks *after_trace,
153 LttvHooks *event)
154 {
155 guint i, nb_trace;
156
157 LttvTrace *trace;
158
159 nb_trace = lttv_traceset_number(traceset);
160
161 for(i = 0 ; i < nb_trace ; i++) {
162 trace = (LttvTrace *)g_ptr_array_index(traceset->traces,i);
163 lttv_trace_remove_hooks(trace,
164 after_trace,
165 event);
166
167 }
168
169 lttv_hooks_remove_list(traceset->event_hooks, event);
170 lttv_hooks_call(after_traceset, traceset);
171
172
173 }
174
175
176 void lttv_trace_add_hooks(LttvTrace *trace,
177 LttvHooks *before_trace,
178 LttvHooks *event)
179 {
180 lttv_hooks_call(before_trace, trace);
181 }
182
183 void lttv_trace_remove_hooks(LttvTrace *trace,
184 LttvHooks *after_trace,
185 LttvHooks *event)
186
187 {
188 lttv_hooks_call(after_trace, trace);
189 }
190
191 void lttv_process_traceset_seek_time(LttvTraceset *traceset, LttTime start)
192 {
193 struct bt_iter_pos seekpos;
194 int ret;
195 if (traceset->iter == NULL) {
196 g_warning("Iterator not valid in seek_time");
197 return;
198 }
199 seekpos.type = BT_SEEK_TIME;
200 seekpos.u.seek_time = ltt_time_to_uint64(start);
201
202 ret = bt_iter_set_pos(bt_ctf_get_iter(traceset->iter), &seekpos);
203 if(ret < 0) {
204 printf("Seek by time error: %s,\n",strerror(-ret));
205 }
206 }
207
208 guint lttv_process_traceset_seek_n_forward(LttvTraceset *traceset,
209 guint n,
210 check_handler *check,
211 gboolean *stop_flag,
212 LttvFilter *filter1,
213 LttvFilter *filter2,
214 LttvFilter *filter3,
215 gpointer data)
216 {
217 unsigned count = 0;
218 while(count < n) {
219 if(bt_iter_next(bt_ctf_get_iter(traceset->iter)) < 0) {
220 printf("ERROR NEXT\n");
221 break;
222 }
223 count++;
224 }
225 return count;
226 }
227
228 guint lttv_process_traceset_seek_n_backward(LttvTraceset *ts,
229 guint n,
230 gdouble ratio,
231 check_handler *check,
232 gboolean *stop_flag,
233 LttvFilter *filter1,
234 LttvFilter *filter2,
235 LttvFilter *filter3,
236 gpointer data)
237 {
238 guint i, count, ret;
239 gint extraEvent = 0;
240 guint64 initialTimeStamp, previousTimeStamp;
241 LttvTracesetPosition *initialPos, *previousPos, *currentPos;
242
243 /*Save initial position of the traceset*/
244 initialPos = lttv_traceset_create_current_position (ts);
245
246 /*Get the timespan of the initial position*/
247 initialTimeStamp = lttv_traceset_position_get_timestamp(initialPos);
248 /*
249 * Create a position before the initial timestamp according
250 * to the ratio of nanosecond/event hopefully before the
251 * the desired seek position
252 */
253 while(1){
254 previousTimeStamp = initialTimeStamp - n*(guint)ceil(ratio);
255
256 previousPos = lttv_traceset_create_time_position(ts,ltt_time_from_uint64(previousTimeStamp));
257 if(initialTimeStamp == previousTimeStamp)
258 break;
259
260 currentPos = lttv_traceset_create_time_position(ts,ltt_time_from_uint64(previousTimeStamp));
261 /*move traceset position */
262 lttv_state_traceset_seek_position(ts, previousPos);
263 /* iterate to the initial position counting the number of event*/
264 count = 0;
265 do {
266 if((ret = lttv_traceset_position_compare(currentPos,initialPos)) == 1){
267 bt_iter_next(bt_ctf_get_iter(ts->iter));
268 lttv_traceset_destroy_position(currentPos);
269 currentPos = lttv_traceset_create_current_position(ts);
270 count++;
271 }
272 }while(ret != 0);
273 lttv_traceset_destroy_position(currentPos);
274 /*substract the desired number of event to the count*/
275 extraEvent = count - n;
276 if(extraEvent >= 0){
277 //if the extraEvent is over 0 go back to previousPos and
278 //move forward the value of extraEvent times
279 lttv_state_traceset_seek_position(ts, previousPos);
280
281 for(i = 0 ; i < extraEvent ; i++){
282 if(bt_iter_next(bt_ctf_get_iter(ts->iter)) < 0){
283 printf("ERROR NEXT\n");
284 break;
285 }
286
287 }
288 break; /* we successfully seeked backward */
289 }
290 else{ /* if the extraEvent is below 0 create a position before and start over*/
291 ratio = ratio * 16;
292 }
293 }
294 return 0;
295 }
This page took 0.040747 seconds and 5 git commands to generate.