update roadmap
[lttv.git] / ltt / branches / poly / lttv / modules / text / precomputeState.c
CommitLineData
f204781b 1/* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
3 * 2005 Mathieu Desnoyers
4 *
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;
8 *
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.
13 *
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,
17 * MA 02111-1307, USA.
18 */
19
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
22 after each trace. */
23
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
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>
37#include <ltt/ltt.h>
38#include <ltt/event.h>
39#include <ltt/type.h>
40#include <ltt/trace.h>
41#include <ltt/facility.h>
42#include <stdio.h>
43
44static gboolean
45 a_field_names,
46 a_state,
47 a_cpu_stats,
6d0cdf22 48 a_process_stats,
49 a_raw;
f204781b 50
51static char
6d0cdf22 52 *a_file_name = NULL,
53 *a_quark_file_name = NULL;
f204781b 54
55static LttvHooks
56 *before_traceset,
57 *after_traceset,
58 *before_trace,
6d0cdf22 59 *after_trace,
f204781b 60 *event_hook;
61
6d0cdf22 62static guint a_event_count = 0;
f204781b 63
64/* Insert the hooks before and after each trace and tracefile, and for each
65 event. Print a global header. */
66
67static FILE *a_file;
68
69static GString *a_string;
70
71static gboolean write_traceset_header(void *hook_data, void *call_data)
72{
73 LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
74
f204781b 75 if(a_file_name == NULL) a_file = stdout;
76 else a_file = fopen(a_file_name, "w");
77
78 if(a_file == NULL) g_error("cannot open file %s", a_file_name);
79
80 /* Print the trace set header */
6d0cdf22 81 if(a_raw) {
82 /* TODO : Write a header that will check for ILP size and endianness */
83 fputc(HDR_TRACESET, a_file);
84 } else {
85 fprintf(a_file,"<TRACESET NUM_TRACES=%d/>\n",
86 lttv_traceset_number(tc->ts));
87 }
f204781b 88
89 return FALSE;
90}
91
92
93static gboolean write_traceset_footer(void *hook_data, void *call_data)
94{
95 LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
6d0cdf22 96 GQuark q;
97 gchar *string;
f204781b 98
6d0cdf22 99 if(a_raw) {
f204781b 100
6d0cdf22 101 } else {
102 fprintf(a_file,"</TRACESET>\n");
f204781b 103 }
104
105 if(a_file_name != NULL) fclose(a_file);
106
6d0cdf22 107 if(a_raw) {
108 if(a_quark_file_name == NULL) {
109 if(a_file_name == NULL) a_file = stdout;
110 else a_file = fopen(a_file_name, "a");
111 } else {
112 if(a_quark_file_name == NULL) a_file = stdout;
113 else a_file = fopen(a_quark_file_name, "w");
114 }
115
116 if(a_file == NULL) g_error("cannot open file %s", a_quark_file_name);
117
118 fputc(HDR_QUARKS, a_file);
119 q = 1;
120 do {
121 string = g_quark_to_string(q);
122 if(string == NULL) break;
123 fputc(HDR_QUARK, a_file);
124 // increment. fwrite(&q, sizeof(GQuark), 1, a_file);
125 fwrite(string, sizeof(char), strlen(string)+1, a_file);
126 q++;
127 } while(1);
128
129 if(a_quark_file_name != NULL || a_file_name != NULL) fclose(a_file);
130
131 }
132
f204781b 133 return FALSE;
134}
135
136
137static gboolean write_trace_header(void *hook_data, void *call_data)
138{
139 LttvTraceContext *tc = (LttvTraceContext *)call_data;
6d0cdf22 140
141 if(a_raw) {
142 fputc(HDR_TRACE, a_file);
143 } else {
144 fprintf(a_file,"<TRACE TRACE_NUMBER=%d/>\n",
145 tc->index);
146 }
147
148 return FALSE;
149}
150
151static gboolean write_trace_footer(void *hook_data, void *call_data)
152{
153 LttvTraceContext *tc = (LttvTraceContext *)call_data;
154
155 if(a_raw) {
156
157 } else {
158 fprintf(a_file,"</TRACE>\n");
159 }
160
f204781b 161 return FALSE;
162}
163
164
165static int for_each_event(void *hook_data, void *call_data)
166{
167 guint *event_count = (guint*)hook_data;
168
169 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
170
171 LttvTracefileContext *tfc = (LttvTracefileContext *)call_data;
172
173 LttvTracefileState *tfs = (LttvTracefileState *)call_data;
174
175 LttEvent *e;
176
177 LttvAttributeValue value_filter;
178
179 /* Only save at LTTV_STATE_SAVE_INTERVAL */
180 if(likely((*event_count)++ < LTTV_STATE_SAVE_INTERVAL))
181 return FALSE;
182 else
183 *event_count = 0;
184
185 guint cpu = tfs->cpu;
186 LttvTraceState *ts = (LttvTraceState*)tfc->t_context;
187 LttvProcessState *process = ts->running_process[cpu];
188
189 e = ltt_tracefile_get_event(tfc->tf);
190
6d0cdf22 191 if(a_raw) {
192 lttv_state_write_raw(ts, tfs->parent.timestamp, a_file);
193 } else {
194 lttv_state_write(ts, tfs->parent.timestamp, a_file);
195 }
f204781b 196
197 return FALSE;
198}
199
200
201static void init()
202{
203 LttvAttributeValue value;
204
205 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
206
6d0cdf22 207 g_info("Init precomputeState.c");
f204781b 208
209 a_string = g_string_new("");
210
211 a_file_name = NULL;
212 lttv_option_add("output", 'o',
213 "output file where the saved states are to be written",
214 "file name",
215 LTTV_OPT_STRING, &a_file_name, NULL, NULL);
216
6d0cdf22 217 a_quark_file_name = NULL;
218 lttv_option_add("qoutput", 'q',
219 "output file where the quarks (tuples integer, string) are to be written",
220 "file name",
221 LTTV_OPT_STRING, &a_quark_file_name, NULL, NULL);
222
223 lttv_option_add("raw", 'r',
224 "Output in raw binary",
225 "Raw binary",
226 LTTV_OPT_NONE, &a_raw, NULL, NULL);
227
f204781b 228 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event",
229 LTTV_POINTER, &value));
230 g_assert((event_hook = *(value.v_pointer)) != NULL);
6d0cdf22 231 lttv_hooks_add(event_hook, for_each_event, &a_event_count, LTTV_PRIO_DEFAULT);
f204781b 232
233 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/before",
234 LTTV_POINTER, &value));
235 g_assert((before_trace = *(value.v_pointer)) != NULL);
236 lttv_hooks_add(before_trace, write_trace_header, NULL, LTTV_PRIO_DEFAULT);
237
6d0cdf22 238 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/after",
239 LTTV_POINTER, &value));
240 g_assert((after_trace = *(value.v_pointer)) != NULL);
241 lttv_hooks_add(after_trace, write_trace_footer, NULL, LTTV_PRIO_DEFAULT);
242
f204781b 243 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/before",
244 LTTV_POINTER, &value));
245 g_assert((before_traceset = *(value.v_pointer)) != NULL);
246 lttv_hooks_add(before_traceset, write_traceset_header, NULL,
247 LTTV_PRIO_DEFAULT);
248
249 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/after",
250 LTTV_POINTER, &value));
251 g_assert((after_traceset = *(value.v_pointer)) != NULL);
252 lttv_hooks_add(after_traceset, write_traceset_footer, NULL,
253 LTTV_PRIO_DEFAULT);
254}
255
256static void destroy()
257{
6d0cdf22 258 g_info("Destroy precomputeState");
f204781b 259
260 lttv_option_remove("output");
261
6d0cdf22 262 lttv_option_remove("qoutput");
263
264 lttv_option_remove("raw");
265
f204781b 266 g_string_free(a_string, TRUE);
267
268 lttv_hooks_remove_data(event_hook, for_each_event, NULL);
269
270 lttv_hooks_remove_data(before_trace, write_trace_header, NULL);
271
272 lttv_hooks_remove_data(before_trace, write_traceset_header, NULL);
273
274 lttv_hooks_remove_data(before_trace, write_traceset_footer, NULL);
275}
276
277
6d0cdf22 278LTTV_MODULE("precomputeState", "Precompute states", \
279 "Precompute states in a trace, XML output.", \
f204781b 280 init, destroy, "stats", "batchAnalysis", "option", "print")
281
This page took 0.033679 seconds and 4 git commands to generate.