1 /* Copyright (c) 2010-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
11 #include "xbt/dynar.h"
12 #include "xbt/asserts.h"
14 #include "simgrid/jedule/jedule_output.h"
18 #define STR_BUF_SIZE 1024
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jed_out, jedule, "Logging specific to Jedule output");
22 xbt_dynar_t jedule_event_list;
24 static FILE *jed_file;
26 static void get_hierarchy_list(xbt_dynar_t hier_list, jed_simgrid_container_t container) {
27 xbt_assert( container != nullptr );
29 if( container->parent != nullptr ) {
31 if( container->parent->container_children == nullptr ) {
32 // we are in the last level
33 get_hierarchy_list(hier_list, container->parent);
37 jed_simgrid_container_t child_container;
39 xbt_dynar_foreach(container->parent->container_children, i, child_container) {
40 if( child_container == container ) {
46 xbt_assert( child_nb > - 1);
47 xbt_dynar_insert_at(hier_list, 0, &child_nb);
49 get_hierarchy_list(hier_list, container->parent);
53 xbt_dynar_insert_at(hier_list, 0, &top_level);
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;
65 hier_list = xbt_dynar_new(sizeof(int), nullptr);
66 get_hierarchy_list(hier_list, container);
68 length = xbt_dynar_length(hier_list);
70 xbt_dynar_foreach(hier_list, iter, number) {
71 if( iter != length-1 ) {
72 snprintf(buf, STR_BUF_SIZE, "%d.", number);
74 snprintf(buf, STR_BUF_SIZE, "%d", number);
76 strncat(outbuf, buf, bufsize-strlen(outbuf));
79 xbt_dynar_free(&hier_list);
82 static void print_key_value_dict(xbt_dict_t key_value_dict) {
83 xbt_dict_cursor_t cursor=nullptr;
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);
93 static void print_resources(jed_simgrid_container_t resource_parent) {
97 char resid[STR_BUF_SIZE];
98 xbt_assert( resource_parent->resource_list != nullptr );
100 res_nb = xbt_dynar_length(resource_parent->resource_list);
102 get_hierarchy_string(resource_parent, resid, STR_BUF_SIZE);
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, "|");
111 fprintf(jed_file, "\" />\n");
114 static void print_container(jed_simgrid_container_t container) {
116 jed_simgrid_container_t child_container;
118 xbt_assert( container != nullptr );
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);
126 print_resources(container);
128 fprintf(jed_file, " </res>\n");
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");
137 static void print_event(jed_event_t event) {
139 jed_res_subset_t subset;
141 xbt_assert( event != nullptr );
142 xbt_assert( event->resource_subsets != nullptr );
144 fprintf(jed_file, " <event>\n");
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);
151 fprintf(jed_file, " <res_util>\n");
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];
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");
165 fprintf(jed_file, " </res_util>\n");
166 if (!xbt_dynar_is_empty(event->characteristics_list)){
167 fprintf(jed_file, " <characteristics>\n");
170 xbt_dynar_foreach(event->characteristics_list, iter, ch) {
171 fprintf(jed_file, " <characteristic name=\"%s\" />\n", ch);
173 fprintf(jed_file, " </characteristics>\n");
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");
182 fprintf(jed_file, " </event>\n");
185 static void print_events(xbt_dynar_t event_list) {
189 fprintf(jed_file, " <events>\n");
190 xbt_dynar_foreach(event_list, i, event) {
193 fprintf(jed_file, " </events>\n");
196 void write_jedule_output(FILE *file, jedule_t jedule, xbt_dynar_t event_list, xbt_dict_t meta_info_dict) {
199 if (!xbt_dynar_is_empty(jedule_event_list)){
201 fprintf(jed_file, "<jedule>\n");
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");
209 print_platform(jedule->root_container);
211 print_events(event_list);
213 fprintf(jed_file, "</jedule>\n");
217 static void jed_event_free_ref(void *evp)
219 jed_event_t ev = *(jed_event_t *)evp;
223 void jedule_init_output() {
224 jedule_event_list = xbt_dynar_new(sizeof(jed_event_t), jed_event_free_ref);
227 void jedule_cleanup_output() {
228 xbt_dynar_free(&jedule_event_list);
231 void jedule_store_event(jed_event_t event) {
232 xbt_assert(event != nullptr);
233 xbt_dynar_push(jedule_event_list, &event);