Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a Vivaldi network model, coded live during SUD'10 ;)
[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 extern xbt_dict_t created_containers; /* declared in general.c */
21 static xbt_dict_t defined_types;
22 static xbt_dict_t created_categories;
23
24 int trace_mask;
25
26 /** \ingroup tracing
27  * \brief Simple initialization of tracing.
28  *
29  * \param filename of the file that will contain the traces
30  * \return 0 if everything is ok
31  */
32 int TRACE_start (const char *filename)
33 {
34   return TRACE_start_with_mask (filename, TRACE_PLATFORM);
35 }
36
37 /** \ingroup tracing
38  * \brief Initialization of tracing.
39  *
40  * Function to be called at first when tracing a simulation
41  *
42  * \param name of the file that will contain the traces
43  * \param mask to take into account during trace
44  * \return 0 if everything is ok
45  */
46 int TRACE_start_with_mask(const char *filename, int mask) {
47   if (IS_TRACING) { /* what? trace is already active... ignore.. */
48         THROW0 (tracing_error, TRACE_ERROR_START,
49                 "TRACE_start called, but tracing is already active");
50     return 0;
51   }
52
53   /* checking if the mask is good (only TRACE_PLATFORM for now) */
54   if (mask != TRACE_PLATFORM){
55     THROW0 (tracing_error, TRACE_ERROR_MASK,
56             "Only TRACE_PLATFORM mask is accepted for now");
57   }
58
59   FILE *file = fopen(filename, "w");
60   if (!file) {
61     THROW1 (tracing_error, TRACE_ERROR_START,
62                 "Tracefile %s could not be opened for writing.", filename);
63   } else {
64     TRACE_paje_start (file);
65   }
66   TRACE_paje_create_header();
67
68   /* setting the mask */
69   trace_mask = mask;
70
71   //check if options are correct
72   if (IS_TRACING_TASKS){
73         if (!IS_TRACING_PROCESSES){
74           TRACE_end();
75       THROW0 (tracing_error, TRACE_ERROR_MASK,
76               "TRACE_PROCESS must be enabled if TRACE_TASK is used");
77         }
78   }
79
80   if (IS_TRACING_PROCESSES|IS_TRACING_TASKS){
81         if (!IS_TRACING_PLATFORM){
82           TRACE_end();
83       THROW0 (tracing_error, TRACE_ERROR_MASK,
84                   "TRACE_PLATFORM must be enabled if TRACE_PROCESS or TRACE_TASK is used");
85         }
86   }
87
88   //defining platform hierarchy
89   if (IS_TRACING_PLATFORM) pajeDefineContainerType("PLATFORM", "0", "platform");
90   if (IS_TRACING_PLATFORM) pajeDefineContainerType("HOST", "PLATFORM", "HOST");
91   if (IS_TRACING_PLATFORM) pajeDefineVariableType ("power", "HOST", "power");
92   if (IS_TRACING_PLATFORM) pajeDefineContainerType("LINK", "PLATFORM", "LINK");
93   if (IS_TRACING_PLATFORM) pajeDefineVariableType ("bandwidth", "LINK", "bandwidth");
94   if (IS_TRACING_PLATFORM) pajeDefineVariableType ("latency", "LINK", "latency");
95
96   if (IS_TRACING_PROCESSES) pajeDefineContainerType("PROCESS", "HOST", "PROCESS");
97   if (IS_TRACING_PROCESSES) pajeDefineStateType("presence", "PROCESS", "presence");
98
99   if (IS_TRACING_PROCESSES){
100         if (IS_TRACING_TASKS) pajeDefineContainerType("TASK", "PROCESS", "TASK");
101   }else{
102         if (IS_TRACING_TASKS) pajeDefineContainerType("TASK", "HOST", "TASK");
103   }
104
105   if (IS_TRACING_PLATFORM) pajeCreateContainer(MSG_get_clock(), "platform", "PLATFORM", "0", "simgrid-platform");
106
107   created_containers = xbt_dict_new();
108   defined_types = xbt_dict_new();
109   created_categories = xbt_dict_new();
110   __TRACE_msg_init();
111   __TRACE_surf_init();
112   __TRACE_msg_process_init ();
113
114   return 0;
115 }
116
117 int TRACE_end() {
118   if (!IS_TRACING) return 1;
119   FILE *file = TRACE_paje_end();
120   fclose (file);
121   return 0;
122 }
123
124 int TRACE_category (const char *category)
125 {
126   if (!IS_TRACING) return 1;
127   static int first_time = 1;
128   if (first_time){
129           TRACE_define_type ("user_type", "0", 1);
130           first_time = 0;
131   }
132   return TRACE_create_category (category, "user_type", "0");
133 }
134
135 void TRACE_define_type (const char *type,
136                 const char *parent_type, int final) {
137   if (!IS_TRACING) return;
138
139   //check if type is already defined
140   if (xbt_dict_get_or_null (defined_types, type)){
141     THROW1 (tracing_error, TRACE_ERROR_TYPE_ALREADY_DEFINED, "Type %s is already defined", type);
142   }
143   //check if parent_type is already defined
144   if (strcmp(parent_type, "0") && !xbt_dict_get_or_null (defined_types, parent_type)) {
145     THROW1 (tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED, "Type (used as parent) %s is not defined", parent_type);
146   }
147
148   pajeDefineContainerType(type, parent_type, type);
149   if (final) {
150     //for m_process_t
151     if (IS_TRACING_PROCESSES) pajeDefineContainerType ("process", type, "process");
152     if (IS_TRACING_PROCESSES) pajeDefineStateType ("process-state", "process", "process-state");
153
154     if (IS_TRACING_TASKS) pajeDefineContainerType ("task", type, "task");
155     if (IS_TRACING_TASKS) pajeDefineStateType ("task-state", "task", "task-state");
156   }
157   xbt_dict_set (defined_types, type, xbt_strdup("1"), xbt_free);
158 }
159
160 int TRACE_create_category (const char *category,
161                 const char *type, const char *parent_category)
162 {
163   if (!IS_TRACING) return;
164
165   //check if type is defined
166   if (!xbt_dict_get_or_null (defined_types, type)) {
167          THROW1 (tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED, "Type %s is not defined", type);
168          return 1;
169   }
170   //check if parent_category exists
171   if (strcmp(parent_category, "0") && !xbt_dict_get_or_null (created_categories, parent_category)){
172      THROW1 (tracing_error, TRACE_ERROR_CATEGORY_NOT_DEFINED, "Category (used as parent) %s is not created", parent_category);
173      return 1;
174   }
175   //check if category is created
176   if (xbt_dict_get_or_null (created_categories, category)){
177      return 1;
178   }
179
180   pajeCreateContainer(MSG_get_clock(), category, type, parent_category, category);
181
182   /* for registering application categories on top of platform */
183   char state[100];
184   snprintf (state, 100, "b%s", category);
185   if (IS_TRACING_PLATFORM) pajeDefineVariableType (state, "LINK", state);
186   snprintf (state, 100, "p%s", category);
187   if (IS_TRACING_PLATFORM) pajeDefineVariableType (state, "HOST", state);
188
189   xbt_dict_set (created_categories, category, xbt_strdup("1"), xbt_free);
190   return 0;
191 }
192
193 void TRACE_set_mask (int mask)
194 {
195   if (mask != TRACE_PLATFORM){
196      return;
197   }else{
198      trace_mask = mask;
199   }
200 }
201
202 #endif /* HAVE_TRACING */