Logo AND Algorithmique Numérique Distribuée

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