| 1 | /* This file is part of the Linux Trace Toolkit viewer |
| 2 | * Copyright (C) 2003-2004 Michel Dagenais |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License Version 2 as |
| 6 | * published by the Free Software Foundation; |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 16 | * MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* This module inserts a hook in the program main loop. This hook processes |
| 20 | all the events in the main tracefile. */ |
| 21 | |
| 22 | |
| 23 | #include <lttv/lttv.h> |
| 24 | #include <lttv/attribute.h> |
| 25 | #include <lttv/hook.h> |
| 26 | #include <lttv/option.h> |
| 27 | #include <lttv/module.h> |
| 28 | #include <lttv/processTrace.h> |
| 29 | #include <lttv/state.h> |
| 30 | #include <lttv/stats.h> |
| 31 | #include <ltt/trace.h> |
| 32 | |
| 33 | static LttvTraceset *traceset; |
| 34 | |
| 35 | static LttvHooks |
| 36 | *before_traceset, |
| 37 | *after_traceset, |
| 38 | *before_trace, |
| 39 | *after_trace, |
| 40 | *before_tracefile, |
| 41 | *after_tracefile, |
| 42 | *before_event, |
| 43 | *after_event, |
| 44 | *main_hooks; |
| 45 | |
| 46 | static char *a_trace; |
| 47 | |
| 48 | static gboolean a_stats; |
| 49 | |
| 50 | void lttv_trace_option(void *hook_data) |
| 51 | { |
| 52 | LttTrace *trace; |
| 53 | |
| 54 | trace = ltt_trace_open(a_trace); |
| 55 | if(trace == NULL) g_critical("cannot open trace %s", a_trace); |
| 56 | lttv_traceset_add(traceset, lttv_trace_new(trace)); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | static gboolean process_traceset(void *hook_data, void *call_data) |
| 61 | { |
| 62 | LttvTracesetStats *tscs; |
| 63 | |
| 64 | LttvTracesetContext *tc; |
| 65 | |
| 66 | LttTime start, end; |
| 67 | |
| 68 | g_info("BatchAnalysis begin process traceset"); |
| 69 | |
| 70 | tscs = g_object_new(LTTV_TRACESET_STATS_TYPE, NULL); |
| 71 | tc = &tscs->parent.parent; |
| 72 | |
| 73 | g_info("BatchAnalysis initialize context"); |
| 74 | |
| 75 | lttv_context_init(tc, traceset); |
| 76 | lttv_state_add_event_hooks(&tscs->parent); |
| 77 | if(a_stats) lttv_stats_add_event_hooks(tscs); |
| 78 | |
| 79 | lttv_traceset_context_add_hooks(tc, |
| 80 | before_traceset, after_traceset, NULL, before_trace, after_trace, |
| 81 | NULL, before_tracefile, after_tracefile, NULL, before_event, after_event); |
| 82 | |
| 83 | start.tv_sec = 0; |
| 84 | start.tv_nsec = 0; |
| 85 | end.tv_sec = G_MAXULONG; |
| 86 | end.tv_nsec = G_MAXULONG; |
| 87 | |
| 88 | g_info("BatchAnalysis process traceset"); |
| 89 | |
| 90 | lttv_process_traceset_seek_time(tc, start); |
| 91 | lttv_process_traceset(tc, end, G_MAXULONG); |
| 92 | |
| 93 | g_info("BatchAnalysis destroy context"); |
| 94 | |
| 95 | lttv_traceset_context_remove_hooks(tc, |
| 96 | before_traceset, after_traceset, NULL, before_trace, after_trace, |
| 97 | NULL, before_tracefile, after_tracefile, NULL, before_event, after_event); |
| 98 | lttv_state_remove_event_hooks(&tscs->parent); |
| 99 | if(a_stats) lttv_stats_remove_event_hooks(tscs); |
| 100 | lttv_context_fini(tc); |
| 101 | g_object_unref(tscs); |
| 102 | |
| 103 | g_info("BatchAnalysis end process traceset"); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | G_MODULE_EXPORT void init(LttvModule *self, int argc, char **argv) |
| 108 | { |
| 109 | LttvAttributeValue value; |
| 110 | |
| 111 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
| 112 | |
| 113 | g_info("Init batchAnalysis.c"); |
| 114 | |
| 115 | lttv_option_add("trace", 't', |
| 116 | "add a trace to the trace set to analyse", |
| 117 | "pathname of the directory containing the trace", |
| 118 | LTTV_OPT_STRING, &a_trace, lttv_trace_option, NULL); |
| 119 | |
| 120 | a_stats = FALSE; |
| 121 | lttv_option_add("stats", 's', |
| 122 | "write the traceset and trace statistics", |
| 123 | "", |
| 124 | LTTV_OPT_NONE, &a_stats, NULL, NULL); |
| 125 | |
| 126 | |
| 127 | traceset = lttv_traceset_new(); |
| 128 | |
| 129 | before_traceset = lttv_hooks_new(); |
| 130 | after_traceset = lttv_hooks_new(); |
| 131 | before_trace = lttv_hooks_new(); |
| 132 | after_trace = lttv_hooks_new(); |
| 133 | before_tracefile = lttv_hooks_new(); |
| 134 | after_tracefile = lttv_hooks_new(); |
| 135 | before_event = lttv_hooks_new(); |
| 136 | after_event = lttv_hooks_new(); |
| 137 | |
| 138 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/before", |
| 139 | LTTV_POINTER, &value)); |
| 140 | *(value.v_pointer) = before_traceset; |
| 141 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/after", |
| 142 | LTTV_POINTER, &value)); |
| 143 | *(value.v_pointer) = after_traceset; |
| 144 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/before", |
| 145 | LTTV_POINTER, &value)); |
| 146 | *(value.v_pointer) = before_trace; |
| 147 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/after", |
| 148 | LTTV_POINTER, &value)); |
| 149 | *(value.v_pointer) = after_trace; |
| 150 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/tracefile/before", |
| 151 | LTTV_POINTER, &value)); |
| 152 | *(value.v_pointer) = before_tracefile; |
| 153 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/tracefile/after", |
| 154 | LTTV_POINTER, &value)); |
| 155 | *(value.v_pointer) = after_tracefile; |
| 156 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event/before", |
| 157 | LTTV_POINTER, &value)); |
| 158 | *(value.v_pointer) = before_event; |
| 159 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event/after", |
| 160 | LTTV_POINTER, &value)); |
| 161 | *(value.v_pointer) = after_event; |
| 162 | |
| 163 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/main/before", |
| 164 | LTTV_POINTER, &value)); |
| 165 | g_assert((main_hooks = *(value.v_pointer)) != NULL); |
| 166 | lttv_hooks_add(main_hooks, process_traceset, NULL); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | G_MODULE_EXPORT void destroy() |
| 171 | { |
| 172 | guint i, nb; |
| 173 | |
| 174 | LttvTrace *trace; |
| 175 | |
| 176 | g_info("Destroy batchAnalysis.c"); |
| 177 | |
| 178 | lttv_option_remove("trace"); |
| 179 | lttv_option_remove("stats"); |
| 180 | |
| 181 | lttv_hooks_destroy(before_traceset); |
| 182 | lttv_hooks_destroy(after_traceset); |
| 183 | lttv_hooks_destroy(before_trace); |
| 184 | lttv_hooks_destroy(after_trace); |
| 185 | lttv_hooks_destroy(before_tracefile); |
| 186 | lttv_hooks_destroy(after_tracefile); |
| 187 | lttv_hooks_destroy(before_event); |
| 188 | lttv_hooks_destroy(after_event); |
| 189 | lttv_hooks_remove_data(main_hooks, process_traceset, NULL); |
| 190 | |
| 191 | nb = lttv_traceset_number(traceset); |
| 192 | for(i = 0 ; i < nb ; i++) { |
| 193 | trace = lttv_traceset_get(traceset, i); |
| 194 | ltt_trace_close(lttv_trace(trace)); |
| 195 | lttv_trace_destroy(trace); |
| 196 | } |
| 197 | |
| 198 | lttv_traceset_destroy(traceset); |
| 199 | } |
| 200 | |