Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cb8cd7daa52eab2da4b9dce9e2b732fdd9bdae74
[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_init (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_initialize();
33 }
34
35 void TRACE_surf_finalize (void)
36 {
37   __TRACE_surf_resource_utilization_finalize();
38 }
39
40 static void __TRACE_surf_set_resource_variable (double date, const char *variable, const char *resource, double value)
41 {
42         char aux[100], key[100];
43         char *last_value = NULL;
44   if (!IS_TRACING) return;
45   snprintf (aux, 100, "%f", value);
46   snprintf (key, 100, "%s%c%s", resource, VARIABLE_SEPARATOR, variable);
47
48   last_value = xbt_dict_get_or_null(resource_variables, key);
49   if (last_value){
50     if (atof(last_value) == value){
51       return;
52     }
53   }
54   if (IS_TRACING_PLATFORM) pajeSetVariable (date, variable, resource, aux);
55   xbt_dict_set (resource_variables, xbt_strdup(key), xbt_strdup(aux), xbt_free);
56 }
57
58 /*
59  * TRACE_surf_link_declaration (name, bandwidth, latency): this function
60  * saves the bandwidth and latency of a link identified by name. This
61  * information is used in the future to create the link container in the trace.
62  *
63  * caller: net_link_new (from each network model)
64  * main: create LINK container, set initial bandwidth and latency
65  * return: void
66  */
67 void TRACE_surf_link_declaration (void *link, char *name, double bw, double lat)
68 {
69   if (!IS_TRACING) return;
70
71   char alias[100];
72   snprintf (alias, 100, "%p", link);
73   pajeCreateContainer (SIMIX_get_clock(), alias, "LINK", "platform", name);
74   xbt_dict_set (created_links, alias, xbt_strdup ("1"), xbt_free);
75   TRACE_surf_link_set_bandwidth (SIMIX_get_clock(), link, bw);
76   TRACE_surf_link_set_latency (SIMIX_get_clock(), link, lat);
77 }
78
79 /*
80  * TRACE_surf_host_declaration (name, power): this function
81  * saves the power of a host identified by name. This information
82  * is used to create the host container in the trace.
83  *
84  * caller: cpu_new (from each cpu model) + router parser
85  * main: create HOST containers, set initial power value
86  * return: void
87  */
88 void TRACE_surf_host_declaration (char *name, double power)
89 {
90   if (!IS_TRACING) return;
91   pajeCreateContainer (SIMIX_get_clock(), name, "HOST", "platform", name);
92   xbt_dict_set (host_containers, name, xbt_strdup("1"), xbt_free);
93   TRACE_surf_host_set_power (SIMIX_get_clock(), name, power);
94 }
95
96 void TRACE_surf_host_set_power (double date, char *resource, double power)
97 {
98   __TRACE_surf_set_resource_variable (date, "power", resource, power);
99 }
100
101 void TRACE_surf_link_set_bandwidth (double date, void *link, double bandwidth)
102 {
103   char resource[100];
104   snprintf (resource, 100, "%p", link);
105   __TRACE_surf_set_resource_variable (date, "bandwidth", resource, bandwidth);
106 }
107
108 void TRACE_surf_link_set_latency (double date, void *link, double latency)
109 {
110   char resource[100];
111   snprintf (resource, 100, "%p", link);
112   __TRACE_surf_set_resource_variable (date, "latency", resource, latency);
113 }
114
115 /* to trace gtnets */
116 void TRACE_surf_gtnets_communicate (void *action, int src, int dst)
117 {
118         char key[100], aux[100];
119   if (!IS_TRACING) return;
120   snprintf (key, 100, "%p", action);
121
122   snprintf (aux, 100, "%d", src);
123   xbt_dict_set (gtnets_src, key, xbt_strdup(aux), xbt_free);
124   snprintf (aux, 100, "%d", dst);
125   xbt_dict_set (gtnets_dst, key, xbt_strdup(aux), xbt_free);
126 }
127
128 int TRACE_surf_gtnets_get_src (void *action)
129 {
130         char key[100];
131         char *aux = NULL;
132   if (!IS_TRACING) return -1;
133   snprintf (key, 100, "%p", action);
134
135   aux = xbt_dict_get_or_null (gtnets_src, key);
136   if (aux){
137         return atoi(aux);
138   }else{
139     return -1;
140   }
141 }
142
143 int TRACE_surf_gtnets_get_dst (void *action)
144 {
145         char key[100];
146         char *aux = NULL;
147   if (!IS_TRACING) return -1;
148   snprintf (key, 100, "%p", action);
149
150   aux = xbt_dict_get_or_null (gtnets_dst, key);
151   if (aux){
152         return atoi(aux);
153   }else{
154     return -1;
155   }
156 }
157
158 void TRACE_surf_gtnets_destroy (void *action)
159 {
160   char key[100];
161   if (!IS_TRACING) return;
162   snprintf (key, 100, "%p", action);
163   xbt_dict_remove (gtnets_src, key);
164   xbt_dict_remove (gtnets_dst, key);
165 }
166
167 void TRACE_surf_link_missing (void)
168 {
169   CRITICAL0("The trace cannot be done because "
170                  "the platform you are using contains "
171                  "routes with more than one link.");
172   THROW0(tracing_error, TRACE_ERROR_COMPLEX_ROUTES, "Tracing failed");
173 }
174
175 void TRACE_msg_clean (void)
176 {
177   char *key, *value;
178   xbt_dict_cursor_t cursor = NULL;
179   TRACE_surf_finalize();
180
181   /* get all host from host_containers */
182   xbt_dict_foreach(host_containers, cursor, key, value) {
183     pajeDestroyContainer (MSG_get_clock(), "HOST", key);
184   }
185   xbt_dict_foreach(created_links, cursor, key, value) {
186     pajeDestroyContainer (MSG_get_clock(), "LINK", key);
187   }
188 }
189
190 void TRACE_surf_host_vivaldi_parse (char *host, double x, double y, double h)
191 {
192         char valuestr[100];
193   if (!IS_TRACING || !IS_TRACING_PLATFORM) return;
194
195   snprintf (valuestr, 100, "%g", x);
196   pajeSetVariable (0, "vivaldi_x", host, valuestr);
197   snprintf (valuestr, 100, "%g", y);
198   pajeSetVariable (0, "vivaldi_y", host, valuestr);
199   snprintf (valuestr, 100, "%g", h);
200   pajeSetVariable (0, "vivaldi_h", host, valuestr);
201 }
202
203 extern routing_global_t global_routing;
204 void TRACE_surf_save_onelink (void)
205 {
206   if (!IS_TRACING) return;
207
208   //get the onelinks from the parsed platform
209   xbt_dynar_t onelink_routes = global_routing->get_onelink_routes();
210   if (!onelink_routes) return;
211
212   //save them in trace file
213   onelink_t onelink;
214   unsigned int iter;
215   xbt_dynar_foreach(onelink_routes, iter, onelink) {
216     char *src = onelink->src;
217     char *dst = onelink->dst;
218     void *link = onelink->link_ptr;
219
220     char resource[100];
221     snprintf (resource, 100, "%p", link);
222
223     pajeNewEvent (0.1, "source", resource, src);
224     pajeNewEvent (0.1, "destination", resource, dst);
225   }
226 }
227
228 #endif