add state saving and restoring for cpu resource
[lttv.git] / ltt / branches / poly / lttv / lttv / state.c
index ba27099de3fb9170fd52f10932acf69141f22482..ff5e27b59312083e53530b0cb2561d5a635fb2c7 100644 (file)
@@ -47,9 +47,8 @@
 GQuark
     LTT_FACILITY_KERNEL,
     LTT_FACILITY_KERNEL_ARCH,
-    LTT_FACILITY_PROCESS,
+    LTT_FACILITY_LIST,
     LTT_FACILITY_FS,
-    LTT_FACILITY_STATEDUMP,
     LTT_FACILITY_USER_GENERIC;
 
 /* Events Quarks */
@@ -63,13 +62,14 @@ GQuark
     LTT_EVENT_IRQ_EXIT,
     LTT_EVENT_SOFT_IRQ_ENTRY,
     LTT_EVENT_SOFT_IRQ_EXIT,
-    LTT_EVENT_SCHEDCHANGE,
-    LTT_EVENT_FORK,
-    LTT_EVENT_KERNEL_THREAD,
-    LTT_EVENT_EXIT,
-    LTT_EVENT_FREE,
+    LTT_EVENT_SCHED_SCHEDULE,
+    LTT_EVENT_PROCESS_FORK,
+    LTT_EVENT_KTHREAD_CREATE,
+    LTT_EVENT_PROCESS_EXIT,
+    LTT_EVENT_PROCESS_FREE,
     LTT_EVENT_EXEC,
-    LTT_EVENT_ENUM_PROCESS_STATE,
+    LTT_EVENT_PROCESS_STATE,
+    LTT_EVENT_STATEDUMP_END,
     LTT_EVENT_FUNCTION_ENTRY,
     LTT_EVENT_FUNCTION_EXIT,
     LTT_EVENT_THREAD_BRAND;
@@ -81,13 +81,14 @@ GQuark
     LTT_FIELD_TRAP_ID,
     LTT_FIELD_IRQ_ID,
     LTT_FIELD_SOFT_IRQ_ID,
-    LTT_FIELD_OUT,
-    LTT_FIELD_IN,
-    LTT_FIELD_OUT_STATE,
+    LTT_FIELD_PREV_PID,
+    LTT_FIELD_NEXT_PID,
+    LTT_FIELD_PREV_STATE,
     LTT_FIELD_PARENT_PID,
     LTT_FIELD_CHILD_PID,
     LTT_FIELD_PID,
     LTT_FIELD_TGID,
+    LTT_FIELD_CHILD_TGID,
     LTT_FIELD_FILENAME,
     LTT_FIELD_NAME,
     LTT_FIELD_TYPE,
@@ -111,7 +112,6 @@ LttvExecutionSubmode
 
 LttvProcessStatus
   LTTV_STATE_UNNAMED,
-  LTTV_STATE_UNBRANDED,
   LTTV_STATE_WAIT_FORK,
   LTTV_STATE_WAIT_CPU,
   LTTV_STATE_EXIT,
@@ -120,10 +120,25 @@ LttvProcessStatus
   LTTV_STATE_RUN,
   LTTV_STATE_DEAD;
 
+GQuark
+  LTTV_STATE_UNBRANDED;
+
 LttvProcessType
   LTTV_STATE_USER_THREAD,
   LTTV_STATE_KERNEL_THREAD;
 
+LttvCPUMode
+  LTTV_CPU_UNKNOWN,
+  LTTV_CPU_IDLE,
+  LTTV_CPU_BUSY,
+  LTTV_CPU_IRQ,
+  LTTV_CPU_TRAP;
+
+LttvIRQMode
+  LTTV_IRQ_UNKNOWN,
+  LTTV_IRQ_IDLE,
+  LTTV_IRQ_BUSY;
+
 static GQuark
   LTTV_STATE_TRACEFILES,
   LTTV_STATE_PROCESSES,
@@ -135,7 +150,8 @@ static GQuark
   LTTV_STATE_TIME,
   LTTV_STATE_HOOKS,
   LTTV_STATE_NAME_TABLES,
-  LTTV_STATE_TRACE_STATE_USE_COUNT;
+  LTTV_STATE_TRACE_STATE_USE_COUNT,
+  LTTV_STATE_RESOURCE_CPUS;
 
 static void create_max_time(LttvTraceState *tcs);
 
@@ -254,8 +270,9 @@ restore_init_state(LttvTraceState *self)
       &g_array_index(self->running_process[i]->execution_stack,
         LttvExecutionState, 0);
     es->t = LTTV_STATE_MODE_UNKNOWN;
+    es->s = LTTV_STATE_UNNAMED;
 
-    self->running_process[i]->state->s = LTTV_STATE_RUN;
+    //self->running_process[i]->state->s = LTTV_STATE_RUN;
     self->running_process[i]->cpu = i;
   }
   
