Add state saving functions and update processTrace accordingly.
[lttv.git] / ltt / branches / poly / lttv / textDump.c
index 189f9202515f61636858e7132b8d9ae409bb2931..dbdd253668466a1c31b3b6e142639cb8d3a6457f 100644 (file)
@@ -2,12 +2,27 @@
    before each trace, to print each event, and to print statistics
    after each trace. */
 
+#include <lttv/lttv.h>
+#include <lttv/option.h>
+#include <lttv/module.h>
+#include <lttv/hook.h>
+#include <lttv/attribute.h>
+#include <lttv/iattribute.h>
+#include <lttv/stats.h>
+#include <ltt/ltt.h>
+#include <ltt/event.h>
+#include <ltt/type.h>
+#include <ltt/trace.h>
+#include <stdio.h>
+
 static gboolean
   a_field_names,
-  a_state;
+  a_state,
+  a_cpu_stats,
+  a_process_stats;
 
 static char
-  *a_file_name;
+  *a_file_name = NULL;
 
 static LttvHooks
   *before_traceset,
@@ -16,67 +31,201 @@ static LttvHooks
   *before_event;
 
 
-void init(int argc, char **argv)
-{
-  LttvAttribute_value *value;
+void print_field(LttEvent *e, LttField *f, GString *s, gboolean field_names) {
 
-  LttvIAttributes *attributes = LTTV_IATTRIBUTES(lttv_global_attributes());
+  LttType *type;
 
-  a_file_name = NULL;
-  lttv_option_add("output", 'o', 
-      "output file where the text is written", 
-      "file name", 
-      LTTV_OPT_STRING, &a_file_name, NULL, NULL);
+  LttField *element;
 
-  a_field_names = FALSE;
-  lttv_option_add("field_names", 'l', 
-      "write the field names for each event", 
-      "", 
-      LTTV_OPT_NONE, &a_field_names, NULL, NULL);
+  char *name;
 
-  a_state = FALSE;
-  lttv_option_add("process_state", 's', 
-      "write the pid and state for each event", 
-      "", 
-      LTTV_OPT_NONE, &a_state, NULL, NULL);
+  int nb, i;
 
-  g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event/before",
-      LTTV_POINTER, &value));
-  g_assert((before_event = *(value->v_pointer)) != NULL);
-  lttv_hooks_add(before_event, write_event_content, NULL);
+  type = ltt_field_type(f);
+  switch(ltt_type_class(type)) {
+    case LTT_INT:
+      g_string_append_printf(s, " %ld", ltt_event_get_long_int(e,f));
+      break;
 
-  g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/before",
-      LTTV_POINTER, &value));
-  g_assert((before_trace = *(value->v_pointer)) != NULL);
-  lttv_hooks_add(before_trace, write_trace_header, NULL);
+    case LTT_UINT:
+      g_string_append_printf(s, " %lu", ltt_event_get_long_unsigned(e,f));
+      break;
 
-  g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/before",
-      LTTV_POINTER, &value));
-  g_assert((before_traceset = *(value->v_pointer)) != NULL);
-  lttv_hooks_add(before_traceset, write_traceset_header, NULL);
+    case LTT_FLOAT:
+      g_string_append_printf(s, " %g", ltt_event_get_double(e,f));
+      break;
 
-  g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/after",
-      LTTV_POINTER, &value));
-  g_assert((after_traceset = *(value->v_pointer)) != NULL);
-  lttv_hooks_add(after_traceset, write_traceset_footer, NULL);
+    case LTT_STRING:
+      g_string_append_printf(s, " \"%s\"", ltt_event_get_string(e,f));
+      break;
+
+    case LTT_ENUM:
+      g_string_append_printf(s, " %s", ltt_enum_string_get(type,
+          ltt_event_get_unsigned(e,f)-1));
+      break;
+
+    case LTT_ARRAY:
+    case LTT_SEQUENCE:
+      g_string_append_printf(s, " {");
+      nb = ltt_event_field_element_number(e,f);
+      element = ltt_field_element(f);
+      for(i = 0 ; i < nb ; i++) {
+        ltt_event_field_element_select(e,f,i);
+        print_field(e, element, s, field_names);
+      }
+      g_string_append_printf(s, " }");
+      break;
+
+    case LTT_STRUCT:
+      g_string_append_printf(s, " {");
+      nb = ltt_type_member_number(type);
+      for(i = 0 ; i < nb ; i++) {
+        element = ltt_field_member(f,i);
+        if(field_names) {
+          ltt_type_member_type(type, i, &name);
+          g_string_append_printf(s, " %s = ", name);
+        }
+        print_field(e, element, s, field_names);
+      }
+      g_string_append_printf(s, " }");
+      break;
+  }
 }
 
 
