add block device resource tracing infrastructure to state.{c,h}
[lttv.git] / ltt / branches / poly / lttv / lttv / state.c
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
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <lttv/lttv.h>
24 #include <lttv/module.h>
25 #include <lttv/state.h>
26 #include <ltt/facility.h>
27 #include <ltt/trace.h>
28 #include <ltt/event.h>
29 #include <ltt/type.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 /* Comment :
34 * Mathieu Desnoyers
35 * usertrace is there only to be able to update the current CPU of the
36 * usertraces when there is a schedchange. it is a way to link the ProcessState
37 * to the associated usertrace. Link only created upon thread creation.
38 *
39 * The cpu id is necessary : it gives us back the current ProcessState when we
40 * are considering data from the usertrace.
41 */
42
43 #define PREALLOCATED_EXECUTION_STACK 10
44
45 /* Facilities Quarks */
46
47 GQuark
48 LTT_FACILITY_KERNEL,
49 LTT_FACILITY_KERNEL_ARCH,
50 LTT_FACILITY_LIST,
51 LTT_FACILITY_FS,
52 LTT_FACILITY_USER_GENERIC,
53 LTT_FACILITY_BLOCK;
54
55 /* Events Quarks */
56
57 GQuark
58 LTT_EVENT_SYSCALL_ENTRY,
59 LTT_EVENT_SYSCALL_EXIT,
60 LTT_EVENT_TRAP_ENTRY,
61 LTT_EVENT_TRAP_EXIT,
62 LTT_EVENT_IRQ_ENTRY,
63 LTT_EVENT_IRQ_EXIT,
64 LTT_EVENT_SOFT_IRQ_ENTRY,
65 LTT_EVENT_SOFT_IRQ_EXIT,
66 LTT_EVENT_SCHED_SCHEDULE,
67 LTT_EVENT_PROCESS_FORK,
68 LTT_EVENT_KTHREAD_CREATE,
69 LTT_EVENT_PROCESS_EXIT,
70 LTT_EVENT_PROCESS_FREE,
71 LTT_EVENT_EXEC,
72 LTT_EVENT_PROCESS_STATE,
73 LTT_EVENT_STATEDUMP_END,
74 LTT_EVENT_FUNCTION_ENTRY,
75 LTT_EVENT_FUNCTION_EXIT,
76 LTT_EVENT_THREAD_BRAND,
77 LTT_EVENT_REQUEST_ISSUE,
78 LTT_EVENT_REQUEST_COMPLETE;
79
80 /* Fields Quarks */
81
82 GQuark
83 LTT_FIELD_SYSCALL_ID,
84 LTT_FIELD_TRAP_ID,
85 LTT_FIELD_IRQ_ID,
86 LTT_FIELD_SOFT_IRQ_ID,
87 LTT_FIELD_PREV_PID,
88 LTT_FIELD_NEXT_PID,
89 LTT_FIELD_PREV_STATE,
90 LTT_FIELD_PARENT_PID,
91 LTT_FIELD_CHILD_PID,
92 LTT_FIELD_PID,
93 LTT_FIELD_TGID,
94 LTT_FIELD_CHILD_TGID,
95 LTT_FIELD_FILENAME,
96 LTT_FIELD_NAME,
97 LTT_FIELD_TYPE,
98 LTT_FIELD_MODE,
99 LTT_FIELD_SUBMODE,
100 LTT_FIELD_STATUS,
101 LTT_FIELD_THIS_FN,
102 LTT_FIELD_CALL_SITE,
103 LTT_FIELD_MINOR,
104 LTT_FIELD_MAJOR,
105 LTT_FIELD_OPERATION;
106
107 LttvExecutionMode
108 LTTV_STATE_MODE_UNKNOWN,
109 LTTV_STATE_USER_MODE,
110 LTTV_STATE_SYSCALL,
111 LTTV_STATE_TRAP,
112 LTTV_STATE_IRQ,
113 LTTV_STATE_SOFT_IRQ;
114
115 LttvExecutionSubmode
116 LTTV_STATE_SUBMODE_UNKNOWN,
117 LTTV_STATE_SUBMODE_NONE;
118
119 LttvProcessStatus
120 LTTV_STATE_UNNAMED,
121 LTTV_STATE_WAIT_FORK,
122 LTTV_STATE_WAIT_CPU,
123 LTTV_STATE_EXIT,
124 LTTV_STATE_ZOMBIE,
125 LTTV_STATE_WAIT,
126 LTTV_STATE_RUN,
127 LTTV_STATE_DEAD;
128
129 GQuark
130 LTTV_STATE_UNBRANDED;
131
132 LttvProcessType
133 LTTV_STATE_USER_THREAD,
134 LTTV_STATE_KERNEL_THREAD;
135
136 LttvCPUMode
137 LTTV_CPU_UNKNOWN,
138 LTTV_CPU_IDLE,
139 LTTV_CPU_BUSY,
140 LTTV_CPU_IRQ,
141 LTTV_CPU_TRAP;
142
143 LttvIRQMode
144 LTTV_IRQ_UNKNOWN,
145 LTTV_IRQ_IDLE,
146 LTTV_IRQ_BUSY;
147
148 LttvBdevMode
149 LTTV_BDEV_UNKNOWN,
150 LTTV_BDEV_IDLE,
151 LTTV_BDEV_BUSY_READING,
152 LTTV_BDEV_BUSY_WRITING;
153
154 static GQuark
155 LTTV_STATE_TRACEFILES,
156 LTTV_STATE_PROCESSES,
157 LTTV_STATE_PROCESS,
158 LTTV_STATE_RUNNING_PROCESS,
159 LTTV_STATE_EVENT,
160 LTTV_STATE_SAVED_STATES,
161 LTTV_STATE_SAVED_STATES_TIME,
162 LTTV_STATE_TIME,
163 LTTV_STATE_HOOKS,
164 LTTV_STATE_NAME_TABLES,
165 LTTV_STATE_TRACE_STATE_USE_COUNT,
166 LTTV_STATE_RESOURCE_CPUS;
167
168 static void create_max_time(LttvTraceState *tcs);
169
170 static void get_max_time(LttvTraceState *tcs);
171
172 static void free_max_time(LttvTraceState *tcs);
173
174 static void create_name_tables(LttvTraceState *tcs);
175
176 static void get_name_tables(LttvTraceState *tcs);
177
178 static void free_name_tables(LttvTraceState *tcs);
179
180 static void free_saved_state(LttvTraceState *tcs);
181
182 static void lttv_state_free_process_table(GHashTable *processes);
183
184 static void lttv_trace_states_read_raw(LttvTraceState *tcs, FILE *fp,
185 GPtrArray *quarktable);
186
187 void lttv_state_save(LttvTraceState *self, LttvAttribute *container)
188 {
189 LTTV_TRACE_STATE_GET_CLASS(self)->state_save(self, container);
190 }
191
192
193 void lttv_state_restore(LttvTraceState *self, LttvAttribute *container)
194 {
195 LTTV_TRACE_STATE_GET_CLASS(self)->state_restore(self, container);
196 }
197
198
199 void lttv_state_state_saved_free(LttvTraceState *self,
200 LttvAttribute *container)
201 {
202 LTTV_TRACE_STATE_GET_CLASS(self)->state_saved_free(self, container);
203 }
204
205
206 guint process_hash(gconstpointer key)
207 {
208 guint pid = ((const LttvProcessState *)key)->pid;
209 return (pid>>8 ^ pid>>4 ^ pid>>2 ^ pid) ;
210 }
211
212
213 /* If the hash table hash function is well distributed,
214 * the process_equal should compare different pid */
215 gboolean process_equal(gconstpointer a, gconstpointer b)
216 {
217 const LttvProcessState *process_a, *process_b;
218 gboolean ret = TRUE;
219
220 process_a = (const LttvProcessState *)a;
221 process_b = (const LttvProcessState *)b;
222
223 if(likely(process_a->pid != process_b->pid)) ret = FALSE;
224 else if(likely(process_a->pid == 0 &&
225 process_a->cpu != process_b->cpu)) ret = FALSE;
226
227 return ret;
228 }
229
230 static void delete_usertrace(gpointer key, gpointer value, gpointer user_data)
231 {
232 g_tree_destroy((GTree*)value);
233 }
234
235 static void lttv_state_free_usertraces(GHashTable *usertraces)
236 {
237 g_hash_table_foreach(usertraces, delete_usertrace, NULL);
238 g_hash_table_destroy(usertraces);
239 }
240
241
242
243 static void
244 restore_init_state(LttvTraceState *self)
245 {
246 guint i, nb_cpus;
247
248 LttvTracefileState *tfcs;
249
250 LttTime start_time, end_time;
251
252 /* Free the process tables */
253 if(self->processes != NULL) lttv_state_free_process_table(self->processes);
254 if(self->usertraces != NULL) lttv_state_free_usertraces(self->usertraces);
255 self->processes = g_hash_table_new(process_hash, process_equal);
256 self->usertraces = g_hash_table_new(g_direct_hash, g_direct_equal);
257 self->nb_event = 0;
258
259 /* Seek time to beginning */
260 // Mathieu : fix : don't seek traceset here : causes inconsistency in seek
261 // closest. It's the tracecontext job to seek the trace to the beginning
262 // anyway : the init state might be used at the middle of the trace as well...
263 //g_tree_destroy(self->parent.ts_context->pqueue);
264 //self->parent.ts_context->pqueue = g_tree_new(compare_tracefile);
265
266 ltt_trace_time_span_get(self->parent.t, &start_time, &end_time);
267
268 //lttv_process_trace_seek_time(&self->parent, ltt_time_zero);
269
270 nb_cpus = ltt_trace_get_num_cpu(self->parent.t);
271
272 /* Put the per cpu running_process to beginning state : process 0. */
273 for(i=0; i< nb_cpus; i++) {
274 LttvExecutionState *es;
275 self->running_process[i] = lttv_state_create_process(self, NULL, i, 0, 0,
276 LTTV_STATE_UNNAMED, &start_time);
277 /* We are not sure is it's a kernel thread or normal thread, put the
278 * bottom stack state to unknown */
279 self->running_process[i]->execution_stack =
280 g_array_set_size(self->running_process[i]->execution_stack, 1);
281 es = self->running_process[i]->state =
282 &g_array_index(self->running_process[i]->execution_stack,
283 LttvExecutionState, 0);
284 es->t = LTTV_STATE_MODE_UNKNOWN;
285 es->s = LTTV_STATE_UNNAMED;
286
287 //self->running_process[i]->state->s = LTTV_STATE_RUN;
288 self->running_process[i]->cpu = i;
289 }
290
291 #if 0
292 nb_tracefile = self->parent.tracefiles->len;
293
294 for(i = 0 ; i < nb_tracefile ; i++) {
295 tfcs =
296 LTTV_TRACEFILE_STATE(g_array_index(self->parent.tracefiles,
297 LttvTracefileContext*, i));
298 ltt_trace_time_span_get(self->parent.t, &tfcs->parent.timestamp, NULL);
299 // tfcs->saved_position = 0;
300 tfcs->process = lttv_state_create_process(tfcs, NULL,0);
301 tfcs->process->state->s = LTTV_STATE_RUN;
302 tfcs->process->last_cpu = tfcs->cpu_name;
303 tfcs->process->last_cpu_index = ltt_tracefile_num(((LttvTracefileContext*)tfcs)->tf);
304 }
305 #endif //0
306 }
307
308 //static LttTime time_zero = {0,0};
309
310 static gint compare_usertraces(gconstpointer a, gconstpointer b,
311 gpointer user_data)
312 {
313 const LttTime *t1 = (const LttTime *)a;
314 const LttTime *t2 = (const LttTime *)b;
315
316 return ltt_time_compare(*t1, *t2);
317 }
318
319 static void free_usertrace_key(gpointer data)
320 {
321 g_free(data);
322 }
323
324 #define MAX_STRING_LEN 4096
325
326 static void
327 state_load_saved_states(LttvTraceState *tcs)
328 {
329 FILE *fp;
330 GPtrArray *quarktable;
331 char *trace_path;
332 char path[PATH_MAX];
333 guint count;
334 guint i;
335 tcs->has_precomputed_states = FALSE;
336 GQuark q;
337 gchar *string;
338 gint hdr;
339 gchar buf[MAX_STRING_LEN];
340 guint len;
341
342 trace_path = g_quark_to_string(ltt_trace_name(tcs->parent.t));
343 strncpy(path, trace_path, PATH_MAX-1);
344 count = strnlen(trace_path, PATH_MAX-1);
345 // quarktable : open, test
346 strncat(path, "/precomputed/quarktable", PATH_MAX-count-1);
347 fp = fopen(path, "r");
348 if(!fp) return;
349 quarktable = g_ptr_array_sized_new(4096);
350
351 /* Index 0 is null */
352 hdr = fgetc(fp);
353 if(hdr == EOF) return;
354 g_assert(hdr == HDR_QUARKS);
355 q = 1;
356 do {
357 hdr = fgetc(fp);
358 if(hdr == EOF) break;
359 g_assert(hdr == HDR_QUARK);
360 g_ptr_array_set_size(quarktable, q+1);
361 i=0;
362 while(1) {
363 fread(&buf[i], sizeof(gchar), 1, fp);
364 if(buf[i] == '\0' || feof(fp)) break;
365 i++;
366 }
367 len = strnlen(buf, MAX_STRING_LEN-1);
368 g_ptr_array_index (quarktable, q) = g_new(gchar, len+1);
369 strncpy(g_ptr_array_index (quarktable, q), buf, len+1);
370 q++;
371 } while(1);
372
373 fclose(fp);
374
375 // saved_states : open, test
376 strncpy(path, trace_path, PATH_MAX-1);
377 count = strnlen(trace_path, PATH_MAX-1);
378 strncat(path, "/precomputed/states", PATH_MAX-count-1);
379 fp = fopen(path, "r");
380 if(!fp) return;
381
382 hdr = fgetc(fp);
383 if(hdr != HDR_TRACE) goto end;
384
385 lttv_trace_states_read_raw(tcs, fp, quarktable);
386
387 tcs->has_precomputed_states = TRUE;
388
389 end:
390 fclose(fp);
391
392 /* Free the quarktable */
393 for(i=0; i<quarktable->len; i++) {
394 string = g_ptr_array_index (quarktable, i);
395 g_free(string);
396 }
397 g_ptr_array_free(quarktable, TRUE);
398 return;
399 }
400
401 static void
402 init(LttvTracesetState *self, LttvTraceset *ts)
403 {
404 guint i, j, nb_trace, nb_tracefile, nb_cpu;
405 guint64 nb_irq;
406
407 LttvTraceContext *tc;
408
409 LttvTraceState *tcs;
410
411 LttvTracefileState *tfcs;
412
413 LttvAttributeValue v;
414
415 LTTV_TRACESET_CONTEXT_CLASS(g_type_class_peek(LTTV_TRACESET_CONTEXT_TYPE))->
416 init((LttvTracesetContext *)self, ts);
417
418 nb_trace = lttv_traceset_number(ts);
419 for(i = 0 ; i < nb_trace ; i++) {
420 tc = self->parent.traces[i];
421 tcs = LTTV_TRACE_STATE(tc);
422 tcs->save_interval = LTTV_STATE_SAVE_INTERVAL;
423 lttv_attribute_find(tcs->parent.t_a, LTTV_STATE_TRACE_STATE_USE_COUNT,
424 LTTV_UINT, &v);
425 (*v.v_uint)++;
426
427 if(*(v.v_uint) == 1) {
428 create_name_tables(tcs);
429 create_max_time(tcs);
430 }
431 get_name_tables(tcs);
432 get_max_time(tcs);
433
434 nb_tracefile = tc->tracefiles->len;
435 nb_cpu = ltt_trace_get_num_cpu(tc->t);
436 nb_irq = tcs->nb_irqs;
437 tcs->processes = NULL;
438 tcs->usertraces = NULL;
439 tcs->running_process = g_new(LttvProcessState*, nb_cpu);
440
441 /* init cpu resource stuff */
442 tcs->cpu_states = g_new(LttvCPUState, nb_cpu);
443 for(j = 0; j<nb_cpu; j++) {
444 tcs->cpu_states[j].mode_stack = g_array_new(FALSE, FALSE, sizeof(LttvCPUMode));
445 g_assert(tcs->cpu_states[j].mode_stack != NULL);
446 }
447
448 /* init irq resource stuff */
449 tcs->irq_states = g_new(LttvIRQState, nb_irq);
450 for(j = 0; j<nb_irq; j++) {
451 tcs->irq_states[j].mode_stack = g_array_new(FALSE, FALSE, sizeof(LttvIRQMode));
452 g_assert(tcs->irq_states[j].mode_stack != NULL);
453 }
454
455 /* init bdev resource stuff */
456 tcs->bdev_states = g_hash_table_new(g_int_hash, g_int_equal);
457
458 restore_init_state(tcs);
459 for(j = 0 ; j < nb_tracefile ; j++) {
460 tfcs =
461 LTTV_TRACEFILE_STATE(g_array_index(tc->tracefiles,
462 LttvTracefileContext*, j));
463 tfcs->tracefile_name = ltt_tracefile_name(tfcs->parent.tf);
464 tfcs->cpu = ltt_tracefile_cpu(tfcs->parent.tf);
465 tfcs->cpu_state = &(tcs->cpu_states[tfcs->cpu]);
466 if(ltt_tracefile_tid(tfcs->parent.tf) != 0) {
467 /* It's a Usertrace */
468 guint tid = ltt_tracefile_tid(tfcs->parent.tf);
469 GTree *usertrace_tree = (GTree*)g_hash_table_lookup(tcs->usertraces,
470 (gconstpointer)tid);
471 if(!usertrace_tree) {
472 usertrace_tree = g_tree_new_full(compare_usertraces,
473 NULL, free_usertrace_key, NULL);
474 g_hash_table_insert(tcs->usertraces,
475 (gpointer)tid, usertrace_tree);
476 }
477 LttTime *timestamp = g_new(LttTime, 1);
478 *timestamp = ltt_interpolate_time_from_tsc(tfcs->parent.tf,
479 ltt_tracefile_creation(tfcs->parent.tf));
480 g_tree_insert(usertrace_tree, timestamp, tfcs);
481 }
482 }
483
484 /* See if the trace has saved states */
485 state_load_saved_states(tcs);
486 }
487 }
488
489 static void
490 fini(LttvTracesetState *self)
491 {
492 guint i, nb_trace;
493
494 LttvTraceState *tcs;
495
496 LttvTracefileState *tfcs;
497
498 LttvAttributeValue v;
499
500 nb_trace = lttv_traceset_number(LTTV_TRACESET_CONTEXT(self)->ts);
501 for(i = 0 ; i < nb_trace ; i++) {
502 tcs = (LttvTraceState *)(LTTV_TRACESET_CONTEXT(self)->traces[i]);
503 lttv_attribute_find(tcs->parent.t_a, LTTV_STATE_TRACE_STATE_USE_COUNT,
504 LTTV_UINT, &v);
505
506 g_assert(*(v.v_uint) != 0);
507 (*v.v_uint)--;
508
509 if(*(v.v_uint) == 0) {
510 free_name_tables(tcs);
511 free_max_time(tcs);
512 free_saved_state(tcs);
513 }
514 g_free(tcs->running_process);
515 tcs->running_process = NULL;
516 lttv_state_free_process_table(tcs->processes);
517 lttv_state_free_usertraces(tcs->usertraces);
518 tcs->processes = NULL;
519 tcs->usertraces = NULL;
520 }
521 LTTV_TRACESET_CONTEXT_CLASS(g_type_class_peek(LTTV_TRACESET_CONTEXT_TYPE))->
522 fini((LttvTracesetContext *)self);
523 }
524
525
526 static LttvTracesetContext *
527 new_traceset_context(LttvTracesetContext *self)
528 {
529 return LTTV_TRACESET_CONTEXT(g_object_new(LTTV_TRACESET_STATE_TYPE, NULL));
530 }
531
532
533 static LttvTraceContext *
534 new_trace_context(LttvTracesetContext *self)
535 {
536 return LTTV_TRACE_CONTEXT(g_object_new(LTTV_TRACE_STATE_TYPE, NULL));
537 }
538
539
540 static LttvTracefileContext *
541 new_tracefile_context(LttvTracesetContext *self)
542 {
543 return LTTV_TRACEFILE_CONTEXT(g_object_new(LTTV_TRACEFILE_STATE_TYPE, NULL));
544 }
545
546
547 /* Write the process state of the trace */
548
549 static void write_process_state(gpointer key, gpointer value,
550 gpointer user_data)
551 {
552 LttvProcessState *process;
553
554 LttvExecutionState *es;
555
556 FILE *fp = (FILE *)user_data;
557
558 guint i;
559 guint64 address;
560
561 process = (LttvProcessState *)value;
562 fprintf(fp,
563 " <PROCESS CORE=%p PID=%u TGID=%u PPID=%u TYPE=\"%s\" CTIME_S=%lu CTIME_NS=%lu ITIME_S=%lu ITIME_NS=%lu NAME=\"%s\" BRAND=\"%s\" CPU=\"%u\">\n",
564 process, process->pid, process->tgid, process->ppid,
565 g_quark_to_string(process->type),
566 process->creation_time.tv_sec,
567 process->creation_time.tv_nsec,
568 process->insertion_time.tv_sec,
569 process->insertion_time.tv_nsec,
570 g_quark_to_string(process->name),
571 g_quark_to_string(process->brand),
572 process->cpu);
573
574 for(i = 0 ; i < process->execution_stack->len; i++) {
575 es = &g_array_index(process->execution_stack, LttvExecutionState, i);
576 fprintf(fp, " <ES MODE=\"%s\" SUBMODE=\"%s\" ENTRY_S=%lu ENTRY_NS=%lu",
577 g_quark_to_string(es->t), g_quark_to_string(es->n),
578 es->entry.tv_sec, es->entry.tv_nsec);
579 fprintf(fp, " CHANGE_S=%lu CHANGE_NS=%lu STATUS=\"%s\"/>\n",
580 es->change.tv_sec, es->change.tv_nsec, g_quark_to_string(es->s));
581 }
582
583 for(i = 0 ; i < process->user_stack->len; i++) {
584 address = &g_array_index(process->user_stack, guint64, i);
585 fprintf(fp, " <USER_STACK ADDRESS=\"%llu\"/>\n",
586 address);
587 }
588
589 if(process->usertrace) {
590 fprintf(fp, " <USERTRACE NAME=\"%s\" CPU=%u\n/>",
591 g_quark_to_string(process->usertrace->tracefile_name),
592 process->usertrace->cpu);
593 }
594
595
596 fprintf(fp, " </PROCESS>\n");
597 }
598
599
600 void lttv_state_write(LttvTraceState *self, LttTime t, FILE *fp)
601 {
602 guint i, nb_tracefile, nb_block, offset;
603 guint64 tsc;
604
605 LttvTracefileState *tfcs;
606
607 LttTracefile *tf;
608
609 LttEventPosition *ep;
610
611 guint nb_cpus;
612
613 ep = ltt_event_position_new();
614
615 fprintf(fp,"<PROCESS_STATE TIME_S=%lu TIME_NS=%lu>\n", t.tv_sec, t.tv_nsec);
616
617 g_hash_table_foreach(self->processes, write_process_state, fp);
618
619 nb_cpus = ltt_trace_get_num_cpu(self->parent.t);
620 for(i=0;i<nb_cpus;i++) {
621 fprintf(fp," <CPU NUM=%u RUNNING_PROCESS=%u>\n",
622 i, self->running_process[i]->pid);
623 }
624
625 nb_tracefile = self->parent.tracefiles->len;
626
627 for(i = 0 ; i < nb_tracefile ; i++) {
628 tfcs =
629 LTTV_TRACEFILE_STATE(g_array_index(self->parent.tracefiles,
630 LttvTracefileContext*, i));
631 fprintf(fp, " <TRACEFILE TIMESTAMP_S=%lu TIMESTAMP_NS=%lu",
632 tfcs->parent.timestamp.tv_sec,
633 tfcs->parent.timestamp.tv_nsec);
634 LttEvent *e = ltt_tracefile_get_event(tfcs->parent.tf);
635 if(e == NULL) fprintf(fp,"/>\n");
636 else {
637 ltt_event_position(e, ep);
638 ltt_event_position_get(ep, &tf, &nb_block, &offset, &tsc);
639 fprintf(fp, " BLOCK=%u OFFSET=%u TSC=%llu/>\n", nb_block, offset,
640 tsc);
641 }
642 }
643 g_free(ep);
644 fprintf(fp,"</PROCESS_STATE>\n");
645 }
646
647
648 static void write_process_state_raw(gpointer key, gpointer value,
649 gpointer user_data)
650 {
651 LttvProcessState *process;
652
653 LttvExecutionState *es;
654
655 FILE *fp = (FILE *)user_data;
656
657 guint i;
658 guint64 address;
659
660 process = (LttvProcessState *)value;
661 fputc(HDR_PROCESS, fp);
662 //fwrite(&header, sizeof(header), 1, fp);
663 //fprintf(fp, "%s", g_quark_to_string(process->type));
664 //fputc('\0', fp);
665 fwrite(&process->type, sizeof(process->type), 1, fp);
666 //fprintf(fp, "%s", g_quark_to_string(process->name));
667 //fputc('\0', fp);
668 fwrite(&process->name, sizeof(process->name), 1, fp);
669 //fprintf(fp, "%s", g_quark_to_string(process->brand));
670 //fputc('\0', fp);
671 fwrite(&process->brand, sizeof(process->brand), 1, fp);
672 fwrite(&process->pid, sizeof(process->pid), 1, fp);
673 fwrite(&process->tgid, sizeof(process->tgid), 1, fp);
674 fwrite(&process->ppid, sizeof(process->ppid), 1, fp);
675 fwrite(&process->cpu, sizeof(process->cpu), 1, fp);
676 fwrite(&process->creation_time, sizeof(process->creation_time), 1, fp);
677 fwrite(&process->insertion_time, sizeof(process->insertion_time), 1, fp);
678
679 #if 0
680 fprintf(fp,
681 " <PROCESS CORE=%p PID=%u TGID=%u PPID=%u TYPE=\"%s\" CTIME_S=%lu CTIME_NS=%lu ITIME_S=%lu ITIME_NS=%lu NAME=\"%s\" BRAND=\"%s\" CPU=\"%u\" PROCESS_TYPE=%u>\n",
682 process, process->pid, process->tgid, process->ppid,
683 g_quark_to_string(process->type),
684 process->creation_time.tv_sec,
685 process->creation_time.tv_nsec,
686 process->insertion_time.tv_sec,
687 process->insertion_time.tv_nsec,
688 g_quark_to_string(process->name),
689 g_quark_to_string(process->brand),
690 process->cpu);
691 #endif //0
692
693 for(i = 0 ; i < process->execution_stack->len; i++) {
694 es = &g_array_index(process->execution_stack, LttvExecutionState, i);
695
696 fputc(HDR_ES, fp);
697 //fprintf(fp, "%s", g_quark_to_string(es->t));
698 //fputc('\0', fp);
699 fwrite(&es->t, sizeof(es->t), 1, fp);
700 //fprintf(fp, "%s", g_quark_to_string(es->n));
701 //fputc('\0', fp);
702 fwrite(&es->n, sizeof(es->n), 1, fp);
703 //fprintf(fp, "%s", g_quark_to_string(es->s));
704 //fputc('\0', fp);
705 fwrite(&es->s, sizeof(es->s), 1, fp);
706 fwrite(&es->entry, sizeof(es->entry), 1, fp);
707 fwrite(&es->change, sizeof(es->change), 1, fp);
708 fwrite(&es->cum_cpu_time, sizeof(es->cum_cpu_time), 1, fp);
709 #if 0
710 fprintf(fp, " <ES MODE=\"%s\" SUBMODE=\"%s\" ENTRY_S=%lu ENTRY_NS=%lu",
711 g_quark_to_string(es->t), g_quark_to_string(es->n),
712 es->entry.tv_sec, es->entry.tv_nsec);
713 fprintf(fp, " CHANGE_S=%lu CHANGE_NS=%lu STATUS=\"%s\"/>\n",
714 es->change.tv_sec, es->change.tv_nsec, g_quark_to_string(es->s));
715 #endif //0
716 }
717
718 for(i = 0 ; i < process->user_stack->len; i++) {
719 address = &g_array_index(process->user_stack, guint64, i);
720 fputc(HDR_USER_STACK, fp);
721 fwrite(&address, sizeof(address), 1, fp);
722 #if 0
723 fprintf(fp, " <USER_STACK ADDRESS=\"%llu\"/>\n",
724 address);
725 #endif //0
726 }
727
728 if(process->usertrace) {
729 fputc(HDR_USERTRACE, fp);
730 //fprintf(fp, "%s", g_quark_to_string(process->usertrace->tracefile_name));
731 //fputc('\0', fp);
732 fwrite(&process->usertrace->tracefile_name,
733 sizeof(process->usertrace->tracefile_name), 1, fp);
734 fwrite(&process->usertrace->cpu, sizeof(process->usertrace->cpu), 1, fp);
735 #if 0
736 fprintf(fp, " <USERTRACE NAME=\"%s\" CPU=%u\n/>",
737 g_quark_to_string(process->usertrace->tracefile_name),
738 process->usertrace->cpu);
739 #endif //0
740 }
741
742 }
743
744
745 void lttv_state_write_raw(LttvTraceState *self, LttTime t, FILE *fp)
746 {
747 guint i, nb_tracefile, nb_block, offset;
748 guint64 tsc;
749
750 LttvTracefileState *tfcs;
751
752 LttTracefile *tf;
753
754 LttEventPosition *ep;
755
756 guint nb_cpus;
757
758 ep = ltt_event_position_new();
759
760 //fprintf(fp,"<PROCESS_STATE TIME_S=%lu TIME_NS=%lu>\n", t.tv_sec, t.tv_nsec);
761 fputc(HDR_PROCESS_STATE, fp);
762 fwrite(&t, sizeof(t), 1, fp);
763
764 g_hash_table_foreach(self->processes, write_process_state_raw, fp);
765
766 nb_cpus = ltt_trace_get_num_cpu(self->parent.t);
767 for(i=0;i<nb_cpus;i++) {
768 fputc(HDR_CPU, fp);
769 fwrite(&i, sizeof(i), 1, fp); /* cpu number */
770 fwrite(&self->running_process[i]->pid,
771 sizeof(self->running_process[i]->pid), 1, fp);
772 //fprintf(fp," <CPU NUM=%u RUNNING_PROCESS=%u>\n",
773 // i, self->running_process[i]->pid);
774 }
775
776 nb_tracefile = self->parent.tracefiles->len;
777
778 for(i = 0 ; i < nb_tracefile ; i++) {
779 tfcs =
780 LTTV_TRACEFILE_STATE(g_array_index(self->parent.tracefiles,
781 LttvTracefileContext*, i));
782 // fprintf(fp, " <TRACEFILE TIMESTAMP_S=%lu TIMESTAMP_NS=%lu",
783 // tfcs->parent.timestamp.tv_sec,
784 // tfcs->parent.timestamp.tv_nsec);
785 fputc(HDR_TRACEFILE, fp);
786 fwrite(&tfcs->parent.timestamp, sizeof(tfcs->parent.timestamp), 1, fp);
787 /* Note : if timestamp if LTT_TIME_INFINITE, there will be no
788 * position following : end of trace */
789 LttEvent *e = ltt_tracefile_get_event(tfcs->parent.tf);
790 if(e != NULL) {
791 ltt_event_position(e, ep);
792 ltt_event_position_get(ep, &tf, &nb_block, &offset, &tsc);
793 //fprintf(fp, " BLOCK=%u OFFSET=%u TSC=%llu/>\n", nb_block, offset,
794 // tsc);
795 fwrite(&nb_block, sizeof(nb_block), 1, fp);
796 fwrite(&offset, sizeof(offset), 1, fp);
797 fwrite(&tsc, sizeof(tsc), 1, fp);
798 }
799 }
800 g_free(ep);
801 }
802
803
804 /* Read process state from a file */
805
806 /* Called because a HDR_PROCESS was found */
807 static void read_process_state_raw(LttvTraceState *self, FILE *fp,
808 GPtrArray *quarktable)
809 {
810 LttvExecutionState *es;
811 LttvProcessState *process, *parent_process;
812 LttvProcessState tmp;
813 GQuark tmpq;
814
815 guint i;
816 guint64 *address;
817 guint cpu;
818
819 /* TODO : check return value */
820 fread(&tmp.type, sizeof(tmp.type), 1, fp);
821 fread(&tmp.name, sizeof(tmp.name), 1, fp);
822 fread(&tmp.brand, sizeof(tmp.brand), 1, fp);
823 fread(&tmp.pid, sizeof(tmp.pid), 1, fp);
824 fread(&tmp.tgid, sizeof(tmp.tgid), 1, fp);
825 fread(&tmp.ppid, sizeof(tmp.ppid), 1, fp);
826 fread(&tmp.cpu, sizeof(tmp.cpu), 1, fp);
827 fread(&tmp.creation_time, sizeof(tmp.creation_time), 1, fp);
828 fread(&tmp.insertion_time, sizeof(tmp.insertion_time), 1, fp);
829
830 if(tmp.pid == 0) {
831 process = lttv_state_find_process(self, tmp.cpu, tmp.pid);
832 } else {
833 /* We must link to the parent */
834 parent_process = lttv_state_find_process_or_create(self, ANY_CPU, tmp.ppid,
835 &ltt_time_zero);
836 process = lttv_state_find_process(self, ANY_CPU, tmp.pid);
837 if(process == NULL) {
838 process = lttv_state_create_process(self, parent_process, tmp.cpu,
839 tmp.pid, tmp.tgid,
840 g_quark_from_string((gchar*)g_ptr_array_index(quarktable, tmp.name)),
841 &tmp.creation_time);
842 }
843 }
844 process->insertion_time = tmp.insertion_time;
845 process->creation_time = tmp.creation_time;
846 process->type = g_quark_from_string(
847 (gchar*)g_ptr_array_index(quarktable, tmp.type));
848 process->tgid = tmp.tgid;
849 process->ppid = tmp.ppid;
850 process->brand = g_quark_from_string(
851 (gchar*)g_ptr_array_index(quarktable, tmp.brand));
852 process->name =
853 g_quark_from_string((gchar*)g_ptr_array_index(quarktable, tmp.name));
854
855
856 do {
857 if(feof(fp) || ferror(fp)) goto end_loop;
858
859 gint hdr = fgetc(fp);
860 if(hdr == EOF) goto end_loop;
861
862 switch(hdr) {
863 case HDR_ES:
864 process->execution_stack =
865 g_array_set_size(process->execution_stack,
866 process->execution_stack->len + 1);
867 es = &g_array_index(process->execution_stack, LttvExecutionState,
868 process->execution_stack->len-1);
869 process->state = es;
870
871 fread(&es->t, sizeof(es->t), 1, fp);
872 es->t = g_quark_from_string(
873 (gchar*)g_ptr_array_index(quarktable, es->t));
874 fread(&es->n, sizeof(es->n), 1, fp);
875 es->n = g_quark_from_string(
876 (gchar*)g_ptr_array_index(quarktable, es->n));
877 fread(&es->s, sizeof(es->s), 1, fp);
878 es->s = g_quark_from_string(
879 (gchar*)g_ptr_array_index(quarktable, es->s));
880 fread(&es->entry, sizeof(es->entry), 1, fp);
881 fread(&es->change, sizeof(es->change), 1, fp);
882 fread(&es->cum_cpu_time, sizeof(es->cum_cpu_time), 1, fp);
883 break;
884 case HDR_USER_STACK:
885 process->user_stack = g_array_set_size(process->user_stack,
886 process->user_stack->len + 1);
887 address = &g_array_index(process->user_stack, guint64,
888 process->user_stack->len-1);
889 fread(address, sizeof(address), 1, fp);
890 process->current_function = *address;
891 break;
892 case HDR_USERTRACE:
893 fread(&tmpq, sizeof(tmpq), 1, fp);
894 fread(&process->usertrace->cpu, sizeof(process->usertrace->cpu), 1, fp);
895 break;
896 default:
897 ungetc(hdr, fp);
898 goto end_loop;
899 };
900 } while(1);
901 end_loop:
902 return;
903 }
904
905
906 /* Called because a HDR_PROCESS_STATE was found */
907 /* Append a saved state to the trace states */
908 void lttv_state_read_raw(LttvTraceState *self, FILE *fp, GPtrArray *quarktable)
909 {
910 guint i, nb_tracefile, nb_block, offset;
911 guint64 tsc;
912 LttvTracefileState *tfcs;
913
914 LttEventPosition *ep;
915
916 guint nb_cpus;
917
918 int hdr;
919
920 LttTime t;
921
922 LttvAttribute *saved_states_tree, *saved_state_tree;
923
924 LttvAttributeValue value;
925 GTree *pqueue = self->parent.ts_context->pqueue;
926 ep = ltt_event_position_new();
927
928 restore_init_state(self);
929
930 fread(&t, sizeof(t), 1, fp);
931
932 do {
933 if(feof(fp) || ferror(fp)) goto end_loop;
934 hdr = fgetc(fp);
935 if(hdr == EOF) goto end_loop;
936
937 switch(hdr) {
938 case HDR_PROCESS:
939 /* Call read_process_state_raw */
940 read_process_state_raw(self, fp, quarktable);
941 break;
942 case HDR_TRACEFILE:
943 case HDR_TRACESET:
944 case HDR_TRACE:
945 case HDR_QUARKS:
946 case HDR_QUARK:
947 case HDR_ES:
948 case HDR_USER_STACK:
949 case HDR_USERTRACE:
950 case HDR_PROCESS_STATE:
951 case HDR_CPU:
952 ungetc(hdr, fp);
953 goto end_loop;
954 break;
955 default:
956 g_error("Error while parsing saved state file : unknown data header %d",
957 hdr);
958 };
959 } while(1);
960 end_loop:
961
962 nb_cpus = ltt_trace_get_num_cpu(self->parent.t);
963 for(i=0;i<nb_cpus;i++) {
964 int cpu_num;
965 hdr = fgetc(fp);
966 g_assert(hdr == HDR_CPU);
967 fread(&cpu_num, sizeof(cpu_num), 1, fp); /* cpu number */
968 g_assert(i == cpu_num);
969 fread(&self->running_process[i]->pid,
970 sizeof(self->running_process[i]->pid), 1, fp);
971 }
972
973 nb_tracefile = self->parent.tracefiles->len;
974
975 for(i = 0 ; i < nb_tracefile ; i++) {
976 tfcs =
977 LTTV_TRACEFILE_STATE(g_array_index(self->parent.tracefiles,
978 LttvTracefileContext*, i));
979 // fprintf(fp, " <TRACEFILE TIMESTAMP_S=%lu TIMESTAMP_NS=%lu",
980 // tfcs->parent.timestamp.tv_sec,
981 // tfcs->parent.timestamp.tv_nsec);
982 g_tree_remove(pqueue, &tfcs->parent);
983 hdr = fgetc(fp);
984 g_assert(hdr == HDR_TRACEFILE);
985 fread(&tfcs->parent.timestamp, sizeof(tfcs->parent.timestamp), 1, fp);
986 /* Note : if timestamp if LTT_TIME_INFINITE, there will be no
987 * position following : end of trace */
988 if(ltt_time_compare(tfcs->parent.timestamp, ltt_time_infinite) != 0) {
989 fread(&nb_block, sizeof(nb_block), 1, fp);
990 fread(&offset, sizeof(offset), 1, fp);
991 fread(&tsc, sizeof(tsc), 1, fp);
992 ltt_event_position_set(ep, tfcs->parent.tf, nb_block, offset, tsc);
993 gint ret = ltt_tracefile_seek_position(tfcs->parent.tf, ep);
994 g_assert(ret == 0);
995 g_tree_insert(pqueue, &tfcs->parent, &tfcs->parent);
996 }
997 }
998 g_free(ep);
999
1000 saved_states_tree = lttv_attribute_find_subdir(self->parent.t_a,
1001 LTTV_STATE_SAVED_STATES);
1002 saved_state_tree = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
1003 value = lttv_attribute_add(saved_states_tree,
1004 lttv_attribute_get_number(saved_states_tree), LTTV_GOBJECT);
1005 *(value.v_gobject) = (GObject *)saved_state_tree;
1006 value = lttv_attribute_add(saved_state_tree, LTTV_STATE_TIME, LTTV_TIME);
1007 *(value.v_time) = t;
1008 lttv_state_save(self, saved_state_tree);
1009 g_debug("Saving state at time %lu.%lu", t.tv_sec,
1010 t.tv_nsec);
1011
1012 *(self->max_time_state_recomputed_in_seek) = t;
1013
1014 }
1015
1016 /* Called when a HDR_TRACE is found */
1017 void lttv_trace_states_read_raw(LttvTraceState *tcs, FILE *fp,
1018 GPtrArray *quarktable)
1019 {
1020 int hdr;
1021
1022 do {
1023 if(feof(fp) || ferror(fp)) goto end_loop;
1024 hdr = fgetc(fp);
1025 if(hdr == EOF) goto end_loop;
1026
1027 switch(hdr) {
1028 case HDR_PROCESS_STATE:
1029 /* Call read_process_state_raw */
1030 lttv_state_read_raw(tcs, fp, quarktable);
1031 break;
1032 case HDR_TRACEFILE:
1033 case HDR_TRACESET:
1034 case HDR_TRACE:
1035 case HDR_QUARKS:
1036 case HDR_QUARK:
1037 case HDR_ES:
1038 case HDR_USER_STACK:
1039 case HDR_USERTRACE:
1040 case HDR_PROCESS:
1041 case HDR_CPU:
1042 g_error("Error while parsing saved state file :"
1043 " unexpected data header %d",
1044 hdr);
1045 break;
1046 default:
1047 g_error("Error while parsing saved state file : unknown data header %d",
1048 hdr);
1049 };
1050 } while(1);
1051 end_loop:
1052 *(tcs->max_time_state_recomputed_in_seek) = tcs->parent.time_span.end_time;
1053 restore_init_state(tcs);
1054 lttv_process_trace_seek_time(tcs, ltt_time_zero);
1055 return;
1056 }
1057
1058
1059
1060 /* Copy each process from an existing hash table to a new one */
1061
1062 static void copy_process_state(gpointer key, gpointer value,gpointer user_data)
1063 {
1064 LttvProcessState *process, *new_process;
1065
1066 GHashTable *new_processes = (GHashTable *)user_data;
1067
1068 guint i;
1069
1070 process = (LttvProcessState *)value;
1071 new_process = g_new(LttvProcessState, 1);
1072 *new_process = *process;
1073 new_process->execution_stack = g_array_sized_new(FALSE, FALSE,
1074 sizeof(LttvExecutionState), PREALLOCATED_EXECUTION_STACK);
1075 new_process->execution_stack =
1076 g_array_set_size(new_process->execution_stack,
1077 process->execution_stack->len);
1078 for(i = 0 ; i < process->execution_stack->len; i++) {
1079 g_array_index(new_process->execution_stack, LttvExecutionState, i) =
1080 g_array_index(process->execution_stack, LttvExecutionState, i);
1081 }
1082 new_process->state = &g_array_index(new_process->execution_stack,
1083 LttvExecutionState, new_process->execution_stack->len - 1);
1084 new_process->user_stack = g_array_sized_new(FALSE, FALSE,
1085 sizeof(guint64), 0);
1086 new_process->user_stack =
1087 g_array_set_size(new_process->user_stack,
1088 process->user_stack->len);
1089 for(i = 0 ; i < process->user_stack->len; i++) {
1090 g_array_index(new_process->user_stack, guint64, i) =
1091 g_array_index(process->user_stack, guint64, i);
1092 }
1093 new_process->current_function = process->current_function;
1094 g_hash_table_insert(new_processes, new_process, new_process);
1095 }
1096
1097
1098 static GHashTable *lttv_state_copy_process_table(GHashTable *processes)
1099 {
1100 GHashTable *new_processes = g_hash_table_new(process_hash, process_equal);
1101
1102 g_hash_table_foreach(processes, copy_process_state, new_processes);
1103 return new_processes;
1104 }
1105
1106 static LttvCPUState *lttv_state_copy_cpu_states(LttvCPUState *states, guint n)
1107 {
1108 guint i,j;
1109 LttvCPUState *retval;
1110
1111 retval = g_malloc(n*sizeof(LttvCPUState));
1112
1113 for(i=0; i<n; i++) {
1114 retval[i].mode_stack = g_array_new(FALSE, FALSE, sizeof(LttvCPUMode));
1115 retval[i].last_irq = states[i].last_irq;
1116 g_array_set_size(retval[i].mode_stack, states[i].mode_stack->len);
1117 for(j=0; j<states[i].mode_stack->len; j++) {
1118 g_array_index(retval[i].mode_stack, GQuark, j) = g_array_index(states[i].mode_stack, GQuark, j);
1119 }
1120 }
1121
1122 return retval;
1123 }
1124
1125 static void lttv_state_free_cpu_states(LttvCPUState *states, guint n)
1126 {
1127 guint i;
1128
1129 for(i=0; i<n; i++) {
1130 g_array_free(states[i].mode_stack, FALSE);
1131 }
1132
1133 g_free(states);
1134 }
1135
1136 /* The saved state for each trace contains a member "processes", which
1137 stores a copy of the process table, and a member "tracefiles" with
1138 one entry per tracefile. Each tracefile has a "process" member pointing
1139 to the current process and a "position" member storing the tracefile
1140 position (needed to seek to the current "next" event. */
1141
1142 static void state_save(LttvTraceState *self, LttvAttribute *container)
1143 {
1144 guint i, nb_tracefile, nb_cpus;
1145
1146 LttvTracefileState *tfcs;
1147
1148 LttvAttribute *tracefiles_tree, *tracefile_tree;
1149
1150 guint *running_process;
1151
1152 LttvAttributeType type;
1153
1154 LttvAttributeValue value;
1155
1156 LttvAttributeName name;
1157
1158 LttEventPosition *ep;
1159
1160 tracefiles_tree = lttv_attribute_find_subdir(container,
1161 LTTV_STATE_TRACEFILES);
1162
1163 value = lttv_attribute_add(container, LTTV_STATE_PROCESSES,
1164 LTTV_POINTER);
1165 *(value.v_pointer) = lttv_state_copy_process_table(self->processes);
1166
1167 /* Add the currently running processes array */
1168 nb_cpus = ltt_trace_get_num_cpu(self->parent.t);
1169 running_process = g_new(guint, nb_cpus);
1170 for(i=0;i<nb_cpus;i++) {
1171 running_process[i] = self->running_process[i]->pid;
1172 }
1173 value = lttv_attribute_add(container, LTTV_STATE_RUNNING_PROCESS,
1174 LTTV_POINTER);
1175 *(value.v_pointer) = running_process;
1176
1177 g_info("State save");
1178
1179 nb_tracefile = self->parent.tracefiles->len;
1180
1181 for(i = 0 ; i < nb_tracefile ; i++) {
1182 tfcs =
1183 LTTV_TRACEFILE_STATE(g_array_index(self->parent.tracefiles,
1184 LttvTracefileContext*, i));
1185 tracefile_tree = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
1186 value = lttv_attribute_add(tracefiles_tree, i,
1187 LTTV_GOBJECT);
1188 *(value.v_gobject) = (GObject *)tracefile_tree;
1189 #if 0
1190 value = lttv_attribute_add(tracefile_tree, LTTV_STATE_PROCESS,
1191 LTTV_UINT);
1192 *(value.v_uint) = tfcs->process->pid;
1193 #endif //0
1194 value = lttv_attribute_add(tracefile_tree, LTTV_STATE_EVENT,
1195 LTTV_POINTER);
1196 /* Only save the position if the tfs has not infinite time. */
1197 //if(!g_tree_lookup(self->parent.ts_context->pqueue, &tfcs->parent)
1198 // && current_tfcs != tfcs) {
1199 if(ltt_time_compare(tfcs->parent.timestamp, ltt_time_infinite) == 0) {
1200 *(value.v_pointer) = NULL;
1201 } else {
1202 LttEvent *e = ltt_tracefile_get_event(tfcs->parent.tf);
1203 ep = ltt_event_position_new();
1204 ltt_event_position(e, ep);
1205 *(value.v_pointer) = ep;
1206
1207 guint nb_block, offset;
1208 guint64 tsc;
1209 LttTracefile *tf;
1210 ltt_event_position_get(ep, &tf, &nb_block, &offset, &tsc);
1211 g_info("Block %u offset %u tsc %llu time %lu.%lu", nb_block, offset,
1212 tsc,
1213 tfcs->parent.timestamp.tv_sec, tfcs->parent.timestamp.tv_nsec);
1214 }
1215 }
1216
1217 value = lttv_attribute_add(container, LTTV_STATE_RESOURCE_CPUS,
1218 LTTV_POINTER);
1219 guint size = sizeof(LttvCPUState)*nb_cpus;
1220 *(value.v_pointer) = lttv_state_copy_cpu_states(self->cpu_states, nb_cpus);
1221 }
1222
1223
1224 static void state_restore(LttvTraceState *self, LttvAttribute *container)
1225 {
1226 guint i, nb_tracefile, pid, nb_cpus;
1227
1228 LttvTracefileState *tfcs;
1229
1230 LttvAttribute *tracefiles_tree, *tracefile_tree;
1231
1232 guint *running_process;
1233
1234 LttvAttributeType type;
1235
1236 LttvAttributeValue value;
1237
1238 LttvAttributeName name;
1239
1240 gboolean is_named;
1241
1242 LttEventPosition *ep;
1243
1244 LttvTracesetContext *tsc = self->parent.ts_context;
1245
1246 tracefiles_tree = lttv_attribute_find_subdir(container,
1247 LTTV_STATE_TRACEFILES);
1248
1249 type = lttv_attribute_get_by_name(container, LTTV_STATE_PROCESSES,
1250 &value);
1251 g_assert(type == LTTV_POINTER);
1252 lttv_state_free_process_table(self->processes);
1253 self->processes = lttv_state_copy_process_table(*(value.v_pointer));
1254
1255 /* Add the currently running processes array */
1256 nb_cpus = ltt_trace_get_num_cpu(self->parent.t);
1257 type = lttv_attribute_get_by_name(container, LTTV_STATE_RUNNING_PROCESS,
1258 &value);
1259 g_assert(type == LTTV_POINTER);
1260 running_process = *(value.v_pointer);
1261 for(i=0;i<nb_cpus;i++) {
1262 pid = running_process[i];
1263 self->running_process[i] = lttv_state_find_process(self, i, pid);
1264 g_assert(self->running_process[i] != NULL);
1265 }
1266
1267 printf("state restore\n");
1268
1269 nb_tracefile = self->parent.tracefiles->len;
1270
1271 //g_tree_destroy(tsc->pqueue);
1272 //tsc->pqueue = g_tree_new(compare_tracefile);
1273
1274 /* restore cpu resource states */
1275 type = lttv_attribute_get_by_name(container, LTTV_STATE_RESOURCE_CPUS, &value);
1276 g_assert(type == LTTV_POINTER);
1277 lttv_state_free_cpu_states(self->cpu_states, nb_cpus);
1278 self->cpu_states = lttv_state_copy_cpu_states(*(value.v_pointer), nb_cpus);
1279
1280 for(i = 0 ; i < nb_tracefile ; i++) {
1281 tfcs =
1282 LTTV_TRACEFILE_STATE(g_array_index(self->parent.tracefiles,
1283 LttvTracefileContext*, i));
1284 type = lttv_attribute_get(tracefiles_tree, i, &name, &value, &is_named);
1285 g_assert(type == LTTV_GOBJECT);
1286 tracefile_tree = *((LttvAttribute **)(value.v_gobject));
1287 #if 0
1288 type = lttv_attribute_get_by_name(tracefile_tree, LTTV_STATE_PROCESS,
1289 &value);
1290 g_assert(type == LTTV_UINT);
1291 pid = *(value.v_uint);
1292 tfcs->process = lttv_state_find_process_or_create(tfcs, pid);
1293 #endif //0
1294 type = lttv_attribute_get_by_name(tracefile_tree, LTTV_STATE_EVENT,
1295 &value);
1296 g_assert(type == LTTV_POINTER);
1297 //g_assert(*(value.v_pointer) != NULL);
1298 ep = *(value.v_pointer);
1299 g_assert(tfcs->parent.t_context != NULL);
1300
1301 tfcs->cpu_state = &self->cpu_states[tfcs->cpu];
1302
1303 LttvTracefileContext *tfc = LTTV_TRACEFILE_CONTEXT(tfcs);
1304 g_tree_remove(tsc->pqueue, tfc);
1305
1306 if(ep != NULL) {
1307 g_assert(ltt_tracefile_seek_position(tfc->tf, ep) == 0);
1308 tfc->timestamp = ltt_event_time(ltt_tracefile_get_event(tfc->tf));
1309 g_assert(ltt_time_compare(tfc->timestamp, ltt_time_infinite) != 0);
1310 g_tree_insert(tsc->pqueue, tfc, tfc);
1311 g_info("Restoring state for a tf at time %lu.%lu", tfc->timestamp.tv_sec, tfc->timestamp.tv_nsec);
1312 } else {
1313 tfc->timestamp = ltt_time_infinite;
1314 }
1315 }
1316 }
1317
1318
1319 static void state_saved_free(LttvTraceState *self, LttvAttribute *container)
1320 {
1321 guint i, nb_tracefile, nb_cpus;
1322
1323 LttvTracefileState *tfcs;
1324
1325 LttvAttribute *tracefiles_tree, *tracefile_tree;
1326
1327 guint *running_process;
1328
1329 LttvAttributeType type;
1330
1331 LttvAttributeValue value;
1332
1333 LttvAttributeName name;
1334
1335 gboolean is_named;
1336
1337 LttEventPosition *ep;
1338
1339 tracefiles_tree = lttv_attribute_find_subdir(container,
1340 LTTV_STATE_TRACEFILES);
1341 g_object_ref(G_OBJECT(tracefiles_tree));
1342 lttv_attribute_remove_by_name(container, LTTV_STATE_TRACEFILES);
1343
1344 type = lttv_attribute_get_by_name(container, LTTV_STATE_PROCESSES,
1345 &value);
1346 g_assert(type == LTTV_POINTER);
1347 lttv_state_free_process_table(*(value.v_pointer));
1348 *(value.v_pointer) = NULL;
1349 lttv_attribute_remove_by_name(container, LTTV_STATE_PROCESSES);
1350
1351 /* Free running processes array */
1352 nb_cpus = ltt_trace_get_num_cpu(self->parent.t);
1353 type = lttv_attribute_get_by_name(container, LTTV_STATE_RUNNING_PROCESS,
1354 &value);
1355 g_assert(type == LTTV_POINTER);
1356 running_process = *(value.v_pointer);
1357 g_free(running_process);
1358
1359 nb_tracefile = self->parent.tracefiles->len;
1360
1361 for(i = 0 ; i < nb_tracefile ; i++) {
1362 tfcs =
1363 LTTV_TRACEFILE_STATE(g_array_index(self->parent.tracefiles,
1364 LttvTracefileContext*, i));
1365 type = lttv_attribute_get(tracefiles_tree, i, &name, &value, &is_named);
1366 g_assert(type == LTTV_GOBJECT);
1367 tracefile_tree = *((LttvAttribute **)(value.v_gobject));
1368
1369 type = lttv_attribute_get_by_name(tracefile_tree, LTTV_STATE_EVENT,
1370 &value);
1371 g_assert(type == LTTV_POINTER);
1372 if(*(value.v_pointer) != NULL) g_free(*(value.v_pointer));
1373 }
1374 g_object_unref(G_OBJECT(tracefiles_tree));
1375 }
1376
1377
1378 static void free_saved_state(LttvTraceState *self)
1379 {
1380 guint i, nb;
1381
1382 LttvAttributeType type;
1383
1384 LttvAttributeValue value;
1385
1386 LttvAttributeName name;
1387
1388 gboolean is_named;
1389
1390 LttvAttribute *saved_states;
1391
1392 saved_states = lttv_attribute_find_subdir(self->parent.t_a,
1393 LTTV_STATE_SAVED_STATES);
1394
1395 nb = lttv_attribute_get_number(saved_states);
1396 for(i = 0 ; i < nb ; i++) {
1397 type = lttv_attribute_get(saved_states, i, &name, &value, &is_named);
1398 g_assert(type == LTTV_GOBJECT);
1399 state_saved_free(self, *((LttvAttribute **)value.v_gobject));
1400 }
1401
1402 lttv_attribute_remove_by_name(self->parent.t_a, LTTV_STATE_SAVED_STATES);
1403 }
1404
1405
1406 static void
1407 create_max_time(LttvTraceState *tcs)
1408 {
1409 LttvAttributeValue v;
1410
1411 lttv_attribute_find(tcs->parent.t_a, LTTV_STATE_SAVED_STATES_TIME,
1412 LTTV_POINTER, &v);
1413 g_assert(*(v.v_pointer) == NULL);
1414 *(v.v_pointer) = g_new(LttTime,1);
1415 *((LttTime *)*(v.v_pointer)) = ltt_time_zero;
1416 }
1417
1418
1419 static void
1420 get_max_time(LttvTraceState *tcs)
1421 {
1422 LttvAttributeValue v;
1423
1424 lttv_attribute_find(tcs->parent.t_a, LTTV_STATE_SAVED_STATES_TIME,
1425 LTTV_POINTER, &v);
1426 g_assert(*(v.v_pointer) != NULL);
1427 tcs->max_time_state_recomputed_in_seek = (LttTime *)*(v.v_pointer);
1428 }
1429
1430
1431 static void
1432 free_max_time(LttvTraceState *tcs)
1433 {
1434 LttvAttributeValue v;
1435
1436 lttv_attribute_find(tcs->parent.t_a, LTTV_STATE_SAVED_STATES_TIME,
1437 LTTV_POINTER, &v);
1438 g_free(*(v.v_pointer));
1439 *(v.v_pointer) = NULL;
1440 }
1441
1442
1443 typedef struct _LttvNameTables {
1444 // FIXME GQuark *eventtype_names;
1445 GQuark *syscall_names;
1446 guint nb_syscalls;
1447 GQuark *trap_names;
1448 guint nb_traps;
1449 GQuark *irq_names;
1450 guint nb_irqs;
1451 GQuark *soft_irq_names;
1452 guint nb_softirqs;
1453 } LttvNameTables;
1454
1455
1456 static void
1457 create_name_tables(LttvTraceState *tcs)
1458 {
1459 int i, nb;
1460
1461 GQuark f_name, e_name;
1462
1463 LttvTraceHook h;
1464
1465 LttvTraceHookByFacility *thf;
1466
1467 LttEventType *et;
1468
1469 LttType *t;
1470
1471 GString *fe_name = g_string_new("");
1472
1473 LttvNameTables *name_tables = g_new(LttvNameTables, 1);
1474
1475 LttvAttributeValue v;
1476
1477 lttv_attribute_find(tcs->parent.t_a, LTTV_STATE_NAME_TABLES,
1478 LTTV_POINTER, &v);
1479 g_assert(*(v.v_pointer) == NULL);
1480 *(v.v_pointer) = name_tables;
1481 #if 0 // Use iteration over the facilities_by_name and then list all event
1482 // types of each facility
1483 nb = ltt_trace_eventtype_number(tcs->parent.t);
1484 name_tables->eventtype_names = g_new(GQuark, nb);
1485 for(i = 0 ; i < nb ; i++) {
1486 et = ltt_trace_eventtype_get(tcs->parent.t, i);
1487 e_name = ltt_eventtype_name(et);
1488 f_name = ltt_facility_name(ltt_eventtype_facility(et));
1489 g_string_printf(fe_name, "%s.%s", f_name, e_name);
1490 name_tables->eventtype_names[i] = g_quark_from_string(fe_name->str);
1491 }
1492 #endif //0
1493 if(!lttv_trace_find_hook(tcs->parent.t,
1494 LTT_FACILITY_KERNEL_ARCH, LTT_EVENT_SYSCALL_ENTRY,
1495 LTT_FIELD_SYSCALL_ID, 0, 0,
1496 NULL, NULL, &h)) {
1497
1498 thf = lttv_trace_hook_get_first(&h);
1499
1500 t = ltt_field_type(thf->f1);
1501 nb = ltt_type_element_number(t);
1502
1503 lttv_trace_hook_destroy(&h);
1504
1505 name_tables->syscall_names = g_new(GQuark, nb);
1506 name_tables->nb_syscalls = nb;
1507
1508 for(i = 0 ; i < nb ; i++) {
1509 name_tables->syscall_names[i] = ltt_enum_string_get(t, i);
1510 if(!name_tables->syscall_names[i]) {
1511 GString *string = g_string_new("");
1512 g_string_printf(string, "syscall %u", i);
1513 name_tables->syscall_names[i] = g_quark_from_string(string->str);
1514 g_string_free(string, TRUE);
1515 }
1516 }
1517
1518 //name_tables->syscall_names = g_new(GQuark, 256);
1519 //for(i = 0 ; i < 256 ; i++) {
1520 // g_string_printf(fe_name, "syscall %d", i);
1521 // name_tables->syscall_names[i] = g_quark_from_string(fe_name->str);
1522 //}
1523 } else {
1524 name_tables->syscall_names = NULL;
1525 name_tables->nb_syscalls = 0;
1526 }
1527
1528 if(!lttv_trace_find_hook(tcs->parent.t, LTT_FACILITY_KERNEL_ARCH,
1529 LTT_EVENT_TRAP_ENTRY,
1530 LTT_FIELD_TRAP_ID, 0, 0,
1531 NULL, NULL, &h)) {
1532
1533 thf = lttv_trace_hook_get_first(&h);
1534
1535 t = ltt_field_type(thf->f1);
1536 //nb = ltt_type_element_number(t);
1537
1538 lttv_trace_hook_destroy(&h);
1539
1540 /*
1541 name_tables->trap_names = g_new(GQuark, nb);
1542 for(i = 0 ; i < nb ; i++) {
1543 name_tables->trap_names[i] = g_quark_from_string(
1544 ltt_enum_string_get(t, i));
1545 }
1546 */
1547 name_tables->nb_traps = 256;
1548 name_tables->trap_names = g_new(GQuark, 256);
1549 for(i = 0 ; i < 256 ; i++) {
1550 g_string_printf(fe_name, "trap %d", i);
1551 name_tables->trap_names[i] = g_quark_from_string(fe_name->str);
1552 }
1553 } else {
1554 name_tables->trap_names = NULL;
1555 name_tables->nb_traps = 0;
1556 }
1557
1558 if(!lttv_trace_find_hook(tcs->parent.t,
1559 LTT_FACILITY_KERNEL, LTT_EVENT_IRQ_ENTRY,
1560 LTT_FIELD_IRQ_ID, 0, 0,
1561 NULL, NULL, &h)) {
1562
1563 thf = lttv_trace_hook_get_first(&h);
1564
1565 t = ltt_field_type(thf->f1);
1566 //nb = ltt_type_element_number(t);
1567
1568 lttv_trace_hook_destroy(&h);
1569
1570 /*
1571 name_tables->irq_names = g_new(GQuark, nb);
1572 for(i = 0 ; i < nb ; i++) {
1573 name_tables->irq_names[i] = g_quark_from_string(ltt_enum_string_get(t, i));
1574 }
1575 */
1576
1577 name_tables->nb_irqs = 256;
1578 name_tables->irq_names = g_new(GQuark, 256);
1579 for(i = 0 ; i < 256 ; i++) {
1580 g_string_printf(fe_name, "irq %d", i);
1581 name_tables->irq_names[i] = g_quark_from_string(fe_name->str);
1582 }
1583 } else {
1584 name_tables->nb_irqs = 0;
1585 name_tables->irq_names = NULL;
1586 }
1587 /*
1588 name_tables->soft_irq_names = g_new(GQuark, nb);
1589 for(i = 0 ; i < nb ; i++) {
1590 name_tables->soft_irq_names[i] = g_quark_from_string(ltt_enum_string_get(t, i));
1591 }
1592 */
1593
1594 name_tables->nb_softirqs = 256;
1595 name_tables->soft_irq_names = g_new(GQuark, 256);
1596 for(i = 0 ; i < 256 ; i++) {
1597 g_string_printf(fe_name, "softirq %d", i);
1598 name_tables->soft_irq_names[i] = g_quark_from_string(fe_name->str);
1599 }
1600
1601
1602 g_string_free(fe_name, TRUE);
1603 }
1604
1605
1606 static void
1607 get_name_tables(LttvTraceState *tcs)
1608 {
1609 LttvNameTables *name_tables;
1610
1611 LttvAttributeValue v;
1612
1613 lttv_attribute_find(tcs->parent.t_a, LTTV_STATE_NAME_TABLES,
1614 LTTV_POINTER, &v);
1615 g_assert(*(v.v_pointer) != NULL);
1616 name_tables = (LttvNameTables *)*(v.v_pointer);
1617 //tcs->eventtype_names = name_tables->eventtype_names;
1618 tcs->syscall_names = name_tables->syscall_names;
1619 tcs->nb_syscalls = name_tables->nb_syscalls;
1620 tcs->trap_names = name_tables->trap_names;
1621 tcs->nb_traps = name_tables->nb_traps;
1622 tcs->irq_names = name_tables->irq_names;
1623 tcs->soft_irq_names = name_tables->soft_irq_names;
1624 tcs->nb_irqs = name_tables->nb_irqs;
1625 tcs->nb_softirqs = name_tables->nb_softirqs;
1626 }
1627
1628
1629 static void
1630 free_name_tables(LttvTraceState *tcs)
1631 {
1632 LttvNameTables *name_tables;
1633
1634 LttvAttributeValue v;
1635
1636 lttv_attribute_find(tcs->parent.t_a, LTTV_STATE_NAME_TABLES,
1637 LTTV_POINTER, &v);
1638 name_tables = (LttvNameTables *)*(v.v_pointer);
1639 *(v.v_pointer) = NULL;
1640
1641 // g_free(name_tables->eventtype_names);
1642 if(name_tables->syscall_names) g_free(name_tables->syscall_names);
1643 if(name_tables->trap_names) g_free(name_tables->trap_names);
1644 if(name_tables->irq_names) g_free(name_tables->irq_names);
1645 if(name_tables->soft_irq_names) g_free(name_tables->soft_irq_names);
1646 if(name_tables) g_free(name_tables);
1647 }
1648
1649 #ifdef HASH_TABLE_DEBUG
1650
1651 static void test_process(gpointer key, gpointer value, gpointer user_data)
1652 {
1653 LttvProcessState *process = (LttvProcessState *)value;
1654
1655 /* Test for process corruption */
1656 guint stack_len = process->execution_stack->len;
1657 }
1658
1659 static void hash_table_check(GHashTable *table)
1660 {
1661 g_hash_table_foreach(table, test_process, NULL);
1662 }
1663
1664
1665 #endif
1666
1667 /* clears the stack and sets the state passed as argument */
1668 static void cpu_set_base_mode(LttvCPUState *cpust, LttvCPUMode state)
1669 {
1670 g_array_set_size(cpust->mode_stack, 1);
1671 ((GQuark *)cpust->mode_stack->data)[0] = state;
1672 }
1673
1674 static void cpu_push_mode(LttvCPUState *cpust, LttvCPUMode state)
1675 {
1676 g_array_set_size(cpust->mode_stack, cpust->mode_stack->len + 1);
1677 ((GQuark *)cpust->mode_stack->data)[cpust->mode_stack->len - 1] = state;
1678 }
1679
1680 static void cpu_pop_mode(LttvCPUState *cpust)
1681 {
1682 if(cpust->mode_stack->len == 1)
1683 cpu_set_base_mode(cpust, LTTV_CPU_UNKNOWN);
1684 else
1685 g_array_set_size(cpust->mode_stack, cpust->mode_stack->len - 1);
1686 }
1687
1688 /* clears the stack and sets the state passed as argument */
1689 static void bdev_set_base_mode(LttvBdevState *bdevst, LttvBdevMode state)
1690 {
1691 g_array_set_size(bdevst->mode_stack, 1);
1692 ((GQuark *)bdevst->mode_stack->data)[0] = state;
1693 }
1694
1695 static void bdev_push_mode(LttvBdevState *bdevst, LttvBdevMode state)
1696 {
1697 g_array_set_size(bdevst->mode_stack, bdevst->mode_stack->len + 1);
1698 ((GQuark *)bdevst->mode_stack->data)[bdevst->mode_stack->len - 1] = state;
1699 }
1700
1701 static void bdev_pop_mode(LttvBdevState *bdevst)
1702 {
1703 if(bdevst->mode_stack->len == 1)
1704 bdev_set_base_mode(bdevst, LTTV_BDEV_UNKNOWN);
1705 else
1706 g_array_set_size(bdevst->mode_stack, bdevst->mode_stack->len - 1);
1707 }
1708
1709 static void irq_set_base_mode(LttvIRQState *irqst, LttvIRQMode state)
1710 {
1711 g_array_set_size(irqst->mode_stack, 1);
1712 ((GQuark *)irqst->mode_stack->data)[0] = state;
1713 }
1714
1715 static void irq_push_mode(LttvIRQState *irqst, LttvIRQMode state)
1716 {
1717 g_array_set_size(irqst->mode_stack, irqst->mode_stack->len + 1);
1718 ((GQuark *)irqst->mode_stack->data)[irqst->mode_stack->len - 1] = state;
1719 }
1720
1721 static void irq_pop_mode(LttvIRQState *irqst)
1722 {
1723 if(irqst->mode_stack->len == 1)
1724 irq_set_base_mode(irqst, LTTV_IRQ_UNKNOWN);
1725 else
1726 g_array_set_size(irqst->mode_stack, irqst->mode_stack->len - 1);
1727 }
1728
1729 static void push_state(LttvTracefileState *tfs, LttvExecutionMode t,
1730 guint state_id)
1731 {
1732 LttvExecutionState *es;
1733
1734 LttvTraceState *ts = (LttvTraceState*)tfs->parent.t_context;
1735 guint cpu = tfs->cpu;
1736
1737 #ifdef HASH_TABLE_DEBUG
1738 hash_table_check(ts->processes);
1739 #endif
1740 LttvProcessState *process = ts->running_process[cpu];
1741
1742 guint depth = process->execution_stack->len;
1743
1744 process->execution_stack =
1745 g_array_set_size(process->execution_stack, depth + 1);
1746 /* Keep in sync */
1747 process->state =
1748 &g_array_index(process->execution_stack, LttvExecutionState, depth - 1);
1749
1750 es = &g_array_index(process->execution_stack, LttvExecutionState, depth);
1751 es->t = t;
1752 es->n = state_id;
1753 es->entry = es->change = tfs->parent.timestamp;
1754 es->cum_cpu_time = ltt_time_zero;
1755 es->s = process->state->s;
1756 process->state = es;
1757 }
1758
1759 /* pop state
1760 * return 1 when empty, else 0 */
1761 int lttv_state_pop_state_cleanup(LttvProcessState *process,
1762 LttvTracefileState *tfs)
1763 {
1764 guint cpu = tfs->cpu;
1765 LttvTraceState *ts = (LttvTraceState*)tfs->parent.t_context;
1766
1767 guint depth = process->execution_stack->len;
1768
1769 if(depth == 1){
1770 return 1;
1771 }
1772
1773 process->execution_stack =
1774 g_array_set_size(process->execution_stack, depth - 1);
1775 process->state = &g_array_index(process->execution_stack, LttvExecutionState,
1776 depth - 2);
1777 process->state->change = tfs->parent.timestamp;
1778
1779 return 0;
1780 }
1781
1782 static void pop_state(LttvTracefileState *tfs, LttvExecutionMode t)
1783 {
1784 guint cpu = tfs->cpu;
1785 LttvTraceState *ts = (LttvTraceState*)tfs->parent.t_context;
1786 LttvProcessState *process = ts->running_process[cpu];
1787
1788 guint depth = process->execution_stack->len;
1789
1790 if(process->state->t != t){
1791 g_info("Different execution mode type (%lu.%09lu): ignore it\n",
1792 tfs->parent.timestamp.tv_sec, tfs->parent.timestamp.tv_nsec);
1793 g_info("process state has %s when pop_int is %s\n",
1794 g_quark_to_string(process->state->t),
1795 g_quark_to_string(t));
1796 g_info("{ %u, %u, %s, %s, %s }\n",
1797 process->pid,
1798 process->ppid,
1799 g_quark_to_string(process->name),
1800 g_quark_to_string(process->brand),
1801 g_quark_to_string(process->state->s));
1802 return;
1803 }
1804
1805 if(depth == 1){
1806 g_info("Trying to pop last state on stack (%lu.%09lu): ignore it\n",
1807 tfs->parent.timestamp.tv_sec, tfs->parent.timestamp.tv_nsec);
1808 return;
1809 }
1810
1811 process->execution_stack =
1812 g_array_set_size(process->execution_stack, depth - 1);
1813 process->state = &g_array_index(process->execution_stack, LttvExecutionState,
1814 depth - 2);
1815 process->state->change = tfs->parent.timestamp;
1816 }
1817
1818 struct search_result {
1819 const LttTime *time; /* Requested time */
1820 LttTime *best; /* Best result */
1821 };
1822
1823 static gint search_usertrace(gconstpointer a, gconstpointer b)
1824 {
1825 const LttTime *elem_time = (const LttTime*)a;
1826 /* Explicit non const cast */
1827 struct search_result *res = (struct search_result *)b;
1828
1829 if(ltt_time_compare(*elem_time, *(res->time)) < 0) {
1830 /* The usertrace was created before the schedchange */
1831 /* Get larger keys */
1832 return 1;
1833 } else if(ltt_time_compare(*elem_time, *(res->time)) >= 0) {
1834 /* The usertrace was created after the schedchange time */
1835 /* Get smaller keys */
1836 if(res->best) {
1837 if(ltt_time_compare(*elem_time, *res->best) < 0) {
1838 res->best = elem_time;
1839 }
1840 } else {
1841 res->best = elem_time;
1842 }
1843 return -1;
1844 }
1845 return 0;
1846 }
1847
1848 static LttvTracefileState *ltt_state_usertrace_find(LttvTraceState *tcs,
1849 guint pid, const LttTime *timestamp)
1850 {
1851 LttvTracefileState *tfs = NULL;
1852 struct search_result res;
1853 /* Find the usertrace associated with a pid and time interval.
1854 * Search in the usertraces by PID (within a hash) and then, for each
1855 * corresponding element of the array, find the first one with creation
1856 * timestamp the lowest, but higher or equal to "timestamp". */
1857 res.time = timestamp;
1858 res.best = NULL;
1859 GTree *usertrace_tree = g_hash_table_lookup(tcs->usertraces, (gpointer)pid);
1860 if(usertrace_tree) {
1861 g_tree_search(usertrace_tree, search_usertrace, &res);
1862 if(res.best)
1863 tfs = g_tree_lookup(usertrace_tree, res.best);
1864 }
1865
1866 return tfs;
1867 }
1868
1869
1870 LttvProcessState *
1871 lttv_state_create_process(LttvTraceState *tcs, LttvProcessState *parent,
1872 guint cpu, guint pid, guint tgid, GQuark name, const LttTime *timestamp)
1873 {
1874 LttvProcessState *process = g_new(LttvProcessState, 1);
1875
1876 LttvExecutionState *es;
1877
1878 LttvTraceContext *tc = (LttvTraceContext*)tcs;
1879
1880 char buffer[128];
1881
1882 process->pid = pid;
1883 process->tgid = tgid;
1884 process->cpu = cpu;
1885 process->name = name;
1886 process->brand = LTTV_STATE_UNBRANDED;
1887 //process->last_cpu = tfs->cpu_name;
1888 //process->last_cpu_index = ltt_tracefile_num(((LttvTracefileContext*)tfs)->tf);
1889 process->type = LTTV_STATE_USER_THREAD;
1890 process->usertrace = ltt_state_usertrace_find(tcs, pid, timestamp);
1891 process->current_function = 0; //function 0x0 by default.
1892
1893 g_info("Process %u, core %p", process->pid, process);
1894 g_hash_table_insert(tcs->processes, process, process);
1895
1896 if(parent) {
1897 process->ppid = parent->pid;
1898 process->creation_time = *timestamp;
1899 }
1900
1901 /* No parent. This process exists but we are missing all information about
1902 its creation. The birth time is set to zero but we remember the time of
1903 insertion */
1904
1905 else {
1906 process->ppid = 0;
1907 process->creation_time = ltt_time_zero;
1908 }
1909
1910 process->insertion_time = *timestamp;
1911 sprintf(buffer,"%d-%lu.%lu",pid, process->creation_time.tv_sec,
1912 process->creation_time.tv_nsec);
1913 process->pid_time = g_quark_from_string(buffer);
1914 process->cpu = cpu;
1915 //process->last_cpu = tfs->cpu_name;
1916 //process->last_cpu_index = ltt_tracefile_num(((LttvTracefileContext*)tfs)->tf);
1917 process->execution_stack = g_array_sized_new(FALSE, FALSE,
1918 sizeof(LttvExecutionState), PREALLOCATED_EXECUTION_STACK);
1919 process->execution_stack = g_array_set_size(process->execution_stack, 2);
1920 es = process->state = &g_array_index(process->execution_stack,
1921 LttvExecutionState, 0);
1922 es->t = LTTV_STATE_USER_MODE;
1923 es->n = LTTV_STATE_SUBMODE_NONE;
1924 es->entry = *timestamp;
1925 //g_assert(timestamp->tv_sec != 0);
1926 es->change = *timestamp;
1927 es->cum_cpu_time = ltt_time_zero;
1928 es->s = LTTV_STATE_RUN;
1929
1930 es = process->state = &g_array_index(process->execution_stack,
1931 LttvExecutionState, 1);
1932 es->t = LTTV_STATE_SYSCALL;
1933 es->n = LTTV_STATE_SUBMODE_NONE;
1934 es->entry = *timestamp;
1935 //g_assert(timestamp->tv_sec != 0);
1936 es->change = *timestamp;
1937 es->cum_cpu_time = ltt_time_zero;
1938 es->s = LTTV_STATE_WAIT_FORK;
1939
1940 /* Allocate an empty function call stack. If it's empty, use 0x0. */
1941 process->user_stack = g_array_sized_new(FALSE, FALSE,
1942 sizeof(guint64), 0);
1943
1944 return process;
1945 }
1946
1947 LttvProcessState *lttv_state_find_process(LttvTraceState *ts, guint cpu,
1948 guint pid)
1949 {
1950 LttvProcessState key;
1951 LttvProcessState *process;
1952
1953 key.pid = pid;
1954 key.cpu = cpu;
1955 process = g_hash_table_lookup(ts->processes, &key);
1956 return process;
1957 }
1958
1959 LttvProcessState *
1960 lttv_state_find_process_or_create(LttvTraceState *ts, guint cpu, guint pid,
1961 const LttTime *timestamp)
1962 {
1963 LttvProcessState *process = lttv_state_find_process(ts, cpu, pid);
1964 LttvExecutionState *es;
1965
1966 /* Put ltt_time_zero creation time for unexisting processes */
1967 if(unlikely(process == NULL)) {
1968 process = lttv_state_create_process(ts,
1969 NULL, cpu, pid, 0, LTTV_STATE_UNNAMED, timestamp);
1970 /* We are not sure is it's a kernel thread or normal thread, put the
1971 * bottom stack state to unknown */
1972 process->execution_stack =
1973 g_array_set_size(process->execution_stack, 1);
1974 process->state = es =
1975 &g_array_index(process->execution_stack, LttvExecutionState, 0);
1976 es->t = LTTV_STATE_MODE_UNKNOWN;
1977 es->s = LTTV_STATE_UNNAMED;
1978 }
1979 return process;
1980 }
1981
1982 /* FIXME : this function should be called when we receive an event telling that
1983 * release_task has been called in the kernel. In happens generally when
1984 * the parent waits for its child terminaison, but may also happen in special
1985 * cases in the child's exit : when the parent ignores its children SIGCCHLD or
1986 * has the flag SA_NOCLDWAIT. It can also happen when the child is part
1987 * of a killed thread ground, but isn't the leader.
1988 */
1989 static void exit_process(LttvTracefileState *tfs, LttvProcessState *process)
1990 {
1991 LttvTraceState *ts = LTTV_TRACE_STATE(tfs->parent.t_context);
1992 LttvProcessState key;
1993
1994 key.pid = process->pid;
1995 key.cpu = process->cpu;
1996 g_hash_table_remove(ts->processes, &key);
1997 g_array_free(process->execution_stack, TRUE);
1998 g_array_free(process->user_stack, TRUE);
1999 g_free(process);
2000 }
2001
2002
2003 static void free_process_state(gpointer key, gpointer value,gpointer user_data)
2004 {
2005 g_array_free(((LttvProcessState *)value)->execution_stack, TRUE);
2006 g_array_free(((LttvProcessState *)value)->user_stack, TRUE);
2007 g_free(value);
2008 }
2009
2010
2011 static void lttv_state_free_process_table(GHashTable *processes)
2012 {
2013 g_hash_table_foreach(processes, free_process_state, NULL);
2014 g_hash_table_destroy(processes);
2015 }
2016
2017
2018 static gboolean syscall_entry(void *hook_data, void *call_data)
2019 {
2020 LttvTracefileState *s = (LttvTracefileState *)call_data;
2021 guint cpu = s->cpu;
2022 LttvTraceState *ts = (LttvTraceState*)s->parent.t_context;
2023 LttvProcessState *process = ts->running_process[cpu];
2024 LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
2025 LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
2026 LttField *f = thf->f1;
2027
2028 LttvExecutionSubmode submode;
2029
2030 guint nb_syscalls = ((LttvTraceState *)(s->parent.t_context))->nb_syscalls;
2031 guint syscall = ltt_event_get_unsigned(e, f);
2032
2033 if(syscall < nb_syscalls) {
2034 submode = ((LttvTraceState *)(s->parent.t_context))->syscall_names[
2035 syscall];
2036 } else {
2037 /* Fixup an incomplete syscall table */
2038 GString *string = g_string_new("");
2039 g_string_printf(string, "syscall %u", syscall);
2040 submode = g_quark_from_string(string->str);
2041 g_string_free(string, TRUE);
2042 }
2043 /* There can be no system call from PID 0 : unknown state */
2044 if(process->pid != 0)
2045 push_state(s, LTTV_STATE_SYSCALL, submode);
2046 return FALSE;
2047 }
2048
2049
2050 static gboolean syscall_exit(void *hook_data, void *call_data)
2051 {
2052 LttvTracefileState *s = (LttvTracefileState *)call_data;
2053 guint cpu = s->cpu;
2054 LttvTraceState *ts = (LttvTraceState*)s->parent.t_context;
2055 LttvProcessState *process = ts->running_process[cpu];
2056
2057 /* There can be no system call from PID 0 : unknown state */
2058 if(process->pid != 0)
2059 pop_state(s, LTTV_STATE_SYSCALL);
2060 return FALSE;
2061 }
2062
2063
2064 static gboolean trap_entry(void *hook_data, void *call_data)
2065 {
2066 LttvTracefileState *s = (LttvTracefileState *)call_data;
2067 LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
2068 LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
2069 LttField *f = thf->f1;
2070
2071 LttvExecutionSubmode submode;
2072
2073 guint64 nb_traps = ((LttvTraceState *)(s->parent.t_context))->nb_traps;
2074 guint64 trap = ltt_event_get_long_unsigned(e, f);
2075
2076 if(trap < nb_traps) {
2077 submode = ((LttvTraceState *)(s->parent.t_context))->trap_names[trap];
2078 } else {
2079 /* Fixup an incomplete trap table */
2080 GString *string = g_string_new("");
2081 g_string_printf(string, "trap %llu", trap);
2082 submode = g_quark_from_string(string->str);
2083 g_string_free(string, TRUE);
2084 }
2085
2086 push_state(s, LTTV_STATE_TRAP, submode);
2087
2088 /* update cpu status */
2089 cpu_push_mode(s->cpu_state, LTTV_CPU_TRAP);
2090
2091 return FALSE;
2092 }
2093
2094 static gboolean trap_exit(void *hook_data, void *call_data)
2095 {
2096 LttvTracefileState *s = (LttvTracefileState *)call_data;
2097
2098 pop_state(s, LTTV_STATE_TRAP);
2099
2100 /* update cpu status */
2101 cpu_pop_mode(s->cpu_state);
2102
2103 return FALSE;
2104 }
2105
2106 static gboolean irq_entry(void *hook_data, void *call_data)
2107 {
2108 LttvTracefileState *s = (LttvTracefileState *)call_data;
2109 LttvTraceState *ts = (LttvTraceState *)s->parent.t_context;
2110 LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
2111 guint8 fac_id = ltt_event_facility_id(e);
2112 guint8 ev_id = ltt_event_eventtype_id(e);
2113 LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
2114 // g_assert(lttv_trace_hook_get_first((LttvTraceHook *)hook_data)->f1 != NULL);
2115 g_assert(thf->f1 != NULL);
2116 // g_assert(thf == lttv_trace_hook_get_first((LttvTraceHook *)hook_data));
2117 LttField *f = thf->f1;
2118
2119 LttvExecutionSubmode submode;
2120 guint64 irq = ltt_event_get_long_unsigned(e, f);
2121 guint64 nb_irqs = ((LttvTraceState *)(s->parent.t_context))->nb_irqs;
2122 GString *string;
2123
2124 if(irq < nb_irqs) {
2125 submode = ((LttvTraceState *)(s->parent.t_context))->irq_names[irq];
2126 } else {
2127 /* Fixup an incomplete irq table */
2128 GString *string = g_string_new("");
2129 g_string_printf(string, "irq %llu", irq);
2130 submode = g_quark_from_string(string->str);
2131 g_string_free(string, TRUE);
2132 }
2133
2134 /* Do something with the info about being in user or system mode when int? */
2135 push_state(s, LTTV_STATE_IRQ, submode);
2136
2137 /* update cpu status */
2138 cpu_push_mode(s->cpu_state, LTTV_CPU_IRQ);
2139
2140 /* update irq status */
2141 s->cpu_state->last_irq = irq;
2142 irq_push_mode(&ts->irq_states[irq], LTTV_IRQ_BUSY);
2143
2144 return FALSE;
2145 }
2146
2147 static gboolean soft_irq_exit(void *hook_data, void *call_data)
2148 {
2149 LttvTracefileState *s = (LttvTracefileState *)call_data;
2150
2151 pop_state(s, LTTV_STATE_SOFT_IRQ);
2152 return FALSE;
2153 }
2154
2155
2156
2157 static gboolean irq_exit(void *hook_data, void *call_data)
2158 {
2159 LttvTracefileState *s = (LttvTracefileState *)call_data;
2160 LttvTraceState *ts = (LttvTraceState *)s->parent.t_context;
2161
2162 pop_state(s, LTTV_STATE_IRQ);
2163
2164 /* update cpu status */
2165 cpu_pop_mode(s->cpu_state);
2166
2167 /* update irq status */
2168 irq_pop_mode(&ts->irq_states[s->cpu_state->last_irq]);
2169
2170 return FALSE;
2171 }
2172
2173 static gboolean soft_irq_entry(void *hook_data, void *call_data)
2174 {
2175 LttvTracefileState *s = (LttvTracefileState *)call_data;
2176 LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
2177 guint8 fac_id = ltt_event_facility_id(e);
2178 guint8 ev_id = ltt_event_eventtype_id(e);
2179 LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
2180 // g_assert(lttv_trace_hook_get_first((LttvTraceHook *)hook_data)->f1 != NULL);
2181 g_assert(thf->f1 != NULL);
2182 // g_assert(thf == lttv_trace_hook_get_first((LttvTraceHook *)hook_data));
2183 LttField *f = thf->f1;
2184
2185 LttvExecutionSubmode submode;
2186 guint64 softirq = ltt_event_get_long_unsigned(e, f);
2187 guint64 nb_softirqs = ((LttvTraceState *)(s->parent.t_context))->nb_softirqs;
2188 GString *string;
2189
2190 if(softirq < nb_softirqs) {
2191 submode = ((LttvTraceState *)(s->parent.t_context))->soft_irq_names[softirq];
2192 } else {
2193 /* Fixup an incomplete irq table */
2194 GString *string = g_string_new("");
2195 g_string_printf(string, "softirq %llu", softirq);
2196 submode = g_quark_from_string(string->str);
2197 g_string_free(string, TRUE);
2198 }
2199
2200 /* Do something with the info about being in user or system mode when int? */
2201 push_state(s, LTTV_STATE_SOFT_IRQ, submode);
2202 return FALSE;
2203 }
2204
2205 LttvBdevState *bdev_state_get(LttvTraceState *ts, guint16 devcode)
2206 {
2207 gint devcode_gint = devcode;
2208 gpointer bdev = g_hash_table_lookup(ts->bdev_states, &devcode_gint);
2209 if(bdev == NULL) {
2210 LttvBdevState *bdevstate = g_malloc(sizeof(LttvBdevState));
2211 bdevstate->mode_stack = g_array_new(FALSE, FALSE, sizeof(GQuark));
2212
2213 gint * key = g_malloc(sizeof(gint));
2214 *key = devcode;
2215 g_hash_table_insert(ts->bdev_states, key, bdevstate);
2216 printf("adding key %u to hash table\n", *key);
2217
2218 bdev = bdevstate;
2219 }
2220
2221 return bdev;
2222 }
2223
2224 static gboolean bdev_request_issue(void *hook_data, void *call_data)
2225 {
2226 LttvTracefileState *s = (LttvTracefileState *)call_data;
2227 LttvTraceState *ts = (LttvTraceState *)s->parent.t_context;
2228 LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
2229 guint8 fac_id = ltt_event_facility_id(e);
2230 guint8 ev_id = ltt_event_eventtype_id(e);
2231 LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
2232
2233 guint major = ltt_event_get_long_unsigned(e, thf->f1);
2234 guint minor = ltt_event_get_long_unsigned(e, thf->f2);
2235 guint oper = ltt_event_get_long_unsigned(e, thf->f3);
2236 guint16 devcode = MKDEV(major,minor);
2237
2238 /* have we seen this block device before? */
2239 gpointer bdev = bdev_state_get(ts, devcode);
2240
2241 if(oper == 0)
2242 bdev_push_mode(bdev, LTTV_BDEV_BUSY_READING);
2243 else
2244 bdev_push_mode(bdev, LTTV_BDEV_BUSY_WRITING);
2245
2246 return FALSE;
2247 }
2248
2249 static gboolean bdev_request_complete(void *hook_data, void *call_data)
2250 {
2251 LttvTracefileState *s = (LttvTracefileState *)call_data;
2252 LttvTraceState *ts = (LttvTraceState *)s->parent.t_context;
2253 LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
2254 LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
2255
2256 guint major = ltt_event_get_long_unsigned(e, thf->f1);
2257 guint minor = ltt_event_get_long_unsigned(e, thf->f2);
2258 guint oper = ltt_event_get_long_unsigned(e, thf->f3);
2259 guint16 devcode = MKDEV(major,minor);
2260
2261 /* have we seen this block device before? */
2262 gpointer bdev = bdev_state_get(ts, devcode);
2263
2264 /* update block device */
2265 bdev_pop_mode(bdev);
2266
2267 return FALSE;
2268 }
2269
2270 static void push_function(LttvTracefileState *tfs, guint64 funcptr)
2271 {
2272 guint64 *new_func;
2273
2274 LttvTraceState *ts = (LttvTraceState*)tfs->parent.t_context;
2275 guint cpu = tfs->cpu;
2276 LttvProcessState *process = ts->running_process[cpu];
2277
2278 guint depth = process->user_stack->len;
2279
2280 process->user_stack =
2281 g_array_set_size(process->user_stack, depth + 1);
2282
2283 new_func = &g_array_index(process->user_stack, guint64, depth);
2284 *new_func = funcptr;
2285 process->current_function = funcptr;
2286 }
2287
2288 static void pop_function(LttvTracefileState *tfs, guint64 funcptr)
2289 {
2290 guint cpu = tfs->cpu;
2291 LttvTraceState *ts = (LttvTraceState*)tfs->parent.t_context;
2292 LttvProcessState *process = ts->running_process[cpu];
2293
2294 if(process->current_function != funcptr){
2295 g_info("Different functions (%lu.%09lu): ignore it\n",
2296 tfs->parent.timestamp.tv_sec, tfs->parent.timestamp.tv_nsec);
2297 g_info("process state has %llu when pop_function is %llu\n",
2298 process->current_function, funcptr);
2299 g_info("{ %u, %u, %s, %s, %s }\n",
2300 process->pid,
2301 process->ppid,
2302 g_quark_to_string(process->name),
2303 g_quark_to_string(process->brand),
2304 g_quark_to_string(process->state->s));
2305 return;
2306 }
2307 guint depth = process->user_stack->len;
2308
2309 if(depth == 0){
2310 g_info("Trying to pop last function on stack (%lu.%09lu): ignore it\n",
2311 tfs->parent.timestamp.tv_sec, tfs->parent.timestamp.tv_nsec);
2312 return;
2313 }
2314
2315 process->user_stack =
2316 g_array_set_size(process->user_stack, depth - 1);
2317 process->current_function =
2318 g_array_index(process->user_stack, guint64, depth - 2);
2319 }
2320
2321
2322 static gboolean function_entry(void *hook_data, void *call_data)
2323 {
2324 LttvTracefileState *s = (LttvTracefileState *)call_data;
2325 LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
2326 guint8 fac_id = ltt_event_facility_id(e);
2327 guint8 ev_id = ltt_event_eventtype_id(e);
2328 LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
2329 g_assert(thf->f1 != NULL);
2330 LttField *f = thf->f1;
2331 guint64 funcptr = ltt_event_get_long_unsigned(e, f);
2332
2333 push_function(s, funcptr);
2334 return FALSE;
2335 }
2336
2337 static gboolean function_exit(void *hook_data, void *call_data)
2338 {
2339 LttvTracefileState *s = (LttvTracefileState *)call_data;
2340 LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
2341 guint8 fac_id = ltt_event_facility_id(e);
2342 guint8 ev_id = ltt_event_eventtype_id(e);
2343 LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
2344 g_assert(thf->f1 != NULL);
2345 LttField *f = thf->f1;
2346 guint64 funcptr = ltt_event_get_long_unsigned(e, f);
2347
2348 LttvExecutionSubmode submode;
2349
2350 pop_function(s, funcptr);
2351 return FALSE;
2352 }
2353
2354 static gboolean schedchange(void *hook_data, void *call_data)
2355 {
2356 LttvTracefileState *s = (LttvTracefileState *)call_data;
2357 guint cpu = s->cpu;
2358 LttvTraceState *ts = (LttvTraceState*)s->parent.t_context;
2359 LttvProcessState *process = ts->running_process[cpu];
2360 LttvProcessState *old_process = ts->running_process[cpu];
2361
2362 LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
2363 LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
2364 guint pid_in, pid_out;
2365 gint64 state_out;
2366
2367 pid_out = ltt_event_get_unsigned(e, thf->f1);
2368 pid_in = ltt_event_get_unsigned(e, thf->f2);
2369 state_out = ltt_event_get_long_int(e, thf->f3);
2370
2371 if(likely(process != NULL)) {
2372
2373 /* We could not know but it was not the idle process executing.
2374 This should only happen at the beginning, before the first schedule
2375 event, and when the initial information (current process for each CPU)
2376 is missing. It is not obvious how we could, after the fact, compensate
2377 the wrongly attributed statistics. */
2378
2379 //This test only makes sense once the state is known and if there is no
2380 //missing events. We need to silently ignore schedchange coming after a
2381 //process_free, or it causes glitches. (FIXME)
2382 //if(unlikely(process->pid != pid_out)) {
2383 // g_assert(process->pid == 0);
2384 //}
2385 if(process->pid == 0
2386 && process->state->t == LTTV_STATE_MODE_UNKNOWN) {
2387 if(pid_out == 0) {
2388 /* Scheduling out of pid 0 at beginning of the trace :
2389 * we know for sure it is in syscall mode at this point. */
2390 g_assert(process->execution_stack->len == 1);
2391 process->state->t = LTTV_STATE_SYSCALL;
2392 process->state->s = LTTV_STATE_WAIT;
2393 process->state->change = s->parent.timestamp;
2394 process->state->entry = s->parent.timestamp;
2395 }
2396 } else {
2397 if(unlikely(process->state->s == LTTV_STATE_EXIT)) {
2398 process->state->s = LTTV_STATE_ZOMBIE;
2399 process->state->change = s->parent.timestamp;
2400 } else {
2401 if(unlikely(state_out == 0)) process->state->s = LTTV_STATE_WAIT_CPU;
2402 else process->state->s = LTTV_STATE_WAIT;
2403 process->state->change = s->parent.timestamp;
2404 }
2405
2406 if(state_out == 32 || state_out == 128)
2407 exit_process(s, process); /* EXIT_DEAD || TASK_DEAD */
2408 /* see sched.h for states */
2409 }
2410 }
2411 process = ts->running_process[cpu] =
2412 lttv_state_find_process_or_create(
2413 (LttvTraceState*)s->parent.t_context,
2414 cpu, pid_in,
2415 &s->parent.timestamp);
2416 process->state->s = LTTV_STATE_RUN;
2417 process->cpu = cpu;
2418 if(process->usertrace)
2419 process->usertrace->cpu = cpu;
2420 // process->last_cpu_index = ltt_tracefile_num(((LttvTracefileContext*)s)->tf);
2421 process->state->change = s->parent.timestamp;
2422
2423 /* update cpu status */
2424 if(pid_in == 0)
2425 cpu_set_base_mode(s->cpu_state, LTTV_CPU_IDLE);
2426 else
2427 cpu_set_base_mode(s->cpu_state, LTTV_CPU_BUSY);
2428
2429 return FALSE;
2430 }
2431
2432 static gboolean process_fork(void *hook_data, void *call_data)
2433 {
2434 LttvTracefileState *s = (LttvTracefileState *)call_data;
2435 LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
2436 LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
2437 guint parent_pid;
2438 guint child_pid; /* In the Linux Kernel, there is one PID per thread. */
2439 guint child_tgid; /* tgid in the Linux kernel is the "real" POSIX PID. */
2440 LttvProcessState *zombie_process;
2441 guint cpu = s->cpu;
2442 LttvTraceState *ts = (LttvTraceState*)s->parent.t_context;
2443 LttvProcessState *process = ts->running_process[cpu];
2444 LttvProcessState *child_process;
2445
2446 /* Parent PID */
2447 parent_pid = ltt_event_get_unsigned(e, thf->f1);
2448
2449 /* Child PID */
2450 child_pid = ltt_event_get_unsigned(e, thf->f2);
2451 s->parent.target_pid = child_pid;
2452
2453 /* Child TGID */
2454 if(thf->f3) child_tgid = ltt_event_get_unsigned(e, thf->f3);
2455 else child_tgid = 0;
2456
2457 /* Mathieu : it seems like the process might have been scheduled in before the
2458 * fork, and, in a rare case, might be the current process. This might happen
2459 * in a SMP case where we don't have enough precision on the clocks.
2460 *
2461 * Test reenabled after precision fixes on time. (Mathieu) */
2462 #if 0
2463 zombie_process = lttv_state_find_process(ts, ANY_CPU, child_pid);
2464
2465 if(unlikely(zombie_process != NULL)) {
2466 /* Reutilisation of PID. Only now we are sure that the old PID
2467 * has been released. FIXME : should know when release_task happens instead.
2468 */
2469 guint num_cpus = ltt_trace_get_num_cpu(ts->parent.t);
2470 guint i;
2471 for(i=0; i< num_cpus; i++) {
2472 g_assert(zombie_process != ts->running_process[i]);
2473 }
2474
2475 exit_process(s, zombie_process);
2476 }
2477 #endif //0
2478 g_assert(process->pid != child_pid);
2479 // FIXME : Add this test in the "known state" section
2480 // g_assert(process->pid == parent_pid);
2481 child_process = lttv_state_find_process(ts, ANY_CPU, child_pid);
2482 if(child_process == NULL) {
2483 child_process = lttv_state_create_process(ts, process, cpu,
2484 child_pid, child_tgid,
2485 LTTV_STATE_UNNAMED, &s->parent.timestamp);
2486 } else {
2487 /* The process has already been created : due to time imprecision between
2488 * multiple CPUs : it has been scheduled in before creation. Note that we
2489 * shouldn't have this kind of imprecision.
2490 *
2491 * Simply put a correct parent.
2492 */
2493 g_assert(0); /* This is a problematic case : the process has been created
2494 before the fork event */
2495 child_process->ppid = process->pid;
2496 child_process->tgid = child_tgid;
2497 }
2498 g_assert(child_process->name == LTTV_STATE_UNNAMED);
2499 child_process->name = process->name;
2500 child_process->brand = process->brand;
2501
2502 return FALSE;
2503 }
2504
2505 /* We stamp a newly created process as kernel_thread.
2506 * The thread should not be running yet. */
2507 static gboolean process_kernel_thread(void *hook_data, void *call_data)
2508 {
2509 LttvTracefileState *s = (LttvTracefileState *)call_data;
2510 LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
2511 LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
2512 guint pid;
2513 guint cpu = s->cpu;
2514 LttvTraceState *ts = (LttvTraceState*)s->parent.t_context;
2515 LttvProcessState *process;
2516 LttvExecutionState *es;
2517
2518 /* PID */
2519 pid = (guint)ltt_event_get_long_unsigned(e, thf->f1);
2520 s->parent.target_pid = pid;
2521
2522 process = lttv_state_find_process_or_create(ts, ANY_CPU, pid,
2523 &ltt_time_zero);
2524 process->execution_stack =
2525 g_array_set_size(process->execution_stack, 1);
2526 es = process->state =
2527 &g_array_index(process->execution_stack, LttvExecutionState, 0);
2528 es->t = LTTV_STATE_SYSCALL;
2529 process->type = LTTV_STATE_KERNEL_THREAD;
2530
2531 return FALSE;
2532 }
2533
2534 static gboolean process_exit(void *hook_data, void *call_data)
2535 {
2536 LttvTracefileState *s = (LttvTracefileState *)call_data;
2537 LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
2538 LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
2539 LttField *f;
2540 guint pid;
2541 guint cpu = s->cpu;
2542 LttvTraceState *ts = (LttvTraceState*)s->parent.t_context;
2543 LttvProcessState *process; // = ts->running_process[cpu];
2544
2545 pid = ltt_event_get_unsigned(e, thf->f1);
2546 s->parent.target_pid = pid;
2547
2548 // FIXME : Add this test in the "known state" section
2549 // g_assert(process->pid == pid);
2550
2551 process = lttv_state_find_process(ts, ANY_CPU, pid);
2552 if(likely(process != NULL)) {
2553 process->state->s = LTTV_STATE_EXIT;
2554 }
2555 return FALSE;
2556 }
2557
2558 static gboolean process_free(void *hook_data, void *call_data)
2559 {
2560 LttvTracefileState *s = (LttvTracefileState *)call_data;
2561 LttvTraceState *ts = (LttvTraceState*)s->parent.t_context;
2562 LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
2563 LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
2564 guint release_pid;
2565 LttvProcessState *process;
2566
2567 /* PID of the process to release */
2568 release_pid = ltt_event_get_unsigned(e, thf->f1);
2569 s->parent.target_pid = release_pid;
2570
2571 g_assert(release_pid != 0);
2572
2573 process = lttv_state_find_process(ts, ANY_CPU, release_pid);
2574
2575 if(likely(process != NULL)) {
2576 /* release_task is happening at kernel level : we can now safely release
2577 * the data structure of the process */
2578 //This test is fun, though, as it may happen that
2579 //at time t : CPU 0 : process_free
2580 //at time t+150ns : CPU 1 : schedule out
2581 //Clearly due to time imprecision, we disable it. (Mathieu)
2582 //If this weird case happen, we have no choice but to put the
2583 //Currently running process on the cpu to 0.
2584 //I re-enable it following time precision fixes. (Mathieu)
2585 //Well, in the case where an process is freed by a process on another CPU
2586 //and still scheduled, it happens that this is the schedchange that will
2587 //drop the last reference count. Do not free it here!
2588 guint num_cpus = ltt_trace_get_num_cpu(ts->parent.t);
2589 guint i;
2590 for(i=0; i< num_cpus; i++) {
2591 //g_assert(process != ts->running_process[i]);
2592 if(process == ts->running_process[i]) {
2593 //ts->running_process[i] = lttv_state_find_process(ts, i, 0);
2594 break;
2595 }
2596 }
2597 if(i == num_cpus) /* process is not scheduled */
2598 exit_process(s, process);
2599 }
2600
2601 return FALSE;
2602 }
2603
2604
2605 static gboolean process_exec(void *hook_data, void *call_data)
2606 {
2607 LttvTracefileState *s = (LttvTracefileState *)call_data;
2608 LttvTraceState *ts = (LttvTraceState*)s->parent.t_context;
2609 LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
2610 LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
2611 //gchar *name;
2612 guint cpu = s->cpu;
2613 LttvProcessState *process = ts->running_process[cpu];
2614
2615 #if 0//how to use a sequence that must be transformed in a string
2616 /* PID of the process to release */
2617 guint64 name_len = ltt_event_field_element_number(e, thf->f1);
2618 //name = ltt_event_get_string(e, thf->f1);
2619 LttField *child = ltt_event_field_element_select(e, thf->f1, 0);
2620 gchar *name_begin =
2621 (gchar*)(ltt_event_data(e)+ltt_event_field_offset(e, child));
2622 gchar *null_term_name = g_new(gchar, name_len+1);
2623 memcpy(null_term_name, name_begin, name_len);
2624 null_term_name[name_len] = '\0';
2625 process->name = g_quark_from_string(null_term_name);
2626 #endif //0
2627
2628 process->name = g_quark_from_string(ltt_event_get_string(e, thf->f1));
2629 process->brand = LTTV_STATE_UNBRANDED;
2630 //g_free(null_term_name);
2631 return FALSE;
2632 }
2633
2634 static gboolean thread_brand(void *hook_data, void *call_data)
2635 {
2636 LttvTracefileState *s = (LttvTracefileState *)call_data;
2637 LttvTraceState *ts = (LttvTraceState*)s->parent.t_context;
2638 LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
2639 LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
2640 gchar *name;
2641 guint cpu = s->cpu;
2642 LttvProcessState *process = ts->running_process[cpu];
2643
2644 name = ltt_event_get_string(e, thf->f1);
2645 process->brand = g_quark_from_string(name);
2646
2647 return FALSE;
2648 }
2649
2650 static void fix_process(gpointer key, gpointer value,
2651 gpointer user_data)
2652 {
2653 LttvProcessState *process;
2654 LttvExecutionState *es;
2655 process = (LttvProcessState *)value;
2656 LttvTracefileContext *tfc = (LttvTracefileContext *)user_data;
2657 LttTime *timestamp = (LttTime*)user_data;
2658
2659 if(process->type == LTTV_STATE_KERNEL_THREAD) {
2660 es = &g_array_index(process->execution_stack, LttvExecutionState, 0);
2661 if(es->t == LTTV_STATE_MODE_UNKNOWN) {
2662 es->t = LTTV_STATE_SYSCALL;
2663 es->n = LTTV_STATE_SUBMODE_NONE;
2664 es->entry = *timestamp;
2665 es->change = *timestamp;
2666 es->cum_cpu_time = ltt_time_zero;
2667 if(es->s == LTTV_STATE_UNNAMED)
2668 es->s = LTTV_STATE_WAIT;
2669 }
2670 } else {
2671 es = &g_array_index(process->execution_stack, LttvExecutionState, 0);
2672 if(es->t == LTTV_STATE_MODE_UNKNOWN) {
2673 es->t = LTTV_STATE_USER_MODE;
2674 es->n = LTTV_STATE_SUBMODE_NONE;
2675 es->entry = *timestamp;
2676 //g_assert(timestamp->tv_sec != 0);
2677 es->change = *timestamp;
2678 es->cum_cpu_time = ltt_time_zero;
2679 if(es->s == LTTV_STATE_UNNAMED)
2680 es->s = LTTV_STATE_RUN;
2681
2682 if(process->execution_stack->len == 1) {
2683 /* Still in bottom unknown mode, means never did a system call
2684 * May be either in user mode, syscall mode, running or waiting.*/
2685 /* FIXME : we may be tagging syscall mode when being user mode */
2686 process->execution_stack =
2687 g_array_set_size(process->execution_stack, 2);
2688 es = process->state = &g_array_index(process->execution_stack,
2689 LttvExecutionState, 1);
2690 es->t = LTTV_STATE_SYSCALL;
2691 es->n = LTTV_STATE_SUBMODE_NONE;
2692 es->entry = *timestamp;
2693 //g_assert(timestamp->tv_sec != 0);
2694 es->change = *timestamp;
2695 es->cum_cpu_time = ltt_time_zero;
2696 if(es->s == LTTV_STATE_WAIT_FORK)
2697 es->s = LTTV_STATE_WAIT;
2698 }
2699 }
2700 }
2701 }
2702
2703 static gboolean statedump_end(void *hook_data, void *call_data)
2704 {
2705 LttvTracefileState *s = (LttvTracefileState *)call_data;
2706 LttvTraceState *ts = (LttvTraceState*)s->parent.t_context;
2707 LttvTracefileContext *tfc = (LttvTracefileContext *)call_data;
2708 LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
2709 LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
2710
2711 /* For all processes */
2712 /* if kernel thread, if stack[0] is unknown, set to syscall mode, wait */
2713 /* else, if stack[0] is unknown, set to user mode, running */
2714
2715 g_hash_table_foreach(ts->processes, fix_process, &tfc->timestamp);
2716 }
2717
2718 static gboolean enum_process_state(void *hook_data, void *call_data)
2719 {
2720 LttvTracefileState *s = (LttvTracefileState *)call_data;
2721 LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
2722 //It's slow : optimise later by doing this before reading trace.
2723 LttEventType *et = ltt_event_eventtype(e);
2724 //
2725 LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
2726 guint parent_pid;
2727 guint pid;
2728 guint tgid;
2729 gchar * command;
2730 guint cpu = s->cpu;
2731 LttvTraceState *ts = (LttvTraceState*)s->parent.t_context;
2732 LttvProcessState *process = ts->running_process[cpu];
2733 LttvProcessState *parent_process;
2734 LttField *f4, *f5, *f6, *f7, *f8;
2735 GQuark type, mode, submode, status;
2736 LttvExecutionState *es;
2737 guint i, nb_cpus;
2738
2739 /* PID */
2740 pid = ltt_event_get_unsigned(e, thf->f1);
2741 s->parent.target_pid = pid;
2742
2743 /* Parent PID */
2744 parent_pid = ltt_event_get_unsigned(e, thf->f2);
2745
2746 /* Command name */
2747 command = ltt_event_get_string(e, thf->f3);
2748
2749 /* type */
2750 f4 = ltt_eventtype_field_by_name(et, LTT_FIELD_TYPE);
2751 type = ltt_enum_string_get(ltt_field_type(f4),
2752 ltt_event_get_unsigned(e, f4));
2753
2754 /* mode */
2755 f5 = ltt_eventtype_field_by_name(et, LTT_FIELD_MODE);
2756 mode = ltt_enum_string_get(ltt_field_type(f5),
2757 ltt_event_get_unsigned(e, f5));
2758
2759 /* submode */
2760 f6 = ltt_eventtype_field_by_name(et, LTT_FIELD_SUBMODE);
2761 submode = ltt_enum_string_get(ltt_field_type(f6),
2762 ltt_event_get_unsigned(e, f6));
2763
2764 /* status */
2765 f7 = ltt_eventtype_field_by_name(et, LTT_FIELD_STATUS);
2766 status = ltt_enum_string_get(ltt_field_type(f7),
2767 ltt_event_get_unsigned(e, f7));
2768
2769 /* TGID */
2770 f8 = ltt_eventtype_field_by_name(et, LTT_FIELD_TGID);
2771 if(f8) tgid = ltt_event_get_unsigned(e, f8);
2772 else tgid = 0;
2773
2774
2775 if(pid == 0) {
2776 nb_cpus = ltt_trace_get_num_cpu(ts->parent.t);
2777 for(i=0; i<nb_cpus; i++) {
2778 process = lttv_state_find_process(ts, i, pid);
2779 g_assert(process != NULL);
2780
2781 process->ppid = parent_pid;
2782 process->tgid = tgid;
2783 process->name = g_quark_from_string(command);
2784 es =
2785 &g_array_index(process->execution_stack, LttvExecutionState, 0);
2786 process->type = LTTV_STATE_KERNEL_THREAD;
2787 }
2788
2789 } else {
2790 /* The process might exist if a process was forked while performing the
2791 * state dump. */
2792 process = lttv_state_find_process(ts, ANY_CPU, pid);
2793 if(process == NULL) {
2794 parent_process = lttv_state_find_process(ts, ANY_CPU, parent_pid);
2795 process = lttv_state_create_process(ts, parent_process, cpu,
2796 pid, tgid, g_quark_from_string(command),
2797 &s->parent.timestamp);
2798
2799 /* Keep the stack bottom : a running user mode */
2800 /* Disabled because of inconsistencies in the current statedump states. */
2801 if(type == LTTV_STATE_KERNEL_THREAD) {
2802 /* Only keep the bottom
2803 * FIXME Kernel thread : can be in syscall or interrupt or trap. */
2804 /* Will cause expected trap when in fact being syscall (even after end of
2805 * statedump event)
2806 * Will cause expected interrupt when being syscall. (only before end of
2807 * statedump event) */
2808 // This will cause a "popping last state on stack, ignoring it."
2809 process->execution_stack = g_array_set_size(process->execution_stack, 1);
2810 es = process->state = &g_array_index(process->execution_stack,
2811 LttvExecutionState, 0);
2812 process->type = LTTV_STATE_KERNEL_THREAD;
2813 es->t = LTTV_STATE_MODE_UNKNOWN;
2814 es->s = LTTV_STATE_UNNAMED;
2815 es->n = LTTV_STATE_SUBMODE_UNKNOWN;
2816 #if 0
2817 es->t = LTTV_STATE_SYSCALL;
2818 es->s = status;
2819 es->n = submode;
2820 #endif //0
2821 } else {
2822 /* User space process :
2823 * bottom : user mode
2824 * either currently running or scheduled out.
2825 * can be scheduled out because interrupted in (user mode or in syscall)
2826 * or because of an explicit call to the scheduler in syscall. Note that
2827 * the scheduler call comes after the irq_exit, so never in interrupt
2828 * context. */
2829 // temp workaround : set size to 1 : only have user mode bottom of stack.
2830 // will cause g_info message of expected syscall mode when in fact being
2831 // in user mode. Can also cause expected trap when in fact being user
2832 // mode in the event of a page fault reenabling interrupts in the handler.
2833 // Expected syscall and trap can also happen after the end of statedump
2834 // This will cause a "popping last state on stack, ignoring it."
2835 process->execution_stack = g_array_set_size(process->execution_stack, 1);
2836 es = process->state = &g_array_index(process->execution_stack,
2837 LttvExecutionState, 0);
2838 es->t = LTTV_STATE_MODE_UNKNOWN;
2839 es->s = LTTV_STATE_UNNAMED;
2840 es->n = LTTV_STATE_SUBMODE_UNKNOWN;
2841 #if 0
2842 es->t = LTTV_STATE_USER_MODE;
2843 es->s = status;
2844 es->n = submode;
2845 #endif //0
2846 }
2847 #if 0
2848 /* UNKNOWN STATE */
2849 {
2850 es = process->state = &g_array_index(process->execution_stack,
2851 LttvExecutionState, 1);
2852 es->t = LTTV_STATE_MODE_UNKNOWN;
2853 es->s = LTTV_STATE_UNNAMED;
2854 es->n = LTTV_STATE_SUBMODE_UNKNOWN;
2855 }
2856 #endif //0
2857 } else {
2858 /* The process has already been created :
2859 * Probably was forked while dumping the process state or
2860 * was simply scheduled in prior to get the state dump event.
2861 */
2862 process->ppid = parent_pid;
2863 process->tgid = tgid;
2864 process->name = g_quark_from_string(command);
2865 process->type = type;
2866 es =
2867 &g_array_index(process->execution_stack, LttvExecutionState, 0);
2868 #if 0
2869 if(es->t == LTTV_STATE_MODE_UNKNOWN) {
2870 if(type == LTTV_STATE_KERNEL_THREAD)
2871 es->t = LTTV_STATE_SYSCALL;
2872 else
2873 es->t = LTTV_STATE_USER_MODE;
2874 }
2875 #endif //0
2876 /* Don't mess around with the stack, it will eventually become
2877 * ok after the end of state dump. */
2878 }
2879 }
2880
2881 return FALSE;
2882 }
2883
2884 gint lttv_state_hook_add_event_hooks(void *hook_data, void *call_data)
2885 {
2886 LttvTracesetState *tss = (LttvTracesetState*)(call_data);
2887
2888 lttv_state_add_event_hooks(tss);
2889
2890 return 0;
2891 }
2892
2893 void lttv_state_add_event_hooks(LttvTracesetState *self)
2894 {
2895 LttvTraceset *traceset = self->parent.ts;
2896
2897 guint i, j, k, l, nb_trace, nb_tracefile;
2898
2899 LttvTraceState *ts;
2900
2901 LttvTracefileState *tfs;
2902
2903 GArray *hooks;
2904
2905 LttvTraceHookByFacility *thf;
2906
2907 LttvTraceHook *hook;
2908
2909 LttvAttributeValue val;
2910
2911 gint ret;
2912 gint hn;
2913
2914 nb_trace = lttv_traceset_number(traceset);
2915 for(i = 0 ; i < nb_trace ; i++) {
2916 ts = (LttvTraceState *)self->parent.traces[i];
2917
2918 /* Find the eventtype id for the following events and register the
2919 associated by id hooks. */
2920
2921 hooks = g_array_sized_new(FALSE, FALSE, sizeof(LttvTraceHook), 19);
2922 hooks = g_array_set_size(hooks, 19); // Max possible number of hooks.
2923 hn = 0;
2924
2925 ret = lttv_trace_find_hook(ts->parent.t,
2926 LTT_FACILITY_KERNEL_ARCH, LTT_EVENT_SYSCALL_ENTRY,
2927 LTT_FIELD_SYSCALL_ID, 0, 0,
2928 syscall_entry, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
2929 if(ret) hn--;
2930
2931 ret = lttv_trace_find_hook(ts->parent.t,
2932 LTT_FACILITY_KERNEL_ARCH, LTT_EVENT_SYSCALL_EXIT,
2933 0, 0, 0,
2934 syscall_exit, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
2935 if(ret) hn--;
2936
2937 ret = lttv_trace_find_hook(ts->parent.t,
2938 LTT_FACILITY_KERNEL_ARCH, LTT_EVENT_TRAP_ENTRY,
2939 LTT_FIELD_TRAP_ID, 0, 0,
2940 trap_entry, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
2941 if(ret) hn--;
2942
2943 ret = lttv_trace_find_hook(ts->parent.t,
2944 LTT_FACILITY_KERNEL_ARCH, LTT_EVENT_TRAP_EXIT,
2945 0, 0, 0,
2946 trap_exit, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
2947 if(ret) hn--;
2948
2949 ret = lttv_trace_find_hook(ts->parent.t,
2950 LTT_FACILITY_KERNEL, LTT_EVENT_IRQ_ENTRY,
2951 LTT_FIELD_IRQ_ID, 0, 0,
2952 irq_entry, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
2953 if(ret) hn--;
2954
2955 ret = lttv_trace_find_hook(ts->parent.t,
2956 LTT_FACILITY_KERNEL, LTT_EVENT_IRQ_EXIT,
2957 0, 0, 0,
2958 irq_exit, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
2959 if(ret) hn--;
2960
2961 ret = lttv_trace_find_hook(ts->parent.t,
2962 LTT_FACILITY_KERNEL, LTT_EVENT_SOFT_IRQ_ENTRY,
2963 LTT_FIELD_SOFT_IRQ_ID, 0, 0,
2964 soft_irq_entry, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
2965 if(ret) hn--;
2966
2967 ret = lttv_trace_find_hook(ts->parent.t,
2968 LTT_FACILITY_KERNEL, LTT_EVENT_SOFT_IRQ_EXIT,
2969 0, 0, 0,
2970 soft_irq_exit, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
2971 if(ret) hn--;
2972
2973 ret = lttv_trace_find_hook(ts->parent.t,
2974 LTT_FACILITY_KERNEL, LTT_EVENT_SCHED_SCHEDULE,
2975 LTT_FIELD_PREV_PID, LTT_FIELD_NEXT_PID, LTT_FIELD_PREV_STATE,
2976 schedchange, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
2977 if(ret) hn--;
2978
2979 ret = lttv_trace_find_hook(ts->parent.t,
2980 LTT_FACILITY_KERNEL, LTT_EVENT_PROCESS_FORK,
2981 LTT_FIELD_PARENT_PID, LTT_FIELD_CHILD_PID, LTT_FIELD_CHILD_TGID,
2982 process_fork, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
2983 if(ret) hn--;
2984
2985 ret = lttv_trace_find_hook(ts->parent.t,
2986 LTT_FACILITY_KERNEL_ARCH, LTT_EVENT_KTHREAD_CREATE,
2987 LTT_FIELD_PID, 0, 0,
2988 process_kernel_thread, NULL, &g_array_index(hooks, LttvTraceHook,
2989 hn++));
2990 if(ret) hn--;
2991
2992 ret = lttv_trace_find_hook(ts->parent.t,
2993 LTT_FACILITY_KERNEL, LTT_EVENT_PROCESS_EXIT,
2994 LTT_FIELD_PID, 0, 0,
2995 process_exit, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
2996 if(ret) hn--;
2997
2998 ret = lttv_trace_find_hook(ts->parent.t,
2999 LTT_FACILITY_KERNEL, LTT_EVENT_PROCESS_FREE,
3000 LTT_FIELD_PID, 0, 0,
3001 process_free, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
3002 if(ret) hn--;
3003
3004 ret = lttv_trace_find_hook(ts->parent.t,
3005 LTT_FACILITY_FS, LTT_EVENT_EXEC,
3006 LTT_FIELD_FILENAME, 0, 0,
3007 process_exec, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
3008 if(ret) hn--;
3009
3010 ret = lttv_trace_find_hook(ts->parent.t,
3011 LTT_FACILITY_USER_GENERIC, LTT_EVENT_THREAD_BRAND,
3012 LTT_FIELD_NAME, 0, 0,
3013 thread_brand, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
3014 if(ret) hn--;
3015
3016 /* statedump-related hooks */
3017 ret = lttv_trace_find_hook(ts->parent.t,
3018 LTT_FACILITY_LIST, LTT_EVENT_PROCESS_STATE,
3019 LTT_FIELD_PID, LTT_FIELD_PARENT_PID, LTT_FIELD_NAME,
3020 enum_process_state, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
3021 if(ret) hn--;
3022
3023 ret = lttv_trace_find_hook(ts->parent.t,
3024 LTT_FACILITY_LIST, LTT_EVENT_STATEDUMP_END,
3025 0, 0, 0,
3026 statedump_end, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
3027 if(ret) hn--;
3028
3029 ret = lttv_trace_find_hook(ts->parent.t,
3030 LTT_FACILITY_BLOCK, LTT_EVENT_REQUEST_ISSUE,
3031 LTT_FIELD_MAJOR, LTT_FIELD_MINOR, LTT_FIELD_OPERATION,
3032 bdev_request_issue, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
3033 if(ret) hn--;
3034
3035 ret = lttv_trace_find_hook(ts->parent.t,
3036 LTT_FACILITY_BLOCK, LTT_EVENT_REQUEST_COMPLETE,
3037 LTT_FIELD_MAJOR, LTT_FIELD_MINOR, LTT_FIELD_OPERATION,
3038 bdev_request_complete, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
3039 if(ret) hn--;
3040
3041 ret = lttv_trace_find_hook(ts->parent.t,
3042 LTT_FACILITY_USER_GENERIC, LTT_EVENT_FUNCTION_ENTRY,
3043 LTT_FIELD_THIS_FN, LTT_FIELD_CALL_SITE, 0,
3044 function_entry, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
3045 if(ret) hn--;
3046
3047 ret = lttv_trace_find_hook(ts->parent.t,
3048 LTT_FACILITY_USER_GENERIC, LTT_EVENT_FUNCTION_EXIT,
3049 LTT_FIELD_THIS_FN, LTT_FIELD_CALL_SITE, 0,
3050 function_exit, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
3051 if(ret) hn--;
3052
3053 hooks = g_array_set_size(hooks, hn);
3054
3055 /* Add these hooks to each event_by_id hooks list */
3056
3057 nb_tracefile = ts->parent.tracefiles->len;
3058
3059 for(j = 0 ; j < nb_tracefile ; j++) {
3060 tfs =
3061 LTTV_TRACEFILE_STATE(g_array_index(ts->parent.tracefiles,
3062 LttvTracefileContext*, j));
3063
3064 for(k = 0 ; k < hooks->len ; k++) {
3065 hook = &g_array_index(hooks, LttvTraceHook, k);
3066 for(l=0;l<hook->fac_list->len;l++) {
3067 thf = g_array_index(hook->fac_list, LttvTraceHookByFacility*, l);
3068 lttv_hooks_add(
3069 lttv_hooks_by_id_find(tfs->parent.event_by_id, thf->id),
3070 thf->h,
3071 thf,
3072 LTTV_PRIO_STATE);
3073 }
3074 }
3075 }
3076 lttv_attribute_find(ts->parent.a, LTTV_STATE_HOOKS, LTTV_POINTER, &val);
3077 *(val.v_pointer) = hooks;
3078 }
3079 }
3080
3081 gint lttv_state_hook_remove_event_hooks(void *hook_data, void *call_data)
3082 {
3083 LttvTracesetState *tss = (LttvTracesetState*)(call_data);
3084
3085 lttv_state_remove_event_hooks(tss);
3086
3087 return 0;
3088 }
3089
3090 void lttv_state_remove_event_hooks(LttvTracesetState *self)
3091 {
3092 LttvTraceset *traceset = self->parent.ts;
3093
3094 guint i, j, k, l, nb_trace, nb_tracefile;
3095
3096 LttvTraceState *ts;
3097
3098 LttvTracefileState *tfs;
3099
3100 GArray *hooks;
3101
3102 LttvTraceHook *hook;
3103
3104 LttvTraceHookByFacility *thf;
3105
3106 LttvAttributeValue val;
3107
3108 nb_trace = lttv_traceset_number(traceset);
3109 for(i = 0 ; i < nb_trace ; i++) {
3110 ts = LTTV_TRACE_STATE(self->parent.traces[i]);
3111
3112 lttv_attribute_find(ts->parent.a, LTTV_STATE_HOOKS, LTTV_POINTER, &val);
3113 hooks = *(val.v_pointer);
3114
3115 /* Remove these hooks from each event_by_id hooks list */
3116
3117 nb_tracefile = ts->parent.tracefiles->len;
3118
3119 for(j = 0 ; j < nb_tracefile ; j++) {
3120 tfs =
3121 LTTV_TRACEFILE_STATE(g_array_index(ts->parent.tracefiles,
3122 LttvTracefileContext*, j));
3123
3124 for(k = 0 ; k < hooks->len ; k++) {
3125 hook = &g_array_index(hooks, LttvTraceHook, k);
3126 for(l=0;l<hook->fac_list->len;l++) {
3127 thf = g_array_index(hook->fac_list, LttvTraceHookByFacility*, l);
3128
3129 lttv_hooks_remove_data(
3130 lttv_hooks_by_id_find(tfs->parent.event_by_id, thf->id),
3131 thf->h,
3132 thf);
3133 }
3134 }
3135 }
3136 for(k = 0 ; k < hooks->len ; k++)
3137 lttv_trace_hook_destroy(&g_array_index(hooks, LttvTraceHook, k));
3138 g_array_free(hooks, TRUE);
3139 }
3140 }
3141
3142 static gboolean state_save_event_hook(void *hook_data, void *call_data)
3143 {
3144 guint *event_count = (guint*)hook_data;
3145
3146 /* Only save at LTTV_STATE_SAVE_INTERVAL */
3147 if(likely((*event_count)++ < LTTV_STATE_SAVE_INTERVAL))
3148 return FALSE;
3149 else
3150 *event_count = 0;
3151
3152 LttvTracefileState *self = (LttvTracefileState *)call_data;
3153
3154 LttvTracefileState *tfcs;
3155
3156 LttvTraceState *tcs = (LttvTraceState *)(self->parent.t_context);
3157
3158 LttEventPosition *ep;
3159
3160 guint i;
3161
3162 LttTracefile *tf;
3163
3164 LttvAttribute *saved_states_tree, *saved_state_tree;
3165
3166 LttvAttributeValue value;
3167
3168 saved_states_tree = lttv_attribute_find_subdir(tcs->parent.t_a,
3169 LTTV_STATE_SAVED_STATES);
3170 saved_state_tree = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
3171 value = lttv_attribute_add(saved_states_tree,
3172 lttv_attribute_get_number(saved_states_tree), LTTV_GOBJECT);
3173 *(value.v_gobject) = (GObject *)saved_state_tree;
3174 value = lttv_attribute_add(saved_state_tree, LTTV_STATE_TIME, LTTV_TIME);
3175 *(value.v_time) = self->parent.timestamp;
3176 lttv_state_save(tcs, saved_state_tree);
3177 g_debug("Saving state at time %lu.%lu", self->parent.timestamp.tv_sec,
3178 self->parent.timestamp.tv_nsec);
3179
3180 *(tcs->max_time_state_recomputed_in_seek) = self->parent.timestamp;
3181
3182 return FALSE;
3183 }
3184
3185 static gboolean state_save_after_trace_hook(void *hook_data, void *call_data)
3186 {
3187 LttvTraceState *tcs = (LttvTraceState *)(call_data);
3188
3189 *(tcs->max_time_state_recomputed_in_seek) = tcs->parent.time_span.end_time;
3190
3191 return FALSE;
3192 }
3193
3194 guint lttv_state_current_cpu(LttvTracefileState *tfs)
3195 {
3196 return tfs->cpu;
3197 }
3198
3199
3200
3201 #if 0
3202 static gboolean block_start(void *hook_data, void *call_data)
3203 {
3204 LttvTracefileState *self = (LttvTracefileState *)call_data;
3205
3206 LttvTracefileState *tfcs;
3207
3208 LttvTraceState *tcs = (LttvTraceState *)(self->parent.t_context);
3209
3210 LttEventPosition *ep;
3211
3212 guint i, nb_block, nb_event, nb_tracefile;
3213
3214 LttTracefile *tf;
3215
3216 LttvAttribute *saved_states_tree, *saved_state_tree;
3217
3218 LttvAttributeValue value;
3219
3220 ep = ltt_event_position_new();
3221
3222 nb_tracefile = tcs->parent.tracefiles->len;
3223
3224 /* Count the number of events added since the last block end in any
3225 tracefile. */
3226
3227 for(i = 0 ; i < nb_tracefile ; i++) {
3228 tfcs =
3229 LTTV_TRACEFILE_STATE(&g_array_index(tcs->parent.tracefiles,
3230 LttvTracefileContext, i));
3231 ltt_event_position(tfcs->parent.e, ep);
3232 ltt_event_position_get(ep, &nb_block, &nb_event, &tf);
3233 tcs->nb_event += nb_event - tfcs->saved_position;
3234 tfcs->saved_position = nb_event;
3235 }
3236 g_free(ep);
3237
3238 if(tcs->nb_event >= tcs->save_interval) {
3239 saved_states_tree = lttv_attribute_find_subdir(tcs->parent.t_a,
3240 LTTV_STATE_SAVED_STATES);
3241 saved_state_tree = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
3242 value = lttv_attribute_add(saved_states_tree,
3243 lttv_attribute_get_number(saved_states_tree), LTTV_GOBJECT);
3244 *(value.v_gobject) = (GObject *)saved_state_tree;
3245 value = lttv_attribute_add(saved_state_tree, LTTV_STATE_TIME, LTTV_TIME);
3246 *(value.v_time) = self->parent.timestamp;
3247 lttv_state_save(tcs, saved_state_tree);
3248 tcs->nb_event = 0;
3249 g_debug("Saving state at time %lu.%lu", self->parent.timestamp.tv_sec,
3250 self->parent.timestamp.tv_nsec);
3251 }
3252 *(tcs->max_time_state_recomputed_in_seek) = self->parent.timestamp;
3253 return FALSE;
3254 }
3255 #endif //0
3256
3257 #if 0
3258 static gboolean block_end(void *hook_data, void *call_data)
3259 {
3260 LttvTracefileState *self = (LttvTracefileState *)call_data;
3261
3262 LttvTraceState *tcs = (LttvTraceState *)(self->parent.t_context);
3263
3264 LttTracefile *tf;
3265
3266 LttEventPosition *ep;
3267
3268 guint nb_block, nb_event;
3269
3270 ep = ltt_event_position_new();
3271 ltt_event_position(self->parent.e, ep);
3272 ltt_event_position_get(ep, &nb_block, &nb_event, &tf);
3273 tcs->nb_event += nb_event - self->saved_position + 1;
3274 self->saved_position = 0;
3275 *(tcs->max_time_state_recomputed_in_seek) = self->parent.timestamp;
3276 g_free(ep);
3277
3278 return FALSE;
3279 }
3280 #endif //0
3281 #if 0
3282 void lttv_state_save_add_event_hooks(LttvTracesetState *self)
3283 {
3284 LttvTraceset *traceset = self->parent.ts;
3285
3286 guint i, j, nb_trace, nb_tracefile;
3287
3288 LttvTraceState *ts;
3289
3290 LttvTracefileState *tfs;
3291
3292 LttvTraceHook hook_start, hook_end;
3293
3294 nb_trace = lttv_traceset_number(traceset);
3295 for(i = 0 ; i < nb_trace ; i++) {
3296 ts = (LttvTraceState *)self->parent.traces[i];
3297
3298 lttv_trace_find_hook(ts->parent.t, "core","block_start",NULL,
3299 NULL, NULL, block_start, &hook_start);
3300 lttv_trace_find_hook(ts->parent.t, "core","block_end",NULL,
3301 NULL, NULL, block_end, &hook_end);
3302
3303 nb_tracefile = ts->parent.tracefiles->len;
3304
3305 for(j = 0 ; j < nb_tracefile ; j++) {
3306 tfs =
3307 LTTV_TRACEFILE_STATE(&g_array_index(ts->parent.tracefiles,
3308 LttvTracefileContext, j));
3309 lttv_hooks_add(lttv_hooks_by_id_find(tfs->parent.event_by_id,
3310 hook_start.id), hook_start.h, NULL, LTTV_PRIO_STATE);
3311 lttv_hooks_add(lttv_hooks_by_id_find(tfs->parent.event_by_id,
3312 hook_end.id), hook_end.h, NULL, LTTV_PRIO_STATE);
3313 }
3314 }
3315 }
3316 #endif //0
3317
3318 void lttv_state_save_add_event_hooks(LttvTracesetState *self)
3319 {
3320 LttvTraceset *traceset = self->parent.ts;
3321
3322 guint i, j, nb_trace, nb_tracefile;
3323
3324 LttvTraceState *ts;
3325
3326 LttvTracefileState *tfs;
3327
3328
3329 nb_trace = lttv_traceset_number(traceset);
3330 for(i = 0 ; i < nb_trace ; i++) {
3331
3332 ts = (LttvTraceState *)self->parent.traces[i];
3333 nb_tracefile = ts->parent.tracefiles->len;
3334
3335 if(ts->has_precomputed_states) continue;
3336
3337 guint *event_count = g_new(guint, 1);
3338 *event_count = 0;
3339
3340 for(j = 0 ; j < nb_tracefile ; j++) {
3341 tfs =
3342 LTTV_TRACEFILE_STATE(g_array_index(ts->parent.tracefiles,
3343 LttvTracefileContext*, j));
3344 lttv_hooks_add(tfs->parent.event,
3345 state_save_event_hook,
3346 event_count,
3347 LTTV_PRIO_STATE);
3348
3349 }
3350 }
3351
3352 lttv_process_traceset_begin(&self->parent,
3353 NULL, NULL, NULL, NULL, NULL);
3354
3355 }
3356
3357 gint lttv_state_save_hook_add_event_hooks(void *hook_data, void *call_data)
3358 {
3359 LttvTracesetState *tss = (LttvTracesetState*)(call_data);
3360
3361 lttv_state_save_add_event_hooks(tss);
3362
3363 return 0;
3364 }
3365
3366
3367 #if 0
3368 void lttv_state_save_remove_event_hooks(LttvTracesetState *self)
3369 {
3370 LttvTraceset *traceset = self->parent.ts;
3371
3372 guint i, j, nb_trace, nb_tracefile;
3373
3374 LttvTraceState *ts;
3375
3376 LttvTracefileState *tfs;
3377
3378 LttvTraceHook hook_start, hook_end;
3379
3380 nb_trace = lttv_traceset_number(traceset);
3381 for(i = 0 ; i < nb_trace ; i++) {
3382 ts = LTTV_TRACE_STATE(self->parent.traces[i]);
3383
3384 lttv_trace_find_hook(ts->parent.t, "core","block_start",NULL,
3385 NULL, NULL, block_start, &hook_start);
3386
3387 lttv_trace_find_hook(ts->parent.t, "core","block_end",NULL,
3388 NULL, NULL, block_end, &hook_end);
3389
3390 nb_tracefile = ts->parent.tracefiles->len;
3391
3392 for(j = 0 ; j < nb_tracefile ; j++) {
3393 tfs =
3394 LTTV_TRACEFILE_STATE(&g_array_index(ts->parent.tracefiles,
3395 LttvTracefileContext, j));
3396 lttv_hooks_remove_data(lttv_hooks_by_id_find(
3397 tfs->parent.event_by_id, hook_start.id), hook_start.h, NULL);
3398 lttv_hooks_remove_data(lttv_hooks_by_id_find(
3399 tfs->parent.event_by_id, hook_end.id), hook_end.h, NULL);
3400 }
3401 }
3402 }
3403 #endif //0
3404
3405 void lttv_state_save_remove_event_hooks(LttvTracesetState *self)
3406 {
3407 LttvTraceset *traceset = self->parent.ts;
3408
3409 guint i, j, nb_trace, nb_tracefile;
3410
3411 LttvTraceState *ts;
3412
3413 LttvTracefileState *tfs;
3414
3415 LttvHooks *after_trace = lttv_hooks_new();
3416
3417 lttv_hooks_add(after_trace,
3418 state_save_after_trace_hook,
3419 NULL,
3420 LTTV_PRIO_STATE);
3421
3422
3423 lttv_process_traceset_end(&self->parent,
3424 NULL, after_trace, NULL, NULL, NULL);
3425
3426 lttv_hooks_destroy(after_trace);
3427
3428 nb_trace = lttv_traceset_number(traceset);
3429 for(i = 0 ; i < nb_trace ; i++) {
3430
3431 ts = (LttvTraceState *)self->parent.traces[i];
3432 nb_tracefile = ts->parent.tracefiles->len;
3433
3434 if(ts->has_precomputed_states) continue;
3435
3436 guint *event_count = NULL;
3437
3438 for(j = 0 ; j < nb_tracefile ; j++) {
3439 tfs =
3440 LTTV_TRACEFILE_STATE(g_array_index(ts->parent.tracefiles,
3441 LttvTracefileContext*, j));
3442 event_count = lttv_hooks_remove(tfs->parent.event,
3443 state_save_event_hook);
3444 }
3445 if(event_count) g_free(event_count);
3446 }
3447 }
3448
3449 gint lttv_state_save_hook_remove_event_hooks(void *hook_data, void *call_data)
3450 {
3451 LttvTracesetState *tss = (LttvTracesetState*)(call_data);
3452
3453 lttv_state_save_remove_event_hooks(tss);
3454
3455 return 0;
3456 }
3457
3458 void lttv_state_traceset_seek_time_closest(LttvTracesetState *self, LttTime t)
3459 {
3460 LttvTraceset *traceset = self->parent.ts;
3461
3462 guint i, nb_trace;
3463
3464 int min_pos, mid_pos, max_pos;
3465
3466 guint call_rest = 0;
3467
3468 LttvTraceState *tcs;
3469
3470 LttvAttributeValue value;
3471
3472 LttvAttributeType type;
3473
3474 LttvAttributeName name;
3475
3476 gboolean is_named;
3477
3478 LttvAttribute *saved_states_tree, *saved_state_tree, *closest_tree;
3479
3480 //g_tree_destroy(self->parent.pqueue);
3481 //self->parent.pqueue = g_tree_new(compare_tracefile);
3482
3483 g_info("Entering seek_time_closest for time %lu.%lu", t.tv_sec, t.tv_nsec);
3484
3485 nb_trace = lttv_traceset_number(traceset);
3486 for(i = 0 ; i < nb_trace ; i++) {
3487 tcs = (LttvTraceState *)self->parent.traces[i];
3488
3489 if(ltt_time_compare(t, *(tcs->max_time_state_recomputed_in_seek)) < 0) {
3490 saved_states_tree = lttv_attribute_find_subdir(tcs->parent.t_a,
3491 LTTV_STATE_SAVED_STATES);
3492 min_pos = -1;
3493
3494 if(saved_states_tree) {
3495 max_pos = lttv_attribute_get_number(saved_states_tree) - 1;
3496 mid_pos = max_pos / 2;
3497 while(min_pos < max_pos) {
3498 type = lttv_attribute_get(saved_states_tree, mid_pos, &name, &value,
3499 &is_named);
3500 g_assert(type == LTTV_GOBJECT);
3501 saved_state_tree = *((LttvAttribute **)(value.v_gobject));
3502 type = lttv_attribute_get_by_name(saved_state_tree, LTTV_STATE_TIME,
3503 &value);
3504 g_assert(type == LTTV_TIME);
3505 if(ltt_time_compare(*(value.v_time), t) < 0) {
3506 min_pos = mid_pos;
3507 closest_tree = saved_state_tree;
3508 }
3509 else max_pos = mid_pos - 1;
3510
3511 mid_pos = (min_pos + max_pos + 1) / 2;
3512 }
3513 }
3514
3515 /* restore the closest earlier saved state */
3516 if(min_pos != -1) {
3517 lttv_state_restore(tcs, closest_tree);
3518 call_rest = 1;
3519 }
3520
3521 /* There is no saved state, yet we want to have it. Restart at T0 */
3522 else {
3523 restore_init_state(tcs);
3524 lttv_process_trace_seek_time(&(tcs->parent), ltt_time_zero);
3525 }
3526 }
3527 /* We want to seek quickly without restoring/updating the state */
3528 else {
3529 restore_init_state(tcs);
3530 lttv_process_trace_seek_time(&(tcs->parent), t);
3531 }
3532 }
3533 if(!call_rest) g_info("NOT Calling restore");
3534 }
3535
3536
3537 static void
3538 traceset_state_instance_init (GTypeInstance *instance, gpointer g_class)
3539 {
3540 }
3541
3542
3543 static void
3544 traceset_state_finalize (LttvTracesetState *self)
3545 {
3546 G_OBJECT_CLASS(g_type_class_peek(LTTV_TRACESET_CONTEXT_TYPE))->
3547 finalize(G_OBJECT(self));
3548 }
3549
3550
3551 static void
3552 traceset_state_class_init (LttvTracesetContextClass *klass)
3553 {
3554 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
3555
3556 gobject_class->finalize = (void (*)(GObject *self)) traceset_state_finalize;
3557 klass->init = (void (*)(LttvTracesetContext *self, LttvTraceset *ts))init;
3558 klass->fini = (void (*)(LttvTracesetContext *self))fini;
3559 klass->new_traceset_context = new_traceset_context;
3560 klass->new_trace_context = new_trace_context;
3561 klass->new_tracefile_context = new_tracefile_context;
3562 }
3563
3564
3565 GType
3566 lttv_traceset_state_get_type(void)
3567 {
3568 static GType type = 0;
3569 if (type == 0) {
3570 static const GTypeInfo info = {
3571 sizeof (LttvTracesetStateClass),
3572 NULL, /* base_init */
3573 NULL, /* base_finalize */
3574 (GClassInitFunc) traceset_state_class_init, /* class_init */
3575 NULL, /* class_finalize */
3576 NULL, /* class_data */
3577 sizeof (LttvTracesetState),
3578 0, /* n_preallocs */
3579 (GInstanceInitFunc) traceset_state_instance_init, /* instance_init */
3580 NULL /* value handling */
3581 };
3582
3583 type = g_type_register_static (LTTV_TRACESET_CONTEXT_TYPE, "LttvTracesetStateType",
3584 &info, 0);
3585 }
3586 return type;
3587 }
3588
3589
3590 static void
3591 trace_state_instance_init (GTypeInstance *instance, gpointer g_class)
3592 {
3593 }
3594
3595
3596 static void
3597 trace_state_finalize (LttvTraceState *self)
3598 {
3599 G_OBJECT_CLASS(g_type_class_peek(LTTV_TRACE_CONTEXT_TYPE))->
3600 finalize(G_OBJECT(self));
3601 }
3602
3603
3604 static void
3605 trace_state_class_init (LttvTraceStateClass *klass)
3606 {
3607 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
3608
3609 gobject_class->finalize = (void (*)(GObject *self)) trace_state_finalize;
3610 klass->state_save = state_save;
3611 klass->state_restore = state_restore;
3612 klass->state_saved_free = state_saved_free;
3613 }
3614
3615
3616 GType
3617 lttv_trace_state_get_type(void)
3618 {
3619 static GType type = 0;
3620 if (type == 0) {
3621 static const GTypeInfo info = {
3622 sizeof (LttvTraceStateClass),
3623 NULL, /* base_init */
3624 NULL, /* base_finalize */
3625 (GClassInitFunc) trace_state_class_init, /* class_init */
3626 NULL, /* class_finalize */
3627 NULL, /* class_data */
3628 sizeof (LttvTraceState),
3629 0, /* n_preallocs */
3630 (GInstanceInitFunc) trace_state_instance_init, /* instance_init */
3631 NULL /* value handling */
3632 };
3633
3634 type = g_type_register_static (LTTV_TRACE_CONTEXT_TYPE,
3635 "LttvTraceStateType", &info, 0);
3636 }
3637 return type;
3638 }
3639
3640
3641 static void
3642 tracefile_state_instance_init (GTypeInstance *instance, gpointer g_class)
3643 {
3644 }
3645
3646
3647 static void
3648 tracefile_state_finalize (LttvTracefileState *self)
3649 {
3650 G_OBJECT_CLASS(g_type_class_peek(LTTV_TRACEFILE_CONTEXT_TYPE))->
3651 finalize(G_OBJECT(self));
3652 }
3653
3654
3655 static void
3656 tracefile_state_class_init (LttvTracefileStateClass *klass)
3657 {
3658 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
3659
3660 gobject_class->finalize = (void (*)(GObject *self)) tracefile_state_finalize;
3661 }
3662
3663
3664 GType
3665 lttv_tracefile_state_get_type(void)
3666 {
3667 static GType type = 0;
3668 if (type == 0) {
3669 static const GTypeInfo info = {
3670 sizeof (LttvTracefileStateClass),
3671 NULL, /* base_init */
3672 NULL, /* base_finalize */
3673 (GClassInitFunc) tracefile_state_class_init, /* class_init */
3674 NULL, /* class_finalize */
3675 NULL, /* class_data */
3676 sizeof (LttvTracefileState),
3677 0, /* n_preallocs */
3678 (GInstanceInitFunc) tracefile_state_instance_init, /* instance_init */
3679 NULL /* value handling */
3680 };
3681
3682 type = g_type_register_static (LTTV_TRACEFILE_CONTEXT_TYPE,
3683 "LttvTracefileStateType", &info, 0);
3684 }
3685 return type;
3686 }
3687
3688
3689 static void module_init()
3690 {
3691 LTTV_STATE_UNNAMED = g_quark_from_string("UNNAMED");
3692 LTTV_STATE_UNBRANDED = g_quark_from_string("UNBRANDED");
3693 LTTV_STATE_MODE_UNKNOWN = g_quark_from_string("MODE_UNKNOWN");
3694 LTTV_STATE_USER_MODE = g_quark_from_string("USER_MODE");
3695 LTTV_STATE_SYSCALL = g_quark_from_string("SYSCALL");
3696 LTTV_STATE_TRAP = g_quark_from_string("TRAP");
3697 LTTV_STATE_IRQ = g_quark_from_string("IRQ");
3698 LTTV_STATE_SOFT_IRQ = g_quark_from_string("SOFTIRQ");
3699 LTTV_STATE_SUBMODE_UNKNOWN = g_quark_from_string("UNKNOWN");
3700 LTTV_STATE_SUBMODE_NONE = g_quark_from_string("NONE");
3701 LTTV_STATE_WAIT_FORK = g_quark_from_string("WAIT_FORK");
3702 LTTV_STATE_WAIT_CPU = g_quark_from_string("WAIT_CPU");
3703 LTTV_STATE_EXIT = g_quark_from_string("EXIT");
3704 LTTV_STATE_ZOMBIE = g_quark_from_string("ZOMBIE");
3705 LTTV_STATE_WAIT = g_quark_from_string("WAIT");
3706 LTTV_STATE_RUN = g_quark_from_string("RUN");
3707 LTTV_STATE_DEAD = g_quark_from_string("DEAD");
3708 LTTV_STATE_USER_THREAD = g_quark_from_string("USER_THREAD");
3709 LTTV_STATE_KERNEL_THREAD = g_quark_from_string("KERNEL_THREAD");
3710 LTTV_STATE_TRACEFILES = g_quark_from_string("tracefiles");
3711 LTTV_STATE_PROCESSES = g_quark_from_string("processes");
3712 LTTV_STATE_PROCESS = g_quark_from_string("process");
3713 LTTV_STATE_RUNNING_PROCESS = g_quark_from_string("running_process");
3714 LTTV_STATE_EVENT = g_quark_from_string("event");
3715 LTTV_STATE_SAVED_STATES = g_quark_from_string("saved states");
3716 LTTV_STATE_SAVED_STATES_TIME = g_quark_from_string("saved states time");
3717 LTTV_STATE_TIME = g_quark_from_string("time");
3718 LTTV_STATE_HOOKS = g_quark_from_string("saved state hooks");
3719 LTTV_STATE_NAME_TABLES = g_quark_from_string("name tables");
3720 LTTV_STATE_TRACE_STATE_USE_COUNT =
3721 g_quark_from_string("trace_state_use_count");
3722 LTTV_STATE_RESOURCE_CPUS = g_quark_from_string("cpu resource states");
3723
3724
3725 LTT_FACILITY_KERNEL = g_quark_from_string("kernel");
3726 LTT_FACILITY_KERNEL_ARCH = g_quark_from_string("kernel_arch");
3727 LTT_FACILITY_FS = g_quark_from_string("fs");
3728 LTT_FACILITY_LIST = g_quark_from_string("list");
3729 LTT_FACILITY_USER_GENERIC = g_quark_from_string("user_generic");
3730 LTT_FACILITY_BLOCK = g_quark_from_string("block");
3731
3732
3733 LTT_EVENT_SYSCALL_ENTRY = g_quark_from_string("syscall_entry");
3734 LTT_EVENT_SYSCALL_EXIT = g_quark_from_string("syscall_exit");
3735 LTT_EVENT_TRAP_ENTRY = g_quark_from_string("trap_entry");
3736 LTT_EVENT_TRAP_EXIT = g_quark_from_string("trap_exit");
3737 LTT_EVENT_IRQ_ENTRY = g_quark_from_string("irq_entry");
3738 LTT_EVENT_IRQ_EXIT = g_quark_from_string("irq_exit");
3739 LTT_EVENT_SOFT_IRQ_ENTRY = g_quark_from_string("soft_irq_entry");
3740 LTT_EVENT_SOFT_IRQ_EXIT = g_quark_from_string("soft_irq_exit");
3741 LTT_EVENT_SCHED_SCHEDULE = g_quark_from_string("sched_schedule");
3742 LTT_EVENT_PROCESS_FORK = g_quark_from_string("process_fork");
3743 LTT_EVENT_KTHREAD_CREATE = g_quark_from_string("kthread_create");
3744 LTT_EVENT_PROCESS_EXIT = g_quark_from_string("process_exit");
3745 LTT_EVENT_PROCESS_FREE = g_quark_from_string("process_free");
3746 LTT_EVENT_EXEC = g_quark_from_string("exec");
3747 LTT_EVENT_PROCESS_STATE = g_quark_from_string("process_state");
3748 LTT_EVENT_STATEDUMP_END = g_quark_from_string("statedump_end");
3749 LTT_EVENT_FUNCTION_ENTRY = g_quark_from_string("function_entry");
3750 LTT_EVENT_FUNCTION_EXIT = g_quark_from_string("function_exit");
3751 LTT_EVENT_THREAD_BRAND = g_quark_from_string("thread_brand");
3752 LTT_EVENT_REQUEST_ISSUE = g_quark_from_string("_blk_request_issue");
3753 LTT_EVENT_REQUEST_COMPLETE = g_quark_from_string("_blk_request_complete");
3754
3755
3756 LTT_FIELD_SYSCALL_ID = g_quark_from_string("syscall_id");
3757 LTT_FIELD_TRAP_ID = g_quark_from_string("trap_id");
3758 LTT_FIELD_IRQ_ID = g_quark_from_string("irq_id");
3759 LTT_FIELD_SOFT_IRQ_ID = g_quark_from_string("softirq_id");
3760 LTT_FIELD_PREV_PID = g_quark_from_string("prev_pid");
3761 LTT_FIELD_NEXT_PID = g_quark_from_string("next_pid");
3762 LTT_FIELD_PREV_STATE = g_quark_from_string("prev_state");
3763 LTT_FIELD_PARENT_PID = g_quark_from_string("parent_pid");
3764 LTT_FIELD_CHILD_PID = g_quark_from_string("child_pid");
3765 LTT_FIELD_PID = g_quark_from_string("pid");
3766 LTT_FIELD_TGID = g_quark_from_string("tgid");
3767 LTT_FIELD_CHILD_TGID = g_quark_from_string("child_tgid");
3768 LTT_FIELD_FILENAME = g_quark_from_string("filename");
3769 LTT_FIELD_NAME = g_quark_from_string("name");
3770 LTT_FIELD_TYPE = g_quark_from_string("type");
3771 LTT_FIELD_MODE = g_quark_from_string("mode");
3772 LTT_FIELD_SUBMODE = g_quark_from_string("submode");
3773 LTT_FIELD_STATUS = g_quark_from_string("status");
3774 LTT_FIELD_THIS_FN = g_quark_from_string("this_fn");
3775 LTT_FIELD_CALL_SITE = g_quark_from_string("call_site");
3776 LTT_FIELD_MAJOR = g_quark_from_string("major");
3777 LTT_FIELD_MINOR = g_quark_from_string("minor");
3778 LTT_FIELD_OPERATION = g_quark_from_string("direction");
3779
3780 LTTV_CPU_UNKNOWN = g_quark_from_string("unknown");
3781 LTTV_CPU_IDLE = g_quark_from_string("idle");
3782 LTTV_CPU_BUSY = g_quark_from_string("busy");
3783 LTTV_CPU_IRQ = g_quark_from_string("irq");
3784 LTTV_CPU_TRAP = g_quark_from_string("trap");
3785
3786 LTTV_IRQ_UNKNOWN = g_quark_from_string("unknown");
3787 LTTV_IRQ_IDLE = g_quark_from_string("idle");
3788 LTTV_IRQ_BUSY = g_quark_from_string("busy");
3789
3790 LTTV_BDEV_UNKNOWN = g_quark_from_string("unknown");
3791 LTTV_BDEV_IDLE = g_quark_from_string("idle");
3792 LTTV_BDEV_BUSY_READING = g_quark_from_string("busy_reading");
3793 LTTV_BDEV_BUSY_WRITING = g_quark_from_string("busy_writing");
3794 }
3795
3796 static void module_destroy()
3797 {
3798 }
3799
3800
3801 LTTV_MODULE("state", "State computation", \
3802 "Update the system state, possibly saving it at intervals", \
3803 module_init, module_destroy)
3804
3805
3806
This page took 0.11406 seconds and 4 git commands to generate.