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