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