@@ -372,7 +389,8 @@ end:
 static void
 init(LttvTracesetState *self, LttvTraceset *ts)
 {
-  guint i, j, nb_trace, nb_tracefile;
+  guint i, j, nb_trace, nb_tracefile, nb_cpu;
+  guint64 nb_irq;
 
   LttvTraceContext *tc;
 
@@ -402,10 +420,26 @@ init(LttvTracesetState *self, LttvTraceset *ts)
     get_max_time(tcs);
 
     nb_tracefile = tc->tracefiles->len;
+    nb_cpu = ltt_trace_get_num_cpu(tc->t);
+    nb_irq = tcs->nb_irqs;
     tcs->processes = NULL;
     tcs->usertraces = NULL;
-    tcs->running_process = g_new(LttvProcessState*, 
-                                 ltt_trace_get_num_cpu(tc->t));
+    tcs->running_process = g_new(LttvProcessState*, nb_cpu);
+
+    /* init cpu resource stuff */
+    tcs->cpu_states = g_new(LttvCPUState, nb_cpu);
+    for(j = 0; j<nb_cpu; j++) {
+      tcs->cpu_states[j].mode_stack = g_array_new(FALSE, FALSE, sizeof(LttvCPUMode));
+      g_assert(tcs->cpu_states[j].mode_stack != NULL);
+    } 
+
+    /* init irq resource stuff */
+    tcs->irq_states = g_new(LttvIRQState, nb_irq);
+    for(j = 0; j<nb_irq; j++) {
+      tcs->irq_states[j].mode_stack = g_array_new(FALSE, FALSE, sizeof(LttvIRQMode));
+      g_assert(tcs->irq_states[j].mode_stack != NULL);
+    } 
+
     restore_init_state(tcs);
     for(j = 0 ; j < nb_tracefile ; j++) {
       tfcs = 
@@ -413,6 +447,7 @@ init(LttvTracesetState *self, LttvTraceset *ts)
                                           LttvTracefileContext*, j));
       tfcs->tracefile_name = ltt_tracefile_name(tfcs->parent.tf);
       tfcs->cpu = ltt_tracefile_cpu(tfcs->parent.tf);
+      tfcs->cpu_state = &(tcs->cpu_states[tfcs->cpu]);
       if(ltt_tracefile_tid(tfcs->parent.tf) != 0) {
         /* It's a Usertrace */
         guint tid = ltt_tracefile_tid(tfcs->parent.tf);
@@ -1053,6 +1088,35 @@ static GHashTable *lttv_state_copy_process_table(GHashTable *processes)
   return new_processes;
 }
 
+static LttvCPUState *lttv_state_copy_cpu_states(LttvCPUState *states, guint n)
+{
+  guint i,j;
+  LttvCPUState *retval;
+
+  retval = g_malloc(n*sizeof(LttvCPUState));
+
+  for(i=0; i<n; i++) {
+    retval[i].mode_stack = g_array_new(FALSE, FALSE, sizeof(LttvCPUMode));
+    retval[i].last_irq = states[i].last_irq;
+    g_array_set_size(retval[i].mode_stack, states[i].mode_stack->len);
+    for(j=0; j<states[i].mode_stack->len; j++) {
+      g_array_index(retval[i].mode_stack, GQuark, j) = g_array_index(states[i].mode_stack, GQuark, j);
+    }
+  }
+
+  return retval;
+}
+
+static void lttv_state_free_cpu_states(LttvCPUState *states, guint n)
+{
+  guint i;
+
+  for(i=0; i<n; i++) {
+    g_array_free(states[i].mode_stack, FALSE);
+  }
+
+  g_free(states);
+}
 
 /* The saved state for each trace contains a member "processes", which
    stores a copy of the process table, and a member "tracefiles" with
@@ -1134,6 +1198,11 @@ static void state_save(LttvTraceState *self, LttvAttribute *container)
           tfcs->parent.timestamp.tv_sec, tfcs->parent.timestamp.tv_nsec);
     }
   }
+
+  value = lttv_attribute_add(container, LTTV_STATE_RESOURCE_CPUS,
+      LTTV_POINTER);
+  guint size = sizeof(LttvCPUState)*nb_cpus;
+  *(value.v_pointer) = lttv_state_copy_cpu_states(self->cpu_states, nb_cpus);
 }
 
 
@@ -1180,11 +1249,18 @@ static void state_restore(LttvTraceState *self, LttvAttribute *container)
     g_assert(self->running_process[i] != NULL);
   }
 
+  printf("state restore\n"); 
+
   nb_tracefile = self->parent.tracefiles->len;
 
   //g_tree_destroy(tsc->pqueue);
   //tsc->pqueue = g_tree_new(compare_tracefile);
+
+  /* restore cpu resource states */
+  type = lttv_attribute_get_by_name(container, LTTV_STATE_RESOURCE_CPUS, &value);
+  g_assert(type == LTTV_POINTER);
+  lttv_state_free_cpu_states(self->cpu_states, nb_cpus);
+  self->cpu_states = lttv_state_copy_cpu_states(*(value.v_pointer), nb_cpus);
  
   for(i = 0 ; i < nb_tracefile ; i++) {
     tfcs = 
@@ -1206,6 +1282,8 @@ static void state_restore(LttvTraceState *self, LttvAttribute *container)
     //g_assert(*(value.v_pointer) != NULL);
     ep = *(value.v_pointer);
     g_assert(tfcs->parent.t_context != NULL);
+
+    tfcs->cpu_state = &self->cpu_states[tfcs->cpu];
     
     LttvTracefileContext *tfc = LTTV_TRACEFILE_CONTEXT(tfcs);
     g_tree_remove(tsc->pqueue, tfc);
@@ -1354,7 +1432,9 @@ typedef struct _LttvNameTables {
   GQuark *trap_names;
   guint nb_traps;
   GQuark *irq_names;
+  guint nb_irqs;
   GQuark *soft_irq_names;
+  guint nb_softirqs;
 } LttvNameTables;
 
 
@@ -1412,6 +1492,12 @@ create_name_tables(LttvTraceState *tcs)
 
     for(i = 0 ; i < nb ; i++) {
       name_tables->syscall_names[i] = ltt_enum_string_get(t, i);
+      if(!name_tables->syscall_names[i]) {
+        GString *string = g_string_new("");
+        g_string_printf(string, "syscall %u", i);
+        name_tables->syscall_names[i] = g_quark_from_string(string->str);
+        g_string_free(string, TRUE);
+      }
     }
 
     //name_tables->syscall_names = g_new(GQuark, 256);
@@ -1424,7 +1510,7 @@ create_name_tables(LttvTraceState *tcs)
     name_tables->nb_syscalls = 0;
   }
 
