Logo AND Algorithmique Numérique Distribuée

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