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