Logo AND Algorithmique Numérique Distribuée

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