Logo AND Algorithmique Numérique Distribuée

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