git-svn-id: http://ltt.polymtl.ca/svn@172 04897980-b3bd-0310-b5e0-8ef037075253
[lttv.git] / ltt / branches / poly / lttv / state.c
1
2 #include <lttv/state.h>
3
4 LttvInterruptType
5 LTTV_STATE_USER_MODE,
6 LTTV_STATE_SYSCALL,
7 LTTV_STATE_TRAP,
8 LTTV_STATE_IRQ;
9
10
11 LttvProcessStatus
12 LTTV_STATE_UNNAMED,
13 LTTV_STATE_WAIT_FORK,
14 LTTV_STATE_WAIT_CPU,
15 LTTV_STATE_EXIT,
16 LTTV_STATE_WAIT,
17 LTTV_STATE_RUN;
18
19
20 static void
21 init(LttvTracesetState *self, LttvTraceset *ts)
22 {
23 guint i, j, nb_trace, nb_tracefile;
24
25 LttvTraceContext *tc;
26
27 LttvTraceState *tcs;
28
29 LttvTracefileContext *tfc;
30
31 LttvTracefileState *tfcs;
32
33 LTTV_TRACESET_CONTEXT_CLASS(g_type_class_peek_parent(LTTV_TRACESET_STATE_GET_CLASS(self)))->init((LttvTracesetContext *)self, ts);
34
35 nb_trace = lttv_traceset_number(ts);
36 for(i = 0 ; i < nb_trace ; i++) {
37 tcs = (LttvTraceState *)tc = (LTTV_TRACESET_CONTEXT(self)->traces[i]);
38 tcs->processes = g_hash_table_new(g_direct_hash, g_direct_equal);
39
40 nb_tracefile = ltt_trace_control_tracefile_number(tc->t);
41 for(j = 0 ; j < nb_tracefile ; j++) {
42 tfcs = (LttvTracefileState *)tfc = tc->control_tracefiles[j];
43 tfcs->process = NULL;
44 }
45
46 nb_tracefile = ltt_trace_per_cpu_tracefile_number(tc->t);
47 for(j = 0 ; j < nb_tracefile ; j++) {
48 tfcs = (LttvTracefileState *)tfc = tc->per_cpu_tracefiles[j];
49 tfcs->process = NULL;
50 }
51 }
52 }
53
54
55 static void
56 fini(LttvTracesetState *self)
57 {
58 guint i, j, nb_trace, nb_tracefile;
59
60 LttvTraceState *tcs;
61
62 LttvTracefileState *tfcs;
63
64 nb_trace = lttv_traceset_number(LTTV_TRACESET_CONTEXT(self)->ts);
65 for(i = 0 ; i < nb_trace ; i++) {
66 tcs = (LttvTraceState *)(LTTV_TRACESET_CONTEXT(self)->traces[i]);
67 remove_all_processes(tcs->processes);
68 g_hash_table_destroy(tcs->processes);
69 }
70 LTTV_TRACESET_CONTEXT_CLASS(g_type_class_peek_parent(LTTV_TRACESET_STATE_GET_CLASS(self)))->fini((LttvTracesetContext *)self);
71 }
72
73
74 LttvTracesetContext *
75 new_traceset_context(LttvTracesetContext *self)
76 {
77 return LTTV_TRACESET_CONTEXT(g_object_new(LTTV_TRACESET_STATE_TYPE, NULL));
78 }
79
80
81 LttvTraceContext *
82 new_trace_context(LttvTracesetContext *self)
83 {
84 return LTTV_TRACE_CONTEXT(g_object_new(LTTV_TRACE_STATE_TYPE, NULL));
85 }
86
87
88 LttvTracefileContext *
89 new_tracefile_context(LttvTracesetContext *self)
90 {
91 return LTTV_TRACEFILE_CONTEXT(g_object_new(LTTV_TRACEFILE_STATE_TYPE, NULL));
92 }
93
94
95 static void
96 traceset_state_instance_init (GTypeInstance *instance, gpointer g_class)
97 {
98 }
99
100
101 static void
102 traceset_state_finalize (LttvTracesetContext *self)
103 {
104 G_OBJECT_CLASS(g_type_class_peek_parent(LTTV_TRACESET_STATE_GET_CLASS(self)))->finalize(G_OBJECT(self));
105 }
106
107
108 static void
109 traceset_state_class_init (LttvTracesetContextClass *klass)
110 {
111 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
112
113 gobject_class->finalize = (void (*)(GObject *self)) traceset_state_finalize;
114 klass->init = (void (*)(LttvTracesetContext *self, LttvTraceset *ts))init;
115 klass->fini = (void (*)(LttvTracesetContext *self))fini;
116 klass->new_traceset_context = new_traceset_context;
117 klass->new_trace_context = new_trace_context;
118 klass->new_tracefile_context = new_tracefile_context;
119 }
120
121
122 GType
123 lttv_traceset_state_get_type(void)
124 {
125 static GType type = 0;
126 if (type == 0) {
127 static const GTypeInfo info = {
128 sizeof (LttvTracesetStateClass),
129 NULL, /* base_init */
130 NULL, /* base_finalize */
131 (GClassInitFunc) traceset_state_class_init, /* class_init */
132 NULL, /* class_finalize */
133 NULL, /* class_data */
134 sizeof (LttvTracesetContext),
135 0, /* n_preallocs */
136 (GInstanceInitFunc) traceset_state_instance_init /* instance_init */
137 };
138
139 type = g_type_register_static (LTTV_TRACESET_CONTEXT_TYPE, "LttvTracesetStateType",
140 &info, 0);
141 }
142 return type;
143 }
144
145
146 static void
147 trace_state_instance_init (GTypeInstance *instance, gpointer g_class)
148 {
149 }
150
151
152 static void
153 trace_state_finalize (LttvTraceContext *self)
154 {
155 G_OBJECT_CLASS(g_type_class_peek_parent(LTTV_TRACE_STATE_GET_CLASS(self)))->finalize(G_OBJECT(self));
156 }
157
158
159 static void
160 trace_state_class_init (LttvTraceContextClass *klass)
161 {
162 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
163
164 gobject_class->finalize = (void (*)(GObject *self)) trace_state_finalize;
165 }
166
167
168 GType
169 lttv_trace_state_get_type(void)
170 {
171 static GType type = 0;
172 if (type == 0) {
173 static const GTypeInfo info = {
174 sizeof (LttvTraceStateClass),
175 NULL, /* base_init */
176 NULL, /* base_finalize */
177 (GClassInitFunc) trace_state_class_init, /* class_init */
178 NULL, /* class_finalize */
179 NULL, /* class_data */
180 sizeof (LttvTracesetState),
181 0, /* n_preallocs */
182 (GInstanceInitFunc) trace_state_instance_init /* instance_init */
183 };
184
185 type = g_type_register_static (LTTV_TRACE_CONTEXT_TYPE,
186 "LttvTraceStateType", &info, 0);
187 }
188 return type;
189 }
190
191
192 static void
193 tracefile_state_instance_init (GTypeInstance *instance, gpointer g_class)
194 {
195 }
196
197
198 static void
199 tracefile_state_finalize (LttvTracefileState *self)
200 {
201 G_OBJECT_CLASS(g_type_class_peek_parent(LTTV_TRACEFILE_STATE_GET_CLASS(self)))->finalize(G_OBJECT(self));
202 }
203
204
205 static void
206 tracefile_state_class_init (LttvTracefileStateClass *klass)
207 {
208 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
209
210 gobject_class->finalize = (void (*)(GObject *self)) tracefile_state_finalize;
211 }
212
213
214 GType
215 lttv_tracefile_state_get_type(void)
216 {
217 static GType type = 0;
218 if (type == 0) {
219 static const GTypeInfo info = {
220 sizeof (LttvTracefileStateClass),
221 NULL, /* base_init */
222 NULL, /* base_finalize */
223 (GClassInitFunc) tracefile_state_class_init, /* class_init */
224 NULL, /* class_finalize */
225 NULL, /* class_data */
226 sizeof (LttvTracefileState),
227 0, /* n_preallocs */
228 (GInstanceInitFunc) tracefile_state_instance_init /* instance_init */
229 };
230
231 type = g_type_register_static (LTTV_TRACEFILE_CONTEXT_TYPE,
232 "LttvTracefileStateType", &info, 0);
233 }
234 return type;
235 }
236
237
238 struct HookData {
239 LttField *f1;
240 LttField *f2;
241 LttField *f3;
242 };
243
244
245 struct HookId {
246 LttvHook h;
247 guint id;
248 void *hook_data;
249 gboolean free_hook_data;
250 };
251
252
253 static void push_state(LttvTracefileState *tfs, LttvInterruptType t,
254 guint state_id)
255 {
256 LttvInterruptState *intr;
257
258 LttvProcessState *process = tfs->process;
259
260 guint depth = process->interrupt_stack->len;
261
262 g_array_set_size(process->interrupt_stack, depth + 1);
263 intr = &g_array_index(process->interrupt_stack, LttvInterruptState, depth);
264 intr->t = t;
265 intr->n = state_id;
266 intr->entry = intr->last_change = LTTV_TRACEFILE_CONTEXT(tfs)->timestamp;
267 intr->s = process->state->s;
268 }
269
270
271 static void pop_state(LttvTracefileState *tfs, LttvInterruptType t)
272 {
273 LttvProcessState *process = tfs->process;
274
275 guint depth = process->interrupt_stack->len - 1;
276
277 g_assert(process->state->t == t);
278 g_array_remove_index(process->interrupt_stack, depth);
279 depth--;
280 process->state = &g_array_index(process->interrupt_stack, LttvInterruptState,
281 depth);
282 }
283
284
285 LttvProcessState *create_process(LttvTracefileState *tfs,
286 LttvProcessState *parent, guint pid)
287 {
288 LttvProcessState *process = g_new(LttvProcessState, 1);
289
290 LttvInterruptState *intr;
291
292 LttvTraceContext *tc;
293
294 LttvTraceContext *tcs;
295
296 tcs = (LttvTraceState *)tc = LTTV_TRACEFILE_CONTEXT(tfs)->ts;
297
298 g_hash_table_insert(tcs->processes, GUINT_TO_POINTER(pid), process);
299 process->pid = pid;
300 process->birth = tfs->timestamp;
301 process->name = LTTV_STATE_UNNAMED;
302 process->interrupt_stack = g_array_new(FALSE, FALSE,
303 sizeof(LttvInterruptState));
304 g_array_set_size(process->interrupt_stack, 1);
305 intr = process->state = &g_array_index(process->interrupt_stack,
306 LttvInterruptState, 0);
307 intr->t = LTTV_STATE_USER_MODE
308 intr->n = 0;
309 intr->entry = tfs->timestamp;
310 intr->last_change = tfs->timestamp;
311 intr->s = LTTV_STATE_WAIT_FORK;
312 }
313
314
315 LttvProcessState *find_process(LttvTraceState *tfs, guint pid)
316 {
317 LttvProcessState *process = g_hash_table_lookup(tfs->processes, pid);
318 if(process == NULL) process = create_process(tfs, NULL, pid);
319 return process;
320 }
321
322
323 void exit_process(LttvTraceState *ts, LttvProcessState *process)
324 {
325 g_hash_table_remove(tfs-ts->processes, process->pid);
326 g_array_free(process->interrupt_stack, TRUE);
327 g_free(process);
328 }
329
330
331 void free_process_state(gpointer key, gpointer value, gpointer user_data)
332 {
333 g_array_free(((LttvProcessState *)value)->interrupt_stack, TRUE);
334 g_free(value);
335 }
336
337
338 void remove_all_processes(GHashTable *processes)
339 {
340 g_hash_table_foreach(processes, free_process_state, NULL);
341 }
342
343
344 gboolean syscall_entry(void *hook_data, void *call_data)
345 {
346 LttField *f = (LttField *)hook_data;
347
348 struct LttvTracefileState *s = (LttvTraceFileState *)call_data;
349
350 push_state(s, LTTV_STATE_SYSCALL, ltt_event_get_unsigned(s->e, f));
351 return FALSE;
352 }
353
354
355 gboolean syscall_exit(void *hook_data, void *call_data)
356 {
357 struct LttvTracefileState *s = (LttvTraceFileState *)call_data;
358
359 pop_state(s, LTTV_STATE_SYSCALL);
360 return FALSE;
361 }
362
363
364 gboolean trap_entry(void *hook_data, void *call_data)
365 {
366 LttField *f = (LttField *)hook_data;
367
368 struct LttvTracefileState *s = (LttvTraceFileState *)call_data;
369
370 push_state(s, LTTV_STATE_TRAP, ltt_event_get_unsigned(s->e, f));
371 return FALSE;
372 }
373
374
375 gboolean trap_exit(void *hook_data, void *call_data)
376 {
377 struct LttvTracefileState *s = (LttvTraceFileState *)call_data;
378
379 pop_state(s, LTTV_STATE_TRAP);
380 return FALSE;
381 }
382
383
384 gboolean irq_entry(void *hook_data, void *call_data)
385 {
386 LttField *f = (LttField *)hook_data;
387
388 struct LttvTracefileState *s = (LttvTraceFileState *)call_data;
389
390 /* Do something with the info about being in user or system mode when int? */
391 push_state(s, LTTV_STATE_IRQ, ltt_event_get_unsigned(s->e, f));
392 return FALSE;
393 }
394
395
396 gboolean irq_exit(void *hook_data, void *call_data)
397 {
398 struct LttvTracefileState *s = (LttvTraceFileState *)call_data;
399
400 pop_state(s, LTTV_STATE_IRQ);
401 return FALSE;
402 }
403
404
405 gboolean schedchange(void *hook_data, void *call_data)
406 {
407 struct HookData *h = (struct HookData *)hook_data;
408
409 struct LttvTracefileState *s = (LttvTraceFileState *)call_data;
410
411 guint pid_in, pid_out, state_out;
412
413 pid_in = ltt_event_get_int(s->e, h->f1);
414 pid_out = ltt_event_get_int(s->e, h->f2);
415 state_out = ltt_event_get_int(s->e, h->f3);
416 if(s->process != NULL) {
417 if(state_out == 0) s->process->state->s = LTTV_STATE_WAIT_CPU;
418 else if(s->process->state->s == LTTV_STATE_EXIT)
419 exit_process(s->tc, s->process);
420 else s->process->state->s = LTTV_STATE_WAIT;
421 }
422 s->process = find_process(s->tc, pid_in);
423 s->process->state->s = LTTV_STATE_RUN;
424 return FALSE;
425 }
426
427
428 gboolean process_fork(void *hook_data, void *call_data)
429 {
430 LttField *f = (LttField *)hook_data;
431
432 struct LttvTracefileState *s = (LttvTraceFileState *)call_data;
433
434 guint child_pid;
435
436 child_pid = ltt_event_get_int(s->e, f);
437 create_process(s->tc, s->process, child_pid);
438 return FALSE;
439 }
440
441
442 gboolean process_exit(void *hook_data, void *call_data)
443 {
444 struct LttvTracefileState *s = (LttvTraceFileState *)call_data;
445
446 if(s->process != NULL) {
447 s->process->state->s = LTTV_STATE_EXIT;
448 }
449 return FALSE;
450 }
451
452
453 static LttField *
454 find_field(LttEventType *et, const char *field)
455 {
456 LttType *t;
457
458 LttField *f;
459
460 guint i, nb;
461
462 char *name;
463
464 if(field == NULL) return NULL;
465
466 f = ltt_eventtype_field(et);
467 t = ltt_eventtype_type(et);
468 g_assert(ltt_type_class(t) == LTT_STRUCT);
469 nb = ltt_type_member_number(t);
470 for(i = 0 ; i < nb ; i++) {
471 ltt_type_member_type(t, i, &name);
472 if(strcmp(name, field) == 0) break;
473 }
474 g_assert(i < nb);
475 return ltt_field_member(f, i);
476 }
477
478
479 static HookId
480 find_hook(LttTrace *t, const char *facility, const char *event,
481 const char *field1, *field2, *field3, LttHook h)
482 {
483 LttFacility *f;
484
485 LttEventType *et;
486
487 guint nb, pos, i;
488
489 struct HookId hook_id;
490
491 struct HookData hook_data, *phook_data;
492
493 char *name;
494
495 nb = ltt_trace_facility_find(t, facility, &pos);
496 if(nb < 1) g_error("No %s facility", facility);
497 f = ltt_facility_get(t, pos);
498 et = ltt_facility_get_by_name(f, event);
499 if(et == NULL) g_error("Event %s does not exist", event);
500
501 hook_id.id = ltt_eventtype_id(et);
502 hook_id.h = h;
503 hook_id.free_hook_data = FALSE;
504 hook_data.f1 = find_field(et, field1);
505 hook_data.f2 = find_field(et, field2);
506 hook_data.f3 = find_field(et, field3);
507 if(hook_data->f1 == NULL) hook_id->hook_data = NULL;
508 else if(hook_data->f2 == NULL) hook_id->hook_data = hook_data->f1;
509 else {
510 phook_data = g_new(struct HookData, 1);
511 *phook_data = hook_data;
512 hook_id.hook_data = phook_data;
513 hook_id.free_hook_data = TRUE;
514 }
515 return hook_id;
516 }
517
518
519 lttv_state_add_event_hooks(LttvTracesetState *self)
520 {
521 LttvTraceset *ts = self->ts;
522
523 guint i, j, nb_trace, nb_control, nb_per_cpu, nb_tracefile;
524
525 LttFacility *f;
526
527 LttEventType *et;
528
529 LttvTraceState *tc;
530
531 LttvTracefileState *tfc;
532
533 void *hook_data;
534
535 GArray *hooks;
536
537 struct HookId hook_id;
538
539 LttvAttributeValue val;
540
541 nb_trace = lttv_traceset_number(ts);
542 for(i = 0 ; i < nb_trace ; i++) {
543 tc = (LttvTraceState *)self->traces[i];
544
545 /* Find the eventtype id for the following events and register the
546 associated by id hooks. */
547
548 hooks = g_array_new(FALSE, FALSE, struct HookId);
549 g_array_add(hooks, find_hook(tc->t, "core","syscall_entry", "syscall_id",
550 NULL, NULL, syscall_entry));
551 g_array_add(hooks, find_hook(tc->t, "core", "syscall_exit", NULL, NULL,
552 NULL, syscall_exit));
553 g_array_add(hooks, find_hook(tc->t, "core", "trap_entry", "trap_id",
554 NULL, trap_entry));
555 g_array_add(hooks, find_hook(tc->t, "core", "trap_exit", NULL, NULL,
556 NULL, trap_exit));
557 g_array_add(hooks, find_hook(tc->t, "core", "irq_entry", "irq_id",
558 NULL, irq_entry));
559 g_array_add(hooks, find_hook(tc->t, "core", "irq_exit", NULL, NULL,
560 NULL, irq_exit));
561 g_array_add(hooks, find_hook(tc->t, "core", "schedchange", "in", "out",
562 "out_state", schedchange));
563 g_array_add(hooks, find_hook(tc->t, "core", "process_fork", "child_pid",
564 NULL, NULL, process_fork));
565 g_array_add(hooks, find_hook(tc->t, "core", "process_exit", NULL, NULL,
566 NULL, process_exit));
567
568 /* Add these hooks to each before_event_by_id hooks list */
569
570 nb_control = ltt_trace_control_tracefile_number(tc->t);
571 nb_per_cpu = ltt_trace_control_tracefile_number(tc->t);
572 nb_tracefile = nb_control + nb_per_cpu;
573 for(j = 0 ; j < nb_tracefile ; j++) {
574 if(j < nb_control) {
575 tfc = tc->control_tracefiles[j];
576 }
577 else {
578 tfc = tc->per_cpu_tracefiles[j];
579 }
580
581 for(k = 0 ; k < hooks->len ; k++) {
582 hook_id = g_array_index(hooks, struct HookId, k);
583 lttv_hooks_add(lttv_hooks_by_id_find(tfc->before_event_by_id,
584 hook_id->id), hook_id->h, hook_id->hook_data);
585 }
586 }
587 lttv_attribute_find(self->a, LTTV_STATE_HOOKS, LTTV_POINTER, val);
588 val->v_pointer = hooks;
589 }
590 }
591
592
593 lttv_state_remove_event_hooks(LttvTracesetState *self)
594 {
595 LttvTraceset *ts = self->ts;
596
597 guint i, j, nb_trace, nb_control, nb_per_cpu, nb_tracefile;
598
599 LttvTraceState *tc;
600
601 LttvTracefileState *tfc;
602
603 void *hook_data;
604
605 GArray *hooks;
606
607 struct HookId hook_id;
608
609 LttvAttributeValue val;
610
611 nb_trace = lttv_traceset_number(ts);
612 for(i = 0 ; i < nb_trace ; i++) {
613 tc = (LttvTraceState *)self->traces[i];
614 lttv_attribute_find(self->a, LTTV_STATE_HOOKS, LTTV_POINTER, val);
615 hooks = val->v_pointer;
616
617 /* Add these hooks to each before_event_by_id hooks list */
618
619 nb_control = ltt_trace_control_tracefile_number(tc->t);
620 nb_per_cpu = ltt_trace_control_tracefile_number(tc->t);
621 nb_tracefile = nb_control + nb_per_cpu;
622 for(j = 0 ; j < nb_tracefile ; j++) {
623 if(j < nb_control) {
624 tfc = tc->control_tracefiles[j];
625 }
626 else {
627 tfc = tc->per_cpu_tracefiles[j];
628 }
629
630 for(k = 0 ; k < hooks->len ; k++) {
631 hook_id = g_array_index(hooks, struct HookId, k);
632 lttv_hooks_remove_data(lttv_hooks_by_id_find(tfc->before_event_by_id,
633 hook_id->id), hook_id->h, hook_id->hook_data);
634
635 }
636 for(k = 0 ; k < hooks->len ; k++) {
637 hook_id = g_array_index(hooks, struct HookId, k);
638 if(hook_id->free_hook_data) g_free(hook_id->hook_data);
639 }
640 }
641 g_array_free(hooks, TRUE);
642 }
643 }
644
645
646 void lttv_state_init(int argc, char **argv)
647 {
648 LTTV_STATE_UNNAMED = g_quark_from_string("unnamed");
649 LTTV_STATE_USER_MODE = g_quark_from_string("user mode");
650 LTTV_STATE_WAIT_FORK = g_quark_from_string("wait fork");
651 LTTV_STATE_SYSCALL = g_quark_from_string("system call");
652 LTTV_STATE_TRAP = g_quark_from_string("trap");
653 LTTV_STATE_IRQ = g_quark_from_string("irq");
654 LTTV_STATE_WAIT_CPU = g_quark_from_string("wait for cpu");
655 LTTV_STATE_EXIT = g_quark_from_string("exiting");
656 LTTV_STATE_WAIT = g_quark_from_string("wait for I/O");
657 LTTV_STATE_RUN = g_quark_from_string("running");
658 LTTV_STATE_HOOKS = g_quark_from_string("saved state hooks");
659 }
660
661 void lttv_state_destroy()
662 {
663 }
664
665
666
667
This page took 0.048804 seconds and 5 git commands to generate.