Logo AND Algorithmique Numérique Distribuée

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