Logo AND Algorithmique Numérique Distribuée

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