Add reference counts to state and stats saved attributes. This way, the
[lttv.git] / ltt / branches / poly / lttv / main / state.c
index 0fa74a1c47ca25774bffca668d822a4235936575..0624fe4b01e17ed3b9180162c15763f6bc54fa1b 100644 (file)
  */
 
 
+#include <lttv/lttv.h>
+#include <lttv/module.h>
 #include <lttv/state.h>
 #include <ltt/facility.h>
 #include <ltt/trace.h>
 #include <ltt/event.h>
 #include <ltt/type.h>
+#include <stdio.h>
 
 LttvExecutionMode
   LTTV_STATE_MODE_UNKNOWN,
@@ -48,18 +51,29 @@ static GQuark
   LTTV_STATE_PROCESS,
   LTTV_STATE_EVENT,
   LTTV_STATE_SAVED_STATES,
+  LTTV_STATE_SAVED_STATES_TIME,
   LTTV_STATE_TIME,
-  LTTV_STATE_HOOKS;
+  LTTV_STATE_HOOKS,
+  LTTV_STATE_NAME_TABLES,
+  LTTV_STATE_TRACE_STATE_USE_COUNT;
 
 
-static void fill_name_tables(LttvTraceState *tcs);
+static void create_max_time(LttvTraceState *tcs);
+
+static void get_max_time(LttvTraceState *tcs);
+
+static void free_max_time(LttvTraceState *tcs);
+
+static void create_name_tables(LttvTraceState *tcs);
+
+static void get_name_tables(LttvTraceState *tcs);
 
 static void free_name_tables(LttvTraceState *tcs);
 
+static void free_saved_state(LttvTraceState *tcs);
+
 static void lttv_state_free_process_table(GHashTable *processes);
 
-static LttvProcessState *create_process(LttvTracefileState *tfs, 
-                                LttvProcessState *parent, guint pid);
 
 void lttv_state_save(LttvTraceState *self, LttvAttribute *container)
 {
@@ -73,48 +87,65 @@ void lttv_state_restore(LttvTraceState *self, LttvAttribute *container)
 }
 
 