-  if(!lttv_trace_find_hook(tcs->parent.t, LTT_FACILITY_KERNEL,
+  if(!lttv_trace_find_hook(tcs->parent.t, LTT_FACILITY_KERNEL_ARCH,
         LTT_EVENT_TRAP_ENTRY,
         LTT_FIELD_TRAP_ID, 0, 0,
         NULL, NULL, &h)) {
@@ -1473,12 +1559,14 @@ create_name_tables(LttvTraceState *tcs)
     }
     */
 
+    name_tables->nb_irqs = 256;
     name_tables->irq_names = g_new(GQuark, 256);
     for(i = 0 ; i < 256 ; i++) {
       g_string_printf(fe_name, "irq %d", i);
       name_tables->irq_names[i] = g_quark_from_string(fe_name->str);
     }
   } else {
+    name_tables->nb_irqs = 0;
     name_tables->irq_names = NULL;
   }
   /*
@@ -1488,6 +1576,7 @@ create_name_tables(LttvTraceState *tcs)
   }
   */
 
+  name_tables->nb_softirqs = 256;
   name_tables->soft_irq_names = g_new(GQuark, 256);
   for(i = 0 ; i < 256 ; i++) {
     g_string_printf(fe_name, "softirq %d", i);
@@ -1517,6 +1606,8 @@ get_name_tables(LttvTraceState *tcs)
   tcs->nb_traps = name_tables->nb_traps;
   tcs->irq_names = name_tables->irq_names;
   tcs->soft_irq_names = name_tables->soft_irq_names;
+  tcs->nb_irqs = name_tables->nb_irqs;
+  tcs->nb_softirqs = name_tables->nb_softirqs;
 }
 
 
@@ -1558,6 +1649,47 @@ static void hash_table_check(GHashTable *table)
 
 #endif
 
+/* clears the stack and sets the state passed as argument */
+static void cpu_set_base_mode(LttvCPUState *cpust, LttvCPUMode state)
+{
+  g_array_set_size(cpust->mode_stack, 1);
+  ((GQuark *)cpust->mode_stack->data)[0] = state;
+}
+
+static void cpu_push_mode(LttvCPUState *cpust, LttvCPUMode state)
+{
+  g_array_set_size(cpust->mode_stack, cpust->mode_stack->len + 1);
+  ((GQuark *)cpust->mode_stack->data)[cpust->mode_stack->len - 1] = state;
+}
+
+static void cpu_pop_mode(LttvCPUState *cpust)
+{
+  if(cpust->mode_stack->len == 1)
+    cpu_set_base_mode(cpust, LTTV_CPU_UNKNOWN);
+  else
+    g_array_set_size(cpust->mode_stack, cpust->mode_stack->len - 1);
+}
+
+/* clears the stack and sets the state passed as argument */
+static void irq_set_base_mode(LttvIRQState *irqst, LttvIRQMode state)
+{
+  g_array_set_size(irqst->mode_stack, 1);
+  ((GQuark *)irqst->mode_stack->data)[0] = state;
+}
+
+static void irq_push_mode(LttvIRQState *irqst, LttvIRQMode state)
+{
+  g_array_set_size(irqst->mode_stack, irqst->mode_stack->len + 1);
+  ((GQuark *)irqst->mode_stack->data)[irqst->mode_stack->len - 1] = state;
+}
+
+static void irq_pop_mode(LttvIRQState *irqst)
+{
+  if(irqst->mode_stack->len == 1)
+    irq_set_base_mode(irqst, LTTV_IRQ_UNKNOWN);
+  else
+    g_array_set_size(irqst->mode_stack, irqst->mode_stack->len - 1);
+}
 
 static void push_state(LttvTracefileState *tfs, LttvExecutionMode t, 
     guint state_id)
@@ -1802,8 +1934,12 @@ lttv_state_find_process_or_create(LttvTraceState *ts, guint cpu, guint pid,
                 NULL, cpu, pid, 0, LTTV_STATE_UNNAMED, timestamp);
     /* We are not sure is it's a kernel thread or normal thread, put the
       * bottom stack state to unknown */
-    es = &g_array_index(process->execution_stack, LttvExecutionState, 0);
+    process->execution_stack = 
+      g_array_set_size(process->execution_stack, 1);
+    process->state = es =
+      &g_array_index(process->execution_stack, LttvExecutionState, 0);
     es->t = LTTV_STATE_MODE_UNKNOWN;
+    es->s = LTTV_STATE_UNNAMED;
   }
   return process;
 }
@@ -1913,22 +2049,29 @@ static gboolean trap_entry(void *hook_data, void *call_data)
   }
 
   push_state(s, LTTV_STATE_TRAP, submode);
+
+  /* update cpu status */
+  cpu_push_mode(s->cpu_state, LTTV_CPU_TRAP);
+
   return FALSE;
 }
 
-
 static gboolean trap_exit(void *hook_data, void *call_data)
 {
   LttvTracefileState *s = (LttvTracefileState *)call_data;
 
   pop_state(s, LTTV_STATE_TRAP);
+
+  /* update cpu status */
+  cpu_pop_mode(s->cpu_state);
+
   return FALSE;
 }
 
-
 static gboolean irq_entry(void *hook_data, void *call_data)
 {
   LttvTracefileState *s = (LttvTracefileState *)call_data;
+  LttvTraceState *ts = (LttvTraceState *)s->parent.t_context;
   LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
   guint8 fac_id = ltt_event_facility_id(e);
   guint8 ev_id = ltt_event_eventtype_id(e);
@@ -1939,12 +2082,30 @@ static gboolean irq_entry(void *hook_data, void *call_data)
   LttField *f = thf->f1;
 
   LttvExecutionSubmode submode;
+  guint64 irq = ltt_event_get_long_unsigned(e, f);
+  guint64 nb_irqs = ((LttvTraceState *)(s->parent.t_context))->nb_irqs;
+  GString *string;
 
-  submode = ((LttvTraceState *)(s->parent.t_context))->irq_names[
-      ltt_event_get_unsigned(e, f)];
+  if(irq < nb_irqs) {
+    submode = ((LttvTraceState *)(s->parent.t_context))->irq_names[irq];
+  } else {
+    /* Fixup an incomplete irq table */
+    GString *string = g_string_new("");
+    g_string_printf(string, "irq %llu", irq);
+    submode = g_quark_from_string(string->str);
+    g_string_free(string, TRUE);
+  }
 
   /* Do something with the info about being in user or system mode when int? */
   push_state(s, LTTV_STATE_IRQ, submode);
+
+  /* update cpu status */
+  cpu_push_mode(s->cpu_state, LTTV_CPU_IRQ);
+
+  /* update irq status */
+  s->cpu_state->last_irq = irq;
+  irq_push_mode(&ts->irq_states[irq], LTTV_IRQ_BUSY);
+
   return FALSE;
 }
 
