Logo AND Algorithmique Numérique Distribuée

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