Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Indent include and src using this command:
[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   if (IS_TRACING_PLATFORM)
69     pajeSetVariable(date, variable, resource, aux);
70   xbt_dict_set(resource_variables, xbt_strdup(key), xbt_strdup(aux),
71                xbt_free);
72 }
73
74 /*
75  * TRACE_surf_link_declaration (name, bandwidth, latency): this function
76  * saves the bandwidth and latency of a link identified by name. This
77  * information is used in the future to create the link container in the trace.
78  *
79  * caller: net_link_new (from each network model)
80  * main: create LINK container, set initial bandwidth and latency
81  * return: void
82  */
83 void TRACE_surf_link_declaration(void *link, char *name, double bw,
84                                  double lat)
85 {
86   if (!IS_TRACING)
87     return;
88
89   //filter out loopback
90   if (!strcmp(name, "loopback") || !strcmp(name, "__loopback__"))
91     return;
92
93   char alias[100];
94   snprintf(alias, 100, "%p", link);
95   pajeCreateContainer(SIMIX_get_clock(), alias, "LINK", "platform", name);
96   xbt_dict_set(created_links, alias, xbt_strdup("1"), xbt_free);
97   TRACE_surf_link_set_bandwidth(SIMIX_get_clock(), link, bw);
98   TRACE_surf_link_set_latency(SIMIX_get_clock(), link, lat);
99 }
100
101 /*
102  * TRACE_surf_host_declaration (name, power): this function
103  * saves the power of a host identified by name. This information
104  * is used to create the host container in the trace.
105  *
106  * caller: cpu_new (from each cpu model) + router parser
107  * main: create HOST containers, set initial power value
108  * return: void
109  */
110 void TRACE_surf_host_declaration(const char *name, double power)
111 {
112   if (!IS_TRACING)
113     return;
114   pajeCreateContainer(SIMIX_get_clock(), name, "HOST", "platform", name);
115   xbt_dict_set(host_containers, name, xbt_strdup("1"), xbt_free);
116   TRACE_surf_host_set_power(SIMIX_get_clock(), name, power);
117 }
118
119 void TRACE_surf_host_set_power(double date, const char *resource,
120                                double power)
121 {
122   TRACE_surf_set_resource_variable(date, "power", resource, power);
123 }
124
125 void TRACE_surf_link_set_bandwidth(double date, void *link,
126                                    double bandwidth)
127 {
128   if (!TRACE_surf_link_is_traced(link))
129     return;
130
131   char resource[100];
132   snprintf(resource, 100, "%p", link);
133   TRACE_surf_set_resource_variable(date, "bandwidth", resource, bandwidth);
134 }
135
136 void TRACE_surf_link_set_latency(double date, void *link, double latency)
137 {
138   if (!TRACE_surf_link_is_traced(link))
139     return;
140
141   char resource[100];
142   snprintf(resource, 100, "%p", link);
143   TRACE_surf_set_resource_variable(date, "latency", resource, latency);
144 }
145
146 /* to trace gtnets */
147 void TRACE_surf_gtnets_communicate(void *action, int src, int dst)
148 {
149   char key[100], aux[100];
150   if (!IS_TRACING)
151     return;
152   snprintf(key, 100, "%p", action);
153
154   snprintf(aux, 100, "%d", src);
155   xbt_dict_set(gtnets_src, key, xbt_strdup(aux), xbt_free);
156   snprintf(aux, 100, "%d", dst);
157   xbt_dict_set(gtnets_dst, key, xbt_strdup(aux), xbt_free);
158 }
159
160 int TRACE_surf_gtnets_get_src(void *action)
161 {
162   char key[100];
163   char *aux = NULL;
164   if (!IS_TRACING)
165     return -1;
166   snprintf(key, 100, "%p", action);
167
168   aux = xbt_dict_get_or_null(gtnets_src, key);
169   if (aux) {
170     return atoi(aux);
171   } else {
172     return -1;
173   }
174 }
175
176 int TRACE_surf_gtnets_get_dst(void *action)
177 {
178   char key[100];
179   char *aux = NULL;
180   if (!IS_TRACING)
181     return -1;
182   snprintf(key, 100, "%p", action);
183
184   aux = xbt_dict_get_or_null(gtnets_dst, key);
185   if (aux) {
186     return atoi(aux);
187   } else {
188     return -1;
189   }
190 }
191
192 void TRACE_surf_gtnets_destroy(void *action)
193 {
194   char key[100];
195   if (!IS_TRACING)
196     return;
197   snprintf(key, 100, "%p", action);
198   xbt_dict_remove(gtnets_src, key);
199   xbt_dict_remove(gtnets_dst, key);
200 }
201
202 void TRACE_surf_host_vivaldi_parse(char *host, double x, double y,
203                                    double h)
204 {
205   char valuestr[100];
206   if (!IS_TRACING || !IS_TRACING_PLATFORM)
207     return;
208
209   snprintf(valuestr, 100, "%g", x);
210   pajeSetVariable(0, "vivaldi_x", host, valuestr);
211   snprintf(valuestr, 100, "%g", y);
212   pajeSetVariable(0, "vivaldi_y", host, valuestr);
213   snprintf(valuestr, 100, "%g", h);
214   pajeSetVariable(0, "vivaldi_h", host, valuestr);
215 }
216
217 extern routing_global_t global_routing;
218 void TRACE_surf_save_onelink(void)
219 {
220   if (!IS_TRACING)
221     return;
222
223   //get the onelinks from the parsed platform
224   xbt_dynar_t onelink_routes = global_routing->get_onelink_routes();
225   if (!onelink_routes)
226     return;
227
228   //save them in trace file
229   onelink_t onelink;
230   unsigned int iter;
231   xbt_dynar_foreach(onelink_routes, iter, onelink) {
232     char *src = onelink->src;
233     char *dst = onelink->dst;
234     void *link = onelink->link_ptr;
235
236     if (TRACE_surf_link_is_traced(link)) {
237       char resource[100];
238       snprintf(resource, 100, "%p", link);
239
240       pajeNewEvent(0.1, "source", resource, src);
241       pajeNewEvent(0.1, "destination", resource, dst);
242     }
243   }
244 }
245
246 int TRACE_surf_link_is_traced(void *link)
247 {
248   char alias[100];
249   snprintf(alias, 100, "%p", link);
250   if (xbt_dict_get_or_null(created_links, alias)) {
251     return 1;
252   } else {
253     return 0;
254   }
255 }
256
257 void TRACE_surf_action(surf_action_t surf_action, const char *category)
258 {
259   if (!IS_TRACING)
260     return;
261   if (!IS_TRACING_PLATFORM)
262     return;
263   if (!category) {
264     xbt_die("invalid tracing category");
265   }
266   surf_action->category = xbt_new(char, strlen(category) + 1);
267   strncpy(surf_action->category, category, strlen(category) + 1);
268 }
269 #endif