Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid to export internal variables.
[simgrid.git] / src / instr / jedule / jedule_output.c
1 /*
2  * jedule_output.c
3  *
4  *  Created on: Dec 1, 2010
5  *      Author: sascha
6  */
7
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "xbt/dynar.h"
14 #include "xbt/asserts.h"
15
16 #include "instr/jedule/jedule_output.h"
17
18 #ifdef HAVE_JEDULE
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jed_out, jedule,
21                                 "Logging specific to Jedule output");
22
23 /*********************************************************/
24
25 xbt_dynar_t jedule_event_list;
26
27 static FILE *jed_file;
28
29 static void print_platform(jed_simgrid_container_t root_container);
30 static void print_container(jed_simgrid_container_t container);
31 static void print_resources(jed_simgrid_container_t resource_parent);
32 static void print_key_value_dict(xbt_dict_t meta_info_dict);
33 static void print_events(xbt_dynar_t event_list);
34 static void get_hierarchy_list(xbt_dynar_t hier_list, jed_simgrid_container_t container);
35
36 /*********************************************************/
37
38
39 static void get_hierarchy_list(xbt_dynar_t hier_list, jed_simgrid_container_t container) {
40
41   xbt_assert( container != NULL );
42
43   if( container->parent != NULL ) {
44
45     if( container->parent->container_children == NULL ) {
46       // we are in the last level
47       get_hierarchy_list(hier_list, container->parent);
48
49     } else {
50       unsigned int i;
51       int child_nb = -1;
52       jed_simgrid_container_t child_container;
53
54       xbt_dynar_foreach(container->parent->container_children, i, child_container) {
55         if( child_container == container ) {
56           child_nb = i;
57           break;
58         }
59       }
60
61       xbt_assert( child_nb > - 1);
62
63       xbt_dynar_insert_at(hier_list, 0, &child_nb);
64
65       get_hierarchy_list(hier_list, container->parent);
66     }
67   } else {
68     int top_level = 0;
69     xbt_dynar_insert_at(hier_list, 0, &top_level);
70   }
71
72 }
73
74 static void get_hierarchy_string(jed_simgrid_container_t container, char *outbuf) {
75     char buf[1024];
76     xbt_dynar_t hier_list;
77     unsigned int iter;
78     int number;
79     int length;
80     
81     outbuf[0] = '\0';
82     hier_list = xbt_dynar_new(sizeof(int), NULL);
83     get_hierarchy_list(hier_list, container);
84     
85     length = xbt_dynar_length(hier_list);
86     
87     xbt_dynar_foreach(hier_list, iter, number) {
88         if( iter != length-1 ) {
89             sprintf(buf, "%d.", number);
90         } else {
91             sprintf(buf, "%d", number);
92         }
93         strcat(outbuf, buf);
94     }
95     
96     xbt_dynar_free(&hier_list);    
97 }
98
99 static void print_key_value_dict(xbt_dict_t key_value_dict) {
100   xbt_dict_cursor_t cursor=NULL;
101   char *key,*data;
102
103   if( key_value_dict != NULL ) {
104     xbt_dict_foreach(key_value_dict,cursor,key,data) {
105       fprintf(jed_file, "<prop key=\"%s\" value=\"%s\" />\n",key,data);
106     }
107   }
108 }
109
110 static void print_container(jed_simgrid_container_t container) {
111   unsigned int i;
112   jed_simgrid_container_t child_container;
113
114   xbt_assert( container != NULL );
115
116   fprintf(jed_file, "<res name=\"%s\">\n", container->name);
117   if( container->container_children != NULL ) {
118     xbt_dynar_foreach(container->container_children, i, child_container) {
119       print_container(child_container);
120     }
121   } else {
122     print_resources(container);
123   }
124   fprintf(jed_file, "</res>\n");
125 }
126
127 static void print_resources(jed_simgrid_container_t resource_parent) {
128   int res_nb;
129   unsigned int i;
130   char *res_name;
131     char resid[1024];
132   xbt_assert( resource_parent->resource_list != NULL );
133
134   res_nb = xbt_dynar_length(resource_parent->resource_list);
135     
136     get_hierarchy_string(resource_parent, resid);
137
138   fprintf(jed_file, "<rset id=\"%s\" nb=\"%d\" names=\"", resid, res_nb);
139   xbt_dynar_foreach(resource_parent->resource_list, i, res_name) {
140     fprintf(jed_file, "%s", res_name);
141     if( i != res_nb-1 ) {
142       fprintf(jed_file, "|");
143     }
144   }
145   fprintf(jed_file, "\" />\n");
146 }
147
148
149 static void print_platform(jed_simgrid_container_t root_container) {
150
151   fprintf(jed_file, "<platform>\n");
152   print_container(root_container);
153   fprintf(jed_file, "</platform>\n");
154 }
155
156 static void print_event(jed_event_t event) {
157   unsigned int i;
158   jed_res_subset_t subset;
159
160
161   xbt_assert( event != NULL );
162   xbt_assert( event->resource_subsets != NULL );
163
164   fprintf(jed_file, "<event>\n");
165
166
167   fprintf(jed_file, "<prop key=\"name\" value=\"%s\" />\n", event->name);
168   fprintf(jed_file, "<prop key=\"start\" value=\"%g\" />\n", event->start_time);
169   fprintf(jed_file, "<prop key=\"end\" value=\"%g\" />\n", event->end_time);
170   fprintf(jed_file, "<prop key=\"type\" value=\"%s\" />\n", event->type);
171
172   fprintf(jed_file, "<res_util>\n");
173
174   xbt_dynar_foreach(event->resource_subsets, i, subset) {
175
176     int start = subset->start_idx;
177     int end   = subset->start_idx + subset->nres - 1;
178     char resid[1024];
179
180     get_hierarchy_string(subset->parent, resid);
181         
182     fprintf(jed_file, "<select resources=\"");
183     fprintf(jed_file, "%s", resid);
184     fprintf(jed_file, ".[%d-%d]", start, end);
185     fprintf(jed_file, "\" />\n");
186
187   }
188
189   fprintf(jed_file, "</res_util>\n");
190
191   fprintf(jed_file, "<characteristics>\n");
192   {
193     char *ch;
194     unsigned int iter;
195     xbt_dynar_foreach(event->characteristics_list, iter, ch) {
196       fprintf(jed_file, "<characteristic name=\"%s\" />\n", ch);
197     }
198   }
199   fprintf(jed_file, "</characteristics>\n");
200
201   fprintf(jed_file, "<info>\n");
202   print_key_value_dict(event->info_hash);
203   fprintf(jed_file, "</info>\n");
204
205   fprintf(jed_file, "</event>\n");
206 }
207
208 static void print_events(xbt_dynar_t event_list)  {
209   unsigned int i;
210   jed_event_t event;
211
212   fprintf(jed_file, "<events>\n");
213   xbt_dynar_foreach(event_list, i, event) {
214     print_event(event);
215   }
216   fprintf(jed_file, "</events>\n");
217 }
218
219
220 void write_jedule_output(FILE *file, jedule_t jedule,
221     xbt_dynar_t event_list, xbt_dict_t meta_info_dict) {
222
223 //  xbt_assert( jed_file != NULL );
224
225   jed_file = file;
226   if (!xbt_dynar_is_empty(jedule_event_list)){
227
228     fprintf(jed_file, "<jedule>\n");
229
230     fprintf(jed_file, "<jedule_meta>\n");
231     print_key_value_dict(jedule->jedule_meta_info);
232     fprintf(jed_file, "</jedule_meta>\n");
233
234     print_platform(jedule->root_container);
235
236     print_events(event_list);
237
238     fprintf(jed_file, "</jedule>\n");
239   }
240 }
241
242 static void jed_event_free_ref(void *evp)
243 {
244   jed_event_t ev = *(jed_event_t *)evp;
245   jed_event_free(ev);
246 }
247
248 void jedule_init_output() {
249   jedule_event_list = xbt_dynar_new(sizeof(jed_event_t), jed_event_free_ref);
250 }
251
252 void jedule_cleanup_output() {
253   xbt_dynar_free(&jedule_event_list);
254 }
255
256 void jedule_store_event(jed_event_t event) {
257   xbt_assert(event != NULL);
258   xbt_dynar_push(jedule_event_list, &event);
259 }
260
261 #endif