Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix so enable_compile_warnings works nice when enable_tracing is on
[simgrid.git] / src / instr / interface.c
1 /*
2  * interface.c
3  *
4  *  Created on: Nov 23, 2009
5  *      Author: Lucas Schnorr
6  *     License: This program is free software; you can redistribute
7  *              it and/or modify it under the terms of the license
8  *              (GNU LGPL) which comes with this package.
9  *
10  *     Copyright (c) 2009 The SimGrid team.
11  */
12
13 #include "instr/private.h"
14
15
16 #ifdef HAVE_TRACING
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(tracing,"Tracing Interface");
19
20 static xbt_dict_t defined_types;
21 static xbt_dict_t created_categories;
22
23 int trace_mask;
24
25 /** \ingroup tracing
26  * \brief Simple initialization of tracing.
27  *
28  * \param filename of the file that will contain the traces
29  * \return 0 if everything is ok
30  */
31 int TRACE_start (const char *filename)
32 {
33   return TRACE_start_with_mask (filename, TRACE_PLATFORM);
34 }
35
36 /** \ingroup tracing
37  * \brief Initialization of tracing.
38  *
39  * Function to be called at first when tracing a simulation
40  *
41  * \param name of the file that will contain the traces
42  * \param mask to take into account during trace
43  * \return 0 if everything is ok
44  */
45 int TRACE_start_with_mask(const char *filename, int mask) {
46   if (IS_TRACING) { /* what? trace is already active... ignore.. */
47         THROW0 (tracing_error, TRACE_ERROR_START,
48                 "TRACE_start called, but tracing is already active");
49     return 0;
50   }
51
52   /* checking if the mask is good (only TRACE_PLATFORM for now) */
53   if (mask != TRACE_PLATFORM){
54     THROW0 (tracing_error, TRACE_ERROR_MASK,
55             "Only TRACE_PLATFORM mask is accepted for now");
56   }
57
58   FILE *file = fopen(filename, "w");
59   if (!file) {
60     THROW1 (tracing_error, TRACE_ERROR_START,
61                 "Tracefile %s could not be opened for writing.", filename);
62   } else {
63     TRACE_paje_start (file);
64   }
65   TRACE_paje_create_header();
66
67   /* setting the mask */
68   trace_mask = mask;
69
70   //check if options are correct
71   if (IS_TRACING_TASKS){
72         if (!IS_TRACING_PROCESSES){
73           TRACE_end();
74       THROW0 (tracing_error, TRACE_ERROR_MASK,
75               "TRACE_PROCESS must be enabled if TRACE_TASK is used");
76         }
77   }
78
79   if (IS_TRACING_PROCESSES|IS_TRACING_TASKS){
80         if (!IS_TRACING_PLATFORM){
81           TRACE_end();
82       THROW0 (tracing_error, TRACE_ERROR_MASK,
83                   "TRACE_PLATFORM must be enabled if TRACE_PROCESS or TRACE_TASK is used");
84         }
85   }
86
87   //defining platform hierarchy
88   if (IS_TRACING_PLATFORM) pajeDefineContainerType("PLATFORM", "0", "platform");
89   if (IS_TRACING_PLATFORM) pajeDefineContainerType("HOST", "PLATFORM", "HOST");
90   if (IS_TRACING_PLATFORM) pajeDefineVariableType ("power", "HOST", "power");
91   if (IS_TRACING_PLATFORM) pajeDefineContainerType("LINK", "PLATFORM", "LINK");
92   if (IS_TRACING_PLATFORM) pajeDefineVariableType ("bandwidth", "LINK", "bandwidth");
93   if (IS_TRACING_PLATFORM) pajeDefineVariableType ("latency", "LINK", "latency");
94
95   if (IS_TRACING_PROCESSES) pajeDefineContainerType("PROCESS", "HOST", "PROCESS");
96   if (IS_TRACING_PROCESSES) pajeDefineStateType("presence", "PROCESS", "presence");
97
98   if (IS_TRACING_PROCESSES){
99         if (IS_TRACING_TASKS) pajeDefineContainerType("TASK", "PROCESS", "TASK");
100   }else{
101         if (IS_TRACING_TASKS) pajeDefineContainerType("TASK", "HOST", "TASK");
102   }
103
104   if (IS_TRACING_PLATFORM) pajeCreateContainer(MSG_get_clock(), "platform", "PLATFORM", "0", "simgrid-platform");
105
106   defined_types = xbt_dict_new();
107   created_categories = xbt_dict_new();
108   __TRACE_msg_init();
109   __TRACE_surf_init();
110   __TRACE_msg_process_init ();
111
112   return 0;
113 }
114
115 int TRACE_end() {
116   if (!IS_TRACING) return 1;
117   FILE *file = TRACE_paje_end();
118   fclose (file);
119   return 0;
120 }
121
122 int TRACE_category (const char *category)
123 {
124   if (!IS_TRACING) return 1;
125   static int first_time = 1;
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   if (!IS_TRACING) return;
136
137   //check if type is already defined
138   if (xbt_dict_get_or_null (defined_types, type)){
139     THROW1 (tracing_error, TRACE_ERROR_TYPE_ALREADY_DEFINED, "Type %s is already defined", type);
140   }
141   //check if parent_type is already defined
142   if (strcmp(parent_type, "0") && !xbt_dict_get_or_null (defined_types, parent_type)) {
143     THROW1 (tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED, "Type (used as parent) %s is not defined", parent_type);
144   }
145
146   pajeDefineContainerType(type, parent_type, type);
147   if (final) {
148     //for m_process_t
149     if (IS_TRACING_PROCESSES) pajeDefineContainerType ("process", type, "process");
150     if (IS_TRACING_PROCESSES) pajeDefineStateType ("process-state", "process", "process-state");
151
152     if (IS_TRACING_TASKS) pajeDefineContainerType ("task", type, "task");
153     if (IS_TRACING_TASKS) pajeDefineStateType ("task-state", "task", "task-state");
154   }
155   xbt_dict_set (defined_types, type, xbt_strdup("1"), xbt_free);
156 }
157
158 int TRACE_create_category (const char *category,
159                 const char *type, const char *parent_category)
160 {
161   if (!IS_TRACING) return 1;
162
163   //check if type is defined
164   if (!xbt_dict_get_or_null (defined_types, type)) {
165          THROW1 (tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED, "Type %s is not defined", type);
166          return 1;
167   }
168   //check if parent_category exists
169   if (strcmp(parent_category, "0") && !xbt_dict_get_or_null (created_categories, parent_category)){
170      THROW1 (tracing_error, TRACE_ERROR_CATEGORY_NOT_DEFINED, "Category (used as parent) %s is not created", parent_category);
171      return 1;
172   }
173   //check if category is created
174   if (xbt_dict_get_or_null (created_categories, category)){
175      return 1;
176   }
177
178   pajeCreateContainer(MSG_get_clock(), category, type, parent_category, category);
179
180   /* for registering application categories on top of platform */
181   char state[100];
182   snprintf (state, 100, "b%s", category);
183   if (IS_TRACING_PLATFORM) pajeDefineVariableType (state, "LINK", state);
184   snprintf (state, 100, "p%s", category);
185   if (IS_TRACING_PLATFORM) pajeDefineVariableType (state, "HOST", state);
186
187   xbt_dict_set (created_categories, category, xbt_strdup("1"), xbt_free);
188   return 0;
189 }
190
191 void TRACE_set_mask (int mask)
192 {
193   if (mask != TRACE_PLATFORM){
194      return;
195   }else{
196      trace_mask = mask;
197   }
198 }
199
200 #endif /* HAVE_TRACING */