Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8b1d08ca5545ce597e702cb0ac504aaa9af8a6a6
[simgrid.git] / src / instr / interface.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5   * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid_config.h"
8
9 #ifdef HAVE_TRACING
10
11 #include "instr/private.h"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(tracing,"Tracing Interface");
14
15 static xbt_dict_t defined_types;
16 static xbt_dict_t created_categories;
17
18 int TRACE_start ()
19 {
20   if (!_TRACE_configured()){
21     THROW0 (tracing_error, TRACE_ERROR_START,
22             "TRACE_start should be called after SimGrid initialization functions.");
23     return 0;
24   }
25
26   if (IS_TRACING) { /* what? trace is already active... ignore.. */
27     THROW0 (tracing_error, TRACE_ERROR_START,
28                  "TRACE_start called, but tracing is already active.");
29     return 0;
30   }
31
32   char *filename = _TRACE_filename ();
33   if (!filename){
34     THROW0 (tracing_error, TRACE_ERROR_START,
35             "Trace filename is not initialized.");
36     return 0;
37   }
38   FILE *file = fopen(filename, "w");
39   if (!file) {
40     THROW1 (tracing_error, TRACE_ERROR_START,
41                 "Tracefile %s could not be opened for writing.", filename);
42   } else {
43     TRACE_paje_start (file);
44   }
45   TRACE_paje_create_header();
46
47   /* define paje hierarchy for tracing */
48   pajeDefineContainerType("PLATFORM", "0", "platform");
49   pajeDefineContainerType("HOST", "PLATFORM", "HOST");
50   pajeDefineContainerType("LINK", "PLATFORM", "LINK");
51
52   if (IS_TRACING_PLATFORM){
53     pajeDefineVariableType ("power", "HOST", "power");
54     pajeDefineVariableType ("bandwidth", "LINK", "bandwidth");
55     pajeDefineVariableType ("latency", "LINK", "latency");
56   }
57
58   if (IS_TRACING_PROCESSES || IS_TRACING_VOLUME){
59     //processes grouped by host
60     pajeDefineContainerType("PROCESS", "HOST", "PROCESS");
61   }
62
63   if (IS_TRACING_PROCESSES){
64     pajeDefineStateType("category", "PROCESS", "category");
65     pajeDefineStateType("presence", "PROCESS", "presence");
66   }
67
68   if (IS_TRACING_VOLUME){
69     pajeDefineLinkType ("volume", "0", "PROCESS", "PROCESS", "volume");
70   }
71
72   if (IS_TRACING_TASKS){
73     //tasks grouped by host
74     pajeDefineContainerType("TASK", "HOST", "TASK");
75     pajeDefineStateType("category", "TASK", "category");
76     pajeDefineStateType("presence", "TASK", "presence");
77   }
78
79   if (IS_TRACING_SMPI){
80     pajeDefineContainerType ("MPI_PROCESS", "HOST", "MPI_PROCESS");
81     pajeDefineStateType ("MPI_STATE", "MPI_PROCESS", "MPI_STATE");
82     pajeDefineLinkType ("MPI_LINK", "0", "MPI_PROCESS", "MPI_PROCESS", "MPI_LINK");
83   }
84
85   /* creating the platform */
86   pajeCreateContainer(MSG_get_clock(), "platform", "PLATFORM", "0", "simgrid-platform");
87
88   /* other trace initialization */
89   defined_types = xbt_dict_new();
90   created_categories = xbt_dict_new();
91   __TRACE_msg_init();
92   __TRACE_category_init ();
93   __TRACE_surf_init();
94   __TRACE_msg_process_init ();
95   __TRACE_smpi_init ();
96
97   return 0;
98 }
99
100 int TRACE_end() {
101         FILE *file = NULL;
102   if (!IS_TRACING) return 1;
103   file = TRACE_paje_end();
104   fclose (file);
105   return 0;
106 }
107
108 int TRACE_category (const char *category)
109 {
110   static int first_time = 1;
111   if (!IS_TRACING) return 1;
112
113   if (first_time){
114           TRACE_define_type ("user_type", "0", 1);
115           first_time = 0;
116   }
117   return TRACE_create_category (category, "user_type", "0");
118 }
119
120 void TRACE_define_type (const char *type,
121                 const char *parent_type, int final) {
122         char *val_one = NULL;
123   if (!IS_TRACING) return;
124
125   //check if type is already defined
126   if (xbt_dict_get_or_null (defined_types, type)){
127     THROW1 (tracing_error, TRACE_ERROR_TYPE_ALREADY_DEFINED, "Type %s is already defined", type);
128   }
129   //check if parent_type is already defined
130   if (strcmp(parent_type, "0") && !xbt_dict_get_or_null (defined_types, parent_type)) {
131     THROW1 (tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED, "Type (used as parent) %s is not defined", parent_type);
132   }
133
134   pajeDefineContainerType(type, parent_type, type);
135   if (final) {
136     //for m_process_t
137     if (IS_TRACING_PROCESSES) pajeDefineContainerType ("process", type, "process");
138     if (IS_TRACING_PROCESSES) pajeDefineStateType ("process-state", "process", "process-state");
139
140     if (IS_TRACING_TASKS) pajeDefineContainerType ("task", type, "task");
141     if (IS_TRACING_TASKS) pajeDefineStateType ("task-state", "task", "task-state");
142   }
143   val_one = xbt_strdup ("1");
144   xbt_dict_set (defined_types, type, &val_one, xbt_free);
145 }
146
147 int TRACE_create_category (const char *category,
148                 const char *type, const char *parent_category)
149 {
150   char state[100];
151   char *val_one = NULL;
152   if (!IS_TRACING) return 1;
153
154   //check if type is defined
155   if (!xbt_dict_get_or_null (defined_types, type)) {
156          THROW1 (tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED, "Type %s is not defined", type);
157          return 1;
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      return 1;
163   }
164   //check if category is created
165   if (xbt_dict_get_or_null (created_categories, category)){
166      return 1;
167   }
168
169   pajeCreateContainer(MSG_get_clock(), category, type, parent_category, category);
170
171   /* for registering application categories on top of platform */
172
173   snprintf (state, 100, "b%s", category);
174   if (IS_TRACING_PLATFORM) pajeDefineVariableType (state, "LINK", state);
175   snprintf (state, 100, "p%s", category);
176   if (IS_TRACING_PLATFORM) pajeDefineVariableType (state, "HOST", state);
177
178   val_one = xbt_strdup ("1");
179   xbt_dict_set (created_categories, category, &val_one, xbt_free);
180   return 0;
181 }
182
183 void TRACE_declare_mark (const char *mark_type)
184 {
185   if (!IS_TRACING) return;
186   if (!mark_type) return;
187
188   pajeDefineEventType (mark_type, "0", mark_type);
189 }
190
191 void TRACE_mark (const char *mark_type, const char *mark_value)
192 {
193   if (!IS_TRACING) return;
194   if (!mark_type || !mark_value) return;
195
196   pajeNewEvent (MSG_get_clock(), mark_type, "0", mark_value);
197 }
198
199 #endif /* HAVE_TRACING */