Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / src / surf / surfxml_parseplatf.cpp
1 /* Copyright (c) 2006-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/misc.h"
8 #include "xbt/log.h"
9 #include "xbt/str.h"
10 #include "xbt/dict.h"
11 #include "simgrid/platf.h"
12 #include "surf/surfxml_parse.h"
13 #include "src/surf/cpu_interface.hpp"
14 #include "src/surf/surf_private.h"
15
16 #ifdef HAVE_LUA
17 extern "C" {
18 #include "src/bindings/lua/simgrid_lua.h"
19
20 #include <lua.h>                /* Always include this when calling Lua */
21 #include <lauxlib.h>            /* Always include this when calling Lua */
22 #include <lualib.h>             /* Prototype for luaL_openlibs(), */
23 }
24 #endif
25
26 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
27
28 /*
29  *  Allow the cluster tag to mess with the parsing buffer.
30  * (this will probably become obsolete once the cluster tag do not mess with the parsing callbacks directly)
31  */
32
33 /* This buffer is used to store the original buffer before substituting it by out own buffer. Useful for the cluster tag */
34 static xbt_dynar_t surfxml_bufferstack_stack = NULL;
35 int surfxml_bufferstack_size = 2048;
36
37 static char *old_buff = NULL;
38
39 XBT_IMPORT_NO_EXPORT(unsigned int) surfxml_buffer_stack_stack_ptr;
40 XBT_IMPORT_NO_EXPORT(unsigned int) surfxml_buffer_stack_stack[1024];
41
42 void surfxml_bufferstack_push(int new_one)
43 {
44   if (!new_one)
45     old_buff = surfxml_bufferstack;
46   else {
47     xbt_dynar_push(surfxml_bufferstack_stack, &surfxml_bufferstack);
48     surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
49   }
50 }
51
52 void surfxml_bufferstack_pop(int new_one)
53 {
54   if (!new_one)
55     surfxml_bufferstack = old_buff;
56   else {
57     free(surfxml_bufferstack);
58     xbt_dynar_pop(surfxml_bufferstack_stack, &surfxml_bufferstack);
59   }
60 }
61
62 /*
63  * Trace related stuff
64  */
65
66 xbt_dict_t traces_set_list = NULL;
67 xbt_dict_t trace_connect_list_host_avail = NULL;
68 xbt_dict_t trace_connect_list_host_speed = NULL;
69 xbt_dict_t trace_connect_list_link_avail = NULL;
70 xbt_dict_t trace_connect_list_link_bw = NULL;
71 xbt_dict_t trace_connect_list_link_lat = NULL;
72
73 /* ***************************************** */
74
75 static int after_config_done;
76 void parse_after_config() {
77   if (!after_config_done) {
78           TRACE_start();
79
80     /* Register classical callbacks */
81     storage_register_callbacks();
82     routing_register_callbacks();
83
84     after_config_done = 1;
85   }
86 }
87
88 /* This function acts as a main in the parsing area. */
89 void parse_platform_file(const char *file)
90 {
91 #ifdef HAVE_LUA
92   int is_lua = (file != NULL && strlen(file) > 3 && file[strlen(file)-3] == 'l' && file[strlen(file)-2] == 'u'
93         && file[strlen(file)-1] == 'a');
94 #endif
95
96   surf_parse_init_callbacks();
97
98 #ifdef HAVE_LUA
99   /* Check if file extension is "lua". If so, we will use
100    * the lua bindings to parse the platform file (since it is
101    * written in lua). If not, we will use the (old?) XML parser
102    */
103   if (is_lua) {
104     lua_State* L = luaL_newstate();
105     luaL_openlibs(L);
106
107     luaL_loadfile(L, file); // This loads the file without executing it.
108
109     /* Run the script */
110     if (lua_pcall(L, 0, 0, 0)) {
111         XBT_ERROR("FATAL ERROR:\n  %s: %s\n\n", "Lua call failed. Errormessage:", lua_tostring(L, -1));
112         xbt_die("Lua call failed. See Log");
113     }
114   }
115   else
116 #endif
117   { // Use XML parser
118
119     int parse_status;
120
121     /* init the flex parser */
122     surfxml_buffer_stack_stack_ptr = 1;
123     surfxml_buffer_stack_stack[0] = 0;
124     after_config_done = 0;
125     surf_parse_open(file);
126
127     traces_set_list = xbt_dict_new_homogeneous(NULL);
128     trace_connect_list_host_avail = xbt_dict_new_homogeneous(free);
129     trace_connect_list_host_speed = xbt_dict_new_homogeneous(free);
130     trace_connect_list_link_avail = xbt_dict_new_homogeneous(free);
131     trace_connect_list_link_bw = xbt_dict_new_homogeneous(free);
132     trace_connect_list_link_lat = xbt_dict_new_homogeneous(free);
133
134     /* Init my data */
135     if (!surfxml_bufferstack_stack)
136       surfxml_bufferstack_stack = xbt_dynar_new(sizeof(char *), NULL);
137
138     /* Do the actual parsing */
139     parse_status = surf_parse();
140
141     /* connect all traces relative to hosts */
142     xbt_dict_cursor_t cursor = NULL;
143     char *trace_name, *elm;
144
145     xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
146       tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
147       xbt_assert(trace, "Trace %s undefined", trace_name);
148
149       simgrid::s4u::Host *host = sg_host_by_name(elm);
150       xbt_assert(host, "Host %s undefined", elm);
151       simgrid::surf::Cpu *cpu = host->pimpl_cpu;
152
153       cpu->set_state_trace(trace);
154     }
155
156     /* Free my data */
157     xbt_dict_free(&trace_connect_list_host_avail);
158     xbt_dict_free(&trace_connect_list_host_speed);
159     xbt_dict_free(&trace_connect_list_link_avail);
160     xbt_dict_free(&trace_connect_list_link_bw);
161     xbt_dict_free(&trace_connect_list_link_lat);
162     xbt_dict_free(&traces_set_list);
163     xbt_dict_free(&random_data_list);
164     xbt_dynar_free(&surfxml_bufferstack_stack);
165
166     surf_parse_close();
167
168     if (parse_status)
169       surf_parse_error("Parse error in %s", file);
170
171   }
172
173
174 }