Logo AND Algorithmique Numérique Distribuée

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