Logo AND Algorithmique Numérique Distribuée

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