-void destroy()
+void lttv_event_to_string(LttEvent *e, LttTracefile *tf, GString *s,
+    gboolean mandatory_fields, gboolean field_names, LttvTracefileState *tfs)
+{ 
+  LttFacility *facility;
+
+  LttEventType *event_type;
+
+  LttType *type;
+
+  LttField *field;
+
+  LttTime time;
+
+  g_string_set_size(s,0);
+
+  facility = ltt_event_facility(e);
+  event_type = ltt_event_eventtype(e);
+  field = ltt_event_field(e);
+
+  if(mandatory_fields) {
+    time = ltt_event_time(e);
+    g_string_append_printf(s,"%s.%s: %ld.%09ld (%s)",
+        ltt_facility_name(facility),
+        ltt_eventtype_name(event_type), (long)time.tv_sec, time.tv_nsec,
+        g_quark_to_string(tfs->cpu_name));
+    /* Print the process id and the state/interrupt type of the process */
+    g_string_append_printf(s,", %u, %u,  %s", tfs->process->pid,
+                   tfs->process->ppid,
+                   g_quark_to_string(tfs->process->state->t));
+  }
+
+  if(field)
+    print_field(e, field, s, field_names);
+} 
+
+
+static void 
+print_tree(FILE *fp, GString *indent, LttvAttribute *tree)
 {
-  lttv_option_remove("output");
+  int i, nb, saved_length;
+
+  LttvAttribute *subtree;
+
+  LttvAttributeName name;
+
+  LttvAttributeValue value;
+
+  LttvAttributeType type;
+
+  nb = lttv_attribute_get_number(tree);
+  for(i = 0 ; i < nb ; i++) {
+    type = lttv_attribute_get(tree, i, &name, &value);
+    fprintf(fp, "%s%s: ", indent->str, g_quark_to_string(name));
+
+    switch(type) {
+      case LTTV_INT:
+        fprintf(fp, "%d\n", *value.v_int);
+        break;
+      case LTTV_UINT:
+        fprintf(fp, "%u\n", *value.v_uint);
+        break;
+      case LTTV_LONG:
+        fprintf(fp, "%ld\n", *value.v_long);
+        break;
+      case LTTV_ULONG:
+        fprintf(fp, "%lu\n", *value.v_ulong);
+        break;
+      case LTTV_FLOAT:
+        fprintf(fp, "%f\n", (double)*value.v_float);
+        break;
+      case LTTV_DOUBLE:
+        fprintf(fp, "%f\n", *value.v_double);
+        break;
+      case LTTV_TIME:
+        fprintf(fp, "%10u.%09u\n", value.v_time->tv_sec, 
+            value.v_time->tv_nsec);
+        break;
+      case LTTV_POINTER:
+        fprintf(fp, "POINTER\n");
+        break;
+      case LTTV_STRING:
+        fprintf(fp, "%s\n", *value.v_string);
+        break;
+      case LTTV_GOBJECT:
+        if(LTTV_IS_ATTRIBUTE(*(value.v_gobject))) {
+          fprintf(fp, "\n");
+          subtree = (LttvAttribute *)*(value.v_gobject);
+          saved_length = indent->len; 
+          g_string_append(indent, "  ");
+          print_tree(fp, indent, subtree);
+          g_string_truncate(indent, saved_length);
+        }
+        else fprintf(fp, "GOBJECT\n");
+        break;
+      case LTTV_NONE:
+        break;
+    }
+  }
+}
 
-  lttv_option_remove("field_names");
 
