Logo AND Algorithmique Numérique Distribuée

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