9c312311 |
1 | /* This file is part of the Linux Trace Toolkit viewer |
2 | * Copyright (C) 2003-2004 Michel Dagenais |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License Version 2 as |
6 | * published by the Free Software Foundation; |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License |
14 | * along with this program; if not, write to the Free Software |
15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
16 | * MA 02111-1307, USA. |
17 | */ |
18 | |
dc877563 |
19 | |
08b1c66e |
20 | #include <lttv/lttv.h> |
21 | #include <lttv/module.h> |
dc877563 |
22 | #include <lttv/state.h> |
ba576a78 |
23 | #include <ltt/facility.h> |
24 | #include <ltt/trace.h> |
308711e5 |
25 | #include <ltt/event.h> |
a5dcde2f |
26 | #include <ltt/type.h> |
dc877563 |
27 | |
b445142a |
28 | LttvExecutionMode |
29 | LTTV_STATE_MODE_UNKNOWN, |
ffd54a90 |
30 | LTTV_STATE_USER_MODE, |
31 | LTTV_STATE_SYSCALL, |
32 | LTTV_STATE_TRAP, |
33 | LTTV_STATE_IRQ; |
34 | |
b445142a |
35 | LttvExecutionSubmode |
36 | LTTV_STATE_SUBMODE_UNKNOWN, |
37 | LTTV_STATE_SUBMODE_NONE; |
ffd54a90 |
38 | |
39 | LttvProcessStatus |
40 | LTTV_STATE_UNNAMED, |
41 | LTTV_STATE_WAIT_FORK, |
42 | LTTV_STATE_WAIT_CPU, |
43 | LTTV_STATE_EXIT, |
44 | LTTV_STATE_WAIT, |
45 | LTTV_STATE_RUN; |
46 | |
ba576a78 |
47 | static GQuark |
308711e5 |
48 | LTTV_STATE_TRACEFILES, |
49 | LTTV_STATE_PROCESSES, |
50 | LTTV_STATE_PROCESS, |
51 | LTTV_STATE_EVENT, |
52 | LTTV_STATE_SAVED_STATES, |
dbb7bb09 |
53 | LTTV_STATE_SAVED_STATES_TIME, |
308711e5 |
54 | LTTV_STATE_TIME, |
ba576a78 |
55 | LTTV_STATE_HOOKS; |
56 | |
b445142a |
57 | |
58 | static void fill_name_tables(LttvTraceState *tcs); |
59 | |
60 | static void free_name_tables(LttvTraceState *tcs); |
61 | |
308711e5 |
62 | static void lttv_state_free_process_table(GHashTable *processes); |
ba576a78 |
63 | |
dc877563 |
64 | |
308711e5 |
65 | void lttv_state_save(LttvTraceState *self, LttvAttribute *container) |
66 | { |
67 | LTTV_TRACE_STATE_GET_CLASS(self)->state_save(self, container); |
68 | } |
69 | |
70 | |
71 | void lttv_state_restore(LttvTraceState *self, LttvAttribute *container) |
72 | { |
73 | LTTV_TRACE_STATE_GET_CLASS(self)->state_restore(self, container); |
74 | } |
75 | |
76 | |
77 | void lttv_state_saved_state_free(LttvTraceState *self, |
78 | LttvAttribute *container) |
79 | { |
80 | LTTV_TRACE_STATE_GET_CLASS(self)->state_restore(self, container); |
81 | } |
82 | |
83 | |
2a2fa4f0 |
84 | guint process_hash(gconstpointer key) |
85 | { |
86 | return ((LttvProcessState *)key)->pid; |
87 | } |
88 | |
89 | |
90 | gboolean process_equal(gconstpointer a, gconstpointer b) |
91 | { |
92 | LttvProcessState *process_a, *process_b; |
93 | |
94 | process_a = (LttvProcessState *)a; |
95 | process_b = (LttvProcessState *)b; |
96 | |
97 | if(process_a->pid != process_b->pid) return FALSE; |
98 | if(process_a->pid == 0 && |
99 | process_a->last_cpu != process_b->last_cpu) return FALSE; |
100 | return TRUE; |
101 | } |
102 | |
103 | |
308711e5 |
104 | static void |
105 | restore_init_state(LttvTraceState *self) |
106 | { |
dbb7bb09 |
107 | guint i, nb_tracefile; |
308711e5 |
108 | |
109 | LttvTracefileState *tfcs; |
110 | |
111 | LttTime null_time = {0,0}; |
112 | |
113 | if(self->processes != NULL) lttv_state_free_process_table(self->processes); |
2a2fa4f0 |
114 | self->processes = g_hash_table_new(process_hash, process_equal); |
308711e5 |
115 | self->nb_event = 0; |
116 | |
dbb7bb09 |
117 | nb_tracefile = ltt_trace_control_tracefile_number(self->parent.t) + |
118 | ltt_trace_per_cpu_tracefile_number(self->parent.t); |
308711e5 |
119 | |
dbb7bb09 |
120 | for(i = 0 ; i < nb_tracefile ; i++) { |
121 | tfcs = LTTV_TRACEFILE_STATE(self->parent.tracefiles[i]); |
308711e5 |
122 | tfcs->parent.timestamp = null_time; |
123 | tfcs->saved_position = 0; |
2a2fa4f0 |
124 | tfcs->process = lttv_state_create_process(tfcs, NULL,0); |
125 | tfcs->process->state->s = LTTV_STATE_RUN; |
126 | tfcs->process->last_cpu = tfcs->cpu_name; |
308711e5 |
127 | } |
128 | } |
129 | |
2a2fa4f0 |
130 | static LttTime time_zero = {0,0}; |
308711e5 |
131 | |
dc877563 |
132 | static void |
133 | init(LttvTracesetState *self, LttvTraceset *ts) |
134 | { |
dbb7bb09 |
135 | guint i, j, nb_trace, nb_tracefile; |
dc877563 |
136 | |
ffd54a90 |
137 | LttvTraceContext *tc; |
dc877563 |
138 | |
ffd54a90 |
139 | LttvTraceState *tcs; |
140 | |
ffd54a90 |
141 | LttvTracefileState *tfcs; |
3d27549e |
142 | |
dbb7bb09 |
143 | LttvAttributeValue v; |
144 | |
b445142a |
145 | LTTV_TRACESET_CONTEXT_CLASS(g_type_class_peek(LTTV_TRACESET_CONTEXT_TYPE))-> |
146 | init((LttvTracesetContext *)self, ts); |
dc877563 |
147 | |
148 | nb_trace = lttv_traceset_number(ts); |
149 | for(i = 0 ; i < nb_trace ; i++) { |
b445142a |
150 | tc = self->parent.traces[i]; |
151 | tcs = (LttvTraceState *)tc; |
dbb7bb09 |
152 | tcs->save_interval = 50000; |
153 | lttv_attribute_find(tcs->parent.t_a, LTTV_STATE_SAVED_STATES_TIME, |
154 | LTTV_POINTER, &v); |
08b1c66e |
155 | if(*(v.v_pointer) == NULL) { |
156 | *(v.v_pointer) = g_new(LttTime,1); |
157 | *((LttTime *)*(v.v_pointer)) = time_zero; |
158 | } |
dbb7bb09 |
159 | tcs->max_time_state_recomputed_in_seek = (LttTime *)*(v.v_pointer); |
160 | |
b445142a |
161 | fill_name_tables(tcs); |
dc877563 |
162 | |
dbb7bb09 |
163 | nb_tracefile = ltt_trace_control_tracefile_number(tc->t) + |
164 | ltt_trace_per_cpu_tracefile_number(tc->t); |
165 | |
dc877563 |
166 | for(j = 0 ; j < nb_tracefile ; j++) { |
dbb7bb09 |
167 | tfcs = LTTV_TRACEFILE_STATE(tc->tracefiles[j]); |
b445142a |
168 | tfcs->cpu_name= g_quark_from_string(ltt_tracefile_name(tfcs->parent.tf)); |
dc877563 |
169 | } |
308711e5 |
170 | tcs->processes = NULL; |
171 | restore_init_state(tcs); |
dc877563 |
172 | } |
173 | } |
174 | |
175 | |
176 | static void |
177 | fini(LttvTracesetState *self) |
178 | { |
dbb7bb09 |
179 | guint i, j, nb_trace; |
dc877563 |
180 | |
ffd54a90 |
181 | LttvTraceState *tcs; |
dc877563 |
182 | |
ffd54a90 |
183 | LttvTracefileState *tfcs; |
dc877563 |
184 | |
ffd54a90 |
185 | nb_trace = lttv_traceset_number(LTTV_TRACESET_CONTEXT(self)->ts); |
dc877563 |
186 | for(i = 0 ; i < nb_trace ; i++) { |
ffd54a90 |
187 | tcs = (LttvTraceState *)(LTTV_TRACESET_CONTEXT(self)->traces[i]); |
308711e5 |
188 | lttv_state_free_process_table(tcs->processes); |
189 | tcs->processes = NULL; |
b445142a |
190 | free_name_tables(tcs); |
dc877563 |
191 | } |
b445142a |
192 | LTTV_TRACESET_CONTEXT_CLASS(g_type_class_peek(LTTV_TRACESET_CONTEXT_TYPE))-> |
193 | fini((LttvTracesetContext *)self); |
dc877563 |
194 | } |
195 | |
196 | |
c432246e |
197 | static LttvTracesetContext * |
dc877563 |
198 | new_traceset_context(LttvTracesetContext *self) |
199 | { |
ffd54a90 |
200 | return LTTV_TRACESET_CONTEXT(g_object_new(LTTV_TRACESET_STATE_TYPE, NULL)); |
dc877563 |
201 | } |
202 | |
203 | |
c432246e |
204 | static LttvTraceContext * |
dc877563 |
205 | new_trace_context(LttvTracesetContext *self) |
206 | { |
ffd54a90 |
207 | return LTTV_TRACE_CONTEXT(g_object_new(LTTV_TRACE_STATE_TYPE, NULL)); |
dc877563 |
208 | } |
209 | |
210 | |
c432246e |
211 | static LttvTracefileContext * |
dc877563 |
212 | new_tracefile_context(LttvTracesetContext *self) |
213 | { |
ffd54a90 |
214 | return LTTV_TRACEFILE_CONTEXT(g_object_new(LTTV_TRACEFILE_STATE_TYPE, NULL)); |
215 | } |
216 | |
217 | |
dbb7bb09 |
218 | /* Write the process state of the trace */ |
219 | |
220 | static void write_process_state(gpointer key, gpointer value, |
221 | gpointer user_data) |
222 | { |
223 | LttvProcessState *process; |
224 | |
225 | LttvExecutionState *es; |
226 | |
227 | FILE *fp = (FILE *)user_data; |
228 | |
229 | guint i; |
230 | |
231 | process = (LttvProcessState *)value; |
232 | fprintf(fp, |
233 | " <PROCESS PID=%u PPID=%u CTIME_S=%lu CTIME_NS=%lu NAME=\"%s\" CPU=\"%s\">\n", |
234 | process->pid, process->ppid, process->creation_time.tv_sec, |
235 | process->creation_time.tv_nsec, g_quark_to_string(process->name), |
236 | g_quark_to_string(process->last_cpu)); |
237 | |
238 | for(i = 0 ; i < process->execution_stack->len; i++) { |
239 | es = &g_array_index(process->execution_stack, LttvExecutionState, i); |
240 | fprintf(fp, " <ES MODE=\"%s\" SUBMODE=\"%s\" ENTRY_S=%lu ENTRY_NS=%lu", |
241 | g_quark_to_string(es->t), g_quark_to_string(es->n), |
242 | es->entry.tv_sec, es->entry.tv_nsec); |
243 | fprintf(fp, " CHANGE_S=%lu CHANGE_NS=%lu STATUS=\"%s\"/>\n", |
244 | es->change.tv_sec, es->change.tv_nsec, g_quark_to_string(es->s)); |
245 | } |
246 | fprintf(fp, " </PROCESS>\n"); |
247 | } |
248 | |
249 | |
250 | void lttv_state_write(LttvTraceState *self, LttTime t, FILE *fp) |
251 | { |
252 | guint i, nb_tracefile, nb_block, nb_event; |
253 | |
254 | LttvTracefileState *tfcs; |
255 | |
256 | LttTracefile *tf; |
257 | |
258 | LttEventPosition *ep; |
259 | |
260 | ep = ltt_event_position_new(); |
261 | |
262 | fprintf(fp,"<PROCESS_STATE TIME_S=%lu TIME_NS=%lu>\n", t.tv_sec, t.tv_nsec); |
263 | |
264 | g_hash_table_foreach(self->processes, write_process_state, fp); |
265 | |
266 | nb_tracefile = ltt_trace_control_tracefile_number(self->parent.t) + |
267 | ltt_trace_per_cpu_tracefile_number(self->parent.t); |
268 | |
269 | for(i = 0 ; i < nb_tracefile ; i++) { |
270 | tfcs = (LttvTracefileState *)self->parent.tracefiles[i]; |
08b1c66e |
271 | fprintf(fp, " <TRACEFILE PID=%u TIMESTAMP_S=%lu TIMESTAMP_NS=%lu", |
272 | tfcs->process->pid, tfcs->parent.timestamp.tv_sec, |
273 | tfcs->parent.timestamp.tv_nsec); |
dbb7bb09 |
274 | if(tfcs->parent.e == NULL) fprintf(fp,"/>\n"); |
275 | else { |
276 | ltt_event_position(tfcs->parent.e, ep); |
277 | ltt_event_position_get(ep, &nb_block, &nb_event, &tf); |
278 | fprintf(fp, " BLOCK=%u EVENT=%u/>\n", nb_block, nb_event); |
279 | } |
280 | } |
281 | g_free(ep); |
282 | fprintf(fp,"</PROCESS_STATE>"); |
283 | } |
284 | |
285 | |
286 | /* Copy each process from an existing hash table to a new one */ |
287 | |
308711e5 |
288 | static void copy_process_state(gpointer key, gpointer value,gpointer user_data) |
ffd54a90 |
289 | { |
308711e5 |
290 | LttvProcessState *process, *new_process; |
ffd54a90 |
291 | |
308711e5 |
292 | GHashTable *new_processes = (GHashTable *)user_data; |
ffd54a90 |
293 | |
308711e5 |
294 | guint i; |
295 | |
296 | process = (LttvProcessState *)value; |
297 | new_process = g_new(LttvProcessState, 1); |
298 | *new_process = *process; |
299 | new_process->execution_stack = g_array_new(FALSE, FALSE, |
300 | sizeof(LttvExecutionState)); |
301 | g_array_set_size(new_process->execution_stack,process->execution_stack->len); |
302 | for(i = 0 ; i < process->execution_stack->len; i++) { |
303 | g_array_index(new_process->execution_stack, LttvExecutionState, i) = |
304 | g_array_index(process->execution_stack, LttvExecutionState, i); |
305 | } |
306 | new_process->state = &g_array_index(new_process->execution_stack, |
307 | LttvExecutionState, new_process->execution_stack->len - 1); |
2a2fa4f0 |
308 | g_hash_table_insert(new_processes, new_process, new_process); |
ffd54a90 |
309 | } |
310 | |
311 | |
308711e5 |
312 | static GHashTable *lttv_state_copy_process_table(GHashTable *processes) |
ffd54a90 |
313 | { |
2a2fa4f0 |
314 | GHashTable *new_processes = g_hash_table_new(process_hash, process_equal); |
ffd54a90 |
315 | |
308711e5 |
316 | g_hash_table_foreach(processes, copy_process_state, new_processes); |
317 | return new_processes; |
dc877563 |
318 | } |
319 | |
320 | |
308711e5 |
321 | /* The saved state for each trace contains a member "processes", which |
322 | stores a copy of the process table, and a member "tracefiles" with |
323 | one entry per tracefile. Each tracefile has a "process" member pointing |
324 | to the current process and a "position" member storing the tracefile |
325 | position (needed to seek to the current "next" event. */ |
326 | |
327 | static void state_save(LttvTraceState *self, LttvAttribute *container) |
dc877563 |
328 | { |
dbb7bb09 |
329 | guint i, nb_tracefile; |
dc877563 |
330 | |
308711e5 |
331 | LttvTracefileState *tfcs; |
332 | |
333 | LttvAttribute *tracefiles_tree, *tracefile_tree; |
334 | |
335 | LttvAttributeType type; |
336 | |
337 | LttvAttributeValue value; |
338 | |
339 | LttvAttributeName name; |
340 | |
341 | LttEventPosition *ep; |
342 | |
343 | tracefiles_tree = lttv_attribute_find_subdir(container, |
344 | LTTV_STATE_TRACEFILES); |
345 | |
346 | value = lttv_attribute_add(container, LTTV_STATE_PROCESSES, |
347 | LTTV_POINTER); |
348 | *(value.v_pointer) = lttv_state_copy_process_table(self->processes); |
349 | |
dbb7bb09 |
350 | nb_tracefile = ltt_trace_control_tracefile_number(self->parent.t) + |
351 | ltt_trace_per_cpu_tracefile_number(self->parent.t); |
308711e5 |
352 | |
353 | for(i = 0 ; i < nb_tracefile ; i++) { |
dbb7bb09 |
354 | tfcs = (LttvTracefileState *)self->parent.tracefiles[i]; |
308711e5 |
355 | tracefile_tree = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL); |
356 | value = lttv_attribute_add(tracefiles_tree, i, |
357 | LTTV_GOBJECT); |
358 | *(value.v_gobject) = (GObject *)tracefile_tree; |
359 | value = lttv_attribute_add(tracefile_tree, LTTV_STATE_PROCESS, |
360 | LTTV_UINT); |
361 | *(value.v_uint) = tfcs->process->pid; |
362 | value = lttv_attribute_add(tracefile_tree, LTTV_STATE_EVENT, |
363 | LTTV_POINTER); |
364 | if(tfcs->parent.e == NULL) *(value.v_pointer) = NULL; |
365 | else { |
a5dcde2f |
366 | ep = ltt_event_position_new(); |
308711e5 |
367 | ltt_event_position(tfcs->parent.e, ep); |
368 | *(value.v_pointer) = ep; |
08b1c66e |
369 | |
370 | guint nb_block, nb_event; |
371 | LttTracefile *tf; |
372 | ltt_event_position_get(ep, &nb_block, &nb_event, &tf); |
373 | g_debug("Block %u event %u time %lu.%lu", nb_block, nb_event, |
374 | tfcs->parent.timestamp.tv_sec, tfcs->parent.timestamp.tv_nsec); |
308711e5 |
375 | } |
dc877563 |
376 | } |
dc877563 |
377 | } |
378 | |
379 | |
308711e5 |
380 | static void state_restore(LttvTraceState *self, LttvAttribute *container) |
dc877563 |
381 | { |
dbb7bb09 |
382 | guint i, nb_tracefile, pid; |
dc877563 |
383 | |
308711e5 |
384 | LttvTracefileState *tfcs; |
dc877563 |
385 | |
308711e5 |
386 | LttvAttribute *tracefiles_tree, *tracefile_tree; |
dc877563 |
387 | |
308711e5 |
388 | LttvAttributeType type; |
dc877563 |
389 | |
308711e5 |
390 | LttvAttributeValue value; |
dc877563 |
391 | |
308711e5 |
392 | LttvAttributeName name; |
dc877563 |
393 | |
308711e5 |
394 | LttEventPosition *ep; |
dc877563 |
395 | |
308711e5 |
396 | tracefiles_tree = lttv_attribute_find_subdir(container, |
397 | LTTV_STATE_TRACEFILES); |
dc877563 |
398 | |
308711e5 |
399 | type = lttv_attribute_get_by_name(container, LTTV_STATE_PROCESSES, |
400 | &value); |
401 | g_assert(type == LTTV_POINTER); |
402 | lttv_state_free_process_table(self->processes); |
403 | self->processes = lttv_state_copy_process_table(*(value.v_pointer)); |
404 | |
dbb7bb09 |
405 | nb_tracefile = ltt_trace_control_tracefile_number(self->parent.t) + |
406 | ltt_trace_per_cpu_tracefile_number(self->parent.t); |
308711e5 |
407 | |
408 | for(i = 0 ; i < nb_tracefile ; i++) { |
dbb7bb09 |
409 | tfcs = (LttvTracefileState *)self->parent.tracefiles[i]; |
308711e5 |
410 | type = lttv_attribute_get(tracefiles_tree, i, &name, &value); |
411 | g_assert(type == LTTV_GOBJECT); |
412 | tracefile_tree = *((LttvAttribute **)(value.v_gobject)); |
413 | |
414 | type = lttv_attribute_get_by_name(tracefile_tree, LTTV_STATE_PROCESS, |
415 | &value); |
416 | g_assert(type == LTTV_UINT); |
2a2fa4f0 |
417 | pid = *(value.v_uint); |
418 | tfcs->process = lttv_state_find_process_or_create(tfcs, pid); |
419 | |
308711e5 |
420 | type = lttv_attribute_get_by_name(tracefile_tree, LTTV_STATE_EVENT, |
421 | &value); |
422 | g_assert(type == LTTV_POINTER); |
423 | if(*(value.v_pointer) == NULL) tfcs->parent.e = NULL; |
424 | else { |
425 | ep = *(value.v_pointer); |
426 | ltt_tracefile_seek_position(tfcs->parent.tf, ep); |
427 | tfcs->parent.e = ltt_tracefile_read(tfcs->parent.tf); |
428 | tfcs->parent.timestamp = ltt_event_time(tfcs->parent.e); |
429 | } |
dc877563 |
430 | } |
dc877563 |
431 | } |
432 | |
433 | |
308711e5 |
434 | static void state_saved_free(LttvTraceState *self, LttvAttribute *container) |
dc877563 |
435 | { |
dbb7bb09 |
436 | guint i, nb_tracefile; |
dc877563 |
437 | |
308711e5 |
438 | LttvTracefileState *tfcs; |
dc877563 |
439 | |
308711e5 |
440 | LttvAttribute *tracefiles_tree, *tracefile_tree; |
dc877563 |
441 | |
308711e5 |
442 | LttvAttributeType type; |
dc877563 |
443 | |
308711e5 |
444 | LttvAttributeValue value; |
dc877563 |
445 | |
308711e5 |
446 | LttvAttributeName name; |
dc877563 |
447 | |
308711e5 |
448 | LttEventPosition *ep; |
dc877563 |
449 | |
308711e5 |
450 | tracefiles_tree = lttv_attribute_find_subdir(container, |
451 | LTTV_STATE_TRACEFILES); |
452 | lttv_attribute_remove_by_name(container, LTTV_STATE_TRACEFILES); |
dc877563 |
453 | |
308711e5 |
454 | type = lttv_attribute_get_by_name(container, LTTV_STATE_PROCESSES, |
455 | &value); |
456 | g_assert(type == LTTV_POINTER); |
457 | lttv_state_free_process_table(*(value.v_pointer)); |
458 | *(value.v_pointer) = NULL; |
459 | lttv_attribute_remove_by_name(container, LTTV_STATE_PROCESSES); |
460 | |
dbb7bb09 |
461 | nb_tracefile = ltt_trace_control_tracefile_number(self->parent.t) + |
462 | ltt_trace_per_cpu_tracefile_number(self->parent.t); |
308711e5 |
463 | |
464 | for(i = 0 ; i < nb_tracefile ; i++) { |
dbb7bb09 |
465 | tfcs = (LttvTracefileState *)self->parent.tracefiles[i]; |
308711e5 |
466 | type = lttv_attribute_get(tracefiles_tree, i, &name, &value); |
467 | g_assert(type == LTTV_GOBJECT); |
468 | tracefile_tree = *((LttvAttribute **)(value.v_gobject)); |
469 | |
470 | type = lttv_attribute_get_by_name(tracefile_tree, LTTV_STATE_EVENT, |
471 | &value); |
472 | g_assert(type == LTTV_POINTER); |
473 | if(*(value.v_pointer) != NULL) g_free(*(value.v_pointer)); |
dc877563 |
474 | } |
308711e5 |
475 | lttv_attribute_recursive_free(tracefiles_tree); |
dc877563 |
476 | } |
477 | |
478 | |
b445142a |
479 | static void |
480 | fill_name_tables(LttvTraceState *tcs) |
481 | { |
482 | int i, nb; |
dc877563 |
483 | |
b445142a |
484 | char *f_name, *e_name; |
dc877563 |
485 | |
b445142a |
486 | LttvTraceHook h; |
487 | |
488 | LttEventType *et; |
489 | |
490 | LttType *t; |
491 | |
492 | GString *fe_name = g_string_new(""); |
493 | |
494 | nb = ltt_trace_eventtype_number(tcs->parent.t); |
495 | tcs->eventtype_names = g_new(GQuark, nb); |
496 | for(i = 0 ; i < nb ; i++) { |
497 | et = ltt_trace_eventtype_get(tcs->parent.t, i); |
498 | e_name = ltt_eventtype_name(et); |
499 | f_name = ltt_facility_name(ltt_eventtype_facility(et)); |
500 | g_string_printf(fe_name, "%s.%s", f_name, e_name); |
501 | tcs->eventtype_names[i] = g_quark_from_string(fe_name->str); |
502 | } |
dc877563 |
503 | |
b445142a |
504 | lttv_trace_find_hook(tcs->parent.t, "core", "syscall_entry", |
505 | "syscall_id", NULL, NULL, NULL, &h); |
506 | t = ltt_field_type(h.f1); |
507 | nb = ltt_type_element_number(t); |
508 | |
509 | /* CHECK syscalls should be an emun but currently are not! |
510 | tcs->syscall_names = g_new(GQuark, nb); |
511 | |
512 | for(i = 0 ; i < nb ; i++) { |
513 | tcs->syscall_names[i] = g_quark_from_string(ltt_enum_string_get(t, i)); |
514 | } |
515 | */ |
516 | |
517 | tcs->syscall_names = g_new(GQuark, 256); |
518 | for(i = 0 ; i < 256 ; i++) { |
519 | g_string_printf(fe_name, "syscall %d", i); |
520 | tcs->syscall_names[i] = g_quark_from_string(fe_name->str); |
521 | } |
522 | |
523 | lttv_trace_find_hook(tcs->parent.t, "core", "trap_entry", |
524 | "trap_id", NULL, NULL, NULL, &h); |
525 | t = ltt_field_type(h.f1); |
526 | nb = ltt_type_element_number(t); |
527 | |
528 | /* |
529 | tcs->trap_names = g_new(GQuark, nb); |
530 | for(i = 0 ; i < nb ; i++) { |
531 | tcs->trap_names[i] = g_quark_from_string(ltt_enum_string_get(t, i)); |
532 | } |
533 | */ |
534 | |
535 | tcs->trap_names = g_new(GQuark, 256); |
536 | for(i = 0 ; i < 256 ; i++) { |
537 | g_string_printf(fe_name, "trap %d", i); |
538 | tcs->trap_names[i] = g_quark_from_string(fe_name->str); |
539 | } |
540 | |
541 | lttv_trace_find_hook(tcs->parent.t, "core", "irq_entry", |
542 | "irq_id", NULL, NULL, NULL, &h); |
543 | t = ltt_field_type(h.f1); |
544 | nb = ltt_type_element_number(t); |
545 | |
546 | /* |
547 | tcs->irq_names = g_new(GQuark, nb); |
548 | for(i = 0 ; i < nb ; i++) { |
549 | tcs->irq_names[i] = g_quark_from_string(ltt_enum_string_get(t, i)); |
550 | } |
551 | */ |
552 | |
553 | tcs->irq_names = g_new(GQuark, 256); |
554 | for(i = 0 ; i < 256 ; i++) { |
555 | g_string_printf(fe_name, "irq %d", i); |
556 | tcs->irq_names[i] = g_quark_from_string(fe_name->str); |
557 | } |
558 | |
559 | g_string_free(fe_name, TRUE); |
560 | } |
561 | |
562 | |
563 | static void |
564 | free_name_tables(LttvTraceState *tcs) |
565 | { |
566 | g_free(tcs->eventtype_names); |
567 | g_free(tcs->syscall_names); |
568 | g_free(tcs->trap_names); |
569 | g_free(tcs->irq_names); |
570 | } |
dc877563 |
571 | |
b445142a |
572 | |
573 | static void push_state(LttvTracefileState *tfs, LttvExecutionMode t, |
ffd54a90 |
574 | guint state_id) |
dc877563 |
575 | { |
b445142a |
576 | LttvExecutionState *es; |
dc877563 |
577 | |
578 | LttvProcessState *process = tfs->process; |
579 | |
b445142a |
580 | guint depth = process->execution_stack->len; |
dc877563 |
581 | |
b445142a |
582 | g_array_set_size(process->execution_stack, depth + 1); |
583 | es = &g_array_index(process->execution_stack, LttvExecutionState, depth); |
584 | es->t = t; |
585 | es->n = state_id; |
586 | es->entry = es->change = tfs->parent.timestamp; |
587 | es->s = process->state->s; |
588 | process->state = es; |
dc877563 |
589 | } |
590 | |
591 | |
b445142a |
592 | static void pop_state(LttvTracefileState *tfs, LttvExecutionMode t) |
dc877563 |
593 | { |
594 | LttvProcessState *process = tfs->process; |
595 | |
b445142a |
596 | guint depth = process->execution_stack->len - 1; |
dc877563 |
597 | |
3d27549e |
598 | if(process->state->t != t){ |
08b1c66e |
599 | g_info("Different execution mode type (%d.%09d): ignore it\n", |
b445142a |
600 | tfs->parent.timestamp.tv_sec, tfs->parent.timestamp.tv_nsec); |
08b1c66e |
601 | g_info("process state has %s when pop_int is %s\n", |
8e8e6b64 |
602 | g_quark_to_string(process->state->t), |
603 | g_quark_to_string(t)); |
08b1c66e |
604 | g_info("{ %u, %u, %s, %s }\n", |
8e8e6b64 |
605 | process->pid, |
606 | process->ppid, |
607 | g_quark_to_string(process->name), |
608 | g_quark_to_string(process->state->s)); |
3d27549e |
609 | return; |
610 | } |
b445142a |
611 | |
612 | if(depth == 0){ |
08b1c66e |
613 | g_info("Trying to pop last state on stack (%d.%09d): ignore it\n", |
b445142a |
614 | tfs->parent.timestamp.tv_sec, tfs->parent.timestamp.tv_nsec); |
615 | return; |
616 | } |
617 | |
618 | g_array_remove_index(process->execution_stack, depth); |
dc877563 |
619 | depth--; |
b445142a |
620 | process->state = &g_array_index(process->execution_stack, LttvExecutionState, |
dc877563 |
621 | depth); |
b445142a |
622 | process->state->change = tfs->parent.timestamp; |
dc877563 |
623 | } |
624 | |
625 | |
2a2fa4f0 |
626 | LttvProcessState * |
627 | lttv_state_create_process(LttvTracefileState *tfs, LttvProcessState *parent, |
628 | guint pid) |
dc877563 |
629 | { |
630 | LttvProcessState *process = g_new(LttvProcessState, 1); |
631 | |
b445142a |
632 | LttvExecutionState *es; |
dc877563 |
633 | |
ffd54a90 |
634 | LttvTraceContext *tc; |
635 | |
ba576a78 |
636 | LttvTraceState *tcs; |
637 | |
b445142a |
638 | char buffer[128]; |
ffd54a90 |
639 | |
b445142a |
640 | tcs = (LttvTraceState *)tc = tfs->parent.t_context; |
60b53e4f |
641 | |
dc877563 |
642 | process->pid = pid; |
2a2fa4f0 |
643 | process->last_cpu = tfs->cpu_name; |
644 | g_hash_table_insert(tcs->processes, process, process); |
b445142a |
645 | |
646 | if(parent) { |
647 | process->ppid = parent->pid; |
648 | process->name = parent->name; |
2a2fa4f0 |
649 | process->creation_time = tfs->parent.timestamp; |
b445142a |
650 | } |
2a2fa4f0 |
651 | |
652 | /* No parent. This process exists but we are missing all information about |
653 | its creation. The birth time is set to zero but we remember the time of |
654 | insertion */ |
655 | |
b445142a |
656 | else { |
657 | process->ppid = 0; |
658 | process->name = LTTV_STATE_UNNAMED; |
2a2fa4f0 |
659 | process->creation_time = ltt_time_zero; |
b445142a |
660 | } |
661 | |
2a2fa4f0 |
662 | process->insertion_time = tfs->parent.timestamp; |
b445142a |
663 | sprintf(buffer,"%d-%lu.%lu",pid, process->creation_time.tv_sec, |
664 | process->creation_time.tv_nsec); |
665 | process->pid_time = g_quark_from_string(buffer); |
2a2fa4f0 |
666 | process->last_cpu = tfs->cpu_name; |
b445142a |
667 | process->execution_stack = g_array_new(FALSE, FALSE, |
668 | sizeof(LttvExecutionState)); |
669 | g_array_set_size(process->execution_stack, 1); |
670 | es = process->state = &g_array_index(process->execution_stack, |
671 | LttvExecutionState, 0); |
672 | es->t = LTTV_STATE_USER_MODE; |
673 | es->n = LTTV_STATE_SUBMODE_NONE; |
674 | es->entry = tfs->parent.timestamp; |
675 | es->change = tfs->parent.timestamp; |
676 | es->s = LTTV_STATE_WAIT_FORK; |
cbe7c836 |
677 | |
678 | return process; |
dc877563 |
679 | } |
680 | |
2a2fa4f0 |
681 | |
682 | LttvProcessState * |
683 | lttv_state_find_process_from_trace(LttvTraceState *ts, GQuark cpu, guint pid) |
dc877563 |
684 | { |
2a2fa4f0 |
685 | LttvProcessState key; |
686 | LttvProcessState *process; |
687 | |
688 | key.pid = pid; |
689 | key.last_cpu = cpu; |
690 | process = g_hash_table_lookup(ts->processes, &key); |
dc877563 |
691 | return process; |
692 | } |
693 | |
2a2fa4f0 |
694 | |
695 | LttvProcessState *lttv_state_find_process(LttvTracefileState *tfs, |
d0cd7f09 |
696 | guint pid) |
697 | { |
2a2fa4f0 |
698 | LttvTraceState *ts =(LttvTraceState *)tfs->parent.t_context; |
699 | return lttv_state_find_process_from_trace(ts, tfs->cpu_name, pid); |
d0cd7f09 |
700 | } |
701 | |
702 | |
2a2fa4f0 |
703 | LttvProcessState * |
704 | lttv_state_find_process_or_create(LttvTracefileState *tfs, guint pid) |
705 | { |
706 | LttvProcessState *process = lttv_state_find_process(tfs, pid); |
707 | |
708 | if(process == NULL) process = lttv_state_create_process(tfs, NULL, pid); |
709 | return process; |
710 | } |
711 | |
dc877563 |
712 | |
b445142a |
713 | static void exit_process(LttvTracefileState *tfs, LttvProcessState *process) |
dc877563 |
714 | { |
ba576a78 |
715 | LttvTraceState *ts = LTTV_TRACE_STATE(tfs->parent.t_context); |
2a2fa4f0 |
716 | LttvProcessState key; |
ba576a78 |
717 | |
2a2fa4f0 |
718 | key.pid = process->pid; |
719 | key.last_cpu = process->last_cpu; |
720 | g_hash_table_remove(ts->processes, &key); |
b445142a |
721 | g_array_free(process->execution_stack, TRUE); |
dc877563 |
722 | g_free(process); |
723 | } |
724 | |
725 | |
b445142a |
726 | static void free_process_state(gpointer key, gpointer value,gpointer user_data) |
dc877563 |
727 | { |
b445142a |
728 | g_array_free(((LttvProcessState *)value)->execution_stack, TRUE); |
dc877563 |
729 | g_free(value); |
730 | } |
731 | |
732 | |
308711e5 |
733 | static void lttv_state_free_process_table(GHashTable *processes) |
dc877563 |
734 | { |
735 | g_hash_table_foreach(processes, free_process_state, NULL); |
308711e5 |
736 | g_hash_table_destroy(processes); |
dc877563 |
737 | } |
738 | |
739 | |
b445142a |
740 | static gboolean syscall_entry(void *hook_data, void *call_data) |
dc877563 |
741 | { |
b445142a |
742 | LttField *f = ((LttvTraceHook *)hook_data)->f1; |
dc877563 |
743 | |
ba576a78 |
744 | LttvTracefileState *s = (LttvTracefileState *)call_data; |
dc877563 |
745 | |
b445142a |
746 | LttvExecutionSubmode submode; |
747 | |
748 | submode = ((LttvTraceState *)(s->parent.t_context))->syscall_names[ |
749 | ltt_event_get_unsigned(s->parent.e, f)]; |
750 | push_state(s, LTTV_STATE_SYSCALL, submode); |
dc877563 |
751 | return FALSE; |
752 | } |
753 | |
754 | |
b445142a |
755 | static gboolean syscall_exit(void *hook_data, void *call_data) |
dc877563 |
756 | { |
ba576a78 |
757 | LttvTracefileState *s = (LttvTracefileState *)call_data; |
dc877563 |
758 | |
ffd54a90 |
759 | pop_state(s, LTTV_STATE_SYSCALL); |
dc877563 |
760 | return FALSE; |
761 | } |
762 | |
763 | |
b445142a |
764 | static gboolean trap_entry(void *hook_data, void *call_data) |
dc877563 |
765 | { |
b445142a |
766 | LttField *f = ((LttvTraceHook *)hook_data)->f1; |
dc877563 |
767 | |
ba576a78 |
768 | LttvTracefileState *s = (LttvTracefileState *)call_data; |
dc877563 |
769 | |
b445142a |
770 | LttvExecutionSubmode submode; |
771 | |
772 | submode = ((LttvTraceState *)(s->parent.t_context))->trap_names[ |
773 | ltt_event_get_unsigned(s->parent.e, f)]; |
774 | push_state(s, LTTV_STATE_TRAP, submode); |
dc877563 |
775 | return FALSE; |
776 | } |
777 | |
778 | |
b445142a |
779 | static gboolean trap_exit(void *hook_data, void *call_data) |
dc877563 |
780 | { |
ba576a78 |
781 | LttvTracefileState *s = (LttvTracefileState *)call_data; |
dc877563 |
782 | |
ffd54a90 |
783 | pop_state(s, LTTV_STATE_TRAP); |
dc877563 |
784 | return FALSE; |
785 | } |
786 | |
787 | |
b445142a |
788 | static gboolean irq_entry(void *hook_data, void *call_data) |
dc877563 |
789 | { |
b445142a |
790 | LttField *f = ((LttvTraceHook *)hook_data)->f1; |
dc877563 |
791 | |
ba576a78 |
792 | LttvTracefileState *s = (LttvTracefileState *)call_data; |
dc877563 |
793 | |
b445142a |
794 | LttvExecutionSubmode submode; |
795 | |
796 | submode = ((LttvTraceState *)(s->parent.t_context))->irq_names[ |
797 | ltt_event_get_unsigned(s->parent.e, f)]; |
798 | |
dc877563 |
799 | /* Do something with the info about being in user or system mode when int? */ |
b445142a |
800 | push_state(s, LTTV_STATE_IRQ, submode); |
dc877563 |
801 | return FALSE; |
802 | } |
803 | |
804 | |
b445142a |
805 | static gboolean irq_exit(void *hook_data, void *call_data) |
dc877563 |
806 | { |
ba576a78 |
807 | LttvTracefileState *s = (LttvTracefileState *)call_data; |
dc877563 |
808 | |
ffd54a90 |
809 | pop_state(s, LTTV_STATE_IRQ); |
dc877563 |
810 | return FALSE; |
811 | } |
812 | |
813 | |
b445142a |
814 | static gboolean schedchange(void *hook_data, void *call_data) |
dc877563 |
815 | { |
b445142a |
816 | LttvTraceHook *h = (LttvTraceHook *)hook_data; |
dc877563 |
817 | |
ba576a78 |
818 | LttvTracefileState *s = (LttvTracefileState *)call_data; |
dc877563 |
819 | |
820 | guint pid_in, pid_out, state_out; |
821 | |
3d27549e |
822 | pid_in = ltt_event_get_unsigned(s->parent.e, h->f1); |
823 | pid_out = ltt_event_get_unsigned(s->parent.e, h->f2); |
824 | state_out = ltt_event_get_unsigned(s->parent.e, h->f3); |
b445142a |
825 | |
dc877563 |
826 | if(s->process != NULL) { |
b445142a |
827 | |
ffd54a90 |
828 | if(state_out == 0) s->process->state->s = LTTV_STATE_WAIT_CPU; |
829 | else if(s->process->state->s == LTTV_STATE_EXIT) |
ba576a78 |
830 | exit_process(s, s->process); |
ffd54a90 |
831 | else s->process->state->s = LTTV_STATE_WAIT; |
3d27549e |
832 | |
833 | if(s->process->pid == 0) |
90ffd2ad |
834 | s->process->pid = pid_out; |
b445142a |
835 | |
836 | s->process->state->change = s->parent.timestamp; |
dc877563 |
837 | } |
2a2fa4f0 |
838 | s->process = lttv_state_find_process_or_create(s, pid_in); |
ffd54a90 |
839 | s->process->state->s = LTTV_STATE_RUN; |
2a2fa4f0 |
840 | s->process->last_cpu = s->cpu_name; |
b445142a |
841 | s->process->state->change = s->parent.timestamp; |
dc877563 |
842 | return FALSE; |
843 | } |
844 | |
845 | |
b445142a |
846 | static gboolean process_fork(void *hook_data, void *call_data) |
dc877563 |
847 | { |
b445142a |
848 | LttField *f = ((LttvTraceHook *)hook_data)->f1; |
dc877563 |
849 | |
ba576a78 |
850 | LttvTracefileState *s = (LttvTracefileState *)call_data; |
dc877563 |
851 | |
852 | guint child_pid; |
853 | |
3d27549e |
854 | child_pid = ltt_event_get_unsigned(s->parent.e, f); |
2a2fa4f0 |
855 | lttv_state_create_process(s, s->process, child_pid); |
dc877563 |
856 | return FALSE; |
857 | } |
858 | |
859 | |
b445142a |
860 | static gboolean process_exit(void *hook_data, void *call_data) |
dc877563 |
861 | { |
ba576a78 |
862 | LttvTracefileState *s = (LttvTracefileState *)call_data; |
dc877563 |
863 | |
864 | if(s->process != NULL) { |
ffd54a90 |
865 | s->process->state->s = LTTV_STATE_EXIT; |
dc877563 |
866 | } |
867 | return FALSE; |
868 | } |
869 | |
870 | |
308711e5 |
871 | void lttv_state_add_event_hooks(LttvTracesetState *self) |
dc877563 |
872 | { |
ba576a78 |
873 | LttvTraceset *traceset = self->parent.ts; |
dc877563 |
874 | |
dbb7bb09 |
875 | guint i, j, k, nb_trace, nb_tracefile; |
dc877563 |
876 | |
ba576a78 |
877 | LttvTraceState *ts; |
dc877563 |
878 | |
ba576a78 |
879 | LttvTracefileState *tfs; |
dc877563 |
880 | |
dc877563 |
881 | GArray *hooks; |
882 | |
b445142a |
883 | LttvTraceHook hook; |
dc877563 |
884 | |
885 | LttvAttributeValue val; |
886 | |
ba576a78 |
887 | nb_trace = lttv_traceset_number(traceset); |
dc877563 |
888 | for(i = 0 ; i < nb_trace ; i++) { |
ba576a78 |
889 | ts = (LttvTraceState *)self->parent.traces[i]; |
dc877563 |
890 | |
891 | /* Find the eventtype id for the following events and register the |
892 | associated by id hooks. */ |
893 | |
b445142a |
894 | hooks = g_array_new(FALSE, FALSE, sizeof(LttvTraceHook)); |
895 | g_array_set_size(hooks, 9); |
896 | |
897 | lttv_trace_find_hook(ts->parent.t, "core","syscall_entry","syscall_id", |
898 | NULL, NULL, syscall_entry, &g_array_index(hooks, LttvTraceHook, 0)); |
cbe7c836 |
899 | |
b445142a |
900 | lttv_trace_find_hook(ts->parent.t, "core", "syscall_exit", NULL, NULL, |
901 | NULL, syscall_exit, &g_array_index(hooks, LttvTraceHook, 1)); |
cbe7c836 |
902 | |
b445142a |
903 | lttv_trace_find_hook(ts->parent.t, "core", "trap_entry", "trap_id", |
904 | NULL, NULL, trap_entry, &g_array_index(hooks, LttvTraceHook, 2)); |
cbe7c836 |
905 | |
b445142a |
906 | lttv_trace_find_hook(ts->parent.t, "core", "trap_exit", NULL, NULL, NULL, |
907 | trap_exit, &g_array_index(hooks, LttvTraceHook, 3)); |
cbe7c836 |
908 | |
b445142a |
909 | lttv_trace_find_hook(ts->parent.t, "core", "irq_entry", "irq_id", NULL, |
910 | NULL, irq_entry, &g_array_index(hooks, LttvTraceHook, 4)); |
cbe7c836 |
911 | |
b445142a |
912 | lttv_trace_find_hook(ts->parent.t, "core", "irq_exit", NULL, NULL, NULL, |
913 | irq_exit, &g_array_index(hooks, LttvTraceHook, 5)); |
cbe7c836 |
914 | |
b445142a |
915 | lttv_trace_find_hook(ts->parent.t, "core", "schedchange", "in", "out", |
916 | "out_state", schedchange, &g_array_index(hooks, LttvTraceHook, 6)); |
cbe7c836 |
917 | |
b445142a |
918 | lttv_trace_find_hook(ts->parent.t, "core", "process_fork", "child_pid", |
919 | NULL, NULL, process_fork, &g_array_index(hooks, LttvTraceHook, 7)); |
cbe7c836 |
920 | |
b445142a |
921 | lttv_trace_find_hook(ts->parent.t, "core", "process_exit", NULL, NULL, |
922 | NULL, process_exit, &g_array_index(hooks, LttvTraceHook, 8)); |
dc877563 |
923 | |
924 | /* Add these hooks to each before_event_by_id hooks list */ |
925 | |
dbb7bb09 |
926 | nb_tracefile = ltt_trace_control_tracefile_number(ts->parent.t) + |
927 | ltt_trace_per_cpu_tracefile_number(ts->parent.t); |
928 | |
dc877563 |
929 | for(j = 0 ; j < nb_tracefile ; j++) { |
dbb7bb09 |
930 | tfs = LTTV_TRACEFILE_STATE(ts->parent.tracefiles[j]); |
dc877563 |
931 | |
932 | for(k = 0 ; k < hooks->len ; k++) { |
b445142a |
933 | hook = g_array_index(hooks, LttvTraceHook, k); |
934 | lttv_hooks_add(lttv_hooks_by_id_find(tfs->parent.after_event_by_id, |
935 | hook.id), hook.h, &g_array_index(hooks, LttvTraceHook, k)); |
ffd54a90 |
936 | } |
dc877563 |
937 | } |
ba576a78 |
938 | lttv_attribute_find(self->parent.a, LTTV_STATE_HOOKS, LTTV_POINTER, &val); |
939 | *(val.v_pointer) = hooks; |
dc877563 |
940 | } |
941 | } |
942 | |
943 | |
308711e5 |
944 | void lttv_state_remove_event_hooks(LttvTracesetState *self) |
dc877563 |
945 | { |
ba576a78 |
946 | LttvTraceset *traceset = self->parent.ts; |
dc877563 |
947 | |
dbb7bb09 |
948 | guint i, j, k, nb_trace, nb_tracefile; |
dc877563 |
949 | |
ba576a78 |
950 | LttvTraceState *ts; |
dc877563 |
951 | |
ba576a78 |
952 | LttvTracefileState *tfs; |
dc877563 |
953 | |
dc877563 |
954 | GArray *hooks; |
955 | |
b445142a |
956 | LttvTraceHook hook; |
dc877563 |
957 | |
958 | LttvAttributeValue val; |
959 | |
ba576a78 |
960 | nb_trace = lttv_traceset_number(traceset); |
dc877563 |
961 | for(i = 0 ; i < nb_trace ; i++) { |
ba576a78 |
962 | ts = LTTV_TRACE_STATE(self->parent.traces[i]); |
963 | lttv_attribute_find(self->parent.a, LTTV_STATE_HOOKS, LTTV_POINTER, &val); |
964 | hooks = *(val.v_pointer); |
dc877563 |
965 | |
966 | /* Add these hooks to each before_event_by_id hooks list */ |
967 | |
dbb7bb09 |
968 | nb_tracefile = ltt_trace_control_tracefile_number(ts->parent.t) + |
969 | ltt_trace_per_cpu_tracefile_number(ts->parent.t); |
970 | |
dc877563 |
971 | for(j = 0 ; j < nb_tracefile ; j++) { |
dbb7bb09 |
972 | tfs = LTTV_TRACEFILE_STATE(ts->parent.tracefiles[j]); |
dc877563 |
973 | |
974 | for(k = 0 ; k < hooks->len ; k++) { |
b445142a |
975 | hook = g_array_index(hooks, LttvTraceHook, k); |
ba576a78 |
976 | lttv_hooks_remove_data( |
b445142a |
977 | lttv_hooks_by_id_find(tfs->parent.after_event_by_id, |
978 | hook.id), hook.h, &g_array_index(hooks, LttvTraceHook, k)); |
ffd54a90 |
979 | } |
dc877563 |
980 | } |
981 | g_array_free(hooks, TRUE); |
982 | } |
983 | } |
984 | |
985 | |
08b1c66e |
986 | static gboolean block_start(void *hook_data, void *call_data) |
308711e5 |
987 | { |
dbb7bb09 |
988 | LttvTracefileState *self = (LttvTracefileState *)call_data; |
308711e5 |
989 | |
dbb7bb09 |
990 | LttvTracefileState *tfcs; |
308711e5 |
991 | |
dbb7bb09 |
992 | LttvTraceState *tcs = (LttvTraceState *)(self->parent.t_context); |
993 | |
994 | LttEventPosition *ep; |
308711e5 |
995 | |
dbb7bb09 |
996 | guint i, nb_block, nb_event, nb_tracefile; |
308711e5 |
997 | |
998 | LttTracefile *tf; |
999 | |
1000 | LttvAttribute *saved_states_tree, *saved_state_tree; |
1001 | |
1002 | LttvAttributeValue value; |
1003 | |
dbb7bb09 |
1004 | ep = ltt_event_position_new(); |
1005 | nb_tracefile = ltt_trace_control_tracefile_number(tcs->parent.t) + |
1006 | ltt_trace_per_cpu_tracefile_number(tcs->parent.t); |
1007 | |
1008 | /* Count the number of events added since the last block end in any |
1009 | tracefile. */ |
1010 | |
1011 | for(i = 0 ; i < nb_tracefile ; i++) { |
1012 | tfcs = (LttvTracefileState *)tcs->parent.tracefiles[i]; |
1013 | ltt_event_position(tfcs->parent.e, ep); |
1014 | ltt_event_position_get(ep, &nb_block, &nb_event, &tf); |
1015 | tcs->nb_event += nb_event - tfcs->saved_position; |
1016 | tfcs->saved_position = nb_event; |
1017 | } |
1018 | g_free(ep); |
308711e5 |
1019 | |
308711e5 |
1020 | if(tcs->nb_event >= tcs->save_interval) { |
1021 | saved_states_tree = lttv_attribute_find_subdir(tcs->parent.t_a, |
1022 | LTTV_STATE_SAVED_STATES); |
1023 | saved_state_tree = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL); |
1024 | value = lttv_attribute_add(saved_states_tree, |
1025 | lttv_attribute_get_number(saved_states_tree), LTTV_GOBJECT); |
1026 | *(value.v_gobject) = (GObject *)saved_state_tree; |
1027 | value = lttv_attribute_add(saved_state_tree, LTTV_STATE_TIME, LTTV_TIME); |
dbb7bb09 |
1028 | *(value.v_time) = self->parent.timestamp; |
308711e5 |
1029 | lttv_state_save(tcs, saved_state_tree); |
1030 | tcs->nb_event = 0; |
08b1c66e |
1031 | g_debug("Saving state at time %lu.%lu", self->parent.timestamp.tv_sec, |
1032 | self->parent.timestamp.tv_nsec); |
308711e5 |
1033 | } |
dbb7bb09 |
1034 | *(tcs->max_time_state_recomputed_in_seek) = self->parent.timestamp; |
308711e5 |
1035 | return FALSE; |
1036 | } |
1037 | |
1038 | |
08b1c66e |
1039 | static gboolean block_end(void *hook_data, void *call_data) |
1040 | { |
1041 | LttvTracefileState *self = (LttvTracefileState *)call_data; |
1042 | |
1043 | LttvTraceState *tcs = (LttvTraceState *)(self->parent.t_context); |
1044 | |
1045 | LttTracefile *tf; |
1046 | |
1047 | LttEventPosition *ep; |
1048 | |
1049 | guint nb_block, nb_event; |
1050 | |
1051 | ep = ltt_event_position_new(); |
1052 | ltt_event_position(self->parent.e, ep); |
1053 | ltt_event_position_get(ep, &nb_block, &nb_event, &tf); |
1054 | tcs->nb_event += nb_event - self->saved_position + 1; |
1055 | self->saved_position = 0; |
1056 | *(tcs->max_time_state_recomputed_in_seek) = self->parent.timestamp; |
1057 | g_free(ep); |
1058 | } |
1059 | |
1060 | |
308711e5 |
1061 | void lttv_state_save_add_event_hooks(LttvTracesetState *self) |
1062 | { |
1063 | LttvTraceset *traceset = self->parent.ts; |
1064 | |
dbb7bb09 |
1065 | guint i, j, k, nb_trace, nb_tracefile; |
308711e5 |
1066 | |
1067 | LttvTraceState *ts; |
1068 | |
1069 | LttvTracefileState *tfs; |
1070 | |
08b1c66e |
1071 | LttvTraceHook hook_start, hook_end; |
308711e5 |
1072 | |
1073 | nb_trace = lttv_traceset_number(traceset); |
1074 | for(i = 0 ; i < nb_trace ; i++) { |
1075 | ts = (LttvTraceState *)self->parent.traces[i]; |
08b1c66e |
1076 | lttv_trace_find_hook(ts->parent.t, "core","block_start",NULL, |
1077 | NULL, NULL, block_start, &hook_start); |
308711e5 |
1078 | lttv_trace_find_hook(ts->parent.t, "core","block_end",NULL, |
08b1c66e |
1079 | NULL, NULL, block_end, &hook_end); |
308711e5 |
1080 | |
dbb7bb09 |
1081 | nb_tracefile = ltt_trace_control_tracefile_number(ts->parent.t) + |
1082 | ltt_trace_per_cpu_tracefile_number(ts->parent.t); |
308711e5 |
1083 | |
dbb7bb09 |
1084 | for(j = 0 ; j < nb_tracefile ; j++) { |
1085 | tfs = LTTV_TRACEFILE_STATE(ts->parent.tracefiles[j]); |
308711e5 |
1086 | lttv_hooks_add(lttv_hooks_by_id_find(tfs->parent.after_event_by_id, |
08b1c66e |
1087 | hook_start.id), hook_start.h, NULL); |
1088 | lttv_hooks_add(lttv_hooks_by_id_find(tfs->parent.after_event_by_id, |
1089 | hook_end.id), hook_end.h, NULL); |
308711e5 |
1090 | } |
1091 | } |
1092 | } |
1093 | |
1094 | |
1095 | void lttv_state_save_remove_event_hooks(LttvTracesetState *self) |
1096 | { |
1097 | LttvTraceset *traceset = self->parent.ts; |
1098 | |
dbb7bb09 |
1099 | guint i, j, k, nb_trace, nb_tracefile; |
308711e5 |
1100 | |
1101 | LttvTraceState *ts; |
1102 | |
1103 | LttvTracefileState *tfs; |
1104 | |
08b1c66e |
1105 | LttvTraceHook hook_start, hook_end; |
308711e5 |
1106 | |
1107 | nb_trace = lttv_traceset_number(traceset); |
1108 | for(i = 0 ; i < nb_trace ; i++) { |
1109 | ts = LTTV_TRACE_STATE(self->parent.traces[i]); |
08b1c66e |
1110 | lttv_trace_find_hook(ts->parent.t, "core","block_start",NULL, |
1111 | NULL, NULL, block_start, &hook_start); |
1112 | |
308711e5 |
1113 | lttv_trace_find_hook(ts->parent.t, "core","block_end",NULL, |
08b1c66e |
1114 | NULL, NULL, block_end, &hook_end); |
308711e5 |
1115 | |
dbb7bb09 |
1116 | nb_tracefile = ltt_trace_control_tracefile_number(ts->parent.t) + |
1117 | ltt_trace_per_cpu_tracefile_number(ts->parent.t); |
308711e5 |
1118 | |
dbb7bb09 |
1119 | for(j = 0 ; j < nb_tracefile ; j++) { |
1120 | tfs = LTTV_TRACEFILE_STATE(ts->parent.tracefiles[j]); |
308711e5 |
1121 | lttv_hooks_remove_data(lttv_hooks_by_id_find( |
08b1c66e |
1122 | tfs->parent.after_event_by_id, hook_start.id), hook_start.h, NULL); |
1123 | lttv_hooks_remove_data(lttv_hooks_by_id_find( |
1124 | tfs->parent.after_event_by_id, hook_end.id), hook_end.h, NULL); |
308711e5 |
1125 | } |
1126 | } |
1127 | } |
1128 | |
1129 | |
dd025f91 |
1130 | void lttv_state_traceset_seek_time_closest(LttvTracesetState *self, LttTime t) |
308711e5 |
1131 | { |
1132 | LttvTraceset *traceset = self->parent.ts; |
1133 | |
1134 | guint i, j, nb_trace, nb_saved_state; |
1135 | |
1136 | int min_pos, mid_pos, max_pos; |
1137 | |
1138 | LttvTraceState *tcs; |
1139 | |
1140 | LttvAttributeValue value; |
1141 | |
1142 | LttvAttributeType type; |
1143 | |
1144 | LttvAttributeName name; |
1145 | |
1146 | LttvAttribute *saved_states_tree, *saved_state_tree, *closest_tree; |
1147 | |
1148 | nb_trace = lttv_traceset_number(traceset); |
1149 | for(i = 0 ; i < nb_trace ; i++) { |
1150 | tcs = (LttvTraceState *)self->parent.traces[i]; |
1151 | |
2a2fa4f0 |
1152 | if(ltt_time_compare(t, *(tcs->max_time_state_recomputed_in_seek)) < 0) { |
1153 | saved_states_tree = lttv_attribute_find_subdir(tcs->parent.t_a, |
1154 | LTTV_STATE_SAVED_STATES); |
1155 | min_pos = -1; |
1156 | |
1157 | if(saved_states_tree) { |
dd025f91 |
1158 | max_pos = lttv_attribute_get_number(saved_states_tree) - 1; |
1159 | mid_pos = max_pos / 2; |
1160 | while(min_pos < max_pos) { |
1161 | type = lttv_attribute_get(saved_states_tree, mid_pos, &name, &value); |
1162 | g_assert(type == LTTV_GOBJECT); |
1163 | saved_state_tree = *((LttvAttribute **)(value.v_gobject)); |
1164 | type = lttv_attribute_get_by_name(saved_state_tree, LTTV_STATE_TIME, |
1165 | &value); |
1166 | g_assert(type == LTTV_TIME); |
1167 | if(ltt_time_compare(*(value.v_time), t) < 0) { |
1168 | min_pos = mid_pos; |
1169 | closest_tree = saved_state_tree; |
1170 | } |
1171 | else max_pos = mid_pos - 1; |
1172 | |
1173 | mid_pos = (min_pos + max_pos + 1) / 2; |
1174 | } |
2a2fa4f0 |
1175 | } |
dd025f91 |
1176 | |
2a2fa4f0 |
1177 | /* restore the closest earlier saved state */ |
1178 | if(min_pos != -1) lttv_state_restore(tcs, closest_tree); |
dd025f91 |
1179 | |
2a2fa4f0 |
1180 | /* There is no saved state, yet we want to have it. Restart at T0 */ |
dd025f91 |
1181 | else { |
1182 | restore_init_state(tcs); |
1183 | lttv_process_trace_seek_time(&(tcs->parent), ltt_time_zero); |
308711e5 |
1184 | } |
9444deae |
1185 | } |
dd025f91 |
1186 | /* We want to seek quickly without restoring/updating the state */ |
1187 | else { |
308711e5 |
1188 | restore_init_state(tcs); |
dd025f91 |
1189 | lttv_process_trace_seek_time(&(tcs->parent), t); |
308711e5 |
1190 | } |
308711e5 |
1191 | } |
1192 | } |
1193 | |
1194 | |
1195 | static void |
1196 | traceset_state_instance_init (GTypeInstance *instance, gpointer g_class) |
1197 | { |
1198 | } |
1199 | |
1200 | |
1201 | static void |
1202 | traceset_state_finalize (LttvTracesetState *self) |
1203 | { |
1204 | G_OBJECT_CLASS(g_type_class_peek(LTTV_TRACESET_CONTEXT_TYPE))-> |
1205 | finalize(G_OBJECT(self)); |
1206 | } |
1207 | |
1208 | |
1209 | static void |
1210 | traceset_state_class_init (LttvTracesetContextClass *klass) |
1211 | { |
1212 | GObjectClass *gobject_class = G_OBJECT_CLASS(klass); |
1213 | |
1214 | gobject_class->finalize = (void (*)(GObject *self)) traceset_state_finalize; |
1215 | klass->init = (void (*)(LttvTracesetContext *self, LttvTraceset *ts))init; |
1216 | klass->fini = (void (*)(LttvTracesetContext *self))fini; |
1217 | klass->new_traceset_context = new_traceset_context; |
1218 | klass->new_trace_context = new_trace_context; |
1219 | klass->new_tracefile_context = new_tracefile_context; |
1220 | } |
1221 | |
1222 | |
1223 | GType |
1224 | lttv_traceset_state_get_type(void) |
1225 | { |
1226 | static GType type = 0; |
1227 | if (type == 0) { |
1228 | static const GTypeInfo info = { |
1229 | sizeof (LttvTracesetStateClass), |
1230 | NULL, /* base_init */ |
1231 | NULL, /* base_finalize */ |
1232 | (GClassInitFunc) traceset_state_class_init, /* class_init */ |
1233 | NULL, /* class_finalize */ |
1234 | NULL, /* class_data */ |
dbb7bb09 |
1235 | sizeof (LttvTracesetState), |
308711e5 |
1236 | 0, /* n_preallocs */ |
1237 | (GInstanceInitFunc) traceset_state_instance_init /* instance_init */ |
1238 | }; |
1239 | |
1240 | type = g_type_register_static (LTTV_TRACESET_CONTEXT_TYPE, "LttvTracesetStateType", |
1241 | &info, 0); |
1242 | } |
1243 | return type; |
1244 | } |
1245 | |
1246 | |
1247 | static void |
1248 | trace_state_instance_init (GTypeInstance *instance, gpointer g_class) |
1249 | { |
1250 | } |
1251 | |
1252 | |
1253 | static void |
1254 | trace_state_finalize (LttvTraceState *self) |
1255 | { |
1256 | G_OBJECT_CLASS(g_type_class_peek(LTTV_TRACE_CONTEXT_TYPE))-> |
1257 | finalize(G_OBJECT(self)); |
1258 | } |
1259 | |
1260 | |
1261 | static void |
1262 | trace_state_class_init (LttvTraceStateClass *klass) |
1263 | { |
1264 | GObjectClass *gobject_class = G_OBJECT_CLASS(klass); |
1265 | |
1266 | gobject_class->finalize = (void (*)(GObject *self)) trace_state_finalize; |
1267 | klass->state_save = state_save; |
1268 | klass->state_restore = state_restore; |
1269 | klass->state_saved_free = state_saved_free; |
1270 | } |
1271 | |
1272 | |
1273 | GType |
1274 | lttv_trace_state_get_type(void) |
1275 | { |
1276 | static GType type = 0; |
1277 | if (type == 0) { |
1278 | static const GTypeInfo info = { |
1279 | sizeof (LttvTraceStateClass), |
1280 | NULL, /* base_init */ |
1281 | NULL, /* base_finalize */ |
1282 | (GClassInitFunc) trace_state_class_init, /* class_init */ |
1283 | NULL, /* class_finalize */ |
1284 | NULL, /* class_data */ |
1285 | sizeof (LttvTraceState), |
1286 | 0, /* n_preallocs */ |
1287 | (GInstanceInitFunc) trace_state_instance_init /* instance_init */ |
1288 | }; |
1289 | |
1290 | type = g_type_register_static (LTTV_TRACE_CONTEXT_TYPE, |
1291 | "LttvTraceStateType", &info, 0); |
1292 | } |
1293 | return type; |
1294 | } |
1295 | |
1296 | |
1297 | static void |
1298 | tracefile_state_instance_init (GTypeInstance *instance, gpointer g_class) |
1299 | { |
1300 | } |
1301 | |
1302 | |
1303 | static void |
1304 | tracefile_state_finalize (LttvTracefileState *self) |
1305 | { |
1306 | G_OBJECT_CLASS(g_type_class_peek(LTTV_TRACEFILE_CONTEXT_TYPE))-> |
1307 | finalize(G_OBJECT(self)); |
1308 | } |
1309 | |
1310 | |
1311 | static void |
1312 | tracefile_state_class_init (LttvTracefileStateClass *klass) |
1313 | { |
1314 | GObjectClass *gobject_class = G_OBJECT_CLASS(klass); |
1315 | |
1316 | gobject_class->finalize = (void (*)(GObject *self)) tracefile_state_finalize; |
1317 | } |
1318 | |
1319 | |
1320 | GType |
1321 | lttv_tracefile_state_get_type(void) |
1322 | { |
1323 | static GType type = 0; |
1324 | if (type == 0) { |
1325 | static const GTypeInfo info = { |
1326 | sizeof (LttvTracefileStateClass), |
1327 | NULL, /* base_init */ |
1328 | NULL, /* base_finalize */ |
1329 | (GClassInitFunc) tracefile_state_class_init, /* class_init */ |
1330 | NULL, /* class_finalize */ |
1331 | NULL, /* class_data */ |
1332 | sizeof (LttvTracefileState), |
1333 | 0, /* n_preallocs */ |
1334 | (GInstanceInitFunc) tracefile_state_instance_init /* instance_init */ |
1335 | }; |
1336 | |
1337 | type = g_type_register_static (LTTV_TRACEFILE_CONTEXT_TYPE, |
1338 | "LttvTracefileStateType", &info, 0); |
1339 | } |
1340 | return type; |
1341 | } |
1342 | |
1343 | |
08b1c66e |
1344 | static void module_init() |
ffd54a90 |
1345 | { |
1346 | LTTV_STATE_UNNAMED = g_quark_from_string("unnamed"); |
b445142a |
1347 | LTTV_STATE_MODE_UNKNOWN = g_quark_from_string("unknown execution mode"); |
ffd54a90 |
1348 | LTTV_STATE_USER_MODE = g_quark_from_string("user mode"); |
1349 | LTTV_STATE_WAIT_FORK = g_quark_from_string("wait fork"); |
1350 | LTTV_STATE_SYSCALL = g_quark_from_string("system call"); |
1351 | LTTV_STATE_TRAP = g_quark_from_string("trap"); |
1352 | LTTV_STATE_IRQ = g_quark_from_string("irq"); |
b445142a |
1353 | LTTV_STATE_SUBMODE_UNKNOWN = g_quark_from_string("unknown submode"); |
1354 | LTTV_STATE_SUBMODE_NONE = g_quark_from_string("(no submode)"); |
ffd54a90 |
1355 | LTTV_STATE_WAIT_CPU = g_quark_from_string("wait for cpu"); |
1356 | LTTV_STATE_EXIT = g_quark_from_string("exiting"); |
1357 | LTTV_STATE_WAIT = g_quark_from_string("wait for I/O"); |
1358 | LTTV_STATE_RUN = g_quark_from_string("running"); |
308711e5 |
1359 | LTTV_STATE_TRACEFILES = g_quark_from_string("tracefiles"); |
1360 | LTTV_STATE_PROCESSES = g_quark_from_string("processes"); |
1361 | LTTV_STATE_PROCESS = g_quark_from_string("process"); |
1362 | LTTV_STATE_EVENT = g_quark_from_string("event"); |
1363 | LTTV_STATE_SAVED_STATES = g_quark_from_string("saved states"); |
dbb7bb09 |
1364 | LTTV_STATE_SAVED_STATES_TIME = g_quark_from_string("saved states time"); |
308711e5 |
1365 | LTTV_STATE_TIME = g_quark_from_string("time"); |
ffd54a90 |
1366 | LTTV_STATE_HOOKS = g_quark_from_string("saved state hooks"); |
1367 | } |
dc877563 |
1368 | |
08b1c66e |
1369 | static void module_destroy() |
ffd54a90 |
1370 | { |
1371 | } |
dc877563 |
1372 | |
1373 | |
08b1c66e |
1374 | LTTV_MODULE("state", "State computation", \ |
1375 | "Update the system state, possibly saving it at intervals", \ |
1376 | module_init, module_destroy) |
1377 | |
dc877563 |
1378 | |
1379 | |