| 1 | /* The text dump facility needs to print headers before the trace set and |
| 2 | before each trace, to print each event, and to print statistics |
| 3 | after each trace. */ |
| 4 | |
| 5 | #include <lttv/lttv.h> |
| 6 | #include <lttv/option.h> |
| 7 | #include <lttv/module.h> |
| 8 | #include <lttv/hook.h> |
| 9 | #include <lttv/attribute.h> |
| 10 | #include <lttv/iattribute.h> |
| 11 | #include <lttv/stats.h> |
| 12 | #include <ltt/ltt.h> |
| 13 | #include <ltt/event.h> |
| 14 | #include <ltt/type.h> |
| 15 | #include <ltt/trace.h> |
| 16 | #include <stdio.h> |
| 17 | |
| 18 | static gboolean |
| 19 | a_field_names, |
| 20 | a_state, |
| 21 | a_cpu_stats, |
| 22 | a_process_stats; |
| 23 | |
| 24 | static char |
| 25 | *a_file_name = NULL; |
| 26 | |
| 27 | static LttvHooks |
| 28 | *before_traceset, |
| 29 | *after_traceset, |
| 30 | *before_trace, |
| 31 | *before_event; |
| 32 | |
| 33 | |
| 34 | void print_field(LttEvent *e, LttField *f, GString *s, gboolean field_names) { |
| 35 | |
| 36 | LttType *type; |
| 37 | |
| 38 | LttField *element; |
| 39 | |
| 40 | char *name; |
| 41 | |
| 42 | int nb, i; |
| 43 | |
| 44 | type = ltt_field_type(f); |
| 45 | switch(ltt_type_class(type)) { |
| 46 | case LTT_INT: |
| 47 | g_string_append_printf(s, " %ld", ltt_event_get_long_int(e,f)); |
| 48 | break; |
| 49 | |
| 50 | case LTT_UINT: |
| 51 | g_string_append_printf(s, " %lu", ltt_event_get_long_unsigned(e,f)); |
| 52 | break; |
| 53 | |
| 54 | case LTT_FLOAT: |
| 55 | g_string_append_printf(s, " %g", ltt_event_get_double(e,f)); |
| 56 | break; |
| 57 | |
| 58 | case LTT_STRING: |
| 59 | g_string_append_printf(s, " \"%s\"", ltt_event_get_string(e,f)); |
| 60 | break; |
| 61 | |
| 62 | case LTT_ENUM: |
| 63 | g_string_append_printf(s, " %s", ltt_enum_string_get(type, |
| 64 | ltt_event_get_unsigned(e,f)-1)); |
| 65 | break; |
| 66 | |
| 67 | case LTT_ARRAY: |
| 68 | case LTT_SEQUENCE: |
| 69 | g_string_append_printf(s, " {"); |
| 70 | nb = ltt_event_field_element_number(e,f); |
| 71 | element = ltt_field_element(f); |
| 72 | for(i = 0 ; i < nb ; i++) { |
| 73 | ltt_event_field_element_select(e,f,i); |
| 74 | print_field(e, element, s, field_names); |
| 75 | } |
| 76 | g_string_append_printf(s, " }"); |
| 77 | break; |
| 78 | |
| 79 | case LTT_STRUCT: |
| 80 | g_string_append_printf(s, " {"); |
| 81 | nb = ltt_type_member_number(type); |
| 82 | for(i = 0 ; i < nb ; i++) { |
| 83 | element = ltt_field_member(f,i); |
| 84 | if(field_names) { |
| 85 | ltt_type_member_type(type, i, &name); |
| 86 | g_string_append_printf(s, " %s = ", name); |
| 87 | } |
| 88 | print_field(e, element, s, field_names); |
| 89 | } |
| 90 | g_string_append_printf(s, " }"); |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
| 96 | void lttv_event_to_string(LttEvent *e, LttTracefile *tf, GString *s, |
| 97 | gboolean mandatory_fields, gboolean field_names, LttvTracefileState *tfs) |
| 98 | { |
| 99 | LttFacility *facility; |
| 100 | |
| 101 | LttEventType *event_type; |
| 102 | |
| 103 | LttType *type; |
| 104 | |
| 105 | LttField *field; |
| 106 | |
| 107 | LttTime time; |
| 108 | |
| 109 | g_string_set_size(s,0); |
| 110 | |
| 111 | facility = ltt_event_facility(e); |
| 112 | event_type = ltt_event_eventtype(e); |
| 113 | field = ltt_event_field(e); |
| 114 | |
| 115 | if(mandatory_fields) { |
| 116 | time = ltt_event_time(e); |
| 117 | g_string_append_printf(s,"%s.%s: %ld.%09ld (%s)", |
| 118 | ltt_facility_name(facility), |
| 119 | ltt_eventtype_name(event_type), (long)time.tv_sec, time.tv_nsec, |
| 120 | g_quark_to_string(tfs->cpu_name)); |
| 121 | /* Print the process id and the state/interrupt type of the process */ |
| 122 | g_string_append_printf(s,", %u, %u, %s", tfs->process->pid, |
| 123 | tfs->process->ppid, |
| 124 | g_quark_to_string(tfs->process->state->t)); |
| 125 | } |
| 126 | |
| 127 | if(field) |
| 128 | print_field(e, field, s, field_names); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | static void |
| 133 | print_tree(FILE *fp, GString *indent, LttvAttribute *tree) |
| 134 | { |
| 135 | int i, nb, saved_length; |
| 136 | |
| 137 | LttvAttribute *subtree; |
| 138 | |
| 139 | LttvAttributeName name; |
| 140 | |
| 141 | LttvAttributeValue value; |
| 142 | |
| 143 | LttvAttributeType type; |
| 144 | |
| 145 | nb = lttv_attribute_get_number(tree); |
| 146 | for(i = 0 ; i < nb ; i++) { |
| 147 | type = lttv_attribute_get(tree, i, &name, &value); |
| 148 | fprintf(fp, "%s%s: ", indent->str, g_quark_to_string(name)); |
| 149 | |
| 150 | switch(type) { |
| 151 | case LTTV_INT: |
| 152 | fprintf(fp, "%d\n", *value.v_int); |
| 153 | break; |
| 154 | case LTTV_UINT: |
| 155 | fprintf(fp, "%u\n", *value.v_uint); |
| 156 | break; |
| 157 | case LTTV_LONG: |
| 158 | fprintf(fp, "%ld\n", *value.v_long); |
| 159 | break; |
| 160 | case LTTV_ULONG: |
| 161 | fprintf(fp, "%lu\n", *value.v_ulong); |
| 162 | break; |
| 163 | case LTTV_FLOAT: |
| 164 | fprintf(fp, "%f\n", (double)*value.v_float); |
| 165 | break; |
| 166 | case LTTV_DOUBLE: |
| 167 | fprintf(fp, "%f\n", *value.v_double); |
| 168 | break; |
| 169 | case LTTV_TIME: |
| 170 | fprintf(fp, "%10u.%09u\n", value.v_time->tv_sec, |
| 171 | value.v_time->tv_nsec); |
| 172 | break; |
| 173 | case LTTV_POINTER: |
| 174 | fprintf(fp, "POINTER\n"); |
| 175 | break; |
| 176 | case LTTV_STRING: |
| 177 | fprintf(fp, "%s\n", *value.v_string); |
| 178 | break; |
| 179 | case LTTV_GOBJECT: |
| 180 | if(LTTV_IS_ATTRIBUTE(*(value.v_gobject))) { |
| 181 | fprintf(fp, "\n"); |
| 182 | subtree = (LttvAttribute *)*(value.v_gobject); |
| 183 | saved_length = indent->len; |
| 184 | g_string_append(indent, " "); |
| 185 | print_tree(fp, indent, subtree); |
| 186 | g_string_truncate(indent, saved_length); |
| 187 | } |
| 188 | else fprintf(fp, "GOBJECT\n"); |
| 189 | break; |
| 190 | case LTTV_NONE: |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | |
| 197 | static void |
| 198 | print_stats(FILE *fp, LttvTracesetStats *tscs) |
| 199 | { |
| 200 | int i, nb, saved_length; |
| 201 | |
| 202 | LttvTraceset *ts; |
| 203 | |
| 204 | LttvTraceStats *tcs; |
| 205 | |
| 206 | GString *indent; |
| 207 | |
| 208 | LttSystemDescription *desc; |
| 209 | |
| 210 | if(tscs->stats == NULL) return; |
| 211 | indent = g_string_new(""); |
| 212 | fprintf(fp, "Traceset statistics:\n\n"); |
| 213 | print_tree(fp, indent, tscs->stats); |
| 214 | |
| 215 | ts = tscs->parent.parent.ts; |
| 216 | nb = lttv_traceset_number(ts); |
| 217 | |
| 218 | for(i = 0 ; i < nb ; i++) { |
| 219 | tcs = (LttvTraceStats *)(LTTV_TRACESET_CONTEXT(tscs)->traces[i]); |
| 220 | desc = ltt_trace_system_description(tcs->parent.parent.t); |
| 221 | fprintf(fp, "Trace on system %s at time %d secs:\n", |
| 222 | ltt_trace_system_description_node_name(desc), |
| 223 | (ltt_trace_system_description_trace_start_time(desc)).tv_sec); |
| 224 | saved_length = indent->len; |
| 225 | g_string_append(indent, " "); |
| 226 | print_tree(fp, indent, tcs->stats); |
| 227 | g_string_truncate(indent, saved_length); |
| 228 | } |
| 229 | g_string_free(indent, TRUE); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /* Insert the hooks before and after each trace and tracefile, and for each |
| 234 | event. Print a global header. */ |
| 235 | |
| 236 | static FILE *a_file; |
| 237 | |
| 238 | static GString *a_string; |
| 239 | |
| 240 | static gboolean write_traceset_header(void *hook_data, void *call_data) |
| 241 | { |
| 242 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
| 243 | |
| 244 | g_info("TextDump traceset header"); |
| 245 | |
| 246 | if(a_file_name == NULL) a_file = stdout; |
| 247 | else a_file = fopen(a_file_name, "w"); |
| 248 | |
| 249 | if(a_file == NULL) g_error("cannot open file %s", a_file_name); |
| 250 | |
| 251 | /* Print the trace set header */ |
| 252 | fprintf(a_file,"Trace set contains %d traces\n\n", |
| 253 | lttv_traceset_number(tc->ts)); |
| 254 | |
| 255 | return FALSE; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | static gboolean write_traceset_footer(void *hook_data, void *call_data) |
| 260 | { |
| 261 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
| 262 | |
| 263 | g_info("TextDump traceset footer"); |
| 264 | |
| 265 | fprintf(a_file,"End trace set\n\n"); |
| 266 | |
| 267 | if(LTTV_IS_TRACESET_STATS(tc)) { |
| 268 | print_stats(a_file, (LttvTracesetStats *)tc); |
| 269 | } |
| 270 | |
| 271 | if(a_file_name != NULL) fclose(a_file); |
| 272 | |
| 273 | return FALSE; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | static gboolean write_trace_header(void *hook_data, void *call_data) |
| 278 | { |
| 279 | LttvTraceContext *tc = (LttvTraceContext *)call_data; |
| 280 | |
| 281 | LttSystemDescription *system = ltt_trace_system_description(tc->t); |
| 282 | |
| 283 | fprintf(a_file," Trace from %s in %s\n%s\n\n", |
| 284 | ltt_trace_system_description_node_name(system), |
| 285 | ltt_trace_system_description_domain_name(system), |
| 286 | ltt_trace_system_description_description(system)); |
| 287 | return FALSE; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | static int write_event_content(void *hook_data, void *call_data) |
| 292 | { |
| 293 | LttvTracefileContext *tfc = (LttvTracefileContext *)call_data; |
| 294 | |
| 295 | LttvTracefileState *tfs = (LttvTracefileState *)call_data; |
| 296 | |
| 297 | LttEvent *e; |
| 298 | |
| 299 | e = tfc->e; |
| 300 | |
| 301 | lttv_event_to_string(e, tfc->tf, a_string, TRUE, a_field_names, tfs); |
| 302 | g_string_append_printf(a_string,"\n"); |
| 303 | |
| 304 | if(a_state) { |
| 305 | g_string_append_printf(a_string, " %s ", |
| 306 | g_quark_to_string(tfs->process->state->s)); |
| 307 | } |
| 308 | |
| 309 | fputs(a_string->str, a_file); |
| 310 | return FALSE; |
| 311 | } |
| 312 | |
| 313 | |
| 314 | G_MODULE_EXPORT void init(LttvModule *self, int argc, char **argv) |
| 315 | { |
| 316 | LttvAttributeValue value; |
| 317 | |
| 318 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
| 319 | |
| 320 | g_info("Init textDump.c"); |
| 321 | |
| 322 | lttv_module_require(self, "libbatchAnalysis", argc, argv); |
| 323 | |
| 324 | a_string = g_string_new(""); |
| 325 | |
| 326 | a_file_name = NULL; |
| 327 | lttv_option_add("output", 'o', |
| 328 | "output file where the text is written", |
| 329 | "file name", |
| 330 | LTTV_OPT_STRING, &a_file_name, NULL, NULL); |
| 331 | |
| 332 | a_field_names = FALSE; |
| 333 | lttv_option_add("field_names", 'l', |
| 334 | "write the field names for each event", |
| 335 | "", |
| 336 | LTTV_OPT_NONE, &a_field_names, NULL, NULL); |
| 337 | |
| 338 | a_state = FALSE; |
| 339 | lttv_option_add("process_state", 's', |
| 340 | "write the pid and state for each event", |
| 341 | "", |
| 342 | LTTV_OPT_NONE, &a_state, NULL, NULL); |
| 343 | |
| 344 | a_cpu_stats = FALSE; |
| 345 | lttv_option_add("cpu_stats", 'c', |
| 346 | "write the per cpu statistics", |
| 347 | "", |
| 348 | LTTV_OPT_NONE, &a_cpu_stats, NULL, NULL); |
| 349 | |
| 350 | a_process_stats = FALSE; |
| 351 | lttv_option_add("process_stats", 'p', |
| 352 | "write the per process statistics", |
| 353 | "", |
| 354 | LTTV_OPT_NONE, &a_process_stats, NULL, NULL); |
| 355 | |
| 356 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event/before", |
| 357 | LTTV_POINTER, &value)); |
| 358 | g_assert((before_event = *(value.v_pointer)) != NULL); |
| 359 | lttv_hooks_add(before_event, write_event_content, NULL); |
| 360 | |
| 361 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/before", |
| 362 | LTTV_POINTER, &value)); |
| 363 | g_assert((before_trace = *(value.v_pointer)) != NULL); |
| 364 | lttv_hooks_add(before_trace, write_trace_header, NULL); |
| 365 | |
| 366 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/before", |
| 367 | LTTV_POINTER, &value)); |
| 368 | g_assert((before_traceset = *(value.v_pointer)) != NULL); |
| 369 | lttv_hooks_add(before_traceset, write_traceset_header, NULL); |
| 370 | |
| 371 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/after", |
| 372 | LTTV_POINTER, &value)); |
| 373 | g_assert((after_traceset = *(value.v_pointer)) != NULL); |
| 374 | lttv_hooks_add(after_traceset, write_traceset_footer, NULL); |
| 375 | } |
| 376 | |
| 377 | |
| 378 | G_MODULE_EXPORT void destroy() |
| 379 | { |
| 380 | g_info("Destroy textDump"); |
| 381 | |
| 382 | lttv_option_remove("output"); |
| 383 | |
| 384 | lttv_option_remove("field_names"); |
| 385 | |
| 386 | lttv_option_remove("process_state"); |
| 387 | |
| 388 | lttv_option_remove("cpu_stats"); |
| 389 | |
| 390 | lttv_option_remove("process_stats"); |
| 391 | |
| 392 | g_string_free(a_string, TRUE); |
| 393 | |
| 394 | lttv_hooks_remove_data(before_event, write_event_content, NULL); |
| 395 | |
| 396 | lttv_hooks_remove_data(before_trace, write_trace_header, NULL); |
| 397 | |
| 398 | lttv_hooks_remove_data(before_trace, write_traceset_header, NULL); |
| 399 | |
| 400 | lttv_hooks_remove_data(before_trace, write_traceset_footer, NULL); |
| 401 | } |
| 402 | |
| 403 | |
| 404 | |
| 405 | |