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