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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jed_out, jedule, "Logging specific to Jedule output");
20 xbt_dynar_t jedule_event_list;
22 static FILE *jed_file;
24 static void get_hierarchy_list(xbt_dynar_t hier_list, jed_simgrid_container_t container) {
25 xbt_assert( container != NULL );
27 if( container->parent != NULL ) {
29 if( container->parent->container_children == NULL ) {
30 // we are in the last level
31 get_hierarchy_list(hier_list, container->parent);
35 jed_simgrid_container_t child_container;
37 xbt_dynar_foreach(container->parent->container_children, i, child_container) {
38 if( child_container == container ) {
44 xbt_assert( child_nb > - 1);
45 xbt_dynar_insert_at(hier_list, 0, &child_nb);
47 get_hierarchy_list(hier_list, container->parent);
51 xbt_dynar_insert_at(hier_list, 0, &top_level);
55 static void get_hierarchy_string(jed_simgrid_container_t container, char *outbuf) {
57 xbt_dynar_t hier_list;
63 hier_list = xbt_dynar_new(sizeof(int), NULL);
64 get_hierarchy_list(hier_list, container);
66 length = xbt_dynar_length(hier_list);
68 xbt_dynar_foreach(hier_list, iter, number) {
69 if( iter != length-1 ) {
70 sprintf(buf, "%d.", number);
72 sprintf(buf, "%d", number);
77 xbt_dynar_free(&hier_list);
80 static void print_key_value_dict(xbt_dict_t key_value_dict) {
81 xbt_dict_cursor_t cursor=NULL;
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);
91 static void print_resources(jed_simgrid_container_t resource_parent) {
96 xbt_assert( resource_parent->resource_list != NULL );
98 res_nb = xbt_dynar_length(resource_parent->resource_list);
100 get_hierarchy_string(resource_parent, resid);
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, "|");
109 fprintf(jed_file, "\" />\n");
112 static void print_container(jed_simgrid_container_t container) {
114 jed_simgrid_container_t child_container;
116 xbt_assert( container != NULL );
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);
124 print_resources(container);
126 fprintf(jed_file, " </res>\n");
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");
135 static void print_event(jed_event_t event) {
137 jed_res_subset_t subset;
139 xbt_assert( event != NULL );
140 xbt_assert( event->resource_subsets != NULL );
142 fprintf(jed_file, " <event>\n");
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);
149 fprintf(jed_file, " <res_util>\n");
151 xbt_dynar_foreach(event->resource_subsets, i, subset) {
152 int start = subset->start_idx;
153 int end = subset->start_idx + subset->nres - 1;
156 get_hierarchy_string(subset->parent, resid);
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");
163 fprintf(jed_file, " </res_util>\n");
164 if (!xbt_dynar_is_empty(event->characteristics_list)){
165 fprintf(jed_file, " <characteristics>\n");
168 xbt_dynar_foreach(event->characteristics_list, iter, ch) {
169 fprintf(jed_file, " <characteristic name=\"%s\" />\n", ch);
171 fprintf(jed_file, " </characteristics>\n");
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");
180 fprintf(jed_file, " </event>\n");
183 static void print_events(xbt_dynar_t event_list) {
187 fprintf(jed_file, " <events>\n");
188 xbt_dynar_foreach(event_list, i, event) {
191 fprintf(jed_file, " </events>\n");
194 void write_jedule_output(FILE *file, jedule_t jedule, xbt_dynar_t event_list, xbt_dict_t meta_info_dict) {
197 if (!xbt_dynar_is_empty(jedule_event_list)){
199 fprintf(jed_file, "<jedule>\n");
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");
207 print_platform(jedule->root_container);
209 print_events(event_list);
211 fprintf(jed_file, "</jedule>\n");
215 static void jed_event_free_ref(void *evp)
217 jed_event_t ev = *(jed_event_t *)evp;
221 void jedule_init_output() {
222 jedule_event_list = xbt_dynar_new(sizeof(jed_event_t), jed_event_free_ref);
225 void jedule_cleanup_output() {
226 xbt_dynar_free(&jedule_event_list);
229 void jedule_store_event(jed_event_t event) {
230 xbt_assert(event != NULL);
231 xbt_dynar_push(jedule_event_list, &event);