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