1 /* Copyright (c) 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "simgrid_config.h"
11 #include "instr/instr_private.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(tracing, "Tracing Interface");
15 static xbt_dict_t defined_types;
16 static xbt_dict_t created_categories;
20 // tracing system must be:
21 // - enabled (with --cfg=tracing:1)
22 // - already configured (TRACE_global_init already called)
23 if (!(TRACE_is_enabled() && TRACE_is_configured())){
27 /* open the trace file */
28 char *filename = TRACE_get_filename();
30 THROW0(tracing_error, TRACE_ERROR_START,
31 "Trace filename is not initialized.");
34 FILE *file = fopen(filename, "w");
36 THROW1(tracing_error, TRACE_ERROR_START,
37 "Tracefile %s could not be opened for writing.", filename);
39 TRACE_paje_start(file);
46 TRACE_paje_create_header();
48 /* define paje hierarchy for tracing */
49 pajeDefineContainerType("PLATFORM", "0", "platform");
50 pajeDefineContainerType("HOST", "PLATFORM", "HOST");
51 pajeDefineContainerType("LINK", "PLATFORM", "LINK");
52 pajeDefineVariableType("power", "HOST", "power");
53 pajeDefineVariableType("bandwidth", "LINK", "bandwidth");
54 pajeDefineVariableType("latency", "LINK", "latency");
55 pajeDefineEventType("source", "LINK", "source");
56 pajeDefineEventType("destination", "LINK", "destination");
58 if (IS_TRACING_PLATFORM) {
59 if (TRACE_uncategorized()){
60 pajeDefineVariableType("power_used", "HOST", "power_used");
61 pajeDefineVariableType("bandwidth_used", "LINK", "bandwidth_used");
65 if (IS_TRACING_PROCESSES || IS_TRACING_VOLUME) {
66 //processes grouped by host
67 pajeDefineContainerType("PROCESS", "HOST", "PROCESS");
70 if (IS_TRACING_PROCESSES) {
71 pajeDefineStateType("category", "PROCESS", "category");
72 pajeDefineStateType("presence", "PROCESS", "presence");
75 if (IS_TRACING_VOLUME) {
76 pajeDefineLinkType("volume", "0", "PROCESS", "PROCESS", "volume");
79 if (IS_TRACING_TASKS) {
80 //tasks grouped by host
81 pajeDefineContainerType("TASK", "HOST", "TASK");
82 pajeDefineStateType("category", "TASK", "category");
83 pajeDefineStateType("presence", "TASK", "presence");
86 if (IS_TRACING_SMPI) {
87 if (TRACE_smpi_is_grouped()){
88 pajeDefineContainerType("MPI_PROCESS", "HOST", "MPI_PROCESS");
90 pajeDefineContainerType("MPI_PROCESS", "PLATFORM", "MPI_PROCESS");
92 pajeDefineStateType("MPI_STATE", "MPI_PROCESS", "MPI_STATE");
93 pajeDefineLinkType("MPI_LINK", "0", "MPI_PROCESS", "MPI_PROCESS",
97 /* creating the platform */
98 pajeCreateContainer(MSG_get_clock(), "platform", "PLATFORM", "0",
101 /* other trace initialization */
102 defined_types = xbt_dict_new();
103 created_categories = xbt_dict_new();
104 TRACE_msg_task_alloc();
105 TRACE_category_alloc();
107 TRACE_msg_process_alloc();
114 if (!TRACE_is_active())
116 FILE *file = TRACE_paje_end();
119 TRACE_desactivate ();
123 int TRACE_category(const char *category)
125 return TRACE_category_with_color (category, NULL);
128 int TRACE_category_with_color (const char *category, const char *color)
130 static int first_time = 1;
131 if (!TRACE_is_active())
135 TRACE_define_type("user_type", "0", 1);
138 return TRACE_create_category_with_color(category, "user_type", "0", color);
141 void TRACE_define_type(const char *type,
142 const char *parent_type, int final)
144 char *val_one = NULL;
145 if (!TRACE_is_active())
148 //check if type is already defined
149 if (xbt_dict_get_or_null(defined_types, type)) {
150 THROW1(tracing_error, TRACE_ERROR_TYPE_ALREADY_DEFINED,
151 "Type %s is already defined", type);
153 //check if parent_type is already defined
154 if (strcmp(parent_type, "0")
155 && !xbt_dict_get_or_null(defined_types, parent_type)) {
156 THROW1(tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED,
157 "Type (used as parent) %s is not defined", parent_type);
160 pajeDefineContainerType(type, parent_type, type);
163 if (IS_TRACING_PROCESSES)
164 pajeDefineContainerType("process", type, "process");
165 if (IS_TRACING_PROCESSES)
166 pajeDefineStateType("process-state", "process", "process-state");
168 if (IS_TRACING_TASKS)
169 pajeDefineContainerType("task", type, "task");
170 if (IS_TRACING_TASKS)
171 pajeDefineStateType("task-state", "task", "task-state");
173 val_one = xbt_strdup("1");
174 xbt_dict_set(defined_types, type, &val_one, xbt_free);
177 int TRACE_create_category(const char *category,
178 const char *type, const char *parent_category)
180 return TRACE_create_category_with_color (category, type, parent_category, NULL);
183 int TRACE_create_category_with_color(const char *category,
185 const char *parent_category,
189 char *val_one = NULL;
190 if (!TRACE_is_active())
193 //check if type is defined
194 if (!xbt_dict_get_or_null(defined_types, type)) {
195 THROW1(tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED,
196 "Type %s is not defined", type);
199 //check if parent_category exists
200 if (strcmp(parent_category, "0")
201 && !xbt_dict_get_or_null(created_categories, parent_category)) {
202 THROW1(tracing_error, TRACE_ERROR_CATEGORY_NOT_DEFINED,
203 "Category (used as parent) %s is not created", parent_category);
206 //check if category is created
207 if (xbt_dict_get_or_null(created_categories, category)) {
211 pajeCreateContainer(MSG_get_clock(), category, type, parent_category,
214 char final_color[INSTR_DEFAULT_STR_SIZE];
216 //generate a random color
217 double red = drand48();
218 double green = drand48();
219 double blue = drand48();
220 snprintf (final_color, INSTR_DEFAULT_STR_SIZE, "%f %f %f", red, green, blue);
222 snprintf (final_color, INSTR_DEFAULT_STR_SIZE, "%s", color);
225 /* for registering application categories on top of platform */
226 snprintf(state, 100, "b%s", category);
227 if (IS_TRACING_PLATFORM)
228 pajeDefineVariableTypeWithColor(state, "LINK", state, final_color);
229 snprintf(state, 100, "p%s", category);
230 if (IS_TRACING_PLATFORM)
231 pajeDefineVariableTypeWithColor(state, "HOST", state, final_color);
233 val_one = xbt_strdup("1");
234 xbt_dict_set(created_categories, category, &val_one, xbt_free);
238 void TRACE_declare_mark(const char *mark_type)
240 if (!TRACE_is_active())
245 pajeDefineEventType(mark_type, "0", mark_type);
248 void TRACE_mark(const char *mark_type, const char *mark_value)
250 if (!TRACE_is_active())
252 if (!mark_type || !mark_value)
255 pajeNewEvent(MSG_get_clock(), mark_type, "0", mark_value);
258 #endif /* HAVE_TRACING */