Logo AND Algorithmique Numérique Distribuée

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