-  lttv_option_remove("process_state");
+static void
+print_stats(FILE *fp, LttvTracesetStats *tscs)
+{
+  int i, nb, saved_length;
+
+  LttvTraceset *ts;
+
+  LttvTraceStats *tcs;
+
+  GString *indent;
 
-  lttv_hooks_remove(before_event, write_event, NULL);
+  LttSystemDescription *desc;
 
-  lttv_hooks_remove(before_trace, write_trace_header, NULL);
+  if(tscs->stats == NULL) return;
+  indent = g_string_new("");
+  fprintf(fp, "Traceset statistics:\n\n");
+  print_tree(fp, indent, tscs->stats);
 
-  lttv_hooks_remove(before_trace, write_traceset_header, NULL);
+  ts = tscs->parent.parent.ts;
+  nb = lttv_traceset_number(ts);
 
-  lttv_hooks_remove(before_trace, write_traceset_footer, NULL);
+  for(i = 0 ; i < nb ; i++) {
+    tcs = (LttvTraceStats *)(LTTV_TRACESET_CONTEXT(tscs)->traces[i]);
+    desc = ltt_trace_system_description(tcs->parent.parent.t);
+    fprintf(fp, "Trace on system %s at time %d secs:\n", desc->node_name, 
+        desc->trace_start.tv_sec);
+    saved_length = indent->len;
+    g_string_append(indent, "  ");
+    print_tree(fp, indent, tcs->stats);
+    g_string_truncate(indent, saved_length);
+  }
+  g_string_free(indent, TRUE);
 }
 
 
@@ -87,10 +236,12 @@ static FILE *a_file;
 
 static GString *a_string;
 
-static static gboolean write_traceset_header(void *hook_data, void *call_data)
+static gboolean write_traceset_header(void *hook_data, void *call_data)
 {
   LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
 
+  g_info("TextDump traceset header");
+
   if(a_file_name == NULL) a_file = stdout;
   else a_file = fopen(a_file_name, "w");
 
@@ -98,20 +249,26 @@ static static gboolean write_traceset_header(void *hook_data, void *call_data)
 
   /* Print the trace set header */
   fprintf(a_file,"Trace set contains %d traces\n\n", 
-      lttv_traceset_number(tc->ta);
+      lttv_traceset_number(tc->ts));
 
   return FALSE;
 }
 
 
-static static gboolean write_traceset_footer(void *hook_data, void *call_data)
+static gboolean write_traceset_footer(void *hook_data, void *call_data)
 {
   LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
 
-  if(a_file_name != NULL) a_file = fclose(a_file);
+  g_info("TextDump traceset footer");
 
   fprintf(a_file,"End trace set\n\n");
 
+  if(LTTV_IS_TRACESET_STATS(tc)) {
+    print_stats(a_file, (LttvTracesetStats *)tc);
+  }
+
+  if(a_file_name != NULL) fclose(a_file);
+
   return FALSE;
 }
 
@@ -138,107 +295,106 @@ static int write_event_content(void *hook_data, void *call_data)
 
   e = tfc->e;
 
-  lttv_event_to_string(e, tfc->tf, a_string, TRUE, a_field_names);
+  lttv_event_to_string(e, tfc->tf, a_string, TRUE, a_field_names, tfs);
+  g_string_append_printf(a_string,"\n");  
 
   if(a_state) {
     g_string_append_printf(a_string, " %s",
-        g_quark_to_string(tfs->process->state->s);
+        g_quark_to_string(tfs->process->state->s));
   }
 
-  fputs(s, c->fd);
+  fputs(a_string->str, a_file);
   return FALSE;
 }
 
 
-void lttv_event_to_string(LttEvent *e, LttTracefile *tf, g_string *s,
-    gboolean mandatory_fields, gboolean field_names)
+G_MODULE_EXPORT void init(LttvModule *self, int argc, char **argv)
 {
-  LttFacility *facility;
+  LttvAttributeValue value;
 
-  LttEventType *event_type;
+  LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
 
-  LttType *type;
+  g_info("Init textDump.c");
 
-  LttField *field;
+  lttv_module_require(self, "libbatchAnalysis", argc, argv);
 
-  LttTime time;
+  a_string = g_string_new("");
 
-  g_string_set_size(s,0);
+  a_file_name = NULL;
+  lttv_option_add("output", 'o', 
+      "output file where the text is written", 
+      "file name", 
+      LTTV_OPT_STRING, &a_file_name, NULL, NULL);
 
-  facility = lttv_event_facility(e);
-  eventtype = ltt_event_eventtype(e);
-  field = ltt_event_field(e);
+  a_field_names = FALSE;
+  lttv_option_add("field_names", 'l', 
+      "write the field names for each event", 
+      "", 
+      LTTV_OPT_NONE, &a_field_names, NULL, NULL);
 
-  if(mandatory_fields) {
-    time = ltt_event_time(e);
-    g_string_append_printf(s,"%s.%s: %ld.%ld (%s)",ltt_facility_name(facility),
-        ltt_eventtype_name(eventtype), (long)time.tv_sec, time.tv_nsec,
-        ltt_tracefile_name(tf));
-  }
+  a_state = FALSE;
+  lttv_option_add("process_state", 's', 
+      "write the pid and state for each event", 
+      "", 
+      LTTV_OPT_NONE, &a_state, NULL, NULL);
 
-  print_field(e,f,s, field_names);
-} 
+  a_cpu_stats = FALSE;
+  lttv_option_add("cpu_stats", 's', 
+      "write the per cpu statistics", 
+      "", 
+      LTTV_OPT_NONE, &a_cpu_stats, NULL, NULL);
 