@@ -1961,8 +2122,16 @@ static gboolean soft_irq_exit(void *hook_data, void *call_data)
 static gboolean irq_exit(void *hook_data, void *call_data)
 {
   LttvTracefileState *s = (LttvTracefileState *)call_data;
+  LttvTraceState *ts = (LttvTraceState *)s->parent.t_context;
 
   pop_state(s, LTTV_STATE_IRQ);
+
+  /* update cpu status */
+  cpu_pop_mode(s->cpu_state);
+
+  /* update irq status */
+  irq_pop_mode(&ts->irq_states[s->cpu_state->last_irq]);
+
   return FALSE;
 }
 
@@ -1979,9 +2148,19 @@ static gboolean soft_irq_entry(void *hook_data, void *call_data)
   LttField *f = thf->f1;
 
   LttvExecutionSubmode submode;
+  guint64 softirq = ltt_event_get_long_unsigned(e, f);
+  guint64 nb_softirqs = ((LttvTraceState *)(s->parent.t_context))->nb_softirqs;
+  GString *string;
 
-  submode = ((LttvTraceState *)(s->parent.t_context))->soft_irq_names[
-      ltt_event_get_long_unsigned(e, f)];
+  if(softirq < nb_softirqs) {
+    submode = ((LttvTraceState *)(s->parent.t_context))->soft_irq_names[softirq];
+  } else {
+    /* Fixup an incomplete irq table */
+    GString *string = g_string_new("");
+    g_string_printf(string, "softirq %llu", softirq);
+    submode = g_quark_from_string(string->str);
+    g_string_free(string, TRUE);
+  }
 
   /* Do something with the info about being in user or system mode when int? */
   push_state(s, LTTV_STATE_SOFT_IRQ, submode);
@@ -2083,11 +2262,11 @@ static gboolean schedchange(void *hook_data, void *call_data)
   LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
   LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
   guint pid_in, pid_out;
-  gint state_out;
+  gint64 state_out;
 
   pid_out = ltt_event_get_unsigned(e, thf->f1);
   pid_in = ltt_event_get_unsigned(e, thf->f2);
-  state_out = ltt_event_get_int(e, thf->f3);
+  state_out = ltt_event_get_long_int(e, thf->f3);
   
   if(likely(process != NULL)) {
 
@@ -2103,24 +2282,31 @@ static gboolean schedchange(void *hook_data, void *call_data)
     //if(unlikely(process->pid != pid_out)) {
     //  g_assert(process->pid == 0);
     //}
-    if(process->pid == 0 && process->state->t == LTTV_STATE_MODE_UNKNOWN) {
-      /* Scheduling out of pid 0 at beginning of the trace :
-       * we know for sure it is in syscall mode at this point. */
-      g_assert(process->execution_stack->len == 1);
-      process->state->t = LTTV_STATE_SYSCALL;
-    }
-    if(unlikely(process->state->s == LTTV_STATE_EXIT)) {
-      process->state->s = LTTV_STATE_ZOMBIE;
-      process->state->change = s->parent.timestamp;
+    if(process->pid == 0
+      && process->state->t == LTTV_STATE_MODE_UNKNOWN) {
+      if(pid_out == 0) {
+        /* Scheduling out of pid 0 at beginning of the trace :
+         * we know for sure it is in syscall mode at this point. */
+        g_assert(process->execution_stack->len == 1);
+        process->state->t = LTTV_STATE_SYSCALL;
+        process->state->s = LTTV_STATE_WAIT;
+        process->state->change = s->parent.timestamp;
+        process->state->entry = s->parent.timestamp;
+      }
     } else {
-      if(unlikely(state_out == 0)) process->state->s = LTTV_STATE_WAIT_CPU;
-      else process->state->s = LTTV_STATE_WAIT;
-      process->state->change = s->parent.timestamp;
+      if(unlikely(process->state->s == LTTV_STATE_EXIT)) {
+        process->state->s = LTTV_STATE_ZOMBIE;
+        process->state->change = s->parent.timestamp;
+      } else {
+        if(unlikely(state_out == 0)) process->state->s = LTTV_STATE_WAIT_CPU;
+        else process->state->s = LTTV_STATE_WAIT;
+        process->state->change = s->parent.timestamp;
+      }
+      
+      if(state_out == 32 || state_out == 128)
+         exit_process(s, process); /* EXIT_DEAD || TASK_DEAD */
+            /* see sched.h for states */
     }
-    
-    if(state_out == 32)
-       exit_process(s, process); /* EXIT_DEAD */
-          /* see sched.h for states */
   }
   process = ts->running_process[cpu] =
               lttv_state_find_process_or_create(
@@ -2133,6 +2319,13 @@ static gboolean schedchange(void *hook_data, void *call_data)
     process->usertrace->cpu = cpu;
  // process->last_cpu_index = ltt_tracefile_num(((LttvTracefileContext*)s)->tf);
   process->state->change = s->parent.timestamp;
+
+  /* update cpu status */
+  if(pid_in == 0)
+    cpu_set_base_mode(s->cpu_state, LTTV_CPU_IDLE);
+  else
+    cpu_set_base_mode(s->cpu_state, LTTV_CPU_BUSY);
+
   return FALSE;
 }
 
@@ -2209,7 +2402,8 @@ static gboolean process_fork(void *hook_data, void *call_data)
   return FALSE;
 }
 
