Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
new tracing functions to deal with trace markers
[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 "instr/private.h"
8
9
10 #ifdef HAVE_TRACING
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(tracing,"Tracing Interface");
13
14 static xbt_dict_t defined_types;
15 static xbt_dict_t created_categories;
16
17 int trace_mask;
18
19 /** \ingroup tracing
20  * \brief Simple initialization of tracing.
21  *
22  * \param filename of the file that will contain the traces
23  * \return 0 if everything is ok
24  */
25 int TRACE_start (const char *filename)
26 {
27   return TRACE_start_with_mask (filename, TRACE_PLATFORM);
28 }
29
30 /** \ingroup tracing
31  * \brief Initialization of tracing.
32  *
33  * Function to be called at first when tracing a simulation
34  *
35  * \param name of the file that will contain the traces
36  * \param mask to take into account during trace
37  * \return 0 if everything is ok
38  */
39 int TRACE_start_with_mask(const char *filename, int mask) {
40         FILE *file = NULL;
41   if (IS_TRACING) { /* what? trace is already active... ignore.. */
42         THROW0 (tracing_error, TRACE_ERROR_START,
43                 "TRACE_start called, but tracing is already active");
44     return 0;
45   }
46
47   /* checking mask */
48   if (!(mask&TRACE_PLATFORM ||
49       mask&TRACE_TASK ||
50       mask&TRACE_PROCESS ||
51       mask&TRACE_VOLUME)){
52     THROW0 (tracing_error, TRACE_ERROR_MASK,
53           "unknown tracing mask");
54   }
55
56   file = fopen(filename, "w");
57   if (!file) {
58     THROW1 (tracing_error, TRACE_ERROR_START,
59                 "Tracefile %s could not be opened for writing.", filename);
60   } else {
61     TRACE_paje_start (file);
62   }
63   TRACE_paje_create_header();
64
65   /* setting the mask */
66   trace_mask = mask;
67
68   /* define paje hierarchy for tracing */
69   pajeDefineContainerType("PLATFORM", "0", "platform");
70   pajeDefineContainerType("HOST", "PLATFORM", "HOST");
71   pajeDefineContainerType("LINK", "PLATFORM", "LINK");
72
73   if (IS_TRACING_PLATFORM){
74     pajeDefineVariableType ("power", "HOST", "power");
75     pajeDefineVariableType ("bandwidth", "LINK", "bandwidth");
76     pajeDefineVariableType ("latency", "LINK", "latency");
77   }
78
79   if (IS_TRACING_PROCESSES || IS_TRACING_VOLUME){
80     //processes grouped by host
81     pajeDefineContainerType("PROCESS", "HOST", "PROCESS");
82   }
83
84   if (IS_TRACING_PROCESSES){
85     pajeDefineStateType("category", "PROCESS", "category");
86     pajeDefineStateType("presence", "PROCESS", "presence");
87   }
88
89   if (IS_TRACING_VOLUME){
90     pajeDefineLinkType ("volume", "0", "PROCESS", "PROCESS", "volume");
91   }
92
93   if (IS_TRACING_TASKS){
94     //tasks grouped by host
95     pajeDefineContainerType("TASK", "HOST", "TASK");
96     pajeDefineStateType("category", "TASK", "category");
97     pajeDefineStateType("presence", "TASK", "presence");
98   }
99
100   /* creating the platform */
101   pajeCreateContainer(MSG_get_clock(), "platform", "PLATFORM", "0", "simgrid-platform");
102
103   /* other trace initialization */
104   defined_types = xbt_dict_new();
105   created_categories = xbt_dict_new();
106   __TRACE_msg_init();
107   __TRACE_surf_init();
108   __TRACE_msg_process_init ();
109
110   return 0;
111 }
112
113 int TRACE_end() {
114         FILE *file = NULL;
115   if (!IS_TRACING) return 1;
116   file = TRACE_paje_end();
117   fclose (file);
118   return 0;
119 }
120
121 int TRACE_category (const char *category)
122 {
123   static int first_time = 1;
124   if (!IS_TRACING) return 1;
125
126   if (first_time){
127           TRACE_define_type ("user_type", "0", 1);
128           first_time = 0;
129   }
130   return TRACE_create_category (category, "user_type", "0");
131 }
132
133 void TRACE_define_type (const char *type,
134                 const char *parent_type, int final) {
135         char *val_one = NULL;
136   if (!IS_TRACING) return;
137
138   //check if type is already defined
139   if (xbt_dict_get_or_null (defined_types, type)){
140     THROW1 (tracing_error, TRACE_ERROR_TYPE_ALREADY_DEFINED, "Type %s is already defined", type);
141   }
142   //check if parent_type is already defined
143   if (strcmp(parent_type, "0") && !xbt_dict_get_or_null (defined_types, parent_type)) {
144     THROW1 (tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED, "Type (used as parent) %s is not defined", parent_type);
145   }
146
147   pajeDefineContainerType(type, parent_type, type);
148   if (final) {
149     //for m_process_t
150     if (IS_TRACING_PROCESSES) pajeDefineContainerType ("process", type, "process");
151     if (IS_TRACING_PROCESSES) pajeDefineStateType ("process-state", "process", "process-state");
152
153     if (IS_TRACING_TASKS) pajeDefineContainerType ("task", type, "task");
154     if (IS_TRACING_TASKS) pajeDefineStateType ("task-state", "task", "task-state");
155   }
156   val_one = xbt_strdup ("1");
157   xbt_dict_set (defined_types, type, &val_one, xbt_free);
158 }
159
160 int TRACE_create_category (const char *category,
161                 const char *type, const char *parent_category)
162 {
163   char state[100];
164   char *val_one = NULL;
165   if (!IS_TRACING) return 1;
166
167   //check if type is defined
168   if (!xbt_dict_get_or_null (defined_types, type)) {
169          THROW1 (tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED, "Type %s is not defined", type);
170          return 1;
171   }
172   //check if parent_category exists
173   if (strcmp(parent_category, "0") && !xbt_dict_get_or_null (created_categories, parent_category)){
174      THROW1 (tracing_error, TRACE_ERROR_CATEGORY_NOT_DEFINED, "Category (used as parent) %s is not created", parent_category);
175      return 1;
176   }
177   //check if category is created
178   if (xbt_dict_get_or_null (created_categories, category)){
179      return 1;
180   }
181
182   pajeCreateContainer(MSG_get_clock(), category, type, parent_category, category);
183
184   /* for registering application categories on top of platform */
185
186   snprintf (state, 100, "b%s", category);
187   if (IS_TRACING_PLATFORM) pajeDefineVariableType (state, "LINK", state);
188   snprintf (state, 100, "p%s", category);
189   if (IS_TRACING_PLATFORM) pajeDefineVariableType (state, "HOST", state);
190
191   val_one = xbt_strdup ("1");
192   xbt_dict_set (created_categories, category, &val_one, xbt_free);
193   return 0;
194 }
195
196 void TRACE_set_mask (int mask)
197 {
198   if (mask != TRACE_PLATFORM){
199      return;
200   }else{
201      trace_mask = mask;
202   }
203 }
204
205 void TRACE_declare_mark (const char *mark_type)
206 {
207   if (!IS_TRACING) return;
208   if (!mark_type) return;
209
210   pajeDefineEventType (mark_type, "0", mark_type);
211 }
212
213 void TRACE_mark (const char *mark_type, const char *mark_value)
214 {
215   if (!IS_TRACING) return;
216   if (!mark_type || !mark_value) return;
217
218   pajeNewEvent (MSG_get_clock(), mark_type, "0", mark_value);
219 }
220
221 #endif /* HAVE_TRACING */