Logo AND Algorithmique Numérique Distribuée

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