Logo AND Algorithmique Numérique Distribuée

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