Logo AND Algorithmique Numérique Distribuée

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