Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
573f498e0d2a6b21b811746c916dbda001aed054
[simgrid.git] / src / instr / interface.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "simgrid_config.h"
8
9 #ifdef HAVE_TRACING
10
11 #include "instr/private.h"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(tracing, "Tracing Interface");
14
15 static xbt_dict_t defined_types;
16 static xbt_dict_t created_categories;
17
18 int TRACE_start()
19 {
20   if (!TRACE_is_configured()) {
21     THROW0(tracing_error, TRACE_ERROR_START,
22            "TRACE_start should be called after SimGrid initialization functions.");
23     return 0;
24   }
25
26   if (IS_TRACING) {             /* what? trace is already active... ignore.. */
27     THROW0(tracing_error, TRACE_ERROR_START,
28            "TRACE_start called, but tracing is already active.");
29     return 0;
30   }
31
32   char *filename = TRACE_get_filename();
33   if (!filename) {
34     THROW0(tracing_error, TRACE_ERROR_START,
35            "Trace filename is not initialized.");
36     return 0;
37   }
38   FILE *file = fopen(filename, "w");
39   if (!file) {
40     THROW1(tracing_error, TRACE_ERROR_START,
41            "Tracefile %s could not be opened for writing.", filename);
42   } else {
43     TRACE_paje_start(file);
44   }
45   TRACE_paje_create_header();
46
47   /* define paje hierarchy for tracing */
48   pajeDefineContainerType("PLATFORM", "0", "platform");
49   pajeDefineContainerType("HOST", "PLATFORM", "HOST");
50   pajeDefineContainerType("LINK", "PLATFORM", "LINK");
51
52   if (IS_TRACING_PLATFORM) {
53     pajeDefineVariableType("power", "HOST", "power");
54     if (TRACE_uncategorized()){
55       pajeDefineVariableType("power_used", "HOST", "power_used");
56       pajeDefineVariableType("bandwidth_used", "LINK", "bandwidth_used");
57     }
58     pajeDefineVariableType("bandwidth", "LINK", "bandwidth");
59     pajeDefineVariableType("latency", "LINK", "latency");
60     pajeDefineEventType("source", "LINK", "source");
61     pajeDefineEventType("destination", "LINK", "destination");
62   }
63
64   if (IS_TRACING_PROCESSES || IS_TRACING_VOLUME) {
65     //processes grouped by host
66     pajeDefineContainerType("PROCESS", "HOST", "PROCESS");
67   }
68
69   if (IS_TRACING_PROCESSES) {
70     pajeDefineStateType("category", "PROCESS", "category");
71     pajeDefineStateType("presence", "PROCESS", "presence");
72   }
73
74   if (IS_TRACING_VOLUME) {
75     pajeDefineLinkType("volume", "0", "PROCESS", "PROCESS", "volume");
76   }
77
78   if (IS_TRACING_TASKS) {
79     //tasks grouped by host
80     pajeDefineContainerType("TASK", "HOST", "TASK");
81     pajeDefineStateType("category", "TASK", "category");
82     pajeDefineStateType("presence", "TASK", "presence");
83   }
84
85   if (IS_TRACING_SMPI) {
86     if (TRACE_smpi_is_grouped()){
87       pajeDefineContainerType("MPI_PROCESS", "HOST", "MPI_PROCESS");
88     }else{
89       pajeDefineContainerType("MPI_PROCESS", "PLATFORM", "MPI_PROCESS");
90     }
91     pajeDefineStateType("MPI_STATE", "MPI_PROCESS", "MPI_STATE");
92     pajeDefineLinkType("MPI_LINK", "0", "MPI_PROCESS", "MPI_PROCESS",
93                        "MPI_LINK");
94   }
95
96   /* creating the platform */
97   pajeCreateContainer(MSG_get_clock(), "platform", "PLATFORM", "0",
98                       "simgrid-platform");
99
100   /* other trace initialization */
101   defined_types = xbt_dict_new();
102   created_categories = xbt_dict_new();
103   TRACE_msg_task_alloc();
104   TRACE_category_alloc();
105   TRACE_surf_alloc();
106   TRACE_msg_process_alloc();
107   TRACE_smpi_alloc();
108
109   return 0;
110 }
111
112 int TRACE_end()
113 {
114   FILE *file = NULL;
115   if (!IS_TRACING)
116     return 1;
117   file = TRACE_paje_end();
118   fclose(file);
119   return 0;
120 }
121
122 int TRACE_category(const char *category)
123 {
124   static int first_time = 1;
125   if (!IS_TRACING)
126     return 1;
127
128   if (first_time) {
129     TRACE_define_type("user_type", "0", 1);
130     first_time = 0;
131   }
132   return TRACE_create_category(category, "user_type", "0");
133 }
134
135 void TRACE_define_type(const char *type,
136                        const char *parent_type, int final)
137 {
138   char *val_one = NULL;
139   if (!IS_TRACING)
140     return;
141
142   //check if type is already defined
143   if (xbt_dict_get_or_null(defined_types, type)) {
144     THROW1(tracing_error, TRACE_ERROR_TYPE_ALREADY_DEFINED,
145            "Type %s is already defined", type);
146   }
147   //check if parent_type is already defined
148   if (strcmp(parent_type, "0")
149       && !xbt_dict_get_or_null(defined_types, parent_type)) {
150     THROW1(tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED,
151            "Type (used as parent) %s is not defined", parent_type);
152   }
153
154   pajeDefineContainerType(type, parent_type, type);
155   if (final) {
156     //for m_process_t
157     if (IS_TRACING_PROCESSES)
158       pajeDefineContainerType("process", type, "process");
159     if (IS_TRACING_PROCESSES)
160       pajeDefineStateType("process-state", "process", "process-state");
161
162     if (IS_TRACING_TASKS)
163       pajeDefineContainerType("task", type, "task");
164     if (IS_TRACING_TASKS)
165       pajeDefineStateType("task-state", "task", "task-state");
166   }
167   val_one = xbt_strdup("1");
168   xbt_dict_set(defined_types, type, &val_one, xbt_free);
169 }
170
171 int TRACE_create_category(const char *category,
172                           const char *type, const char *parent_category)
173 {
174   char state[100];
175   char *val_one = NULL;
176   if (!IS_TRACING)
177     return 1;
178
179   //check if type is defined
180   if (!xbt_dict_get_or_null(defined_types, type)) {
181     THROW1(tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED,
182            "Type %s is not defined", type);
183     return 1;
184   }
185   //check if parent_category exists
186   if (strcmp(parent_category, "0")
187       && !xbt_dict_get_or_null(created_categories, parent_category)) {
188     THROW1(tracing_error, TRACE_ERROR_CATEGORY_NOT_DEFINED,
189            "Category (used as parent) %s is not created", parent_category);
190     return 1;
191   }
192   //check if category is created
193   if (xbt_dict_get_or_null(created_categories, category)) {
194     return 1;
195   }
196
197   pajeCreateContainer(MSG_get_clock(), category, type, parent_category,
198                       category);
199
200   /* for registering application categories on top of platform */
201
202   snprintf(state, 100, "b%s", category);
203   if (IS_TRACING_PLATFORM)
204     pajeDefineVariableType(state, "LINK", state);
205   snprintf(state, 100, "p%s", category);
206   if (IS_TRACING_PLATFORM)
207     pajeDefineVariableType(state, "HOST", state);
208
209   val_one = xbt_strdup("1");
210   xbt_dict_set(created_categories, category, &val_one, xbt_free);
211   return 0;
212 }
213
214 void TRACE_declare_mark(const char *mark_type)
215 {
216   if (!IS_TRACING)
217     return;
218   if (!mark_type)
219     return;
220
221   pajeDefineEventType(mark_type, "0", mark_type);
222 }
223
224 void TRACE_mark(const char *mark_type, const char *mark_value)
225 {
226   if (!IS_TRACING)
227     return;
228   if (!mark_type || !mark_value)
229     return;
230
231   pajeNewEvent(MSG_get_clock(), mark_type, "0", mark_value);
232 }
233
234 #endif                          /* HAVE_TRACING */