| 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 | |
| 44 | static gboolean |
| 45 | a_field_names, |
| 46 | a_state, |
| 47 | a_cpu_stats, |
| 48 | a_process_stats, |
| 49 | a_raw; |
| 50 | |
| 51 | static char |
| 52 | *a_file_name = NULL, |
| 53 | *a_quark_file_name = NULL; |
| 54 | |
| 55 | static LttvHooks |
| 56 | *before_traceset, |
| 57 | *after_traceset, |
| 58 | *before_trace, |
| 59 | *after_trace, |
| 60 | *event_hook; |
| 61 | |
| 62 | static guint a_event_count = 0; |
| 63 | |
| 64 | /* Insert the hooks before and after each trace and tracefile, and for each |
| 65 | event. Print a global header. */ |
| 66 | |
| 67 | static FILE *a_file; |
| 68 | |
| 69 | static GString *a_string; |
| 70 | |
| 71 | static gboolean write_traceset_header(void *hook_data, void *call_data) |
| 72 | { |
| 73 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
| 74 | |
| 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 */ |
| 81 | if(a_raw) { |
| 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 */ |
| 85 | } else { |
| 86 | fprintf(a_file,"<TRACESET NUM_TRACES=%d/>\n", |
| 87 | lttv_traceset_number(tc->ts)); |
| 88 | } |
| 89 | |
| 90 | return FALSE; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | static gboolean write_traceset_footer(void *hook_data, void *call_data) |
| 95 | { |
| 96 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
| 97 | GQuark q; |
| 98 | gchar *string; |
| 99 | |
| 100 | if(a_raw) { |
| 101 | |
| 102 | } else { |
| 103 | fprintf(a_file,"</TRACESET>\n"); |
| 104 | } |
| 105 | |
| 106 | if(a_file_name != NULL) fclose(a_file); |
| 107 | |
| 108 | if(a_raw) { |
| 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"); |
| 112 | } else { |
| 113 | if(a_quark_file_name == NULL) a_file = stdout; |
| 114 | else a_file = fopen(a_quark_file_name, "w"); |
| 115 | } |
| 116 | |
| 117 | if(a_file == NULL) g_error("cannot open file %s", a_quark_file_name); |
| 118 | |
| 119 | fputc(HDR_QUARKS, a_file); |
| 120 | q = 1; |
| 121 | do { |
| 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); |
| 127 | q++; |
| 128 | } while(1); |
| 129 | |
| 130 | if(a_quark_file_name != NULL || a_file_name != NULL) fclose(a_file); |
| 131 | |
| 132 | } |
| 133 | |
| 134 | return FALSE; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | static gboolean write_trace_header(void *hook_data, void *call_data) |
| 139 | { |
| 140 | LttvTraceContext *tc = (LttvTraceContext *)call_data; |
| 141 | |
| 142 | if(a_raw) { |
| 143 | fputc(HDR_TRACE, a_file); |
| 144 | } else { |
| 145 | fprintf(a_file,"<TRACE TRACE_NUMBER=%d/>\n", |
| 146 | tc->index); |
| 147 | } |
| 148 | |
| 149 | return FALSE; |
| 150 | } |
| 151 | |
| 152 | static gboolean write_trace_footer(void *hook_data, void *call_data) |
| 153 | { |
| 154 | LttvTraceContext *tc = (LttvTraceContext *)call_data; |
| 155 | |
| 156 | if(a_raw) { |
| 157 | |
| 158 | } else { |
| 159 | fprintf(a_file,"</TRACE>\n"); |
| 160 | } |
| 161 | |
| 162 | return FALSE; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | static int for_each_event(void *hook_data, void *call_data) |
| 167 | { |
| 168 | guint *event_count = (guint*)hook_data; |
| 169 | |
| 170 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
| 171 | |
| 172 | LttvTracefileContext *tfc = (LttvTracefileContext *)call_data; |
| 173 | |
| 174 | LttvTracefileState *tfs = (LttvTracefileState *)call_data; |
| 175 | |
| 176 | LttEvent *e; |
| 177 | |
| 178 | LttvAttributeValue value_filter; |
| 179 | |
| 180 | /* Only save at LTTV_STATE_SAVE_INTERVAL */ |
| 181 | if(likely((*event_count)++ < LTTV_STATE_SAVE_INTERVAL)) |
| 182 | return FALSE; |
| 183 | else |
| 184 | *event_count = 0; |
| 185 | |
| 186 | guint cpu = tfs->cpu; |
| 187 | LttvTraceState *ts = (LttvTraceState*)tfc->t_context; |
| 188 | LttvProcessState *process = ts->running_process[cpu]; |
| 189 | |
| 190 | e = ltt_tracefile_get_event(tfc->tf); |
| 191 | |
| 192 | if(a_raw) { |
| 193 | lttv_state_write_raw(ts, tfs->parent.timestamp, a_file); |
| 194 | } else { |
| 195 | lttv_state_write(ts, tfs->parent.timestamp, a_file); |
| 196 | } |
| 197 | |
| 198 | return FALSE; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | static void init() |
| 203 | { |
| 204 | LttvAttributeValue value; |
| 205 | |
| 206 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
| 207 | |
| 208 | g_info("Init precomputeState.c"); |
| 209 | |
| 210 | a_string = g_string_new(""); |
| 211 | |
| 212 | a_file_name = NULL; |
| 213 | lttv_option_add("output", 'o', |
| 214 | "output file where the saved states are to be written", |
| 215 | "file name", |
| 216 | LTTV_OPT_STRING, &a_file_name, NULL, NULL); |
| 217 | |
| 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", |
| 221 | "file name", |
| 222 | LTTV_OPT_STRING, &a_quark_file_name, NULL, NULL); |
| 223 | |
| 224 | lttv_option_add("raw", 'r', |
| 225 | "Output in raw binary", |
| 226 | "Raw binary", |
| 227 | LTTV_OPT_NONE, &a_raw, NULL, NULL); |
| 228 | |
| 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); |
| 233 | |
| 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); |
| 238 | |
| 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); |
| 243 | |
| 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, |
| 248 | LTTV_PRIO_DEFAULT); |
| 249 | |
| 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, |
| 254 | LTTV_PRIO_DEFAULT); |
| 255 | } |
| 256 | |
| 257 | static void destroy() |
| 258 | { |
| 259 | g_info("Destroy precomputeState"); |
| 260 | |
| 261 | lttv_option_remove("output"); |
| 262 | |
| 263 | lttv_option_remove("qoutput"); |
| 264 | |
| 265 | lttv_option_remove("raw"); |
| 266 | |
| 267 | g_string_free(a_string, TRUE); |
| 268 | |
| 269 | lttv_hooks_remove_data(event_hook, for_each_event, NULL); |
| 270 | |
| 271 | lttv_hooks_remove_data(before_trace, write_trace_header, NULL); |
| 272 | |
| 273 | lttv_hooks_remove_data(before_trace, write_traceset_header, NULL); |
| 274 | |
| 275 | lttv_hooks_remove_data(before_trace, write_traceset_footer, NULL); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | LTTV_MODULE("precomputeState", "Precompute states", \ |
| 280 | "Precompute states in a trace, XML or binary output.", \ |
| 281 | init, destroy, "stats", "batchAnalysis", "option", "print") |
| 282 | |