git-svn-id: http://ltt.polymtl.ca/svn@172 04897980-b3bd-0310-b5e0-8ef037075253
[lttv.git] / ltt / branches / poly / lttv / state.c
CommitLineData
dc877563 1
2#include <lttv/state.h>
3
ffd54a90 4LttvInterruptType
5 LTTV_STATE_USER_MODE,
6 LTTV_STATE_SYSCALL,
7 LTTV_STATE_TRAP,
8 LTTV_STATE_IRQ;
9
10
11LttvProcessStatus
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
dc877563 19
20static void
21init(LttvTracesetState *self, LttvTraceset *ts)
22{
23 guint i, j, nb_trace, nb_tracefile;
24
ffd54a90 25 LttvTraceContext *tc;
dc877563 26
ffd54a90 27 LttvTraceState *tcs;
28
29 LttvTracefileContext *tfc;
dc877563 30
ffd54a90 31 LttvTracefileState *tfcs;
32
33 LTTV_TRACESET_CONTEXT_CLASS(g_type_class_peek_parent(LTTV_TRACESET_STATE_GET_CLASS(self)))->init((LttvTracesetContext *)self, ts);
dc877563 34
35 nb_trace = lttv_traceset_number(ts);
36 for(i = 0 ; i < nb_trace ; i++) {
ffd54a90 37 tcs = (LttvTraceState *)tc = (LTTV_TRACESET_CONTEXT(self)->traces[i]);
38 tcs->processes = g_hash_table_new(g_direct_hash, g_direct_equal);
dc877563 39
40 nb_tracefile = ltt_trace_control_tracefile_number(tc->t);
41 for(j = 0 ; j < nb_tracefile ; j++) {
ffd54a90 42 tfcs = (LttvTracefileState *)tfc = tc->control_tracefiles[j];
43 tfcs->process = NULL;
dc877563 44 }
45
46 nb_tracefile = ltt_trace_per_cpu_tracefile_number(tc->t);
47 for(j = 0 ; j < nb_tracefile ; j++) {
ffd54a90 48 tfcs = (LttvTracefileState *)tfc = tc->per_cpu_tracefiles[j];
49 tfcs->process = NULL;
dc877563 50 }
51 }
52}
53
54
55static void
56fini(LttvTracesetState *self)
57{
58 guint i, j, nb_trace, nb_tracefile;
59
ffd54a90 60 LttvTraceState *tcs;
dc877563 61
ffd54a90 62 LttvTracefileState *tfcs;
dc877563 63
ffd54a90 64 nb_trace = lttv_traceset_number(LTTV_TRACESET_CONTEXT(self)->ts);
dc877563 65 for(i = 0 ; i < nb_trace ; i++) {
ffd54a90 66 tcs = (LttvTraceState *)(LTTV_TRACESET_CONTEXT(self)->traces[i]);
67 remove_all_processes(tcs->processes);
68 g_hash_table_destroy(tcs->processes);
dc877563 69 }
ffd54a90 70 LTTV_TRACESET_CONTEXT_CLASS(g_type_class_peek_parent(LTTV_TRACESET_STATE_GET_CLASS(self)))->fini((LttvTracesetContext *)self);
dc877563 71}
72
73
74LttvTracesetContext *
75new_traceset_context(LttvTracesetContext *self)
76{
ffd54a90 77 return LTTV_TRACESET_CONTEXT(g_object_new(LTTV_TRACESET_STATE_TYPE, NULL));
dc877563 78}
79
80
81LttvTraceContext *
82new_trace_context(LttvTracesetContext *self)
83{
ffd54a90 84 return LTTV_TRACE_CONTEXT(g_object_new(LTTV_TRACE_STATE_TYPE, NULL));
dc877563 85}
86
87
88LttvTracefileContext *
89new_tracefile_context(LttvTracesetContext *self)
90{
ffd54a90 91 return LTTV_TRACEFILE_CONTEXT(g_object_new(LTTV_TRACEFILE_STATE_TYPE, NULL));
92}
93
94
95static void
96traceset_state_instance_init (GTypeInstance *instance, gpointer g_class)
97{
98}
99
100
101static void
102traceset_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
108static void
109traceset_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;
dc877563 119}
120
121
122GType
123lttv_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 */
ffd54a90 131 (GClassInitFunc) traceset_state_class_init, /* class_init */
dc877563 132 NULL, /* class_finalize */
133 NULL, /* class_data */
134 sizeof (LttvTracesetContext),
135 0, /* n_preallocs */
ffd54a90 136 (GInstanceInitFunc) traceset_state_instance_init /* instance_init */
dc877563 137 };
138
139 type = g_type_register_static (LTTV_TRACESET_CONTEXT_TYPE, "LttvTracesetStateType",
140 &info, 0);
141 }
142 return type;
143}
144
145
146static void
ffd54a90 147trace_state_instance_init (GTypeInstance *instance, gpointer g_class)
dc877563 148{
149}
150
151
152static void
ffd54a90 153trace_state_finalize (LttvTraceContext *self)
dc877563 154{
ffd54a90 155 G_OBJECT_CLASS(g_type_class_peek_parent(LTTV_TRACE_STATE_GET_CLASS(self)))->finalize(G_OBJECT(self));
dc877563 156}
157
158
159static void
ffd54a90 160trace_state_class_init (LttvTraceContextClass *klass)
dc877563 161{
162 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
163
ffd54a90 164 gobject_class->finalize = (void (*)(GObject *self)) trace_state_finalize;
dc877563 165}
166
167
168GType
169lttv_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 */
ffd54a90 177 (GClassInitFunc) trace_state_class_init, /* class_init */
dc877563 178 NULL, /* class_finalize */
179 NULL, /* class_data */
180 sizeof (LttvTracesetState),
181 0, /* n_preallocs */
ffd54a90 182 (GInstanceInitFunc) trace_state_instance_init /* instance_init */
dc877563 183 };
184
185 type = g_type_register_static (LTTV_TRACE_CONTEXT_TYPE,
186 "LttvTraceStateType", &info, 0);
187 }
188 return type;
189}
190
191
192static void
ffd54a90 193tracefile_state_instance_init (GTypeInstance *instance, gpointer g_class)
dc877563 194{
195}
196
197
198static void
ffd54a90 199tracefile_state_finalize (LttvTracefileState *self)
dc877563 200{
ffd54a90 201 G_OBJECT_CLASS(g_type_class_peek_parent(LTTV_TRACEFILE_STATE_GET_CLASS(self)))->finalize(G_OBJECT(self));
dc877563 202}
203
204
205static void
ffd54a90 206tracefile_state_class_init (LttvTracefileStateClass *klass)
dc877563 207{
208 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
209
ffd54a90 210 gobject_class->finalize = (void (*)(GObject *self)) tracefile_state_finalize;
dc877563 211}
212
213
214GType
215lttv_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 */
ffd54a90 223 (GClassInitFunc) tracefile_state_class_init, /* class_init */
dc877563 224 NULL, /* class_finalize */
225 NULL, /* class_data */
226 sizeof (LttvTracefileState),
227 0, /* n_preallocs */
ffd54a90 228 (GInstanceInitFunc) tracefile_state_instance_init /* instance_init */
dc877563 229 };
230
231 type = g_type_register_static (LTTV_TRACEFILE_CONTEXT_TYPE,
232 "LttvTracefileStateType", &info, 0);
233 }
234 return type;
235}
236
237
dc877563 238struct HookData {
239 LttField *f1;
240 LttField *f2;
241 LttField *f3;
ffd54a90 242};
dc877563 243
244
245struct HookId {
246 LttvHook h;
247 guint id;
248 void *hook_data;
249 gboolean free_hook_data;
ffd54a90 250};
dc877563 251
252
ffd54a90 253static void push_state(LttvTracefileState *tfs, LttvInterruptType t,
254 guint state_id)
dc877563 255{
ffd54a90 256 LttvInterruptState *intr;
dc877563 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;
ffd54a90 266 intr->entry = intr->last_change = LTTV_TRACEFILE_CONTEXT(tfs)->timestamp;
dc877563 267 intr->s = process->state->s;
268}
269
270
ffd54a90 271static void pop_state(LttvTracefileState *tfs, LttvInterruptType t)
dc877563 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
ffd54a90 285LttvProcessState *create_process(LttvTracefileState *tfs,
286 LttvProcessState *parent, guint pid)
dc877563 287{
288 LttvProcessState *process = g_new(LttvProcessState, 1);
289
290 LttvInterruptState *intr;
291
ffd54a90 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);
dc877563 299 process->pid = pid;
ffd54a90 300 process->birth = tfs->timestamp;
dc877563 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);
ffd54a90 305 intr = process->state = &g_array_index(process->interrupt_stack,
306 LttvInterruptState, 0);
dc877563 307 intr->t = LTTV_STATE_USER_MODE
308 intr->n = 0;
ffd54a90 309 intr->entry = tfs->timestamp;
310 intr->last_change = tfs->timestamp;
dc877563 311 intr->s = LTTV_STATE_WAIT_FORK;
312}
313
314
315LttvProcessState *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
323void 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
331void 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
338void remove_all_processes(GHashTable *processes)
339{
340 g_hash_table_foreach(processes, free_process_state, NULL);
341}
342
343
344gboolean syscall_entry(void *hook_data, void *call_data)
345{
346 LttField *f = (LttField *)hook_data;
347
348 struct LttvTracefileState *s = (LttvTraceFileState *)call_data;
349
ffd54a90 350 push_state(s, LTTV_STATE_SYSCALL, ltt_event_get_unsigned(s->e, f));
dc877563 351 return FALSE;
352}
353
354
355gboolean syscall_exit(void *hook_data, void *call_data)
356{
357 struct LttvTracefileState *s = (LttvTraceFileState *)call_data;
358
ffd54a90 359 pop_state(s, LTTV_STATE_SYSCALL);
dc877563 360 return FALSE;
361}
362
363
364gboolean trap_entry(void *hook_data, void *call_data)
365{
366 LttField *f = (LttField *)hook_data;
367
368 struct LttvTracefileState *s = (LttvTraceFileState *)call_data;
369
ffd54a90 370 push_state(s, LTTV_STATE_TRAP, ltt_event_get_unsigned(s->e, f));
dc877563 371 return FALSE;
372}
373
374
375gboolean trap_exit(void *hook_data, void *call_data)
376{
377 struct LttvTracefileState *s = (LttvTraceFileState *)call_data;
378
ffd54a90 379 pop_state(s, LTTV_STATE_TRAP);
dc877563 380 return FALSE;
381}
382
383
384gboolean 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? */
ffd54a90 391 push_state(s, LTTV_STATE_IRQ, ltt_event_get_unsigned(s->e, f));
dc877563 392 return FALSE;
393}
394
395
396gboolean irq_exit(void *hook_data, void *call_data)
397{
398 struct LttvTracefileState *s = (LttvTraceFileState *)call_data;
399
ffd54a90 400 pop_state(s, LTTV_STATE_IRQ);
dc877563 401 return FALSE;
402}
403
404
405gboolean 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) {
ffd54a90 417 if(state_out == 0) s->process->state->s = LTTV_STATE_WAIT_CPU;
418 else if(s->process->state->s == LTTV_STATE_EXIT)
dc877563 419 exit_process(s->tc, s->process);
ffd54a90 420 else s->process->state->s = LTTV_STATE_WAIT;
dc877563 421 }
422 s->process = find_process(s->tc, pid_in);
ffd54a90 423 s->process->state->s = LTTV_STATE_RUN;
dc877563 424 return FALSE;
425}
426
427
428gboolean 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
442gboolean process_exit(void *hook_data, void *call_data)
443{
444 struct LttvTracefileState *s = (LttvTraceFileState *)call_data;
445
446 if(s->process != NULL) {
ffd54a90 447 s->process->state->s = LTTV_STATE_EXIT;
dc877563 448 }
449 return FALSE;
450}
451
452
453static LttField *
454find_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
479static HookId
480find_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
519lttv_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);
ffd54a90 585 }
dc877563 586 }
ffd54a90 587 lttv_attribute_find(self->a, LTTV_STATE_HOOKS, LTTV_POINTER, val);
dc877563 588 val->v_pointer = hooks;
589 }
590}
591
592
593lttv_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];
ffd54a90 614 lttv_attribute_find(self->a, LTTV_STATE_HOOKS, LTTV_POINTER, val);
dc877563 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);
ffd54a90 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 }
dc877563 640 }
641 g_array_free(hooks, TRUE);
642 }
643}
644
645
ffd54a90 646void 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}
dc877563 660
ffd54a90 661void lttv_state_destroy()
662{
663}
dc877563 664
665
666
667
This page took 0.048791 seconds and 4 git commands to generate.