Logo AND Algorithmique Numérique Distribuée

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