Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use homogeneous dictionaries whenever possible.
[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 print_key_value_dict(xbt_dict_t key_value_dict) {
75         xbt_dict_cursor_t cursor=NULL;
76         char *key,*data;
77
78         if( key_value_dict != NULL ) {
79                 xbt_dict_foreach(key_value_dict,cursor,key,data) {
80                         fprintf(jed_file, "<prop key=\"%s\" values=\"%s\" />\n",key,data);
81                 }
82         }
83 }
84
85 static void print_container(jed_simgrid_container_t container) {
86         unsigned int i;
87         jed_simgrid_container_t child_container;
88
89         xbt_assert( container != NULL );
90
91         fprintf(jed_file, "<res name=\"%s\">\n", container->name);
92         if( container->container_children != NULL ) {
93                 xbt_dynar_foreach(container->container_children, i, child_container) {
94                         print_container(child_container);
95                 }
96         } else {
97                 print_resources(container);
98         }
99         fprintf(jed_file, "</res>\n");
100 }
101
102 static void print_resources(jed_simgrid_container_t resource_parent) {
103         int res_nb;
104         unsigned int i;
105         char *res_name;
106         xbt_assert( resource_parent->resource_list != NULL );
107
108         res_nb = xbt_dynar_length(resource_parent->resource_list);
109
110         fprintf(jed_file, "<rset nb=\"%d\" names=\"", res_nb);
111         xbt_dynar_foreach(resource_parent->resource_list, i, res_name) {
112                 fprintf(jed_file, "%s", res_name);
113                 if( i != res_nb-1 ) {
114                         fprintf(jed_file, "|");
115                 }
116         }
117         fprintf(jed_file, "\" />\n");
118 }
119
120
121 static void print_platform(jed_simgrid_container_t root_container) {
122
123         fprintf(jed_file, "<platform>\n");
124         print_container(root_container);
125         fprintf(jed_file, "</platform>\n");
126 }
127
128 static void print_event(jed_event_t event) {
129         unsigned int i;
130         jed_res_subset_t subset;
131
132
133         xbt_assert( event != NULL );
134         xbt_assert( event->resource_subsets != NULL );
135
136         fprintf(jed_file, "<event>\n");
137
138
139         fprintf(jed_file, "<prop value=\"name\" value=\"%s\" />\n", event->name);
140         fprintf(jed_file, "<prop value=\"start\" value=\"%g\" />\n", event->start_time);
141         fprintf(jed_file, "<prop value=\"end\" value=\"%g\" />\n", event->end_time);
142         fprintf(jed_file, "<prop value=\"type\" value=\"%s\" />\n", event->type);
143
144         fprintf(jed_file, "<res_util>\n");
145
146         xbt_dynar_foreach(event->resource_subsets, i, subset) {
147
148                 int start = subset->start_idx;
149                 int end   = subset->start_idx + subset->nres - 1;
150                 xbt_dynar_t hier_list;
151                 unsigned int iter;
152                 int number;
153
154                 hier_list = xbt_dynar_new(sizeof(int), NULL);
155
156                 //printf("subset start %d nres %d\n", subset->start_idx, subset->nres);
157
158                 //printf("parent %s\n", event->resource_selection[i]->parent->name);
159                 get_hierarchy_list(hier_list, subset->parent);
160
161                 fprintf(jed_file, "<select resources=\"");
162
163                 xbt_dynar_foreach(hier_list, iter, number) {
164                         fprintf(jed_file, "%d.", number);
165                 }
166                 fprintf(jed_file, "[%d-%d]", start, end);
167                 fprintf(jed_file, "\" />\n");
168
169                 xbt_dynar_free(&hier_list);
170         }
171
172         fprintf(jed_file, "</res_util>\n");
173
174         fprintf(jed_file, "<characteristics>\n");
175         {
176                 char *ch;
177                 unsigned int iter;
178                 xbt_dynar_foreach(event->characteristics_list, iter, ch) {
179                         fprintf(jed_file, "<characteristic name=\"%s\" />\n", ch);
180                 }
181         }
182         fprintf(jed_file, "</characteristics>\n");
183
184         fprintf(jed_file, "<info>\n");
185         print_key_value_dict(event->info_hash);
186         fprintf(jed_file, "</info>\n");
187
188         fprintf(jed_file, "</event>\n");
189 }
190
191 static void print_events(xbt_dynar_t event_list)  {
192         unsigned int i;
193         jed_event_t event;
194
195         fprintf(jed_file, "<events>\n");
196         xbt_dynar_foreach(event_list, i, event) {
197                 print_event(event);
198         }
199         fprintf(jed_file, "</events>\n");
200 }
201
202
203 void write_jedule_output(FILE *file, jedule_t jedule,
204                 xbt_dynar_t event_list, xbt_dict_t meta_info_dict) {
205
206 //      xbt_assert( jed_file != NULL );
207
208         jed_file = file;
209
210         fprintf(jed_file, "<jedule>\n");
211
212         fprintf(jed_file, "<jedule_meta>\n");
213         print_key_value_dict(jedule->jedule_meta_info);
214         fprintf(jed_file, "</jedule_meta>\n");
215
216         print_platform(jedule->root_container);
217
218         print_events(event_list);
219
220         fprintf(jed_file, "</jedule>\n");
221
222 }
223
224 void jedule_init_output() {
225         jedule_event_list = xbt_dynar_new(sizeof(jed_event_t), NULL);
226 }
227
228 void jedule_cleanup_output() {
229         xbt_dynar_free(&jedule_event_list);
230 }
231
232 void jedule_store_event(jed_event_t event) {
233         xbt_assert(event != NULL);
234         xbt_dynar_push(jedule_event_list, &event);
235 }
236
237 #endif