1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
3 * 2005 Mathieu Desnoyers
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License Version 2 as
7 * published by the Free Software Foundation;
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 /* The text dump facility needs to print headers before the trace set and
21 before each trace, to print each event, and to print statistics
28 #include <lttv/lttv.h>
29 #include <lttv/option.h>
30 #include <lttv/module.h>
31 #include <lttv/hook.h>
32 #include <lttv/attribute.h>
33 #include <lttv/iattribute.h>
34 #include <lttv/stats.h>
35 #include <lttv/filter.h>
36 #include <lttv/print.h>
38 #include <ltt/event.h>
40 #include <ltt/trace.h>
41 #include <ltt/facility.h>
53 *a_quark_file_name
= NULL
;
62 static guint a_event_count
= 0;
64 /* Insert the hooks before and after each trace and tracefile, and for each
65 event. Print a global header. */
69 static GString
*a_string
;
71 static gboolean
write_traceset_header(void *hook_data
, void *call_data
)
73 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
75 if(a_file_name
== NULL
) a_file
= stdout
;
76 else a_file
= fopen(a_file_name
, "w");
78 if(a_file
== NULL
) g_error("cannot open file %s", a_file_name
);
80 /* Print the trace set header */
82 /* TODO : Write a header that will check for ILP size and endianness */
83 //fputc(HDR_TRACESET, a_file);
84 g_assert(lttv_traceset_number(tc
->ts
) == 1); /* Only one trace in traceset */
86 fprintf(a_file
,"<TRACESET NUM_TRACES=%d/>\n",
87 lttv_traceset_number(tc
->ts
));
94 static gboolean
write_traceset_footer(void *hook_data
, void *call_data
)
96 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
103 fprintf(a_file
,"</TRACESET>\n");
106 if(a_file_name
!= NULL
) fclose(a_file
);
109 if(a_quark_file_name
== NULL
) {
110 if(a_file_name
== NULL
) a_file
= stdout
;
111 else a_file
= fopen(a_file_name
, "a");
113 if(a_quark_file_name
== NULL
) a_file
= stdout
;
114 else a_file
= fopen(a_quark_file_name
, "w");
117 if(a_file
== NULL
) g_error("cannot open file %s", a_quark_file_name
);
119 fputc(HDR_QUARKS
, a_file
);
122 string
= g_quark_to_string(q
);
123 if(string
== NULL
) break;
124 fputc(HDR_QUARK
, a_file
);
125 // increment. fwrite(&q, sizeof(GQuark), 1, a_file);
126 fwrite(string
, sizeof(char), strlen(string
)+1, a_file
);
130 if(a_quark_file_name
!= NULL
|| a_file_name
!= NULL
) fclose(a_file
);
138 static gboolean
write_trace_header(void *hook_data
, void *call_data
)
140 LttvTraceContext
*tc
= (LttvTraceContext
*)call_data
;
143 fputc(HDR_TRACE
, a_file
);
145 fprintf(a_file
,"<TRACE TRACE_NUMBER=%d/>\n",
152 static gboolean
write_trace_footer(void *hook_data
, void *call_data
)
154 LttvTraceContext
*tc
= (LttvTraceContext
*)call_data
;
159 fprintf(a_file
,"</TRACE>\n");
166 static int for_each_event(void *hook_data
, void *call_data
)
168 guint
*event_count
= (guint
*)hook_data
;
170 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
172 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)call_data
;
174 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
178 LttvAttributeValue value_filter
;
180 /* Only save at LTTV_STATE_SAVE_INTERVAL */
181 if(likely((*event_count
)++ < LTTV_STATE_SAVE_INTERVAL
))
186 guint cpu
= tfs
->cpu
;
187 LttvTraceState
*ts
= (LttvTraceState
*)tfc
->t_context
;
188 LttvProcessState
*process
= ts
->running_process
[cpu
];
190 e
= ltt_tracefile_get_event(tfc
->tf
);
193 lttv_state_write_raw(ts
, tfs
->parent
.timestamp
, a_file
);
195 lttv_state_write(ts
, tfs
->parent
.timestamp
, a_file
);
204 LttvAttributeValue value
;
206 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
208 g_info("Init precomputeState.c");
210 a_string
= g_string_new("");
213 lttv_option_add("output", 'o',
214 "output file where the saved states are to be written",
216 LTTV_OPT_STRING
, &a_file_name
, NULL
, NULL
);
218 a_quark_file_name
= NULL
;
219 lttv_option_add("qoutput", 'q',
220 "output file where the quarks (tuples integer, string) are to be written",
222 LTTV_OPT_STRING
, &a_quark_file_name
, NULL
, NULL
);
224 lttv_option_add("raw", 'r',
225 "Output in raw binary",
227 LTTV_OPT_NONE
, &a_raw
, NULL
, NULL
);
229 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/event",
230 LTTV_POINTER
, &value
));
231 g_assert((event_hook
= *(value
.v_pointer
)) != NULL
);
232 lttv_hooks_add(event_hook
, for_each_event
, &a_event_count
, LTTV_PRIO_DEFAULT
);
234 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/trace/before",
235 LTTV_POINTER
, &value
));
236 g_assert((before_trace
= *(value
.v_pointer
)) != NULL
);
237 lttv_hooks_add(before_trace
, write_trace_header
, NULL
, LTTV_PRIO_DEFAULT
);
239 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/trace/after",
240 LTTV_POINTER
, &value
));
241 g_assert((after_trace
= *(value
.v_pointer
)) != NULL
);
242 lttv_hooks_add(after_trace
, write_trace_footer
, NULL
, LTTV_PRIO_DEFAULT
);
244 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/before",
245 LTTV_POINTER
, &value
));
246 g_assert((before_traceset
= *(value
.v_pointer
)) != NULL
);
247 lttv_hooks_add(before_traceset
, write_traceset_header
, NULL
,
250 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/after",
251 LTTV_POINTER
, &value
));
252 g_assert((after_traceset
= *(value
.v_pointer
)) != NULL
);
253 lttv_hooks_add(after_traceset
, write_traceset_footer
, NULL
,
257 static void destroy()
259 g_info("Destroy precomputeState");
261 lttv_option_remove("output");
263 lttv_option_remove("qoutput");
265 lttv_option_remove("raw");
267 g_string_free(a_string
, TRUE
);
269 lttv_hooks_remove_data(event_hook
, for_each_event
, NULL
);
271 lttv_hooks_remove_data(before_trace
, write_trace_header
, NULL
);
273 lttv_hooks_remove_data(before_trace
, write_traceset_header
, NULL
);
275 lttv_hooks_remove_data(before_trace
, write_traceset_footer
, NULL
);
279 LTTV_MODULE("precomputeState", "Precompute states", \
280 "Precompute states in a trace, XML or binary output.", \
281 init
, destroy
, "stats", "batchAnalysis", "option", "print")