+  a_process_stats = FALSE;
+  lttv_option_add("process_stats", 's', 
+      "write the per process statistics", 
+      "", 
+      LTTV_OPT_NONE, &a_process_stats, NULL, NULL);
 
-void print_field(LttEvent *e, LttField *f, g_string *s, gboolean field_names) {
+  g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event/before",
+      LTTV_POINTER, &value));
+  g_assert((before_event = *(value.v_pointer)) != NULL);
+  lttv_hooks_add(before_event, write_event_content, NULL);
 
-  LttType *type;
+  g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/before",
+      LTTV_POINTER, &value));
+  g_assert((before_trace = *(value.v_pointer)) != NULL);
+  lttv_hooks_add(before_trace, write_trace_header, NULL);
 
-  LttField *element;
+  g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/before",
+      LTTV_POINTER, &value));
+  g_assert((before_traceset = *(value.v_pointer)) != NULL);
+  lttv_hooks_add(before_traceset, write_traceset_header, NULL);
 
-  char *name;
+  g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/after",
+      LTTV_POINTER, &value));
+  g_assert((after_traceset = *(value.v_pointer)) != NULL);
+  lttv_hooks_add(after_traceset, write_traceset_footer, NULL);
+}
 
-  int nb, i;
 
-  type = ltt_field_type(f);
-  switch(ltt_type_class(type)) {
-    case LTT_INT:
-      g_string_append_printf(s, " %ld", ltt_event_get_long_int(e,f));
-      break;
+G_MODULE_EXPORT void destroy()
+{
+  g_info("Destroy textDump");
 
-    case LTT_UINT:
-      g_string_append_printf(s, " %lu", ltt_event_get_long_unsigned(e,f));
-      break;
+  lttv_option_remove("output");
 
-    case LTT_FLOAT:
-      g_string_append_printf(s, " %g", ltt_event_get_double(e,f));
-      break;
+  lttv_option_remove("field_names");
 
-    case LTT_STRING:
-      g_string_append_printf(s, " \"%s\"", ltt_event_get_string(e,f));
-      break;
+  lttv_option_remove("process_state");
 
-    case LTT_ENUM:
-      g_string_append_printf(s, " %s", ltt_enum_string_get(type,
-          event_get_unsigned(e,f));
-      break;
+  lttv_option_remove("cpu_stats");
 
-    case LTT_ARRAY:
-    case LTT_SEQUENCE:
-      g_string_append_printf(s, " {");
-      nb = ltt_event_field_element_number(e,f);
-      element = ltt_field_element(f);
-      for(i = 0 ; i < nb ; i++) {
-        ltt_event_field_element_select(e,f,i);
-        print_field(e,element,s);
-      }
-      g_string_append_printf(s, " }");
-      break;
+  lttv_option_remove("process_stats");
 
-    case LTT_STRUCT:
-      g_string_append_printf(s, " {");
-      nb = ltt_type_member_number(type);
-      for(i = 0 ; i < nb ; i++) {
-        element = ltt_field_member(f,i);
-        if(name) {
-          ltt_type_member_type(type, &name);
-          g_string_append_printf(s, " %s = ", field_names);
-        }
-        print_field(e,element,s);
-      }
-      g_string_append_printf(s, " }");
-      break;
-  }
+  g_string_free(a_string, TRUE);
+
+  lttv_hooks_remove_data(before_event, write_event_content, NULL);
+
+  lttv_hooks_remove_data(before_trace, write_trace_header, NULL);
+
+  lttv_hooks_remove_data(before_trace, write_traceset_header, NULL);
+
+  lttv_hooks_remove_data(before_trace, write_traceset_footer, NULL);
 }
 
 
This page took 0.028658 seconds and 4 git commands to generate.