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
6 #include <lttv/option.h>
7 #include <lttv/module.h>
9 #include <lttv/attribute.h>
10 #include <lttv/iattribute.h>
11 #include <lttv/stats.h>
13 #include <ltt/event.h>
15 #include <ltt/trace.h>
34 void print_field(LttEvent
*e
, LttField
*f
, GString
*s
, gboolean field_names
) {
44 type
= ltt_field_type(f
);
45 switch(ltt_type_class(type
)) {
47 g_string_append_printf(s
, " %ld", ltt_event_get_long_int(e
,f
));
51 g_string_append_printf(s
, " %lu", ltt_event_get_long_unsigned(e
,f
));
55 g_string_append_printf(s
, " %g", ltt_event_get_double(e
,f
));
59 g_string_append_printf(s
, " \"%s\"", ltt_event_get_string(e
,f
));
63 g_string_append_printf(s
, " %s", ltt_enum_string_get(type
,
64 ltt_event_get_unsigned(e
,f
)-1));
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
);
76 g_string_append_printf(s
, " }");
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
);
85 ltt_type_member_type(type
, i
, &name
);
86 g_string_append_printf(s
, " %s = ", name
);
88 print_field(e
, element
, s
, field_names
);
90 g_string_append_printf(s
, " }");
96 void lttv_event_to_string(LttEvent
*e
, LttTracefile
*tf
, GString
*s
,
97 gboolean mandatory_fields
, gboolean field_names
, LttvTracefileState
*tfs
)
99 LttFacility
*facility
;
101 LttEventType
*event_type
;
109 g_string_set_size(s
,0);
111 facility
= ltt_event_facility(e
);
112 event_type
= ltt_event_eventtype(e
);
113 field
= ltt_event_field(e
);
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
,
124 g_quark_to_string(tfs
->process
->state
->t
));
128 print_field(e
, field
, s
, field_names
);
133 print_tree(FILE *fp
, GString
*indent
, LttvAttribute
*tree
)
135 int i
, nb
, saved_length
;
137 LttvAttribute
*subtree
;
139 LttvAttributeName name
;
141 LttvAttributeValue value
;
143 LttvAttributeType type
;
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
));
152 fprintf(fp
, "%d\n", *value
.v_int
);
155 fprintf(fp
, "%u\n", *value
.v_uint
);
158 fprintf(fp
, "%ld\n", *value
.v_long
);
161 fprintf(fp
, "%lu\n", *value
.v_ulong
);
164 fprintf(fp
, "%f\n", (double)*value
.v_float
);
167 fprintf(fp
, "%f\n", *value
.v_double
);
170 fprintf(fp
, "%10u.%09u\n", value
.v_time
->tv_sec
,
171 value
.v_time
->tv_nsec
);
174 fprintf(fp
, "POINTER\n");
177 fprintf(fp
, "%s\n", *value
.v_string
);
180 if(LTTV_IS_ATTRIBUTE(*(value
.v_gobject
))) {
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
);
188 else fprintf(fp
, "GOBJECT\n");
198 print_stats(FILE *fp
, LttvTracesetStats
*tscs
)
200 int i
, nb
, saved_length
;
208 LttSystemDescription
*desc
;
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
);
215 ts
= tscs
->parent
.parent
.ts
;
216 nb
= lttv_traceset_number(ts
);
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
);
229 g_string_free(indent
, TRUE
);
233 /* Insert the hooks before and after each trace and tracefile, and for each
234 event. Print a global header. */
238 static GString
*a_string
;
240 static gboolean
write_traceset_header(void *hook_data
, void *call_data
)
242 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
244 g_info("TextDump traceset header");
246 if(a_file_name
== NULL
) a_file
= stdout
;
247 else a_file
= fopen(a_file_name
, "w");
249 if(a_file
== NULL
) g_error("cannot open file %s", a_file_name
);
251 /* Print the trace set header */
252 fprintf(a_file
,"Trace set contains %d traces\n\n",
253 lttv_traceset_number(tc
->ts
));
259 static gboolean
write_traceset_footer(void *hook_data
, void *call_data
)
261 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
263 g_info("TextDump traceset footer");
265 fprintf(a_file
,"End trace set\n\n");
267 if(LTTV_IS_TRACESET_STATS(tc
)) {
268 print_stats(a_file
, (LttvTracesetStats
*)tc
);
271 if(a_file_name
!= NULL
) fclose(a_file
);
277 static gboolean
write_trace_header(void *hook_data
, void *call_data
)
279 LttvTraceContext
*tc
= (LttvTraceContext
*)call_data
;
281 LttSystemDescription
*system
= ltt_trace_system_description(tc
->t
);
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
));
291 static int write_event_content(void *hook_data
, void *call_data
)
293 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)call_data
;
295 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
301 lttv_event_to_string(e
, tfc
->tf
, a_string
, TRUE
, a_field_names
, tfs
);
302 g_string_append_printf(a_string
,"\n");
305 g_string_append_printf(a_string
, " %s",
306 g_quark_to_string(tfs
->process
->state
->s
));
309 fputs(a_string
->str
, a_file
);
314 G_MODULE_EXPORT
void init(LttvModule
*self
, int argc
, char **argv
)
316 LttvAttributeValue value
;
318 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
320 g_info("Init textDump.c");
322 lttv_module_require(self
, "libbatchAnalysis", argc
, argv
);
324 a_string
= g_string_new("");
327 lttv_option_add("output", 'o',
328 "output file where the text is written",
330 LTTV_OPT_STRING
, &a_file_name
, NULL
, NULL
);
332 a_field_names
= FALSE
;
333 lttv_option_add("field_names", 'l',
334 "write the field names for each event",
336 LTTV_OPT_NONE
, &a_field_names
, NULL
, NULL
);
339 lttv_option_add("process_state", 's',
340 "write the pid and state for each event",
342 LTTV_OPT_NONE
, &a_state
, NULL
, NULL
);
345 lttv_option_add("cpu_stats", 's',
346 "write the per cpu statistics",
348 LTTV_OPT_NONE
, &a_cpu_stats
, NULL
, NULL
);
350 a_process_stats
= FALSE
;
351 lttv_option_add("process_stats", 's',
352 "write the per process statistics",
354 LTTV_OPT_NONE
, &a_process_stats
, NULL
, NULL
);
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
);
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
);
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
);
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
);
378 G_MODULE_EXPORT
void destroy()
380 g_info("Destroy textDump");
382 lttv_option_remove("output");
384 lttv_option_remove("field_names");
386 lttv_option_remove("process_state");
388 lttv_option_remove("cpu_stats");
390 lttv_option_remove("process_stats");
392 g_string_free(a_string
, TRUE
);
394 lttv_hooks_remove_data(before_event
, write_event_content
, NULL
);
396 lttv_hooks_remove_data(before_trace
, write_trace_header
, NULL
);
398 lttv_hooks_remove_data(before_trace
, write_traceset_header
, NULL
);
400 lttv_hooks_remove_data(before_trace
, write_traceset_footer
, NULL
);