Logo AND Algorithmique Numérique Distribuée

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