-/* We stamp a newly created process as kernel_thread */
+/* We stamp a newly created process as kernel_thread.
+ * The thread should not be running yet. */
 static gboolean process_kernel_thread(void *hook_data, void *call_data)
 {
   LttvTracefileState *s = (LttvTracefileState *)call_data;
@@ -2222,11 +2416,15 @@ static gboolean process_kernel_thread(void *hook_data, void *call_data)
   LttvExecutionState *es;
 
   /* PID */
-  pid = ltt_event_get_unsigned(e, thf->f1);
+  pid = (guint)ltt_event_get_long_unsigned(e, thf->f1);
   s->parent.target_pid = pid;
 
-  process = lttv_state_find_process(ts, ANY_CPU, pid);
-  es = &g_array_index(process->execution_stack, LttvExecutionState, 0);
+  process = lttv_state_find_process_or_create(ts, ANY_CPU, pid,
+               &ltt_time_zero);
+  process->execution_stack = 
+    g_array_set_size(process->execution_stack, 1);
+  es = process->state =
+    &g_array_index(process->execution_stack, LttvExecutionState, 0);
   es->t = LTTV_STATE_SYSCALL;
   process->type = LTTV_STATE_KERNEL_THREAD;
 
@@ -2314,6 +2512,7 @@ static gboolean process_exec(void *hook_data, void *call_data)
   guint cpu = s->cpu;
   LttvProcessState *process = ts->running_process[cpu];
 
+#if 0//how to use a sequence that must be transformed in a string
   /* PID of the process to release */
   guint64 name_len = ltt_event_field_element_number(e, thf->f1);
   //name = ltt_event_get_string(e, thf->f1);
@@ -2323,10 +2522,12 @@ static gboolean process_exec(void *hook_data, void *call_data)
   gchar *null_term_name = g_new(gchar, name_len+1);
   memcpy(null_term_name, name_begin, name_len);
   null_term_name[name_len] = '\0';
-
   process->name = g_quark_from_string(null_term_name);
+#endif //0
+
+  process->name = g_quark_from_string(ltt_event_get_string(e, thf->f1));
   process->brand = LTTV_STATE_UNBRANDED;
-  g_free(null_term_name);
+  //g_free(null_term_name);
   return FALSE;
 }
 
@@ -2346,6 +2547,74 @@ static gboolean thread_brand(void *hook_data, void *call_data)
   return FALSE;
 }
 
