Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / surf / surfxml_parseplatf.c
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/surf_private.h"
14
15 #ifdef HAVE_LUA
16 #include "src/bindings/lua/simgrid_lua.h"
17 #include "src/bindings/lua/lua_state_cloner.h"
18
19 #include <lua.h>                /* Always include this when calling Lua */
20 #include <lauxlib.h>            /* Always include this when calling Lua */
21 #include <lualib.h>             /* Prototype for luaL_openlibs(), */
22 #endif
23
24 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
25
26 /*
27  *  Allow the cluster tag to mess with the parsing buffer.
28  * (this will probably become obsolete once the cluster tag do not mess with the parsing callbacks directly)
29  */
30
31 /* This buffer is used to store the original buffer before substituting it by out own buffer. Useful for the cluster tag */
32 static xbt_dynar_t surfxml_bufferstack_stack = NULL;
33 int surfxml_bufferstack_size = 2048;
34
35 static char *old_buff = NULL;
36
37 XBT_IMPORT_NO_EXPORT(unsigned int) surfxml_buffer_stack_stack_ptr;
38 XBT_IMPORT_NO_EXPORT(unsigned int) surfxml_buffer_stack_stack[1024];
39
40 void surfxml_bufferstack_push(int new)
41 {
42   if (!new)
43     old_buff = surfxml_bufferstack;
44   else {
45     xbt_dynar_push(surfxml_bufferstack_stack, &surfxml_bufferstack);
46     surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
47   }
48 }
49
50 void surfxml_bufferstack_pop(int new)
51 {
52   if (!new)
53     surfxml_bufferstack = old_buff;
54   else {
55     free(surfxml_bufferstack);
56     xbt_dynar_pop(surfxml_bufferstack_stack, &surfxml_bufferstack);
57   }
58 }
59
60 /*
61  * Trace related stuff
62  */
63
64 xbt_dict_t traces_set_list = NULL;
65 xbt_dict_t trace_connect_list_host_avail = NULL;
66 xbt_dict_t trace_connect_list_power = NULL;
67 xbt_dict_t trace_connect_list_link_avail = NULL;
68 xbt_dict_t trace_connect_list_bandwidth = NULL;
69 xbt_dict_t trace_connect_list_latency = NULL;
70
71 /* ********************************************* */
72 /* TUTORIAL: New TAG                             */
73 /* This function should be in gpu.c              */
74 /* because sg_platf_gpu_add_cb take a staic fct  */
75 XBT_PUBLIC(void) gpu_register_callbacks(void){
76   sg_platf_gpu_add_cb(NULL);
77 }
78 /* ***************************************** */
79
80 static int after_config_done;
81 void parse_after_config() {
82   if (!after_config_done) {
83           TRACE_start();
84
85     /* Register classical callbacks */
86     storage_register_callbacks();
87     routing_register_callbacks();
88     gpu_register_callbacks();
89
90     /* ***************************************** */
91     /* TUTORIAL: New TAG                         */
92     /* ***************************************** */
93     after_config_done = 1;
94   }
95 }
96
97 /* This function acts as a main in the parsing area. */
98 void parse_platform_file(const char *file)
99 {
100 #ifdef HAVE_LUA
101   int is_lua = (file != NULL && strlen(file) > 3 && file[strlen(file)-3] == 'l' && file[strlen(file)-2] == 'u'
102         && file[strlen(file)-1] == 'a');
103 #endif
104
105   surf_parse_init_callbacks();
106
107 #ifdef HAVE_LUA
108   /* Check if file extension is "lua". If so, we will use
109    * the lua bindings to parse the platform file (since it is
110    * written in lua). If not, we will use the (old?) XML parser
111    */
112   if (is_lua) {
113     // Get maestro state. In case we're calling Lua from
114     // C only, this will be NULL -- no Lua code has been
115     // executed yet and hence, the SimGrid module has not
116     // yet been loaded.
117     // NOTE: After executing the lua_pcall() below,
118     // sglua_get_maestro() will not be NULL, since the
119     // SimGrid module was loaded!
120     lua_State* L = sglua_get_maestro();
121
122     // We may want to remove the task_copy_callback from
123     // the SimGrid module if we're using C code only (this
124     // callback is used for Lua-only code).
125     int remove_callback = FALSE;
126     if (L == NULL) {
127         L = luaL_newstate();
128         remove_callback = TRUE;
129     }
130     luaL_openlibs(L);
131
132     luaL_loadfile(L, file); // This loads the file without executing it.
133
134     /* Run the script */
135     if (lua_pcall(L, 0, 0, 0)) {
136         XBT_ERROR("FATAL ERROR:\n  %s: %s\n\n", "Lua call failed. Errormessage:", lua_tostring(L, -1));
137         xbt_die("Lua call failed. See Log");
138     }
139     // Without this, task_copy_callback() will try to copy
140     // some tasks -- but these don't exist in case we're using
141     // C. Hence, we need to remove the callback -- we don't
142     // want to segfault.
143     if (remove_callback) {
144       MSG_task_set_copy_callback(NULL);
145     }
146
147   }
148   else
149 #endif
150   { // Use XML parser
151
152     int parse_status;
153
154     /* init the flex parser */
155     surfxml_buffer_stack_stack_ptr = 1;
156     surfxml_buffer_stack_stack[0] = 0;
157     after_config_done = 0;
158     surf_parse_open(file);
159
160     traces_set_list = xbt_dict_new_homogeneous(NULL);
161     trace_connect_list_host_avail = xbt_dict_new_homogeneous(free);
162     trace_connect_list_power = xbt_dict_new_homogeneous(free);
163     trace_connect_list_link_avail = xbt_dict_new_homogeneous(free);
164     trace_connect_list_bandwidth = xbt_dict_new_homogeneous(free);
165     trace_connect_list_latency = xbt_dict_new_homogeneous(free);
166
167     /* Init my data */
168     if (!surfxml_bufferstack_stack)
169       surfxml_bufferstack_stack = xbt_dynar_new(sizeof(char *), NULL);
170
171     /* Do the actual parsing */
172     parse_status = surf_parse();
173
174     /* Free my data */
175     xbt_dict_free(&trace_connect_list_host_avail);
176     xbt_dict_free(&trace_connect_list_power);
177     xbt_dict_free(&trace_connect_list_link_avail);
178     xbt_dict_free(&trace_connect_list_bandwidth);
179     xbt_dict_free(&trace_connect_list_latency);
180     xbt_dict_free(&traces_set_list);
181     xbt_dict_free(&random_data_list);
182     xbt_dynar_free(&surfxml_bufferstack_stack);
183
184     surf_parse_close();
185
186     if (parse_status)
187       surf_parse_error("Parse error in %s", file);
188
189   }
190
191
192 }