Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
getting rid of dynar in instr (part 1)
[simgrid.git] / src / instr / jedule / jedule_sd_binding.cpp
1 /* Copyright (c) 2010-2015. 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 "xbt/asserts.h"
8 #include "xbt/dynar.h"
9
10 #include "src/surf/surf_private.h"
11 #include "surf/surf.h"
12
13 #include "simgrid/jedule/jedule_sd_binding.h"
14 #include "simgrid/jedule/jedule_events.h"
15 #include "simgrid/jedule/jedule_platform.h"
16 #include "simgrid/jedule/jedule_output.h"
17
18 #include "simgrid/simdag.h"
19 #include "simgrid/s4u/As.hpp"
20 #include "simgrid/s4u/engine.hpp"
21
22 #include <stdio.h>
23 #include "../../simdag/simdag_private.hpp"
24
25 #if HAVE_JEDULE
26
27 XBT_LOG_NEW_CATEGORY(jedule, "Logging specific to Jedule");
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jed_sd, jedule, "Logging specific to Jedule SD binding");
29
30 jedule_t jedule;
31
32 void jedule_log_sd_event(SD_task_t task)
33 {
34   jed_event_t event;
35
36   xbt_assert(task != nullptr);
37
38   xbt_dynar_t host_list = xbt_dynar_new(sizeof(char*), nullptr);
39
40   for(auto host: *task->allocation){
41     const char *hostname = sg_host_get_name(host);
42     xbt_dynar_push(host_list, &hostname);
43   }
44
45   create_jed_event(&event, (char*)SD_task_get_name(task), task->start_time, task->finish_time,"SD");
46
47   jed_event_add_resources(event, host_list);
48   jedule_store_event(event);
49
50   xbt_dynar_free(&host_list);
51 }
52
53 static void create_hierarchy(AS_t current_comp, jed_simgrid_container_t current_container)
54 {
55   xbt_dict_cursor_t cursor = nullptr;
56   char *key;
57   AS_t elem;
58   xbt_dict_t routing_sons = current_comp->children();
59
60   if (xbt_dict_is_empty(routing_sons)) {
61     // I am no AS
62     // add hosts to jedule platform
63     xbt_dynar_t table = current_comp->hosts();
64     xbt_dynar_t hosts;
65     unsigned int dynar_cursor;
66     sg_host_t host_elem;
67
68     hosts = xbt_dynar_new(sizeof(char*), nullptr);
69
70     xbt_dynar_foreach(table, dynar_cursor, host_elem) {
71       xbt_dynar_push_as(hosts, const char*, sg_host_get_name(host_elem));
72     }
73
74     jed_simgrid_add_resources(current_container, hosts);
75     xbt_dynar_free(&hosts);
76     xbt_dynar_free(&table);
77   } else {
78     xbt_dict_foreach(routing_sons, cursor, key, elem) {
79       jed_simgrid_container_t child_container;
80       jed_simgrid_create_container(&child_container, elem->name());
81       jed_simgrid_add_container(current_container, child_container);
82       XBT_DEBUG("name : %s\n", elem->name());
83       create_hierarchy(elem, child_container);
84     }
85   }
86 }
87
88 void jedule_setup_platform()
89 {
90   jed_create_jedule(&jedule);
91
92   AS_t root_comp = simgrid::s4u::Engine::instance()->rootAs();
93   XBT_DEBUG("root name %s\n", root_comp->name());
94
95   jed_simgrid_container_t root_container;
96   jed_simgrid_create_container(&root_container, root_comp->name());
97   jedule->root_container = root_container;
98
99   create_hierarchy(root_comp, root_container);
100 }
101
102 void jedule_sd_cleanup()
103 {
104   jedule_cleanup_output();
105 }
106
107 void jedule_sd_init()
108 {
109   jedule_init_output();
110 }
111
112 void jedule_sd_exit(void)
113 {
114   if (jedule) {
115     jed_free_jedule(jedule);
116     jedule = nullptr;
117   }
118 }
119
120 void jedule_sd_dump(const char * filename)
121 {
122   if (jedule) {
123     char *fname;
124     FILE *fh;
125     if (!filename) {
126       fname = bprintf("%s.jed", xbt_binary_name);
127     } else {
128       fname = xbt_strdup(filename);
129     }
130
131     fh = fopen(fname, "w");
132
133     write_jedule_output(fh, jedule, jedule_event_list, nullptr);
134
135     fclose(fh);
136     free(fname);
137   }
138 }
139 #endif