Logo AND Algorithmique Numérique Distribuée

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