Logo AND Algorithmique Numérique Distribuée

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