Logo AND Algorithmique Numérique Distribuée

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