Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
allowing the user to set the three tracing masks
[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 mask */
53   if (mask != TRACE_PLATFORM &&
54       mask != TRACE_TASK &&
55       mask != TRACE_PROCESS){
56     THROW0 (tracing_error, TRACE_ERROR_MASK,
57           "unknown tracing mask");
58   }
59
60   FILE *file = fopen(filename, "w");
61   if (!file) {
62     THROW1 (tracing_error, TRACE_ERROR_START,
63                 "Tracefile %s could not be opened for writing.", filename);
64   } else {
65     TRACE_paje_start (file);
66   }
67   TRACE_paje_create_header();
68
69   /* setting the mask */
70   trace_mask = mask;
71
72   /* define paje hierarchy for tracing */
73   pajeDefineContainerType("PLATFORM", "0", "platform");
74   pajeDefineContainerType("HOST", "PLATFORM", "HOST");
75   pajeDefineContainerType("LINK", "PLATFORM", "LINK");
76
77   if (IS_TRACING_PLATFORM){
78     pajeDefineVariableType ("power", "HOST", "power");
79     pajeDefineVariableType ("bandwidth", "LINK", "bandwidth");
80     pajeDefineVariableType ("latency", "LINK", "latency");
81   }
82
83   if (IS_TRACING_PROCESSES){
84     //processes grouped by host
85     pajeDefineContainerType("PROCESS", "HOST", "PROCESS");
86     pajeDefineStateType("presence", "PROCESS", "presence");
87   }
88
89   if (IS_TRACING_TASKS){
90     //tasks grouped by host
91     pajeDefineContainerType("TASK", "HOST", "TASK");
92     pajeDefineStateType("presence", "TASK", "presence");
93   }
94
95   /* creating the platform */
96   pajeCreateContainer(MSG_get_clock(), "platform", "PLATFORM", "0", "simgrid-platform");
97
98   /* other trace initialization */
99   defined_types = xbt_dict_new();
100   created_categories = xbt_dict_new();
101   __TRACE_msg_init();
102   __TRACE_surf_init();
103   __TRACE_msg_process_init ();
104
105   return 0;
106 }
107
108 int TRACE_end() {
109   if (!IS_TRACING) return 1;
110   FILE *file = TRACE_paje_end();
111   fclose (file);
112   return 0;
113 }
114
115 int TRACE_category (const char *category)
116 {
117   if (!IS_TRACING) return 1;
118   static int first_time = 1;
119   if (first_time){
120           TRACE_define_type ("user_type", "0", 1);
121           first_time = 0;
122   }
123   return TRACE_create_category (category, "user_type", "0");
124 }
125
126 void TRACE_define_type (const char *type,
127                 const char *parent_type, int final) {
128   if (!IS_TRACING) return;
129
130   //check if type is already defined
131   if (xbt_dict_get_or_null (defined_types, type)){
132     THROW1 (tracing_error, TRACE_ERROR_TYPE_ALREADY_DEFINED, "Type %s is already defined", type);
133   }
134   //check if parent_type is already defined
135   if (strcmp(parent_type, "0") && !xbt_dict_get_or_null (defined_types, parent_type)) {
136     THROW1 (tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED, "Type (used as parent) %s is not defined", parent_type);
137   }
138
139   pajeDefineContainerType(type, parent_type, type);
140   if (final) {
141     //for m_process_t
142     if (IS_TRACING_PROCESSES) pajeDefineContainerType ("process", type, "process");
143     if (IS_TRACING_PROCESSES) pajeDefineStateType ("process-state", "process", "process-state");
144
145     if (IS_TRACING_TASKS) pajeDefineContainerType ("task", type, "task");
146     if (IS_TRACING_TASKS) pajeDefineStateType ("task-state", "task", "task-state");
147   }
148   xbt_dict_set (defined_types, type, xbt_strdup("1"), xbt_free);
149 }
150
151 int TRACE_create_category (const char *category,
152                 const char *type, const char *parent_category)
153 {
154   if (!IS_TRACING) return 1;
155
156   //check if type is defined
157   if (!xbt_dict_get_or_null (defined_types, type)) {
158          THROW1 (tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED, "Type %s is not defined", type);
159          return 1;
160   }
161   //check if parent_category exists
162   if (strcmp(parent_category, "0") && !xbt_dict_get_or_null (created_categories, parent_category)){
163      THROW1 (tracing_error, TRACE_ERROR_CATEGORY_NOT_DEFINED, "Category (used as parent) %s is not created", parent_category);
164      return 1;
165   }
166   //check if category is created
167   if (xbt_dict_get_or_null (created_categories, category)){
168      return 1;
169   }
170
171   pajeCreateContainer(MSG_get_clock(), category, type, parent_category, category);
172
173   /* for registering application categories on top of platform */
174   char state[100];
175   snprintf (state, 100, "b%s", category);
176   if (IS_TRACING_PLATFORM) pajeDefineVariableType (state, "LINK", state);
177   snprintf (state, 100, "p%s", category);
178   if (IS_TRACING_PLATFORM) pajeDefineVariableType (state, "HOST", state);
179
180   xbt_dict_set (created_categories, category, xbt_strdup("1"), xbt_free);
181   return 0;
182 }
183
184 void TRACE_set_mask (int mask)
185 {
186   if (mask != TRACE_PLATFORM){
187      return;
188   }else{
189      trace_mask = mask;
190   }
191 }
192
193 #endif /* HAVE_TRACING */