+static void fix_process(gpointer key, gpointer value,
+   gpointer user_data)
+{
+  LttvProcessState *process;
+  LttvExecutionState *es;
+  process = (LttvProcessState *)value;
+  LttvTracefileContext *tfc = (LttvTracefileContext *)user_data;
+  LttTime *timestamp = (LttTime*)user_data;
+
+  if(process->type == LTTV_STATE_KERNEL_THREAD) {
+    es = &g_array_index(process->execution_stack, LttvExecutionState, 0);
+    if(es->t == LTTV_STATE_MODE_UNKNOWN) {
+      es->t = LTTV_STATE_SYSCALL;
+      es->n = LTTV_STATE_SUBMODE_NONE;
+      es->entry = *timestamp;
+      es->change = *timestamp;
+      es->cum_cpu_time = ltt_time_zero;
+      if(es->s == LTTV_STATE_UNNAMED)
+        es->s = LTTV_STATE_WAIT;
+    }
+  } else {
+    es = &g_array_index(process->execution_stack, LttvExecutionState, 0);
+    if(es->t == LTTV_STATE_MODE_UNKNOWN) {
+      es->t = LTTV_STATE_USER_MODE;
+      es->n = LTTV_STATE_SUBMODE_NONE;
+      es->entry = *timestamp;
+      //g_assert(timestamp->tv_sec != 0);
+      es->change = *timestamp;
+      es->cum_cpu_time = ltt_time_zero;
+      if(es->s == LTTV_STATE_UNNAMED)
+        es->s = LTTV_STATE_RUN;
+
+      if(process->execution_stack->len == 1) {
+        /* Still in bottom unknown mode, means never did a system call
+        * May be either in user mode, syscall mode, running or waiting.*/
+       /* FIXME : we may be tagging syscall mode when being user mode */
+        process->execution_stack =
+          g_array_set_size(process->execution_stack, 2);
+        es = process->state = &g_array_index(process->execution_stack, 
+          LttvExecutionState, 1);
+        es->t = LTTV_STATE_SYSCALL;
+        es->n = LTTV_STATE_SUBMODE_NONE;
+        es->entry = *timestamp;
+        //g_assert(timestamp->tv_sec != 0);
+        es->change = *timestamp;
+        es->cum_cpu_time = ltt_time_zero;
+       if(es->s == LTTV_STATE_WAIT_FORK)
+          es->s = LTTV_STATE_WAIT;
+      }
+    }
+  }
+}
+
+static gboolean statedump_end(void *hook_data, void *call_data)
+{
+  LttvTracefileState *s = (LttvTracefileState *)call_data;
+  LttvTraceState *ts = (LttvTraceState*)s->parent.t_context;
+  LttvTracefileContext *tfc = (LttvTracefileContext *)call_data;
+  LttEvent *e = ltt_tracefile_get_event(s->parent.tf);
+  LttvTraceHookByFacility *thf = (LttvTraceHookByFacility *)hook_data;
+  
+  /* For all processes */
+    /* if kernel thread, if stack[0] is unknown, set to syscall mode, wait */
+    /* else, if stack[0] is unknown, set to user mode, running */
+
+  g_hash_table_foreach(ts->processes, fix_process, &tfc->timestamp);
+}
+
 static gboolean enum_process_state(void *hook_data, void *call_data)
 {
   LttvTracefileState *s = (LttvTracefileState *)call_data;
@@ -2365,6 +2634,7 @@ static gboolean enum_process_state(void *hook_data, void *call_data)
   LttField *f4, *f5, *f6, *f7, *f8;
   GQuark type, mode, submode, status;
   LttvExecutionState *es;
+  guint i, nb_cpus;
 
   /* PID */
   pid = ltt_event_get_unsigned(e, thf->f1);
@@ -2401,57 +2671,111 @@ static gboolean enum_process_state(void *hook_data, void *call_data)
   if(f8) tgid = ltt_event_get_unsigned(e, f8);
   else tgid = 0;
 
-  /* The process might exist if a process was forked while performing the state 
-   * dump. */
-  process = lttv_state_find_process(ts, ANY_CPU, pid);
-  if(process == NULL) {
-    parent_process = lttv_state_find_process(ts, ANY_CPU, parent_pid);
-    process = lttv_state_create_process(ts, parent_process, cpu,
-                              pid, tgid, g_quark_from_string(command),
-                              &s->parent.timestamp);
-  
-    /* Keep the stack bottom : a running user mode */
-    /* Disabled because of inconsistencies in the current statedump states. */
-    if(type == LTTV_STATE_KERNEL_THREAD) {
-      /* Only keep the bottom */
-      process->execution_stack = g_array_set_size(process->execution_stack, 1);
-      es = process->state = &g_array_index(process->execution_stack, 
-          LttvExecutionState, 0);
-      es->t = LTTV_STATE_SYSCALL;
-      es->s = status;
-      es->n = submode;
-    } else {
-      /* On top of it : */
-      es = process->state = &g_array_index(process->execution_stack, 
-          LttvExecutionState, 1);
-      es->t = LTTV_STATE_USER_MODE;
-      es->s = status;
-      es->n = submode;
+
+  if(pid == 0) {
+    nb_cpus = ltt_trace_get_num_cpu(ts->parent.t);
+    for(i=0; i<nb_cpus; i++) {
+      process = lttv_state_find_process(ts, i, pid);
+      g_assert(process != NULL);
+
+      process->ppid = parent_pid;
+      process->tgid = tgid;
+      process->name = g_quark_from_string(command);
+      es =
+        &g_array_index(process->execution_stack, LttvExecutionState, 0);
+      process->type = LTTV_STATE_KERNEL_THREAD;
     }
+
+  } else {
+    /* The process might exist if a process was forked while performing the
+     * state dump. */
+    process = lttv_state_find_process(ts, ANY_CPU, pid);
+    if(process == NULL) {
+      parent_process = lttv_state_find_process(ts, ANY_CPU, parent_pid);
+      process = lttv_state_create_process(ts, parent_process, cpu,
+                                pid, tgid, g_quark_from_string(command),
+                                &s->parent.timestamp);
+    
+      /* Keep the stack bottom : a running user mode */
+      /* Disabled because of inconsistencies in the current statedump states. */
+      if(type == LTTV_STATE_KERNEL_THREAD) {
+        /* Only keep the bottom 
+         * FIXME Kernel thread : can be in syscall or interrupt or trap. */
+        /* Will cause expected trap when in fact being syscall (even after end of
+         * statedump event)
+         * Will cause expected interrupt when being syscall. (only before end of
+         * statedump event) */
+        // This will cause a "popping last state on stack, ignoring it."
+        process->execution_stack = g_array_set_size(process->execution_stack, 1);
+        es = process->state = &g_array_index(process->execution_stack, 
+            LttvExecutionState, 0);
+        process->type = LTTV_STATE_KERNEL_THREAD;
+        es->t = LTTV_STATE_MODE_UNKNOWN;
+        es->s = LTTV_STATE_UNNAMED;
+        es->n = LTTV_STATE_SUBMODE_UNKNOWN;
+  #if 0
+        es->t = LTTV_STATE_SYSCALL;
+        es->s = status;
+        es->n = submode;
+  #endif //0
+      } else {
+        /* User space process :
+         * bottom : user mode
+         * either currently running or scheduled out.
+         * can be scheduled out because interrupted in (user mode or in syscall)
+         * or because of an explicit call to the scheduler in syscall. Note that
+         * the scheduler call comes after the irq_exit, so never in interrupt
+         * context. */
+        // temp workaround : set size to 1 : only have user mode bottom of stack.
+        // will cause g_info message of expected syscall mode when in fact being
+        // in user mode. Can also cause expected trap when in fact being user
+        // mode in the event of a page fault reenabling interrupts in the handler.
+        // Expected syscall and trap can also happen after the end of statedump
+        // This will cause a "popping last state on stack, ignoring it."
+        process->execution_stack = g_array_set_size(process->execution_stack, 1);
+        es = process->state = &g_array_index(process->execution_stack, 
+            LttvExecutionState, 0);
+        es->t = LTTV_STATE_MODE_UNKNOWN;
+        es->s = LTTV_STATE_UNNAMED;
+        es->n = LTTV_STATE_SUBMODE_UNKNOWN;
+  #if 0
+        es->t = LTTV_STATE_USER_MODE;
+        es->s = status;
+        es->n = submode;
+  #endif //0
+      }
+  #if 0
+      /* UNKNOWN STATE */
+      {
+        es = process->state = &g_array_index(process->execution_stack, 
+            LttvExecutionState, 1);
+        es->t = LTTV_STATE_MODE_UNKNOWN;
+        es->s = LTTV_STATE_UNNAMED;
+        es->n = LTTV_STATE_SUBMODE_UNKNOWN;
+      }
+  #endif //0
+    } else {
+      /* The process has already been created :
+       * Probably was forked while dumping the process state or
+       * was simply scheduled in prior to get the state dump event.
+       */
+      process->ppid = parent_pid;
+      process->tgid = tgid;
+      process->name = g_quark_from_string(command);
+      process->type = type;
+      es =
+        &g_array_index(process->execution_stack, LttvExecutionState, 0);
 #if 0
-    /* UNKNOWN STATE */
-    {
-      es = process->state = &g_array_index(process->execution_stack, 
-          LttvExecutionState, 1);
-      es->t = LTTV_STATE_MODE_UNKNOWN;
-      es->s = LTTV_STATE_UNNAMED;
-      es->n = LTTV_STATE_SUBMODE_UNKNOWN;
-    }
+      if(es->t == LTTV_STATE_MODE_UNKNOWN) {
+        if(type == LTTV_STATE_KERNEL_THREAD)
+          es->t = LTTV_STATE_SYSCALL;
+        else
+          es->t = LTTV_STATE_USER_MODE;
+      }
 #endif //0
-  } else {
-    /* The process has already been created :
-     * Probably was forked while dumping the process state or
-     * was simply scheduled in prior to get the state dump event.
-     * We know for sure if it is a user space thread.
-     */
-    process->ppid = parent_pid;
-    process->tgid = tgid;
-    process->name = g_quark_from_string(command);
-    es = &g_array_index(process->execution_stack, LttvExecutionState, 0);
-    if(type != LTTV_STATE_KERNEL_THREAD)
-      es->t = LTTV_STATE_USER_MODE;
-    /* Don't mess around with the stack, it will eventually become
-     * ok after the end of state dump. */
+      /* Don't mess around with the stack, it will eventually become
+       * ok after the end of state dump. */
+    }
   }
   
   return FALSE;
@@ -2494,8 +2818,8 @@ void lttv_state_add_event_hooks(LttvTracesetState *self)
     /* Find the eventtype id for the following events and register the
        associated by id hooks. */
 
-    hooks = g_array_sized_new(FALSE, FALSE, sizeof(LttvTraceHook), 18);
-    hooks = g_array_set_size(hooks, 18); // Max possible number of hooks.
+    hooks = g_array_sized_new(FALSE, FALSE, sizeof(LttvTraceHook), 19);
+    hooks = g_array_set_size(hooks, 19); // Max possible number of hooks.
     hn = 0;
 
     ret = lttv_trace_find_hook(ts->parent.t,
@@ -2511,13 +2835,13 @@ void lttv_state_add_event_hooks(LttvTracesetState *self)
     if(ret) hn--;
 
     ret = lttv_trace_find_hook(ts->parent.t,
-        LTT_FACILITY_KERNEL, LTT_EVENT_TRAP_ENTRY,
+        LTT_FACILITY_KERNEL_ARCH, LTT_EVENT_TRAP_ENTRY,
         LTT_FIELD_TRAP_ID, 0, 0,
         trap_entry, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
     if(ret) hn--;
 
     ret = lttv_trace_find_hook(ts->parent.t,
-        LTT_FACILITY_KERNEL, LTT_EVENT_TRAP_EXIT,
+        LTT_FACILITY_KERNEL_ARCH, LTT_EVENT_TRAP_EXIT,
         0, 0, 0, 
         trap_exit, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
     if(ret) hn--;
@@ -2547,32 +2871,32 @@ void lttv_state_add_event_hooks(LttvTracesetState *self)
     if(ret) hn--;
 
     ret = lttv_trace_find_hook(ts->parent.t,
-        LTT_FACILITY_PROCESS, LTT_EVENT_SCHEDCHANGE,
-        LTT_FIELD_OUT, LTT_FIELD_IN, LTT_FIELD_OUT_STATE,
+        LTT_FACILITY_KERNEL, LTT_EVENT_SCHED_SCHEDULE,
+        LTT_FIELD_PREV_PID, LTT_FIELD_NEXT_PID, LTT_FIELD_PREV_STATE,
         schedchange, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
     if(ret) hn--;
 
     ret = lttv_trace_find_hook(ts->parent.t,
-        LTT_FACILITY_PROCESS, LTT_EVENT_FORK,
-        LTT_FIELD_PARENT_PID, LTT_FIELD_CHILD_PID, LTT_FIELD_TGID,
+        LTT_FACILITY_KERNEL, LTT_EVENT_PROCESS_FORK,
+        LTT_FIELD_PARENT_PID, LTT_FIELD_CHILD_PID, LTT_FIELD_CHILD_TGID,
         process_fork, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
     if(ret) hn--;
 
     ret = lttv_trace_find_hook(ts->parent.t,
-        LTT_FACILITY_PROCESS, LTT_EVENT_KERNEL_THREAD,
+        LTT_FACILITY_KERNEL_ARCH, LTT_EVENT_KTHREAD_CREATE,
         LTT_FIELD_PID, 0, 0,
         process_kernel_thread, NULL, &g_array_index(hooks, LttvTraceHook,
           hn++));
     if(ret) hn--;
 
     ret = lttv_trace_find_hook(ts->parent.t,
-        LTT_FACILITY_PROCESS, LTT_EVENT_EXIT,
+        LTT_FACILITY_KERNEL, LTT_EVENT_PROCESS_EXIT,
         LTT_FIELD_PID, 0, 0,
         process_exit, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
     if(ret) hn--;
     
     ret = lttv_trace_find_hook(ts->parent.t,
-        LTT_FACILITY_PROCESS, LTT_EVENT_FREE,
+        LTT_FACILITY_KERNEL, LTT_EVENT_PROCESS_FREE,
         LTT_FIELD_PID, 0, 0,
         process_free, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
     if(ret) hn--;
@@ -2591,11 +2915,17 @@ void lttv_state_add_event_hooks(LttvTracesetState *self)
 
      /* statedump-related hooks */
     ret = lttv_trace_find_hook(ts->parent.t,
-        LTT_FACILITY_STATEDUMP, LTT_EVENT_ENUM_PROCESS_STATE,
+        LTT_FACILITY_LIST, LTT_EVENT_PROCESS_STATE,
         LTT_FIELD_PID, LTT_FIELD_PARENT_PID, LTT_FIELD_NAME,
         enum_process_state, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
     if(ret) hn--;
 
+    ret = lttv_trace_find_hook(ts->parent.t,
+        LTT_FACILITY_LIST, LTT_EVENT_STATEDUMP_END,
+        0, 0, 0,
+        statedump_end, NULL, &g_array_index(hooks, LttvTraceHook, hn++));
+    if(ret) hn--;
+
     ret = lttv_trace_find_hook(ts->parent.t,
         LTT_FACILITY_USER_GENERIC, LTT_EVENT_FUNCTION_ENTRY,
         LTT_FIELD_THIS_FN, LTT_FIELD_CALL_SITE, 0,
@@ -3277,13 +3607,13 @@ static void module_init()
   LTTV_STATE_NAME_TABLES = g_quark_from_string("name tables");
   LTTV_STATE_TRACE_STATE_USE_COUNT = 
       g_quark_from_string("trace_state_use_count");
+  LTTV_STATE_RESOURCE_CPUS = g_quark_from_string("cpu resource states");
 
   
   LTT_FACILITY_KERNEL     = g_quark_from_string("kernel");
   LTT_FACILITY_KERNEL_ARCH = g_quark_from_string("kernel_arch");
-  LTT_FACILITY_PROCESS    = g_quark_from_string("process");
   LTT_FACILITY_FS    = g_quark_from_string("fs");
-  LTT_FACILITY_STATEDUMP    = g_quark_from_string("statedump");
+  LTT_FACILITY_LIST = g_quark_from_string("list");
   LTT_FACILITY_USER_GENERIC    = g_quark_from_string("user_generic");
 
   
@@ -3295,13 +3625,14 @@ static void module_init()
   LTT_EVENT_IRQ_EXIT      = g_quark_from_string("irq_exit");
   LTT_EVENT_SOFT_IRQ_ENTRY     = g_quark_from_string("soft_irq_entry");
   LTT_EVENT_SOFT_IRQ_EXIT      = g_quark_from_string("soft_irq_exit");
-  LTT_EVENT_SCHEDCHANGE   = g_quark_from_string("schedchange");
-  LTT_EVENT_FORK          = g_quark_from_string("fork");
-  LTT_EVENT_KERNEL_THREAD = g_quark_from_string("kernel_thread");
-  LTT_EVENT_EXIT          = g_quark_from_string("exit");
-  LTT_EVENT_FREE          = g_quark_from_string("free");
+  LTT_EVENT_SCHED_SCHEDULE   = g_quark_from_string("sched_schedule");
+  LTT_EVENT_PROCESS_FORK          = g_quark_from_string("process_fork");
+  LTT_EVENT_KTHREAD_CREATE = g_quark_from_string("kthread_create");
+  LTT_EVENT_PROCESS_EXIT          = g_quark_from_string("process_exit");
+  LTT_EVENT_PROCESS_FREE          = g_quark_from_string("process_free");
   LTT_EVENT_EXEC          = g_quark_from_string("exec");
-  LTT_EVENT_ENUM_PROCESS_STATE  = g_quark_from_string("enumerate_process_state");
+  LTT_EVENT_PROCESS_STATE  = g_quark_from_string("process_state");
+  LTT_EVENT_STATEDUMP_END  = g_quark_from_string("statedump_end");
   LTT_EVENT_FUNCTION_ENTRY  = g_quark_from_string("function_entry");
   LTT_EVENT_FUNCTION_EXIT  = g_quark_from_string("function_exit");
   LTT_EVENT_THREAD_BRAND  = g_quark_from_string("thread_brand");
@@ -3311,13 +3642,14 @@ static void module_init()
   LTT_FIELD_TRAP_ID       = g_quark_from_string("trap_id");
   LTT_FIELD_IRQ_ID        = g_quark_from_string("irq_id");
   LTT_FIELD_SOFT_IRQ_ID        = g_quark_from_string("softirq_id");
-  LTT_FIELD_OUT           = g_quark_from_string("out");
-  LTT_FIELD_IN            = g_quark_from_string("in");
-  LTT_FIELD_OUT_STATE     = g_quark_from_string("out_state");
+  LTT_FIELD_PREV_PID            = g_quark_from_string("prev_pid");
+  LTT_FIELD_NEXT_PID           = g_quark_from_string("next_pid");
+  LTT_FIELD_PREV_STATE     = g_quark_from_string("prev_state");
   LTT_FIELD_PARENT_PID    = g_quark_from_string("parent_pid");
   LTT_FIELD_CHILD_PID     = g_quark_from_string("child_pid");
   LTT_FIELD_PID           = g_quark_from_string("pid");
   LTT_FIELD_TGID          = g_quark_from_string("tgid");
+  LTT_FIELD_CHILD_TGID    = g_quark_from_string("child_tgid");
   LTT_FIELD_FILENAME      = g_quark_from_string("filename");
   LTT_FIELD_NAME          = g_quark_from_string("name");
   LTT_FIELD_TYPE          = g_quark_from_string("type");
@@ -3327,6 +3659,15 @@ static void module_init()
   LTT_FIELD_THIS_FN       = g_quark_from_string("this_fn");
   LTT_FIELD_CALL_SITE     = g_quark_from_string("call_site");
   
+  LTTV_CPU_UNKNOWN = g_quark_from_string("unknown");
+  LTTV_CPU_IDLE = g_quark_from_string("idle");
+  LTTV_CPU_BUSY = g_quark_from_string("busy");
+  LTTV_CPU_IRQ = g_quark_from_string("irq");
+  LTTV_CPU_TRAP = g_quark_from_string("trap");
+
+  LTTV_IRQ_UNKNOWN = g_quark_from_string("unknown");
+  LTTV_IRQ_IDLE = g_quark_from_string("idle");
+  LTTV_IRQ_BUSY = g_quark_from_string("busy");
 }
 
 static void module_destroy() 
This page took 0.034376 seconds and 4 git commands to generate.