Logo AND Algorithmique Numérique Distribuée

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