Logo AND Algorithmique Numérique Distribuée

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