Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f1079cc1dc013473a3d4b4038238ac56fca14c68
[simgrid.git] / src / surf / sg_platf.cpp
1 /* Copyright (c) 2006-2014. 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 "xbt/RngStream.h"
12 #include <xbt/signal.hpp>
13 #include "simgrid/platf_interface.h"
14 #include "surf/surf_routing.h"
15 #include "surf/surf.h"
16
17 #include "src/simix/smx_private.h"
18
19 #include "cpu_interface.hpp"
20 #include "host_interface.hpp"
21
22 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
23 static simgrid::xbt::signal<void(sg_platf_link_cbarg_t)> on_link;
24 static simgrid::xbt::signal<void(sg_platf_cluster_cbarg_t)> on_cluster;
25 static simgrid::xbt::signal<void(void)> on_postparse;
26
27 static int surf_parse_models_setup_already_called = 0;
28
29 /* one RngStream for the platform, to respect some statistic rules */
30 static RngStream sg_platf_rng_stream = NULL;
31
32 /** Module management function: creates all internal data structures */
33 void sg_platf_init(void) {
34 }
35
36 /** Module management function: frees all internal data structures */
37 void sg_platf_exit(void) {
38   on_link.disconnect_all_slots();
39   on_cluster.disconnect_all_slots();
40   on_postparse.disconnect_all_slots();
41
42   /* make sure that we will reinit the models while loading the platf once reinited */
43   surf_parse_models_setup_already_called = 0;
44 }
45
46 void sg_platf_new_host(sg_platf_host_cbarg_t host)
47 {
48   xbt_assert(! sg_host_by_name(host->id),
49                      "Refusing to create a second host named '%s'.", host->id);
50
51   simgrid::surf::NetCard *net = NULL;
52   simgrid::surf::As* current_routing = routing_get_current();
53   if (current_routing)
54     net = routing_add_host(current_routing, host);
55
56   sg_host_t h = simgrid::s4u::Host::by_name_or_create(host->id);
57   simgrid::surf::Cpu *cpu = surf_cpu_model_pm->createCpu(
58                 h,
59         host->speed_peak,
60         host->pstate,
61         host->speed_scale,
62         host->speed_trace,
63         host->core_amount,
64         host->initiallyOn,
65         host->state_trace);
66   surf_host_model->createHost(host->id, net, cpu, host->properties)->attach(h);
67   simgrid::s4u::Host::onCreation(*h);
68
69   if (TRACE_is_enabled() && TRACE_needs_platform())
70     sg_instr_new_host(host);
71 }
72
73 /**
74  * \brief Add a "router" to the network element list
75  */
76 void sg_platf_new_router(sg_platf_router_cbarg_t router)
77 {
78   simgrid::surf::As* current_routing = routing_get_current();
79
80   if (current_routing->p_hierarchy == SURF_ROUTING_NULL)
81     current_routing->p_hierarchy = SURF_ROUTING_BASE;
82   xbt_assert(!xbt_lib_get_or_null(as_router_lib, router->id, ROUTING_ASR_LEVEL),
83              "Reading a router, processing unit \"%s\" already exists",
84              router->id);
85
86   simgrid::surf::NetCard *info = new simgrid::surf::NetCardImpl(
87     xbt_strdup(router->id), -1, SURF_NETWORK_ELEMENT_ROUTER, current_routing);
88   info->setId(current_routing->parsePU(info));
89   xbt_lib_set(as_router_lib, router->id, ROUTING_ASR_LEVEL, (void *) info);
90   XBT_DEBUG("Having set name '%s' id '%d'", router->id, info->getId());
91   simgrid::surf::netcardCreatedCallbacks(info);
92
93   if (router->coord && strcmp(router->coord, "")) {
94     unsigned int cursor;
95     char*str;
96
97     if (!COORD_ASR_LEVEL)
98       xbt_die ("To use host coordinates, please add --cfg=network/coordinates:yes to your command line");
99     /* Pre-parse the host coordinates */
100     xbt_dynar_t ctn_str = xbt_str_split_str(router->coord, " ");
101     xbt_dynar_t ctn = xbt_dynar_new(sizeof(double),NULL);
102     xbt_dynar_foreach(ctn_str,cursor, str) {
103       double val = atof(str);
104       xbt_dynar_push(ctn,&val);
105     }
106     xbt_dynar_shrink(ctn, 0);
107     xbt_dynar_free(&ctn_str);
108     xbt_lib_set(as_router_lib, router->id, COORD_ASR_LEVEL, (void *) ctn);
109     XBT_DEBUG("Having set router coordinates for '%s'",router->id);
110   }
111
112   if (TRACE_is_enabled())
113     sg_instr_new_router(router);
114 }
115
116 void sg_platf_new_link(sg_platf_link_cbarg_t link){
117   on_link(link);
118 }
119
120 void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster)
121 {
122   routing_new_cluster(cluster);
123   on_cluster(cluster);
124 }
125
126 void sg_platf_new_storage(sg_platf_storage_cbarg_t storage)
127 {
128   xbt_assert(!xbt_lib_get_or_null(storage_lib, storage->id,ROUTING_STORAGE_LEVEL),
129                "Refusing to add a second storage named \"%s\"", storage->id);
130
131   void* stype = xbt_lib_get_or_null(storage_type_lib, storage->type_id,ROUTING_STORAGE_TYPE_LEVEL);
132   xbt_assert(stype,"No storage type '%s'", storage->type_id);
133
134   XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
135       storage->id,
136       storage->type_id,
137       storage->content);
138
139   xbt_lib_set(storage_lib,
140       storage->id,
141       ROUTING_STORAGE_LEVEL,
142       (void *) xbt_strdup(storage->type_id));
143
144   // if storage content is not specified use the content of storage_type if any
145   if(!strcmp(storage->content,"") && strcmp(((storage_type_t) stype)->content,"")){
146     storage->content = ((storage_type_t) stype)->content;
147     storage->content_type = ((storage_type_t) stype)->content_type;
148     XBT_DEBUG("For disk '%s' content is empty, inherit the content (of type %s) from storage type '%s' ",
149         storage->id,((storage_type_t) stype)->content_type,
150         ((storage_type_t) stype)->type_id);
151   }
152
153   XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s' "
154       "\n\t\tmodel '%s' \n\t\tcontent '%s'\n\t\tcontent_type '%s' "
155       "\n\t\tproperties '%p''\n",
156       storage->id,
157       ((storage_type_t) stype)->model,
158       ((storage_type_t) stype)->type_id,
159       storage->content,
160       storage->content_type,
161           storage->properties);
162
163   surf_storage_model->createStorage(storage->id,
164                                      ((storage_type_t) stype)->type_id,
165                                      storage->content,
166                                      storage->content_type,
167                                                                          storage->properties,
168                                      storage->attach);
169 }
170 void sg_platf_new_storage_type(sg_platf_storage_type_cbarg_t storage_type){
171
172   xbt_assert(!xbt_lib_get_or_null(storage_type_lib, storage_type->id,ROUTING_STORAGE_TYPE_LEVEL),
173                "Reading a storage type, processing unit \"%s\" already exists", storage_type->id);
174
175   storage_type_t stype = xbt_new0(s_storage_type_t, 1);
176   stype->model = xbt_strdup(storage_type->model);
177   stype->properties = storage_type->properties;
178   stype->content = xbt_strdup(storage_type->content);
179   stype->content_type = xbt_strdup(storage_type->content_type);
180   stype->type_id = xbt_strdup(storage_type->id);
181   stype->size = storage_type->size;
182   stype->model_properties = storage_type->model_properties;
183
184   XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s', "
185       "content '%s', and content_type '%s'",
186       stype->type_id,
187       stype->model,
188       storage_type->content,
189       storage_type->content_type);
190
191   xbt_lib_set(storage_type_lib,
192       stype->type_id,
193       ROUTING_STORAGE_TYPE_LEVEL,
194       (void *) stype);
195 }
196 void sg_platf_new_mstorage(sg_platf_mstorage_cbarg_t mstorage)
197 {
198   THROW_UNIMPLEMENTED;
199 //  mount_t mnt = xbt_new0(s_mount_t, 1);
200 //  mnt->id = xbt_strdup(mstorage->type_id);
201 //  mnt->name = xbt_strdup(mstorage->name);
202 //
203 //  if(!mount_list){
204 //    XBT_DEBUG("Creata a Mount list for %s",A_surfxml_host_id);
205 //    mount_list = xbt_dynar_new(sizeof(char *), NULL);
206 //  }
207 //  xbt_dynar_push(mount_list,(void *) mnt);
208 //  free(mnt->id);
209 //  free(mnt->name);
210 //  xbt_free(mnt);
211 //  XBT_DEBUG("ROUTING Mount a storage name '%s' with type_id '%s'",mstorage->name, mstorage->id);
212 }
213
214 static void mount_free(void *p)
215 {
216   mount_t mnt = (mount_t) p;
217   xbt_free(mnt->name);
218 }
219
220 void sg_platf_new_mount(sg_platf_mount_cbarg_t mount){
221   // Verification of an existing storage
222 #ifndef NDEBUG
223   void* storage = xbt_lib_get_or_null(storage_lib, mount->storageId, ROUTING_STORAGE_LEVEL);
224 #endif
225   xbt_assert(storage,"Disk id \"%s\" does not exists", mount->storageId);
226
227   XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->storageId, mount->name);
228
229   s_mount_t mnt;
230   mnt.storage = surf_storage_resource_priv(surf_storage_resource_by_name(mount->storageId));
231   mnt.name = xbt_strdup(mount->name);
232
233   if(!mount_list){
234     XBT_DEBUG("Create a Mount list for %s",A_surfxml_host_id);
235     mount_list = xbt_dynar_new(sizeof(s_mount_t), mount_free);
236   }
237   xbt_dynar_push(mount_list, &mnt);
238 }
239
240 void sg_platf_new_route(sg_platf_route_cbarg_t route)
241 {
242   routing_get_current()->parseRoute(route);
243 }
244
245 void sg_platf_new_ASroute(sg_platf_route_cbarg_t ASroute)
246 {
247   routing_get_current()->parseASroute(ASroute);
248 }
249
250 void sg_platf_new_bypassRoute(sg_platf_route_cbarg_t bypassRoute)
251 {
252   routing_get_current()->parseBypassroute(bypassRoute);
253 }
254
255 void sg_platf_new_bypassASroute(sg_platf_route_cbarg_t bypassASroute)
256 {
257   routing_get_current()->parseBypassroute(bypassASroute);
258 }
259
260 void sg_platf_new_process(sg_platf_process_cbarg_t process)
261 {
262   if (!simix_global)
263     xbt_die("Cannot create process without SIMIX.");
264
265   sg_host_t host = sg_host_by_name(process->host);
266   if (!host)
267     THROWF(arg_error, 0, "Host '%s' unknown", process->host);
268   xbt_main_func_t parse_code = SIMIX_get_registered_function(process->function);
269   xbt_assert(parse_code, "Function '%s' unknown", process->function);
270
271   double start_time = process->start_time;
272   double kill_time  = process->kill_time;
273   int auto_restart = process->on_failure == SURF_PROCESS_ON_FAILURE_DIE ? 0 : 1;
274
275   smx_process_arg_t arg = NULL;
276   smx_process_t process_created = NULL;
277
278   arg = xbt_new0(s_smx_process_arg_t, 1);
279   arg->code = parse_code;
280   arg->data = NULL;
281   arg->hostname = sg_host_get_name(host);
282   arg->argc = process->argc;
283   arg->argv = xbt_new(char *,process->argc);
284   int i;
285   for (i=0; i<process->argc; i++)
286     arg->argv[i] = xbt_strdup(process->argv[i]);
287   arg->name = xbt_strdup(arg->argv[0]);
288   arg->kill_time = kill_time;
289   arg->properties = current_property_set;
290   if (!sg_host_simix(host)->boot_processes) {
291     sg_host_simix(host)->boot_processes = xbt_dynar_new(sizeof(smx_process_arg_t), _SIMIX_host_free_process_arg);
292   }
293   xbt_dynar_push_as(sg_host_simix(host)->boot_processes,smx_process_arg_t,arg);
294
295   if (start_time > SIMIX_get_clock()) {
296     arg = xbt_new0(s_smx_process_arg_t, 1);
297     arg->name = (char*)(process->argv)[0];
298     arg->code = parse_code;
299     arg->data = NULL;
300     arg->hostname = sg_host_get_name(host);
301     arg->argc = process->argc;
302     arg->argv = (char**)(process->argv);
303     arg->kill_time = kill_time;
304     arg->properties = current_property_set;
305
306     XBT_DEBUG("Process %s(%s) will be started at time %f", arg->name,
307            arg->hostname, start_time);
308     SIMIX_timer_set(start_time, [](void* arg) {
309       SIMIX_process_create_from_wrapper((smx_process_arg_t) arg);
310     }, arg);
311   } else {                      // start_time <= SIMIX_get_clock()
312     XBT_DEBUG("Starting Process %s(%s) right now", process->argv[0], sg_host_get_name(host));
313
314     if (simix_global->create_process_function)
315       process_created = simix_global->create_process_function(
316                                             (char*)(process->argv)[0],
317                                             parse_code,
318                                             NULL,
319                                             sg_host_get_name(host),
320                                             kill_time,
321                                             process->argc,
322                                             (char**)(process->argv),
323                                             current_property_set,
324                                             auto_restart, NULL);
325     else
326       process_created = simcall_process_create((char*)(process->argv)[0], parse_code, NULL, sg_host_get_name(host), kill_time, process->argc,
327           (char**)process->argv, current_property_set,auto_restart);
328
329     /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
330     if (!process_created) {
331       return;
332     }
333   }
334   current_property_set = NULL;
335 }
336
337 void sg_platf_route_begin (sg_platf_route_cbarg_t route){
338   route->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
339 }
340 void sg_platf_ASroute_begin (sg_platf_route_cbarg_t ASroute){
341   ASroute->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
342 }
343
344 void sg_platf_route_end (sg_platf_route_cbarg_t route){
345   sg_platf_new_route(route);
346 }
347 void sg_platf_ASroute_end (sg_platf_route_cbarg_t ASroute){
348   sg_platf_new_ASroute(ASroute);
349 }
350
351 void sg_platf_route_add_link (const char* link_id, sg_platf_route_cbarg_t route){
352   char *link_name = xbt_strdup(link_id);
353   xbt_dynar_push(route->link_list, &link_name);
354 }
355 void sg_platf_ASroute_add_link (const char* link_id, sg_platf_route_cbarg_t ASroute){
356   char *link_name = xbt_strdup(link_id);
357   xbt_dynar_push(ASroute->link_list, &link_name);
358 }
359
360 void sg_platf_begin() { /* Do nothing: just for symmetry of user code */ }
361
362 void sg_platf_end() {
363   on_postparse();
364 }
365
366 void sg_platf_new_AS_begin(sg_platf_AS_cbarg_t AS)
367 {
368   if (!surf_parse_models_setup_already_called) {
369     /* Initialize the surf models. That must be done after we got all config, and before we need the models.
370      * That is, after the last <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
371      *
372      * I'm not sure for <trace> and <trace_connect>, there may be a bug here
373      * (FIXME: check it out by creating a file beginning with one of these tags)
374      * but cluster and peer create ASes internally, so putting the code in there is ok.
375      *
376      * TODO, There used to be a guard protecting here against
377      * xbt_dynar_length(sg_platf_AS_begin_cb_list) because we don't want to
378      * initialize the models if we are parsing the file to get the deployment.
379      * That could happen if the same file would be used for platf and deploy:
380      * it'd contain AS tags even during the deploy parsing. Removing that guard
381      * would result of the models to get re-inited when parsing for deploy.
382      * Currently using the same file for platform and deployment is broken
383      * however. This guard will have to ba adapted in order to make this feature
384      * work again.
385      */
386     surf_parse_models_setup_already_called = 1;
387     surf_config_models_setup();
388   }
389
390   routing_AS_begin(AS);
391   if (TRACE_is_enabled())
392     sg_instr_AS_begin(AS);
393 }
394
395 void sg_platf_new_AS_end()
396 {
397   routing_AS_end();
398   if (TRACE_is_enabled())
399     sg_instr_AS_end();
400 }
401 /* ***************************************** */
402
403 void sg_platf_link_add_cb(sg_platf_link_cb_t fct) {
404   on_link.connect(fct);
405 }
406 void sg_platf_cluster_add_cb(sg_platf_cluster_cb_t fct) {
407   on_cluster.connect(fct);
408 }
409 void sg_platf_postparse_add_cb(void_f_void_t fct) {
410   on_postparse.connect(fct);
411 }
412
413 void sg_platf_rng_stream_init(unsigned long seed[6]) {
414   RngStream_SetPackageSeed(seed);
415   sg_platf_rng_stream = RngStream_CreateStream(NULL);
416 }
417
418 RngStream sg_platf_rng_stream_get(const char* id) {
419   RngStream stream = NULL;
420   unsigned int id_hash;
421
422   stream = RngStream_CopyStream(sg_platf_rng_stream);
423   id_hash = xbt_str_hash(id);
424   RngStream_AdvanceState(stream, 0, (long)id_hash);
425
426   return stream;
427 }