-void lttv_state_saved_state_free(LttvTraceState *self, 
+void lttv_state__state_saved_free(LttvTraceState *self, 
     LttvAttribute *container)
 {
-  LTTV_TRACE_STATE_GET_CLASS(self)->state_restore(self, container);
+  LTTV_TRACE_STATE_GET_CLASS(self)->state_saved_free(self, container);
+}
+
+
+guint process_hash(gconstpointer key) 
+{
+  return ((LttvProcessState *)key)->pid;
+}
+
+
+gboolean process_equal(gconstpointer a, gconstpointer b)
+{
+  LttvProcessState *process_a, *process_b;
+
+  process_a = (LttvProcessState *)a;
+  process_b = (LttvProcessState *)b;
+
+  if(process_a->pid != process_b->pid) return FALSE;
+  if(process_a->pid == 0 && 
+      process_a->last_cpu != process_b->last_cpu) return FALSE;
+  return TRUE;
 }
 
 
 static void
 restore_init_state(LttvTraceState *self)
 {
-  guint i, nb_control, nb_per_cpu, nb_tracefile;
+  guint i, nb_tracefile;
 
   LttvTracefileState *tfcs;
   
   LttTime null_time = {0,0};
 
   if(self->processes != NULL) lttv_state_free_process_table(self->processes);
-  self->processes = g_hash_table_new(g_direct_hash, g_direct_equal);
+  self->processes = g_hash_table_new(process_hash, process_equal);
   self->nb_event = 0;
 
-  nb_control = ltt_trace_control_tracefile_number(self->parent.t);
-  nb_per_cpu = ltt_trace_per_cpu_tracefile_number(self->parent.t);
-  nb_tracefile = nb_control + nb_per_cpu;
-  for(i = 0 ; i < nb_tracefile ; i++) {
-    if(i < nb_control) {
-      tfcs = LTTV_TRACEFILE_STATE(self->parent.control_tracefiles[i]);
-    }
-    else {
-      tfcs = LTTV_TRACEFILE_STATE(self->parent.per_cpu_tracefiles[i - nb_control]);
-    }
+  nb_tracefile = ltt_trace_control_tracefile_number(self->parent.t) +
+      ltt_trace_per_cpu_tracefile_number(self->parent.t);
 
+  for(i = 0 ; i < nb_tracefile ; i++) {
+    tfcs = LTTV_TRACEFILE_STATE(self->parent.tracefiles[i]);
     tfcs->parent.timestamp = null_time;
     tfcs->saved_position = 0;
-    tfcs->process = create_process(tfcs, NULL,0);
+    tfcs->process = lttv_state_create_process(tfcs, NULL,0);
+    tfcs->process->state->s = LTTV_STATE_RUN;
+    tfcs->process->last_cpu = tfcs->cpu_name;
   }
 }
 
+static LttTime time_zero = {0,0};
 
 static void
 init(LttvTracesetState *self, LttvTraceset *ts)
 {
-  guint i, j, nb_trace, nb_control, nb_per_cpu, nb_tracefile;
+  guint i, j, nb_trace, nb_tracefile;
 
   LttvTraceContext *tc;
 
@@ -122,6 +153,8 @@ init(LttvTracesetState *self, LttvTraceset *ts)
 
   LttvTracefileState *tfcs;
   
+  LttvAttributeValue v;
+
   LTTV_TRACESET_CONTEXT_CLASS(g_type_class_peek(LTTV_TRACESET_CONTEXT_TYPE))->
       init((LttvTracesetContext *)self, ts);
 
@@ -129,21 +162,23 @@ init(LttvTracesetState *self, LttvTraceset *ts)
   for(i = 0 ; i < nb_trace ; i++) {
     tc = self->parent.traces[i];
     tcs = (LttvTraceState *)tc;
-    tcs->save_interval = 100000;
-    tcs->recompute_state_in_seek = TRUE;
-    tcs->saved_state_ready = FALSE;
-    fill_name_tables(tcs);
-
-    nb_control = ltt_trace_control_tracefile_number(tc->t);
-    nb_per_cpu = ltt_trace_per_cpu_tracefile_number(tc->t);
-    nb_tracefile = nb_control + nb_per_cpu;
+    tcs->save_interval = 50000;
+    lttv_attribute_find(tcs->parent.t_a, LTTV_STATE_TRACE_STATE_USE_COUNT, 
+        LTTV_UINT, &v);
+    (*v.v_uint)++;
+
+    if(*(v.v_uint) == 1) {
+      create_name_tables(tcs);
+      create_max_time(tcs);
+    }
+    get_name_tables(tcs);
+    get_max_time(tcs);
+
+    nb_tracefile = ltt_trace_control_tracefile_number(tc->t) +
+        ltt_trace_per_cpu_tracefile_number(tc->t);
+
     for(j = 0 ; j < nb_tracefile ; j++) {
-      if(j < nb_control) {
-        tfcs = LTTV_TRACEFILE_STATE(tc->control_tracefiles[j]);
-      }
-      else {
-        tfcs = LTTV_TRACEFILE_STATE(tc->per_cpu_tracefiles[j - nb_control]);
-      }
+      tfcs = LTTV_TRACEFILE_STATE(tc->tracefiles[j]);
       tfcs->cpu_name= g_quark_from_string(ltt_tracefile_name(tfcs->parent.tf));
     }
     tcs->processes = NULL;
@@ -155,18 +190,29 @@ init(LttvTracesetState *self, LttvTraceset *ts)
 static void
 fini(LttvTracesetState *self)
 {
-  guint i, j, nb_trace, nb_tracefile;
+  guint i, j, nb_trace;
 
   LttvTraceState *tcs;
 
   LttvTracefileState *tfcs;
 
+  LttvAttributeValue v;
+
   nb_trace = lttv_traceset_number(LTTV_TRACESET_CONTEXT(self)->ts);
   for(i = 0 ; i < nb_trace ; i++) {
     tcs = (LttvTraceState *)(LTTV_TRACESET_CONTEXT(self)->traces[i]);
+    lttv_attribute_find(tcs->parent.t_a, LTTV_STATE_TRACE_STATE_USE_COUNT, 
+        LTTV_UINT, &v);
+    (*v.v_uint)--;
+
+    g_assert(*(v.v_uint) >= 0);
+    if(*(v.v_uint) == 0) {
+      free_name_tables(tcs);
+      free_max_time(tcs);
+      free_saved_state(tcs);
+    }
     lttv_state_free_process_table(tcs->processes);
     tcs->processes = NULL;
-    free_name_tables(tcs);
   }
   LTTV_TRACESET_CONTEXT_CLASS(g_type_class_peek(LTTV_TRACESET_CONTEXT_TYPE))->
       fini((LttvTracesetContext *)self);
@@ -194,6 +240,76 @@ new_tracefile_context(LttvTracesetContext *self)
 }
 
 
+/* Write the process state of the trace */
+
+static void write_process_state(gpointer key, gpointer value,
+    gpointer user_data)
+{
+  LttvProcessState *process;
+
+  LttvExecutionState *es;
+
+  FILE *fp = (FILE *)user_data;
+
+  guint i;
+
+  process = (LttvProcessState *)value;
+  fprintf(fp,
+"  <PROCESS CORE=%p PID=%u PPID=%u CTIME_S=%lu CTIME_NS=%lu NAME=\"%s\" CPU=\"%s\">\n",
+      process, process->pid, process->ppid, process->creation_time.tv_sec,
+      process->creation_time.tv_nsec, g_quark_to_string(process->name),
+      g_quark_to_string(process->last_cpu));
+
+  for(i = 0 ; i < process->execution_stack->len; i++) {
+    es = &g_array_index(process->execution_stack, LttvExecutionState, i);
+    fprintf(fp, "    <ES MODE=\"%s\" SUBMODE=\"%s\" ENTRY_S=%lu ENTRY_NS=%lu",
+           g_quark_to_string(es->t), g_quark_to_string(es->n),
+            es->entry.tv_sec, es->entry.tv_nsec);
+    fprintf(fp, " CHANGE_S=%lu CHANGE_NS=%lu STATUS=\"%s\"/>\n",
+            es->change.tv_sec, es->change.tv_nsec, g_quark_to_string(es->s)); 
+  }
+  fprintf(fp, "  </PROCESS>\n");
+}
+
+
+void lttv_state_write(LttvTraceState *self, LttTime t, FILE *fp)
+{
+  guint i, nb_tracefile, nb_block, nb_event;
+
+  LttvTracefileState *tfcs;
+
+  LttTracefile *tf;
+
+  LttEventPosition *ep;
+
+  ep = ltt_event_position_new();
+
+  fprintf(fp,"<PROCESS_STATE TIME_S=%lu TIME_NS=%lu>\n", t.tv_sec, t.tv_nsec);
+
+  g_hash_table_foreach(self->processes, write_process_state, fp);
+
+  nb_tracefile = ltt_trace_control_tracefile_number(self->parent.t) +
+      ltt_trace_per_cpu_tracefile_number(self->parent.t);
+
+  for(i = 0 ; i < nb_tracefile ; i++) {
+    tfcs = (LttvTracefileState *)self->parent.tracefiles[i];
+    fprintf(fp, "  <TRACEFILE PID=%u TIMESTAMP_S=%lu TIMESTAMP_NS=%lu", 
+        tfcs->process->pid, tfcs->parent.timestamp.tv_sec, 
+        tfcs->parent.timestamp.tv_nsec);
+    if(tfcs->parent.e == NULL) fprintf(fp,"/>\n");
+    else {
+      ltt_event_position(tfcs->parent.e, ep);
+      ltt_event_position_get(ep, &nb_block, &nb_event, &tf);
+      fprintf(fp, " BLOCK=%u EVENT=%u/>\n", nb_block, nb_event);
+    }
+  }
+  g_free(ep);
+  fprintf(fp,"</PROCESS_STATE>");
+}
+
+
+/* Copy each process from an existing hash table to a new one */
+
 static void copy_process_state(gpointer key, gpointer value,gpointer user_data)
 {
   LttvProcessState *process, *new_process;
@@ -214,14 +330,13 @@ static void copy_process_state(gpointer key, gpointer value,gpointer user_data)
   }
   new_process->state = &g_array_index(new_process->execution_stack, 
       LttvExecutionState, new_process->execution_stack->len - 1);
-  g_hash_table_insert(new_processes, GUINT_TO_POINTER(new_process->pid), 
-      new_process);
+  g_hash_table_insert(new_processes, new_process, new_process);
 }
 
 
 static GHashTable *lttv_state_copy_process_table(GHashTable *processes)
 {
-  GHashTable *new_processes = g_hash_table_new(g_direct_hash, g_direct_equal);
+  GHashTable *new_processes = g_hash_table_new(process_hash, process_equal);
 
   g_hash_table_foreach(processes, copy_process_state, new_processes);
   return new_processes;
@@ -236,7 +351,7 @@ static GHashTable *lttv_state_copy_process_table(GHashTable *processes)
 
 static void state_save(LttvTraceState *self, LttvAttribute *container)
 {
-  guint i, nb_control, nb_per_cpu, nb_tracefile;
+  guint i, nb_tracefile;
 
   LttvTracefileState *tfcs;
 
@@ -257,16 +372,11 @@ static void state_save(LttvTraceState *self, LttvAttribute *container)
       LTTV_POINTER);
   *(value.v_pointer) = lttv_state_copy_process_table(self->processes);
 
-  nb_control = ltt_trace_control_tracefile_number(self->parent.t);
-  nb_per_cpu = ltt_trace_per_cpu_tracefile_number(self->parent.t);
-  nb_tracefile = nb_control + nb_per_cpu;
+  nb_tracefile = ltt_trace_control_tracefile_number(self->parent.t) +
+      ltt_trace_per_cpu_tracefile_number(self->parent.t);
 
   for(i = 0 ; i < nb_tracefile ; i++) {
-    if(i < nb_control) 
-        tfcs = (LttvTracefileState *)self->parent.control_tracefiles[i];
-    else tfcs = (LttvTracefileState *)
-        self->parent.per_cpu_tracefiles[i - nb_control];
-
+    tfcs = (LttvTracefileState *)self->parent.tracefiles[i];
     tracefile_tree = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
     value = lttv_attribute_add(tracefiles_tree, i, 
         LTTV_GOBJECT);
@@ -281,6 +391,12 @@ static void state_save(LttvTraceState *self, LttvAttribute *container)
       ep = ltt_event_position_new();
       ltt_event_position(tfcs->parent.e, ep);
       *(value.v_pointer) = ep;
+
+      guint nb_block, nb_event;
+      LttTracefile *tf;
+      ltt_event_position_get(ep, &nb_block, &nb_event, &tf);
+      g_debug("Block %u event %u time %lu.%lu", nb_block, nb_event,
+          tfcs->parent.timestamp.tv_sec, tfcs->parent.timestamp.tv_nsec);
     }
   }
 }
@@ -288,7 +404,7 @@ static void state_save(LttvTraceState *self, LttvAttribute *container)
 
 static void state_restore(LttvTraceState *self, LttvAttribute *container)
 {
-  guint i, nb_control, nb_per_cpu, nb_tracefile;
+  guint i, nb_tracefile, pid;
 
   LttvTracefileState *tfcs;
 
@@ -311,16 +427,11 @@ static void state_restore(LttvTraceState *self, LttvAttribute *container)
   lttv_state_free_process_table(self->processes);
   self->processes = lttv_state_copy_process_table(*(value.v_pointer));
 
-  nb_control = ltt_trace_control_tracefile_number(self->parent.t);
-  nb_per_cpu = ltt_trace_per_cpu_tracefile_number(self->parent.t);
-  nb_tracefile = nb_control + nb_per_cpu;
+  nb_tracefile = ltt_trace_control_tracefile_number(self->parent.t) +
+      ltt_trace_per_cpu_tracefile_number(self->parent.t);
 
   for(i = 0 ; i < nb_tracefile ; i++) {
-    if(i < nb_control) tfcs = (LttvTracefileState *)
-        self->parent.control_tracefiles[i];
-    else tfcs = (LttvTracefileState *)
-        self->parent.per_cpu_tracefiles[i - nb_control];
-
+    tfcs = (LttvTracefileState *)self->parent.tracefiles[i];
     type = lttv_attribute_get(tracefiles_tree, i, &name, &value);
     g_assert(type == LTTV_GOBJECT);
     tracefile_tree = *((LttvAttribute **)(value.v_gobject));
@@ -328,7 +439,9 @@ static void state_restore(LttvTraceState *self, LttvAttribute *container)
     type = lttv_attribute_get_by_name(tracefile_tree, LTTV_STATE_PROCESS, 
         &value);
     g_assert(type == LTTV_UINT);
-    tfcs->process = lttv_state_find_process(tfcs, *(value.v_uint));
+    pid = *(value.v_uint);
+    tfcs->process = lttv_state_find_process_or_create(tfcs, pid);
+
     type = lttv_attribute_get_by_name(tracefile_tree, LTTV_STATE_EVENT, 
         &value);
     g_assert(type == LTTV_POINTER);
@@ -345,7 +458,7 @@ static void state_restore(LttvTraceState *self, LttvAttribute *container)
 
 static void state_saved_free(LttvTraceState *self, LttvAttribute *container)
 {
-  guint i, nb_control, nb_per_cpu, nb_tracefile;
+  guint i, nb_tracefile;
 
   LttvTracefileState *tfcs;
 
@@ -370,16 +483,11 @@ static void state_saved_free(LttvTraceState *self, LttvAttribute *container)
   *(value.v_pointer) = NULL;
   lttv_attribute_remove_by_name(container, LTTV_STATE_PROCESSES);
 
-  nb_control = ltt_trace_control_tracefile_number(self->parent.t);
-  nb_per_cpu = ltt_trace_per_cpu_tracefile_number(self->parent.t);
-  nb_tracefile = nb_control + nb_per_cpu;
+  nb_tracefile = ltt_trace_control_tracefile_number(self->parent.t) +
+      ltt_trace_per_cpu_tracefile_number(self->parent.t);
 
   for(i = 0 ; i < nb_tracefile ; i++) {
-    if(i < nb_control) tfcs = (LttvTracefileState *)
-        self->parent.control_tracefiles[i];
-    else tfcs = (LttvTracefileState *)
-        self->parent.per_cpu_tracefiles[i - nb_control];
-
+    tfcs = (LttvTracefileState *)self->parent.tracefiles[i];
     type = lttv_attribute_get(tracefiles_tree, i, &name, &value);
     g_assert(type == LTTV_GOBJECT);
     tracefile_tree = *((LttvAttribute **)(value.v_gobject));
@@ -393,8 +501,80 @@ static void state_saved_free(LttvTraceState *self, LttvAttribute *container)
 }
 
 
+static void free_saved_state(LttvTraceState *self)
+{
+  guint i, nb;
+
+  LttvAttributeType type;
+
+  LttvAttributeValue value;
+
+  LttvAttributeName name;
+
+  LttvAttribute *saved_states;
+
+  saved_states = lttv_attribute_find_subdir(self->parent.t_a,
+      LTTV_STATE_SAVED_STATES);
+
+  nb = lttv_attribute_get_number(saved_states);
+  for(i = 0 ; i < nb ; i++) {
+    type = lttv_attribute_get(saved_states, i, &name, &value);
+    g_assert(type == LTTV_GOBJECT);
+    state_saved_free(self, *((LttvAttribute **)value.v_gobject));
+  }
+
+  lttv_attribute_remove_by_name(self->parent.t_a, LTTV_STATE_SAVED_STATES);
+  lttv_attribute_recursive_free(saved_states);
+}
+
+
+static void 
+create_max_time(LttvTraceState *tcs) 
+{
+  LttvAttributeValue v;
+
+  lttv_attribute_find(tcs->parent.t_a, LTTV_STATE_SAVED_STATES_TIME, 
+        LTTV_POINTER, &v);
+  g_assert(*(v.v_pointer) == NULL);
+  *(v.v_pointer) = g_new(LttTime,1);
+  *((LttTime *)*(v.v_pointer)) = time_zero;
+}
+
+
 static void 
-fill_name_tables(LttvTraceState *tcs) 
+get_max_time(LttvTraceState *tcs) 
+{
+  LttvAttributeValue v;
+
+  lttv_attribute_find(tcs->parent.t_a, LTTV_STATE_SAVED_STATES_TIME, 
+        LTTV_POINTER, &v);
+  g_assert(*(v.v_pointer) != NULL);
+  tcs->max_time_state_recomputed_in_seek = (LttTime *)*(v.v_pointer);
+}
+
+
+static void 
+free_max_time(LttvTraceState *tcs) 
+{
+  LttvAttributeValue v;
+
+  lttv_attribute_find(tcs->parent.t_a, LTTV_STATE_SAVED_STATES_TIME, 
+        LTTV_POINTER, &v);
+  g_free(*(v.v_pointer));
+  *(v.v_pointer) = NULL;
+}
+
+
+typedef struct _LttvNameTables {
+  GQuark *eventtype_names;
+  GQuark *syscall_names;
+  GQuark *trap_names;
+  GQuark *irq_names;
+} LttvNameTables;
+
+
+static void 
+create_name_tables(LttvTraceState *tcs) 
 {
   int i, nb;
 
@@ -408,14 +588,23 @@ fill_name_tables(LttvTraceState *tcs)
 
   GString *fe_name = g_string_new("");
 
+  LttvNameTables *name_tables = g_new(LttvNameTables, 1);
+
+  LttvAttributeValue v;
+
+  lttv_attribute_find(tcs->parent.t_a, LTTV_STATE_NAME_TABLES, 
+      LTTV_POINTER, &v);
+  g_assert(*(v.v_pointer) == NULL);
+  *(v.v_pointer) = name_tables;
+
   nb = ltt_trace_eventtype_number(tcs->parent.t);
-  tcs->eventtype_names = g_new(GQuark, nb);
+  name_tables->eventtype_names = g_new(GQuark, nb);
   for(i = 0 ; i < nb ; i++) {
     et = ltt_trace_eventtype_get(tcs->parent.t, i);
     e_name = ltt_eventtype_name(et);
     f_name = ltt_facility_name(ltt_eventtype_facility(et));
     g_string_printf(fe_name, "%s.%s", f_name, e_name);
-    tcs->eventtype_names[i] = g_quark_from_string(fe_name->str);    
+    name_tables->eventtype_names[i] = g_quark_from_string(fe_name->str);    
   }
 
   lttv_trace_find_hook(tcs->parent.t, "core", "syscall_entry",
@@ -424,17 +613,18 @@ fill_name_tables(LttvTraceState *tcs)
   nb = ltt_type_element_number(t);
 
   /* CHECK syscalls should be an emun but currently are not!  
-  tcs->syscall_names = g_new(GQuark, nb);
+  name_tables->syscall_names = g_new(GQuark, nb);
 
   for(i = 0 ; i < nb ; i++) {
-    tcs->syscall_names[i] = g_quark_from_string(ltt_enum_string_get(t, i));
+    name_tables->syscall_names[i] = g_quark_from_string(
+        ltt_enum_string_get(t, i));
   }
   */
 
-  tcs->syscall_names = g_new(GQuark, 256);
+  name_tables->syscall_names = g_new(GQuark, 256);
   for(i = 0 ; i < 256 ; i++) {
     g_string_printf(fe_name, "syscall %d", i);
-    tcs->syscall_names[i] = g_quark_from_string(fe_name->str);
+    name_tables->syscall_names[i] = g_quark_from_string(fe_name->str);
   }
 
   lttv_trace_find_hook(tcs->parent.t, "core", "trap_entry",
@@ -443,16 +633,17 @@ fill_name_tables(LttvTraceState *tcs)
   nb = ltt_type_element_number(t);
 
   /*
-  tcs->trap_names = g_new(GQuark, nb);
+  name_tables->trap_names = g_new(GQuark, nb);
   for(i = 0 ; i < nb ; i++) {
-    tcs->trap_names[i] = g_quark_from_string(ltt_enum_string_get(t, i));
+    name_tables->trap_names[i] = g_quark_from_string(
+        ltt_enum_string_get(t, i));
   }
   */
 
-  tcs->trap_names = g_new(GQuark, 256);
+  name_tables->trap_names = g_new(GQuark, 256);
   for(i = 0 ; i < 256 ; i++) {
     g_string_printf(fe_name, "trap %d", i);
-    tcs->trap_names[i] = g_quark_from_string(fe_name->str);
+    name_tables->trap_names[i] = g_quark_from_string(fe_name->str);
   }
 
   lttv_trace_find_hook(tcs->parent.t, "core", "irq_entry",
@@ -461,29 +652,57 @@ fill_name_tables(LttvTraceState *tcs)
   nb = ltt_type_element_number(t);
 
   /*
-  tcs->irq_names = g_new(GQuark, nb);
+  name_tables->irq_names = g_new(GQuark, nb);
   for(i = 0 ; i < nb ; i++) {
-    tcs->irq_names[i] = g_quark_from_string(ltt_enum_string_get(t, i));
+    name_tables->irq_names[i] = g_quark_from_string(ltt_enum_string_get(t, i));
   }
   */
 
-  tcs->irq_names = g_new(GQuark, 256);
+  name_tables->irq_names = g_new(GQuark, 256);
   for(i = 0 ; i < 256 ; i++) {
     g_string_printf(fe_name, "irq %d", i);
-    tcs->irq_names[i] = g_quark_from_string(fe_name->str);
+    name_tables->irq_names[i] = g_quark_from_string(fe_name->str);
   }
 
   g_string_free(fe_name, TRUE);
 }
 
 
+static void 
+get_name_tables(LttvTraceState *tcs) 
+{
+  LttvNameTables *name_tables;
+
+  LttvAttributeValue v;
+
+  lttv_attribute_find(tcs->parent.t_a, LTTV_STATE_NAME_TABLES, 
+        LTTV_POINTER, &v);
+  g_assert(*(v.v_pointer) != NULL);
+  name_tables = (LttvNameTables *)*(v.v_pointer);
+  tcs->eventtype_names = name_tables->eventtype_names;
+  tcs->syscall_names = name_tables->syscall_names;
+  tcs->trap_names = name_tables->trap_names;
+  tcs->irq_names = name_tables->irq_names;
+}
+
+
 static void 
 free_name_tables(LttvTraceState *tcs) 
 {
-  g_free(tcs->eventtype_names);
-  g_free(tcs->syscall_names);
-  g_free(tcs->trap_names);
-  g_free(tcs->irq_names);
+  LttvNameTables *name_tables;
+
+  LttvAttributeValue v;
+
+  lttv_attribute_find(tcs->parent.t_a, LTTV_STATE_NAME_TABLES, 
+        LTTV_POINTER, &v);
+  name_tables = (LttvNameTables *)*(v.v_pointer);
+  *(v.v_pointer) = NULL;
+
+  g_free(name_tables->eventtype_names);
+  g_free(name_tables->syscall_names);
+  g_free(name_tables->trap_names);
+  g_free(name_tables->irq_names);
+  g_free(name_tables);
 } 
 
 
@@ -510,15 +729,15 @@ static void pop_state(LttvTracefileState *tfs, LttvExecutionMode t)
 {
   LttvProcessState *process = tfs->process;
 
-  guint depth = process->execution_stack->len - 1;
+  guint depth = process->execution_stack->len;
 
   if(process->state->t != t){
-    g_warning("Different execution mode type (%d.%09d): ignore it\n",
+    g_info("Different execution mode type (%d.%09d): ignore it\n",
         tfs->parent.timestamp.tv_sec, tfs->parent.timestamp.tv_nsec);
-    g_warning("process state has %s when pop_int is %s\n",
+    g_info("process state has %s when pop_int is %s\n",
                    g_quark_to_string(process->state->t),
                    g_quark_to_string(t));
-    g_warning("{ %u, %u, %s, %s }\n",
+    g_info("{ %u, %u, %s, %s }\n",
                    process->pid,
                    process->ppid,
                    g_quark_to_string(process->name),
@@ -526,22 +745,22 @@ static void pop_state(LttvTracefileState *tfs, LttvExecutionMode t)
     return;
   }
 
-  if(depth == 0){
-    g_warning("Trying to pop last state on stack (%d.%09d): ignore it\n",
+  if(depth == 1){
+    g_info("Trying to pop last state on stack (%d.%09d): ignore it\n",
         tfs->parent.timestamp.tv_sec, tfs->parent.timestamp.tv_nsec);
     return;
   }
 
-  g_array_remove_index(process->execution_stack, depth);
-  depth--;
+  g_array_set_size(process->execution_stack, depth - 1);
   process->state = &g_array_index(process->execution_stack, LttvExecutionState,
-      depth);
+      depth - 2);
   process->state->change = tfs->parent.timestamp;
 }
 
 
-static LttvProcessState *create_process(LttvTracefileState *tfs, 
-    LttvProcessState *parent, guint pid)
+LttvProcessState *
+lttv_state_create_process(LttvTracefileState *tfs, LttvProcessState *parent, 
+    guint pid)
 {
   LttvProcessState *process = g_new(LttvProcessState, 1);
 
@@ -553,24 +772,34 @@ static LttvProcessState *create_process(LttvTracefileState *tfs,
 
   char buffer[128];
 
-  tcs = (LttvTraceState *)tc = tfs->parent.t_context;
+  tcs = ((LttvTraceState *)tc = tfs->parent.t_context);
        
-  g_hash_table_insert(tcs->processes, GUINT_TO_POINTER(pid), process);
   process->pid = pid;
+  process->last_cpu = tfs->cpu_name;
+  g_warning("Process %u, core %p", process->pid, process);
+  g_hash_table_insert(tcs->processes, process, process);
 
   if(parent) {
     process->ppid = parent->pid;
     process->name = parent->name;
+    process->creation_time = tfs->parent.timestamp;
   }
+
+  /* No parent. This process exists but we are missing all information about
+     its creation. The birth time is set to zero but we remember the time of
+     insertion */
+
   else {
     process->ppid = 0;
     process->name = LTTV_STATE_UNNAMED;
+    process->creation_time = ltt_time_zero;
   }
 
-  process->creation_time = tfs->parent.timestamp;
+  process->insertion_time = tfs->parent.timestamp;
   sprintf(buffer,"%d-%lu.%lu",pid, process->creation_time.tv_sec, 
          process->creation_time.tv_nsec);
   process->pid_time = g_quark_from_string(buffer);
+  process->last_cpu = tfs->cpu_name;
   process->execution_stack = g_array_new(FALSE, FALSE, 
       sizeof(LttvExecutionState));
   g_array_set_size(process->execution_stack, 1);
@@ -585,35 +814,46 @@ static LttvProcessState *create_process(LttvTracefileState *tfs,
   return process;
 }
 
-LttvProcessState *lttv_state_find_process(LttvTracefileState *tfs, 
-    guint pid)
+
+LttvProcessState *
+lttv_state_find_process_from_trace(LttvTraceState *ts, GQuark cpu, guint pid)
 {
-  LttvTraceState *ts =(LttvTraceState *)LTTV_TRACEFILE_CONTEXT(tfs)->t_context;
-  LttvProcessState *process = g_hash_table_lookup(ts->processes, 
-      GUINT_TO_POINTER(pid));
-  if(process == NULL) process = create_process(tfs, NULL, pid);
+  LttvProcessState key;
+  LttvProcessState *process;
+
+  key.pid = pid;
+  key.last_cpu = cpu;
+  process = g_hash_table_lookup(ts->processes, &key);
   return process;
 }
 
-LttvProcessState *lttv_state_find_process_from_trace(LttvTraceState *ts, 
+
+LttvProcessState *lttv_state_find_process(LttvTracefileState *tfs, 
     guint pid)
 {
-  LttvProcessState *process = g_hash_table_lookup(ts->processes, 
-      GUINT_TO_POINTER(pid));
-  //We do not create a process at this level, because we can be called
-  //from outside of state.c, and therefore cannot assume a tracefile
-  //exists.
-  //if(process == NULL) process = create_process_from_trace(ts, NULL, pid);
-  return process;
+  LttvTraceState *ts =(LttvTraceState *)tfs->parent.t_context;
+  return lttv_state_find_process_from_trace(ts, tfs->cpu_name, pid);
 }
 
 
+LttvProcessState *
+lttv_state_find_process_or_create(LttvTracefileState *tfs, guint pid)
+{
+  LttvProcessState *process = lttv_state_find_process(tfs, pid);
+
+  if(process == NULL) process = lttv_state_create_process(tfs, NULL, pid);
+  return process;
+}
+
 
 static void exit_process(LttvTracefileState *tfs, LttvProcessState *process) 
 {
   LttvTraceState *ts = LTTV_TRACE_STATE(tfs->parent.t_context);
+  LttvProcessState key;
 
-  g_hash_table_remove(ts->processes, GUINT_TO_POINTER(process->pid));
+  key.pid = process->pid;
+  key.last_cpu = process->last_cpu;
+  g_hash_table_remove(ts->processes, &key);
   g_array_free(process->execution_stack, TRUE);
   g_free(process);
 }
@@ -721,18 +961,26 @@ static gboolean schedchange(void *hook_data, void *call_data)
 
   if(s->process != NULL) {
 
+    /* We could not know but it was not the idle process executing.
+       This should only happen at the beginning, before the first schedule
+       event, and when the initial information (current process for each CPU)
+       is missing. It is not obvious how we could, after the fact, compensate
+       the wrongly attributed statistics. */
+
+    if(s->process->pid != pid_out) {
+      g_assert(s->process->pid == 0);
+    }
+
     if(state_out == 0) s->process->state->s = LTTV_STATE_WAIT_CPU;
     else if(s->process->state->s == LTTV_STATE_EXIT) 
         exit_process(s, s->process);
     else s->process->state->s = LTTV_STATE_WAIT;
 
-    if(s->process->pid == 0)
-      s->process->pid = pid_out;
-
     s->process->state->change = s->parent.timestamp;
   }
-  s->process = lttv_state_find_process(s, pid_in);
+  s->process = lttv_state_find_process_or_create(s, pid_in);
   s->process->state->s = LTTV_STATE_RUN;
+  s->process->last_cpu = s->cpu_name;
   s->process->state->change = s->parent.timestamp;
   return FALSE;
 }
@@ -747,7 +995,7 @@ static gboolean process_fork(void *hook_data, void *call_data)
   guint child_pid;
 
   child_pid = ltt_event_get_unsigned(s->parent.e, f);
-  create_process(s, s->process, child_pid);
+  lttv_state_create_process(s, s->process, child_pid);
   return FALSE;
 }
 
@@ -767,7 +1015,7 @@ void lttv_state_add_event_hooks(LttvTracesetState *self)
 {
   LttvTraceset *traceset = self->parent.ts;
 
-  guint i, j, k, nb_trace, nb_control, nb_per_cpu, nb_tracefile;
+  guint i, j, k, nb_trace, nb_tracefile;
 
   LttvTraceState *ts;
 
@@ -818,16 +1066,11 @@ void lttv_state_add_event_hooks(LttvTracesetState *self)
 
     /* Add these hooks to each before_event_by_id hooks list */
 
-    nb_control = ltt_trace_control_tracefile_number(ts->parent.t);
-    nb_per_cpu = ltt_trace_per_cpu_tracefile_number(ts->parent.t);
-    nb_tracefile = nb_control + nb_per_cpu;
+    nb_tracefile = ltt_trace_control_tracefile_number(ts->parent.t) +
+        ltt_trace_per_cpu_tracefile_number(ts->parent.t);
+
     for(j = 0 ; j < nb_tracefile ; j++) {
-      if(j < nb_control) {
-        tfs = LTTV_TRACEFILE_STATE(ts->parent.control_tracefiles[j]);
-      }
-      else {
-        tfs = LTTV_TRACEFILE_STATE(ts->parent.per_cpu_tracefiles[j-nb_control]);
-      }
+      tfs = LTTV_TRACEFILE_STATE(ts->parent.tracefiles[j]);
 
       for(k = 0 ; k < hooks->len ; k++) {
         hook = g_array_index(hooks, LttvTraceHook, k);
@@ -845,7 +1088,7 @@ void lttv_state_remove_event_hooks(LttvTracesetState *self)
 {
   LttvTraceset *traceset = self->parent.ts;
 
-  guint i, j, k, nb_trace, nb_control, nb_per_cpu, nb_tracefile;
+  guint i, j, k, nb_trace, nb_tracefile;
 
   LttvTraceState *ts;
 
@@ -865,16 +1108,11 @@ void lttv_state_remove_event_hooks(LttvTracesetState *self)
 
     /* Add these hooks to each before_event_by_id hooks list */
 
-    nb_control = ltt_trace_control_tracefile_number(ts->parent.t);
-    nb_per_cpu = ltt_trace_per_cpu_tracefile_number(ts->parent.t);
-    nb_tracefile = nb_control + nb_per_cpu;
+    nb_tracefile = ltt_trace_control_tracefile_number(ts->parent.t) +
+        ltt_trace_per_cpu_tracefile_number(ts->parent.t);
+
     for(j = 0 ; j < nb_tracefile ; j++) {
-      if(j < nb_control) {
-        tfs = LTTV_TRACEFILE_STATE(ts->parent.control_tracefiles[j]);
-      }
-      else {
-        tfs = LTTV_TRACEFILE_STATE(ts->parent.per_cpu_tracefiles[j-nb_control]);
-      }
+      tfs = LTTV_TRACEFILE_STATE(ts->parent.tracefiles[j]);
 
       for(k = 0 ; k < hooks->len ; k++) {
         hook = g_array_index(hooks, LttvTraceHook, k);
@@ -888,15 +1126,17 @@ void lttv_state_remove_event_hooks(LttvTracesetState *self)
 }
 
 
-static gboolean block_end(void *hook_data, void *call_data)
+static gboolean block_start(void *hook_data, void *call_data)
 {
-  LttvTracefileState *tfcs = (LttvTracefileState *)call_data;
+  LttvTracefileState *self = (LttvTracefileState *)call_data;
+
+  LttvTracefileState *tfcs;
 
-  LttvTraceState *tcs = (LttvTraceState *)(tfcs->parent.t_context);
+  LttvTraceState *tcs = (LttvTraceState *)(self->parent.t_context);
 
-  LttEventPosition *ep = ltt_event_position_new();
+  LttEventPosition *ep;
 
-  guint nb_block, nb_event;
+  guint i, nb_block, nb_event, nb_tracefile;
 
   LttTracefile *tf;
 
@@ -904,11 +1144,22 @@ static gboolean block_end(void *hook_data, void *call_data)
 
   LttvAttributeValue value;
 
-  ltt_event_position(tfcs->parent.e, ep);
+  ep = ltt_event_position_new();
+  nb_tracefile = ltt_trace_control_tracefile_number(tcs->parent.t) +
+      ltt_trace_per_cpu_tracefile_number(tcs->parent.t);
+
+  /* Count the number of events added since the last block end in any
+     tracefile. */
+
+  for(i = 0 ; i < nb_tracefile ; i++) {
+    tfcs = (LttvTracefileState *)tcs->parent.tracefiles[i];
+    ltt_event_position(tfcs->parent.e, ep);
+    ltt_event_position_get(ep, &nb_block, &nb_event, &tf);
+    tcs->nb_event += nb_event - tfcs->saved_position;
+    tfcs->saved_position = nb_event;
+  }
+  g_free(ep);
 
-  ltt_event_position_get(ep, &nb_block, &nb_event, &tf);
-  tcs->nb_event += nb_event - tfcs->saved_position;
-  tfcs->saved_position = 0;
   if(tcs->nb_event >= tcs->save_interval) {
     saved_states_tree = lttv_attribute_find_subdir(tcs->parent.t_a, 
         LTTV_STATE_SAVED_STATES);
@@ -917,45 +1168,68 @@ static gboolean block_end(void *hook_data, void *call_data)
         lttv_attribute_get_number(saved_states_tree), LTTV_GOBJECT);
     *(value.v_gobject) = (GObject *)saved_state_tree;
     value = lttv_attribute_add(saved_state_tree, LTTV_STATE_TIME, LTTV_TIME);
-    *(value.v_time) = tfcs->parent.timestamp;
+    *(value.v_time) = self->parent.timestamp;
     lttv_state_save(tcs, saved_state_tree);
     tcs->nb_event = 0;
+    g_debug("Saving state at time %lu.%lu", self->parent.timestamp.tv_sec,
+           self->parent.timestamp.tv_nsec);
   }
+  *(tcs->max_time_state_recomputed_in_seek) = self->parent.timestamp;
   return FALSE;
 }
 
 
+static gboolean block_end(void *hook_data, void *call_data)
+{
+  LttvTracefileState *self = (LttvTracefileState *)call_data;
+
+  LttvTraceState *tcs = (LttvTraceState *)(self->parent.t_context);
+
+  LttTracefile *tf;
+
+  LttEventPosition *ep;
+
+  guint nb_block, nb_event;
+
+  ep = ltt_event_position_new();
+  ltt_event_position(self->parent.e, ep);
+  ltt_event_position_get(ep, &nb_block, &nb_event, &tf);
+  tcs->nb_event += nb_event - self->saved_position + 1;
+  self->saved_position = 0;
+  *(tcs->max_time_state_recomputed_in_seek) = self->parent.timestamp;
+  g_free(ep);
+}
+
+
 void lttv_state_save_add_event_hooks(LttvTracesetState *self)
 {
   LttvTraceset *traceset = self->parent.ts;
 
-  guint i, j, k, nb_trace, nb_control, nb_per_cpu, nb_tracefile;
+  guint i, j, k, nb_trace, nb_tracefile;
 
   LttvTraceState *ts;
 
   LttvTracefileState *tfs;
 
-  LttvTraceHook hook;
+  LttvTraceHook hook_start, hook_end;
 
   nb_trace = lttv_traceset_number(traceset);
   for(i = 0 ; i < nb_trace ; i++) {
     ts = (LttvTraceState *)self->parent.traces[i];
+    lttv_trace_find_hook(ts->parent.t, "core","block_start",NULL, 
+       NULL, NULL, block_start, &hook_start);
     lttv_trace_find_hook(ts->parent.t, "core","block_end",NULL, 
-       NULL, NULL, block_end, &hook);
+       NULL, NULL, block_end, &hook_end);
 
-    nb_control = ltt_trace_control_tracefile_number(ts->parent.t);
-    nb_per_cpu = ltt_trace_per_cpu_tracefile_number(ts->parent.t);
-    nb_tracefile = nb_control + nb_per_cpu;
-    for(j = 0 ; j < nb_tracefile ; j++) {
-      if(j < nb_control) {
-        tfs = LTTV_TRACEFILE_STATE(ts->parent.control_tracefiles[j]);
-      }
-      else {
-        tfs =LTTV_TRACEFILE_STATE(ts->parent.per_cpu_tracefiles[j-nb_control]);
-      }
+    nb_tracefile = ltt_trace_control_tracefile_number(ts->parent.t) +
+        ltt_trace_per_cpu_tracefile_number(ts->parent.t);
 
+    for(j = 0 ; j < nb_tracefile ; j++) {
+      tfs = LTTV_TRACEFILE_STATE(ts->parent.tracefiles[j]);
+      lttv_hooks_add(lttv_hooks_by_id_find(tfs->parent.after_event_by_id, 
+         hook_start.id), hook_start.h, NULL);
       lttv_hooks_add(lttv_hooks_by_id_find(tfs->parent.after_event_by_id, 
-         hook.id), hook.h, NULL);
+         hook_end.id), hook_end.h, NULL);
     }
   }
 }
@@ -965,33 +1239,32 @@ void lttv_state_save_remove_event_hooks(LttvTracesetState *self)
 {
   LttvTraceset *traceset = self->parent.ts;
 
-  guint i, j, k, nb_trace, nb_control, nb_per_cpu, nb_tracefile;
+  guint i, j, k, nb_trace, nb_tracefile;
 
   LttvTraceState *ts;
 
   LttvTracefileState *tfs;
 
-  LttvTraceHook hook;
+  LttvTraceHook hook_start, hook_end;
 
   nb_trace = lttv_traceset_number(traceset);
   for(i = 0 ; i < nb_trace ; i++) {
     ts = LTTV_TRACE_STATE(self->parent.traces[i]);
+    lttv_trace_find_hook(ts->parent.t, "core","block_start",NULL, 
+       NULL, NULL, block_start, &hook_start);
+
     lttv_trace_find_hook(ts->parent.t, "core","block_end",NULL, 
-       NULL, NULL, block_end, &hook);
+       NULL, NULL, block_end, &hook_end);
 
-    nb_control = ltt_trace_control_tracefile_number(ts->parent.t);
-    nb_per_cpu = ltt_trace_per_cpu_tracefile_number(ts->parent.t);
-    nb_tracefile = nb_control + nb_per_cpu;
-    for(j = 0 ; j < nb_tracefile ; j++) {
-      if(j < nb_control) {
-        tfs = LTTV_TRACEFILE_STATE(ts->parent.control_tracefiles[j]);
-      }
-      else {
-        tfs =LTTV_TRACEFILE_STATE(ts->parent.per_cpu_tracefiles[j-nb_control]);
-      }
+    nb_tracefile = ltt_trace_control_tracefile_number(ts->parent.t) +
+        ltt_trace_per_cpu_tracefile_number(ts->parent.t);
 
+    for(j = 0 ; j < nb_tracefile ; j++) {
+      tfs = LTTV_TRACEFILE_STATE(ts->parent.tracefiles[j]);
       lttv_hooks_remove_data(lttv_hooks_by_id_find(
-          tfs->parent.after_event_by_id, hook.id), hook.h, NULL);
+          tfs->parent.after_event_by_id, hook_start.id), hook_start.h, NULL);
+      lttv_hooks_remove_data(lttv_hooks_by_id_find(
+          tfs->parent.after_event_by_id, hook_end.id), hook_end.h, NULL);
     }
   }
 }
@@ -1019,11 +1292,12 @@ void lttv_state_traceset_seek_time_closest(LttvTracesetState *self, LttTime t)
   for(i = 0 ; i < nb_trace ; i++) {
     tcs = (LttvTraceState *)self->parent.traces[i];
 
-    if(tcs->recompute_state_in_seek) {
-      if(tcs->saved_state_available) {
-        saved_states_tree = lttv_attribute_find_subdir(tcs->parent.t_a,
-            LTTV_STATE_SAVED_STATES);
-        min_pos = -1;
+    if(ltt_time_compare(t, *(tcs->max_time_state_recomputed_in_seek)) < 0) {
+      saved_states_tree = lttv_attribute_find_subdir(tcs->parent.t_a,
+          LTTV_STATE_SAVED_STATES);
+      min_pos = -1;
+
+      if(saved_states_tree) {
         max_pos = lttv_attribute_get_number(saved_states_tree) - 1;
         mid_pos = max_pos / 2;
         while(min_pos < max_pos) {
@@ -1041,12 +1315,14 @@ void lttv_state_traceset_seek_time_closest(LttvTracesetState *self, LttTime t)
 
           mid_pos = (min_pos + max_pos + 1) / 2;
         }
+      }
 
-        /* restore the closest earlier saved state */
-        if(min_pos != -1) lttv_state_restore(tcs, closest_tree);
-
+      /* restore the closest earlier saved state */
+      if(min_pos != -1) {
+        lttv_state_restore(tcs, closest_tree);
       }
-      /* There is no saved state yet we want to have it. Restart at T0 */
+
+      /* There is no saved state, yet we want to have it. Restart at T0 */
       else {
         restore_init_state(tcs);
         lttv_process_trace_seek_time(&(tcs->parent), ltt_time_zero);
@@ -1101,7 +1377,7 @@ lttv_traceset_state_get_type(void)
       (GClassInitFunc) traceset_state_class_init,   /* class_init */
       NULL,   /* class_finalize */
       NULL,   /* class_data */
-      sizeof (LttvTracesetContext),
+      sizeof (LttvTracesetState),
       0,      /* n_preallocs */
       (GInstanceInitFunc) traceset_state_instance_init    /* instance_init */
     };
@@ -1210,7 +1486,7 @@ lttv_tracefile_state_get_type(void)
 }
 
 
-void lttv_state_init(int argc, char **argv)
+static void module_init()
 {
   LTTV_STATE_UNNAMED = g_quark_from_string("unnamed");
   LTTV_STATE_MODE_UNKNOWN = g_quark_from_string("unknown execution mode");
@@ -1230,14 +1506,22 @@ void lttv_state_init(int argc, char **argv)
   LTTV_STATE_PROCESS = g_quark_from_string("process");
   LTTV_STATE_EVENT = g_quark_from_string("event");
   LTTV_STATE_SAVED_STATES = g_quark_from_string("saved states");
+  LTTV_STATE_SAVED_STATES_TIME = g_quark_from_string("saved states time");
   LTTV_STATE_TIME = g_quark_from_string("time");
   LTTV_STATE_HOOKS = g_quark_from_string("saved state hooks");
+  LTTV_STATE_NAME_TABLES = g_quark_from_string("name tables");
+  LTTV_STATE_TRACE_STATE_USE_COUNT = 
+      g_quark_from_string("trace_state_use_count");
 }
 
-void lttv_state_destroy() 
+static void module_destroy() 
 {
 }
 
 
+LTTV_MODULE("state", "State computation", \
+    "Update the system state, possibly saving it at intervals", \
+    module_init, module_destroy)
+
 
 
This page took 0.037168 seconds and 4 git commands to generate.