Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b790425c3d6511599ee9146860687001bcf3c782
[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 resource_variables;   /* (host|link)#variable -> value */
15
16 /* to trace gtnets */
17 static xbt_dict_t gtnets_src;   /* %p (action) -> %s */
18 static xbt_dict_t gtnets_dst;   /* %p (action) -> %s */
19
20 void TRACE_surf_alloc(void)
21 {
22   resource_variables = xbt_dict_new();
23   gtnets_src = xbt_dict_new();
24   gtnets_dst = xbt_dict_new();
25
26   TRACE_surf_resource_utilization_alloc();
27 }
28
29 void TRACE_surf_release(void)
30 {
31   TRACE_surf_resource_utilization_release();
32 }
33
34 static void TRACE_surf_set_resource_variable(double date,
35                                              const char *variable,
36                                              const char *resource,
37                                              double value)
38 {
39   char aux[100], key[100];
40   char *last_value = NULL;
41   if (!TRACE_is_active())
42     return;
43   snprintf(aux, 100, "%f", value);
44   snprintf(key, 100, "%s#%s", resource, variable);
45
46   last_value = xbt_dict_get_or_null(resource_variables, key);
47   if (last_value) {
48     if (atof(last_value) == value) {
49       return;
50     }
51   }
52   pajeSetVariable(date, variable, resource, aux);
53   xbt_dict_set(resource_variables, xbt_strdup(key), xbt_strdup(aux),
54                xbt_free);
55 }
56
57 void TRACE_surf_host_set_power(double date, const char *resource,
58                                double power)
59 {
60   if (!TRACE_is_active())
61     return;
62   TRACE_surf_set_resource_variable(date, "power", resource, power);
63 }
64
65 void TRACE_surf_link_set_bandwidth(double date, void *link,
66                                    double bandwidth)
67 {
68   if (!TRACE_is_active())
69     return;
70   if (!TRACE_surf_link_is_traced(link))
71     return;
72
73   char resource[100];
74   snprintf(resource, 100, "%p", link);
75   TRACE_surf_set_resource_variable(date, "bandwidth", resource, bandwidth);
76 }
77
78 void TRACE_surf_link_set_latency(double date, void *link, double latency)
79 {
80   if (!TRACE_is_active())
81     return;
82   if (!TRACE_surf_link_is_traced(link))
83     return;
84
85   char resource[100];
86   snprintf(resource, 100, "%p", link);
87   TRACE_surf_set_resource_variable(date, "latency", resource, latency);
88 }
89
90 /* to trace gtnets */
91 void TRACE_surf_gtnets_communicate(void *action, int src, int dst)
92 {
93   char key[100], aux[100];
94   if (!TRACE_is_active())
95     return;
96   snprintf(key, 100, "%p", action);
97
98   snprintf(aux, 100, "%d", src);
99   xbt_dict_set(gtnets_src, key, xbt_strdup(aux), xbt_free);
100   snprintf(aux, 100, "%d", dst);
101   xbt_dict_set(gtnets_dst, key, xbt_strdup(aux), xbt_free);
102 }
103
104 int TRACE_surf_gtnets_get_src(void *action)
105 {
106   char key[100];
107   char *aux = NULL;
108   if (!TRACE_is_active())
109     return -1;
110   snprintf(key, 100, "%p", action);
111
112   aux = xbt_dict_get_or_null(gtnets_src, key);
113   if (aux) {
114     return atoi(aux);
115   } else {
116     return -1;
117   }
118 }
119
120 int TRACE_surf_gtnets_get_dst(void *action)
121 {
122   char key[100];
123   char *aux = NULL;
124   if (!TRACE_is_active())
125     return -1;
126   snprintf(key, 100, "%p", action);
127
128   aux = xbt_dict_get_or_null(gtnets_dst, key);
129   if (aux) {
130     return atoi(aux);
131   } else {
132     return -1;
133   }
134 }
135
136 void TRACE_surf_gtnets_destroy(void *action)
137 {
138   char key[100];
139   if (!TRACE_is_active())
140     return;
141   snprintf(key, 100, "%p", action);
142   xbt_dict_remove(gtnets_src, key);
143   xbt_dict_remove(gtnets_dst, key);
144 }
145
146 void TRACE_surf_host_vivaldi_parse(char *host, double x, double y,
147                                    double h)
148 {
149   char valuestr[100];
150   if (!TRACE_is_active() || !TRACE_platform_is_enabled())
151     return;
152
153   snprintf(valuestr, 100, "%g", x);
154   pajeSetVariable(0, "vivaldi_x", host, valuestr);
155   snprintf(valuestr, 100, "%g", y);
156   pajeSetVariable(0, "vivaldi_y", host, valuestr);
157   snprintf(valuestr, 100, "%g", h);
158   pajeSetVariable(0, "vivaldi_h", host, valuestr);
159 }
160
161 extern routing_global_t global_routing;
162 void TRACE_surf_save_onelink(void)
163 {
164   if (!TRACE_is_active())
165     return;
166
167   //get the onelinks from the parsed platform
168   xbt_dynar_t onelink_routes = global_routing->get_onelink_routes();
169   if (!onelink_routes)
170     return;
171
172   //save them in trace file
173   onelink_t onelink;
174   unsigned int iter;
175   xbt_dynar_foreach(onelink_routes, iter, onelink) {
176     char *src = onelink->src;
177     char *dst = onelink->dst;
178     void *link = onelink->link_ptr;
179
180     if (TRACE_surf_link_is_traced(link)) {
181       char resource[100];
182       snprintf(resource, 100, "%p", link);
183
184       pajeNewEvent(0, "source", resource, src);
185       pajeNewEvent(0, "destination", resource, dst);
186     }
187   }
188 }
189
190 void TRACE_surf_action(surf_action_t surf_action, const char *category)
191 {
192   if (!TRACE_is_active())
193     return;
194   if (!TRACE_categorized ())
195     return;
196   if (!category)
197     return;
198
199   surf_action->category = xbt_strdup(category);
200 }
201 #endif /* HAVE_TRACING */