Logo AND Algorithmique Numérique Distribuée

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