Logo AND Algorithmique Numérique Distribuée

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