Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not request status if not requested by caller.
[simgrid.git] / src / instr / surf_instr.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 "instr/private.h"
8 #include "surf/surf_private.h"
9
10 #ifdef HAVE_TRACING
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(tracing_surf, tracing, "Tracing Surf");
13
14 #define VARIABLE_SEPARATOR '#'
15
16 static xbt_dict_t created_links;
17 static xbt_dict_t host_containers;
18 static xbt_dict_t resource_variables;   /* (host|link)#variable -> value */
19
20 /* to trace gtnets */
21 static xbt_dict_t gtnets_src;   /* %p (action) -> %s */
22 static xbt_dict_t gtnets_dst;   /* %p (action) -> %s */
23
24 void TRACE_surf_alloc(void)
25 {
26   created_links = xbt_dict_new();
27   host_containers = xbt_dict_new();
28   resource_variables = xbt_dict_new();
29   gtnets_src = xbt_dict_new();
30   gtnets_dst = xbt_dict_new();
31
32   TRACE_surf_resource_utilization_alloc();
33 }
34
35 void TRACE_surf_release(void)
36 {
37   char *key, *value;
38   xbt_dict_cursor_t cursor = NULL;
39   TRACE_surf_resource_utilization_release();
40
41   /* get all host from host_containers */
42   xbt_dict_foreach(host_containers, cursor, key, value) {
43     pajeDestroyContainer(MSG_get_clock(), "HOST", key);
44   }
45   xbt_dict_foreach(created_links, cursor, key, value) {
46     pajeDestroyContainer(MSG_get_clock(), "LINK", key);
47   }
48 }
49
50 static void TRACE_surf_set_resource_variable(double date,
51                                              const char *variable,
52                                              const char *resource,
53                                              double value)
54 {
55   char aux[100], key[100];
56   char *last_value = NULL;
57   if (!IS_TRACING)
58     return;
59   snprintf(aux, 100, "%f", value);
60   snprintf(key, 100, "%s%c%s", resource, VARIABLE_SEPARATOR, variable);
61
62   last_value = xbt_dict_get_or_null(resource_variables, key);
63   if (last_value) {
64     if (atof(last_value) == value) {
65       return;
66     }
67   }
68   pajeSetVariable(date, variable, resource, aux);
69   xbt_dict_set(resource_variables, xbt_strdup(key), xbt_strdup(aux),
70                xbt_free);
71 }
72
73 /*
74  * TRACE_surf_link_declaration (name, bandwidth, latency): this function
75  * saves the bandwidth and latency of a link identified by name. This
76  * information is used in the future to create the link container in the trace.
77  *
78  * caller: net_link_new (from each network model)
79  * main: create LINK container, set initial bandwidth and latency
80  * return: void
81  */
82 void TRACE_surf_link_declaration(void *link, char *name, double bw,
83                                  double lat)
84 {
85   if (!IS_TRACING)
86     return;
87
88   if (!link){
89     xbt_die ("link is NULL");
90   }
91
92   //filter out loopback
93   if (!strcmp(name, "loopback") || !strcmp(name, "__loopback__"))
94     return;
95
96   char alias[100];
97   snprintf(alias, 100, "%p", link);
98   pajeCreateContainer(SIMIX_get_clock(), alias, "LINK", "platform", name);
99   xbt_dict_set(created_links, alias, xbt_strdup("1"), xbt_free);
100   TRACE_surf_link_set_bandwidth(SIMIX_get_clock(), link, bw);
101   TRACE_surf_link_set_latency(SIMIX_get_clock(), link, lat);
102 }
103
104 /*
105  * TRACE_surf_host_declaration (name, power): this function
106  * saves the power of a host identified by name. This information
107  * is used to create the host container in the trace.
108  *
109  * caller: cpu_new (from each cpu model) + router parser
110  * main: create HOST containers, set initial power value
111  * return: void
112  */
113 void TRACE_surf_host_declaration(const char *name, double power)
114 {
115   if (!IS_TRACING)
116     return;
117   pajeCreateContainer(SIMIX_get_clock(), name, "HOST", "platform", name);
118   xbt_dict_set(host_containers, name, xbt_strdup("1"), xbt_free);
119   TRACE_surf_host_set_power(SIMIX_get_clock(), name, power);
120 }
121
122 void TRACE_surf_host_set_power(double date, const char *resource,
123                                double power)
124 {
125   if (!IS_TRACING)
126     return;
127   TRACE_surf_set_resource_variable(date, "power", resource, power);
128 }
129
130 void TRACE_surf_link_set_bandwidth(double date, void *link,
131                                    double bandwidth)
132 {
133   if (!IS_TRACING)
134     return;
135   if (!TRACE_surf_link_is_traced(link))
136     return;
137
138   char resource[100];
139   snprintf(resource, 100, "%p", link);
140   TRACE_surf_set_resource_variable(date, "bandwidth", resource, bandwidth);
141 }
142
143 void TRACE_surf_link_set_latency(double date, void *link, double latency)
144 {
145   if (!IS_TRACING)
146     return;
147   if (!TRACE_surf_link_is_traced(link))
148     return;
149
150   char resource[100];
151   snprintf(resource, 100, "%p", link);
152   TRACE_surf_set_resource_variable(date, "latency", resource, latency);
153 }
154
155 /* to trace gtnets */
156 void TRACE_surf_gtnets_communicate(void *action, int src, int dst)
157 {
158   char key[100], aux[100];
159   if (!IS_TRACING)
160     return;
161   snprintf(key, 100, "%p", action);
162
163   snprintf(aux, 100, "%d", src);
164   xbt_dict_set(gtnets_src, key, xbt_strdup(aux), xbt_free);
165   snprintf(aux, 100, "%d", dst);
166   xbt_dict_set(gtnets_dst, key, xbt_strdup(aux), xbt_free);
167 }
168
169 int TRACE_surf_gtnets_get_src(void *action)
170 {
171   char key[100];
172   char *aux = NULL;
173   if (!IS_TRACING)
174     return -1;
175   snprintf(key, 100, "%p", action);
176
177   aux = xbt_dict_get_or_null(gtnets_src, key);
178   if (aux) {
179     return atoi(aux);
180   } else {
181     return -1;
182   }
183 }
184
185 int TRACE_surf_gtnets_get_dst(void *action)
186 {
187   char key[100];
188   char *aux = NULL;
189   if (!IS_TRACING)
190     return -1;
191   snprintf(key, 100, "%p", action);
192
193   aux = xbt_dict_get_or_null(gtnets_dst, key);
194   if (aux) {
195     return atoi(aux);
196   } else {
197     return -1;
198   }
199 }
200
201 void TRACE_surf_gtnets_destroy(void *action)
202 {
203   char key[100];
204   if (!IS_TRACING)
205     return;
206   snprintf(key, 100, "%p", action);
207   xbt_dict_remove(gtnets_src, key);
208   xbt_dict_remove(gtnets_dst, key);
209 }
210
211 void TRACE_surf_host_vivaldi_parse(char *host, double x, double y,
212                                    double h)
213 {
214   char valuestr[100];
215   if (!IS_TRACING || !IS_TRACING_PLATFORM)
216     return;
217
218   snprintf(valuestr, 100, "%g", x);
219   pajeSetVariable(0, "vivaldi_x", host, valuestr);
220   snprintf(valuestr, 100, "%g", y);
221   pajeSetVariable(0, "vivaldi_y", host, valuestr);
222   snprintf(valuestr, 100, "%g", h);
223   pajeSetVariable(0, "vivaldi_h", host, valuestr);
224 }
225
226 extern routing_global_t global_routing;
227 void TRACE_surf_save_onelink(void)
228 {
229   if (!IS_TRACING)
230     return;
231
232   //get the onelinks from the parsed platform
233   xbt_dynar_t onelink_routes = global_routing->get_onelink_routes();
234   if (!onelink_routes)
235     return;
236
237   //save them in trace file
238   onelink_t onelink;
239   unsigned int iter;
240   xbt_dynar_foreach(onelink_routes, iter, onelink) {
241     char *src = onelink->src;
242     char *dst = onelink->dst;
243     void *link = onelink->link_ptr;
244
245     if (TRACE_surf_link_is_traced(link)) {
246       char resource[100];
247       snprintf(resource, 100, "%p", link);
248
249       pajeNewEvent(0, "source", resource, src);
250       pajeNewEvent(0, "destination", resource, dst);
251     }
252   }
253 }
254
255 int TRACE_surf_link_is_traced(void *link)
256 {
257   char alias[100];
258   snprintf(alias, 100, "%p", link);
259   if (xbt_dict_get_or_null(created_links, alias)) {
260     return 1;
261   } else {
262     return 0;
263   }
264 }
265
266 void TRACE_surf_action(surf_action_t surf_action, const char *category)
267 {
268   if (!IS_TRACING)
269     return;
270   if (!IS_TRACING_PLATFORM)
271     return;
272   if (!category)
273     return;
274
275   surf_action->category = xbt_new(char, strlen(category) + 1);
276   strncpy(surf_action->category, category, strlen(category) + 1);
277 }
278 #endif