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/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 if (!TRACE_is_configured()) {
21 THROW0(tracing_error, TRACE_ERROR_START,
22 "TRACE_start should be called after SimGrid initialization functions.");
26 if (!TRACE_is_enabled()){
30 if (IS_TRACING) { /* what? trace is already active... ignore.. */
31 THROW0(tracing_error, TRACE_ERROR_START,
32 "TRACE_start called, but tracing is already active.");
36 char *filename = TRACE_get_filename();
38 THROW0(tracing_error, TRACE_ERROR_START,
39 "Trace filename is not initialized.");
42 FILE *file = fopen(filename, "w");
44 THROW1(tracing_error, TRACE_ERROR_START,
45 "Tracefile %s could not be opened for writing.", filename);
47 TRACE_paje_start(file);
49 TRACE_paje_create_header();
51 /* define paje hierarchy for tracing */
52 pajeDefineContainerType("PLATFORM", "0", "platform");
53 pajeDefineContainerType("HOST", "PLATFORM", "HOST");
54 pajeDefineContainerType("LINK", "PLATFORM", "LINK");
55 pajeDefineVariableType("power", "HOST", "power");
56 pajeDefineVariableType("bandwidth", "LINK", "bandwidth");
57 pajeDefineVariableType("latency", "LINK", "latency");
58 pajeDefineEventType("source", "LINK", "source");
59 pajeDefineEventType("destination", "LINK", "destination");
61 if (IS_TRACING_PLATFORM) {
62 if (TRACE_uncategorized()){
63 pajeDefineVariableType("power_used", "HOST", "power_used");
64 pajeDefineVariableType("bandwidth_used", "LINK", "bandwidth_used");
68 if (IS_TRACING_PROCESSES || IS_TRACING_VOLUME) {
69 //processes grouped by host
70 pajeDefineContainerType("PROCESS", "HOST", "PROCESS");
73 if (IS_TRACING_PROCESSES) {
74 pajeDefineStateType("category", "PROCESS", "category");
75 pajeDefineStateType("presence", "PROCESS", "presence");
78 if (IS_TRACING_VOLUME) {
79 pajeDefineLinkType("volume", "0", "PROCESS", "PROCESS", "volume");
82 if (IS_TRACING_TASKS) {
83 //tasks grouped by host
84 pajeDefineContainerType("TASK", "HOST", "TASK");
85 pajeDefineStateType("category", "TASK", "category");
86 pajeDefineStateType("presence", "TASK", "presence");
89 if (IS_TRACING_SMPI) {
90 if (TRACE_smpi_is_grouped()){
91 pajeDefineContainerType("MPI_PROCESS", "HOST", "MPI_PROCESS");
93 pajeDefineContainerType("MPI_PROCESS", "PLATFORM", "MPI_PROCESS");
95 pajeDefineStateType("MPI_STATE", "MPI_PROCESS", "MPI_STATE");
96 pajeDefineLinkType("MPI_LINK", "0", "MPI_PROCESS", "MPI_PROCESS",
100 /* creating the platform */
101 pajeCreateContainer(MSG_get_clock(), "platform", "PLATFORM", "0",
104 /* other trace initialization */
105 defined_types = xbt_dict_new();
106 created_categories = xbt_dict_new();
107 TRACE_msg_task_alloc();
108 TRACE_category_alloc();
110 TRACE_msg_process_alloc();
121 file = TRACE_paje_end();
126 int TRACE_category(const char *category)
128 return TRACE_category_with_color (category, NULL);
131 int TRACE_category_with_color (const char *category, const char *color)
133 static int first_time = 1;
138 TRACE_define_type("user_type", "0", 1);
141 return TRACE_create_category_with_color(category, "user_type", "0", color);
144 void TRACE_define_type(const char *type,
145 const char *parent_type, int final)
147 char *val_one = NULL;
151 //check if type is already defined
152 if (xbt_dict_get_or_null(defined_types, type)) {
153 THROW1(tracing_error, TRACE_ERROR_TYPE_ALREADY_DEFINED,
154 "Type %s is already defined", type);
156 //check if parent_type is already defined
157 if (strcmp(parent_type, "0")
158 && !xbt_dict_get_or_null(defined_types, parent_type)) {
159 THROW1(tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED,
160 "Type (used as parent) %s is not defined", parent_type);
163 pajeDefineContainerType(type, parent_type, type);
166 if (IS_TRACING_PROCESSES)
167 pajeDefineContainerType("process", type, "process");
168 if (IS_TRACING_PROCESSES)
169 pajeDefineStateType("process-state", "process", "process-state");
171 if (IS_TRACING_TASKS)
172 pajeDefineContainerType("task", type, "task");
173 if (IS_TRACING_TASKS)
174 pajeDefineStateType("task-state", "task", "task-state");
176 val_one = xbt_strdup("1");
177 xbt_dict_set(defined_types, type, &val_one, xbt_free);
180 int TRACE_create_category(const char *category,
181 const char *type, const char *parent_category)
183 return TRACE_create_category_with_color (category, type, parent_category, NULL);
186 int TRACE_create_category_with_color(const char *category,
188 const char *parent_category,
192 char *val_one = NULL;
196 //check if type is defined
197 if (!xbt_dict_get_or_null(defined_types, type)) {
198 THROW1(tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED,
199 "Type %s is not defined", type);
202 //check if parent_category exists
203 if (strcmp(parent_category, "0")
204 && !xbt_dict_get_or_null(created_categories, parent_category)) {
205 THROW1(tracing_error, TRACE_ERROR_CATEGORY_NOT_DEFINED,
206 "Category (used as parent) %s is not created", parent_category);
209 //check if category is created
210 if (xbt_dict_get_or_null(created_categories, category)) {
214 pajeCreateContainer(MSG_get_clock(), category, type, parent_category,
217 char final_color[INSTR_DEFAULT_STR_SIZE];
219 //generate a random color
220 double red = drand48();
221 double green = drand48();
222 double blue = drand48();
223 snprintf (final_color, INSTR_DEFAULT_STR_SIZE, "%f %f %f", red, green, blue);
225 snprintf (final_color, INSTR_DEFAULT_STR_SIZE, "%s", color);
228 /* for registering application categories on top of platform */
229 snprintf(state, 100, "b%s", category);
230 if (IS_TRACING_PLATFORM)
231 pajeDefineVariableTypeWithColor(state, "LINK", state, final_color);
232 snprintf(state, 100, "p%s", category);
233 if (IS_TRACING_PLATFORM)
234 pajeDefineVariableTypeWithColor(state, "HOST", state, final_color);
236 val_one = xbt_strdup("1");
237 xbt_dict_set(created_categories, category, &val_one, xbt_free);
241 void TRACE_declare_mark(const char *mark_type)
248 pajeDefineEventType(mark_type, "0", mark_type);
251 void TRACE_mark(const char *mark_type, const char *mark_value)
255 if (!mark_type || !mark_value)
258 pajeNewEvent(MSG_get_clock(), mark_type, "0", mark_value);
261 #endif /* HAVE_TRACING */