Logo AND Algorithmique Numérique Distribuée

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