1 /* Copyright (c) 2004-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/dynar.h"
12 #include "xbt/graph.h"
14 #include "xbt/config.h"
15 #include "src/internal_config.h"
16 #include "surf/surf_routing.h"
17 #include "surf/datatypes.h"
19 #include "surf/surf_routing.h"
20 #include "simgrid/datatypes.h"
21 #include "simgrid/forward.h"
24 /* Actions and models are highly connected structures... */
26 /* user-visible parameters */
27 extern XBT_PRIVATE double sg_tcp_gamma;
28 extern XBT_PRIVATE double sg_sender_gap;
29 extern XBT_PRIVATE double sg_latency_factor;
30 extern XBT_PRIVATE double sg_bandwidth_factor;
31 extern XBT_PRIVATE double sg_weight_S_parameter;
32 extern XBT_PRIVATE int sg_network_crosstraffic;
47 class NetworkCm02Link;
50 class StorageActionLmm;
54 typedef simgrid::surf::Model surf_Model;
55 typedef simgrid::surf::CpuModel surf_CpuModel;
56 typedef simgrid::surf::Cpu surf_Cpu;
57 typedef simgrid::surf::HostModel surf_HostModel;
58 typedef simgrid::surf::NetworkModel surf_NetworkModel;
59 typedef simgrid::surf::StorageModel surf_StorageModel;
60 typedef simgrid::surf::Resource surf_Resource;
61 typedef simgrid::surf::ResourceLmm surf_ResourceLmm;
62 typedef simgrid::surf::HostImpl surf_Host;
63 typedef simgrid::surf::HostCLM03 surf_HostCLM03;
64 typedef simgrid::surf::NetworkCm02Link surf_NetworkCm02Link;
65 typedef simgrid::surf::Action surf_Action;
66 typedef simgrid::surf::ActionLmm surf_ActionLmm;
67 typedef simgrid::surf::StorageActionLmm surf_StorageActionLmm;
71 typedef struct surf_Model surf_Model;
72 typedef struct surf_CpuModel surf_CpuModel;
73 typedef struct surf_Cpu surf_Cpu;
74 typedef struct surf_HostModel surf_HostModel;
75 typedef struct surf_NetworkModel surf_NetworkModel;
76 typedef struct surf_StorageModel surf_StorageModel;
77 typedef struct surf_Resource surf_Resource;
78 typedef struct surf_ResourceLmm surf_ResourceLmm;
79 typedef struct surf_HostCLM03 surf_HostCLM03;
80 typedef struct surf_Host surf_Host;
81 typedef struct surf_NetworkCm02Link surf_NetworkCm02Link;
82 typedef struct surf_Action surf_Action;
83 typedef struct surf_ActionLmm surf_ActionLmm;
84 typedef struct surf_StorageActionLmm surf_StorageActionLmm;
88 /** @ingroup SURF_c_bindings
89 * \brief Model datatype
91 * Generic data structure for a model. The hosts,
92 * the CPUs and the network links are examples of models.
94 typedef surf_Model *surf_model_t;
95 typedef surf_CpuModel *surf_cpu_model_t;
96 typedef surf_Cpu* surf_cpu_t;
97 typedef surf_HostModel *surf_host_model_t;
98 typedef surf_NetworkModel *surf_network_model_t;
99 typedef surf_StorageModel *surf_storage_model_t;
101 typedef xbt_dictelm_t surf_resource_t;
102 typedef surf_Resource *surf_cpp_resource_t;
103 typedef surf_Host *surf_host_t;
104 typedef surf_Cpu *surf_cpu_t;
106 /** @ingroup SURF_c_bindings
107 * \brief Action structure
109 * Never create s_surf_action_t by yourself ! The actions are created
110 * on the fly when you call execute or communicate on a model.
112 * \see e_surf_action_state_t
114 typedef surf_Action *surf_action_t;
116 typedef struct surf_file *surf_file_t;
118 /** \brief Resource model description
120 struct surf_model_description {
122 const char *description;
123 void_f_void_t model_init_preparse;
125 typedef struct surf_model_description s_surf_model_description_t;
126 typedef struct surf_model_description* surf_model_description_t;
128 XBT_PUBLIC(int) find_model_description(s_surf_model_description_t * table, const char *name);
129 XBT_PUBLIC(void) model_help(const char *category, s_surf_model_description_t * table);
131 /***************************/
132 /* Generic model object */
133 /***************************/
135 static inline void *surf_storage_resource_priv(const void *storage){
136 return (void*)xbt_lib_get_level((xbt_dictelm_t)storage, SURF_STORAGE_LEVEL);
139 static inline void *surf_storage_resource_by_name(const char *name){
140 return xbt_lib_get_elm_or_null(storage_lib, name);
143 /** @{ @ingroup SURF_c_bindings */
146 * @brief Pop an action from the done actions set
148 * @param model The model from which the action is extracted
149 * @return An action in done state
151 XBT_PUBLIC(surf_action_t) surf_model_extract_done_action_set(surf_model_t model);
154 * @brief Pop an action from the failed actions set
156 * @param model The model from which the action is extracted
157 * @return An action in failed state
159 XBT_PUBLIC(surf_action_t) surf_model_extract_failed_action_set(surf_model_t model);
162 * @brief Get the size of the running action set of a model
164 * @param model The model
165 * @return The size of the running action set
167 XBT_PUBLIC(int) surf_model_running_action_set_size(surf_model_t model);
169 /** @brief Create a file opening action on the given host */
170 XBT_PUBLIC(surf_action_t) surf_host_open(sg_host_t host, const char* fullpath);
172 /** @brief Create a file closing action on the given host */
173 XBT_PUBLIC(surf_action_t) surf_host_close(sg_host_t host, surf_file_t fd);
175 /** @brief Create a file reading action on the given host */
176 XBT_PUBLIC(surf_action_t) surf_host_read(sg_host_t host, surf_file_t fd, sg_size_t size);
178 /** @brief Create a file writing action on the given host */
179 XBT_PUBLIC(surf_action_t) surf_host_write(sg_host_t host, surf_file_t fd, sg_size_t size);
182 * @brief Get the information of a file descriptor
183 * @details The returned xbt_dynar_t contains:
184 * - the size of the file,
186 * - the storage name,
187 * - the storage typeId,
188 * - the storage content type
190 * @param host The surf host
191 * @param fd The file descriptor
192 * @return An xbt_dynar_t with the file information
194 XBT_PUBLIC(xbt_dynar_t) surf_host_get_info(sg_host_t host, surf_file_t fd);
197 * @brief Get the available space of the storage at the mount point
199 * @param resource The surf host
200 * @param name The mount point
201 * @return The amount of available space in bytes
203 XBT_PUBLIC(sg_size_t) surf_host_get_free_size(sg_host_t resource, const char* name);
206 * @brief Get the used space of the storage at the mount point
208 * @param resource The surf host
209 * @param name The mount point
210 * @return The amount of used space in bytes
212 XBT_PUBLIC(sg_size_t) surf_host_get_used_size(sg_host_t resource, const char* name);
215 * @brief Unlink a file descriptor
217 * @param host The surf host
218 * @param fd The file descriptor
220 * @return 0 if failed to unlink, 1 otherwise
222 XBT_PUBLIC(int) surf_host_unlink(sg_host_t host, surf_file_t fd);
225 * @brief Get the size of a file on a host
227 * @param host The surf host
228 * @param fd The file descriptor
230 * @return The size in bytes of the file
232 XBT_PUBLIC(size_t) surf_host_get_size(sg_host_t host, surf_file_t fd);
235 * @brief Get the current position of the file descriptor
237 * @param host The surf host
238 * @param fd The file descriptor
239 * @return The current position of the file descriptor
241 XBT_PUBLIC(size_t) surf_host_file_tell(sg_host_t host, surf_file_t fd);
244 * @brief Move a file to another location on the *same mount point*.
245 * @details [long description]
247 * @param host The surf host
248 * @param fd The file descriptor
249 * @param fullpath The new full path
251 * @return MSG_OK if successful, otherwise MSG_TASK_CANCELED
253 XBT_PUBLIC(int) surf_host_file_move(sg_host_t host, surf_file_t fd, const char* fullpath);
256 * @brief Set the position indictator assiociated with the file descriptor to a new position
257 * @details [long description]
259 * @param host The surf host
260 * @param fd The file descriptor
261 * @param offset The offset from the origin
262 * @param origin Position used as a reference for the offset
263 * - SEEK_SET: beginning of the file
264 * - SEEK_CUR: current position indicator
265 * - SEEK_END: end of the file
266 * @return MSG_OK if successful, otherwise MSG_TASK_CANCELED
268 XBT_PUBLIC(int) surf_host_file_seek(sg_host_t host, surf_file_t fd, sg_offset_t offset, int origin);
271 * @brief Get the content of a storage
273 * @param resource The surf storage
274 * @return A xbt_dict_t with path as keys and size in bytes as values
276 XBT_PUBLIC(xbt_dict_t) surf_storage_get_content(surf_resource_t resource);
279 * @brief Get the size in bytes of a storage
281 * @param resource The surf storage
282 * @return The size in bytes of the storage
284 XBT_PUBLIC(sg_size_t) surf_storage_get_size(surf_resource_t resource);
287 * @brief Get the available size in bytes of a storage
289 * @param resource The surf storage
290 * @return The available size in bytes of the storage
292 XBT_PUBLIC(sg_size_t) surf_storage_get_free_size(surf_resource_t resource);
295 * @brief Get the size in bytes of a storage
297 * @param resource The surf storage
298 * @return The used size in bytes of the storage
300 XBT_PUBLIC(sg_size_t) surf_storage_get_used_size(surf_resource_t resource);
302 /** @brief return the properties set associated to that storage */
303 XBT_PUBLIC(xbt_dict_t) surf_storage_get_properties(surf_resource_t resource);
306 * @brief [brief description]
307 * @details [long description]
309 * @param action The surf cpu action
310 * @param bound [description]
312 XBT_PUBLIC(void) surf_cpu_action_set_bound(surf_action_t action, double bound);
315 * @brief [brief description]
316 * @details [long description]
318 * @param action The surf network action
320 XBT_PUBLIC(double) surf_network_action_get_latency_limited(surf_action_t action);
323 * @brief Get the file associated to a storage action
325 * @param action The surf storage action
326 * @return The file associated to a storage action
328 XBT_PUBLIC(surf_file_t) surf_storage_action_get_file(surf_action_t action);
331 * @brief Get the result dictionary of an ls action
333 * @param action The surf storage action
334 * @return The dictionry listing a path
336 XBT_PUBLIC(xbt_dict_t) surf_storage_action_get_ls_dict(surf_action_t action);
340 * @brief Get the host the storage is attached to
342 * @param resource The surf storage
343 * @return The host name
346 XBT_PUBLIC(const char * ) surf_storage_get_host(surf_resource_t resource);
350 /**************************************/
351 /* Implementations of model object */
352 /**************************************/
354 /** \ingroup SURF_models
355 * \brief The CPU model object for the physical machine layer
357 XBT_PUBLIC_DATA(surf_cpu_model_t) surf_cpu_model_pm;
359 /** \ingroup SURF_models
360 * \brief The CPU model object for the virtual machine layer
362 XBT_PUBLIC_DATA(surf_cpu_model_t) surf_cpu_model_vm;
365 /** \ingroup SURF_models
366 * \brief Initializes the CPU model with the model Cas01
368 * By default, this model uses the lazy optimization mechanism that relies on partial invalidation in LMM and a heap
369 * for lazy action update.
370 * You can change this behavior by setting the cpu/optim configuration variable to a different value.
372 * You shouldn't have to call it by yourself.
374 XBT_PUBLIC(void) surf_cpu_model_init_Cas01();
376 /** \ingroup SURF_models
377 * \brief Initializes the CPU model with trace integration [Deprecated]
379 * You shouldn't have to call it by yourself.
381 XBT_PUBLIC(void) surf_cpu_model_init_ti();
383 /** \ingroup SURF_models
384 * \brief The list of all available optimization modes (both for cpu and networks).
385 * These optimization modes can be set using --cfg=cpu/optim:... and --cfg=network/optim:...
387 XBT_PUBLIC_DATA(s_surf_model_description_t) surf_optimization_mode_description[];
389 /** \ingroup SURF_plugins
390 * \brief The list of all available surf plugins
392 XBT_PUBLIC_DATA(s_surf_model_description_t) surf_plugin_description[];
394 /** \ingroup SURF_models
395 * \brief The list of all available cpu model models
397 XBT_PUBLIC_DATA(s_surf_model_description_t) surf_cpu_model_description[];
399 /** \ingroup SURF_models
400 * \brief The network model
402 * When creating a new API on top on SURF, you shouldn't use the network model unless you know what you are doing.
403 * Only the host model should be accessed because depending on the platform model, the network model can be NULL.
405 XBT_PUBLIC_DATA(surf_network_model_t) surf_network_model;
407 /** \ingroup SURF_models
408 * \brief Same as network model 'LagrangeVelho', only with different correction factors.
410 * This model is proposed by Pierre-Nicolas Clauss and Martin Quinson and Stéphane Génaud based on the model 'LV08' and
411 * different correction factors depending on the communication size (< 1KiB, < 64KiB, >= 64KiB).
412 * See comments in the code for more information.
414 * \see surf_host_model_init_SMPI()
416 XBT_PUBLIC(void) surf_network_model_init_SMPI();
418 /** \ingroup SURF_models
419 * \brief Same as network model 'LagrangeVelho', only with different correction factors.
421 * This model impelments a variant of the contention model on Infinband networks based on
422 * the works of Jérôme Vienne : http://mescal.imag.fr/membres/jean-marc.vincent/index.html/PhD/Vienne.pdf
424 * \see surf_host_model_init_IB()
426 XBT_PUBLIC(void) surf_network_model_init_IB();
428 /** \ingroup SURF_models
429 * \brief Initializes the platform with the network model 'LegrandVelho'
431 * This model is proposed by Arnaud Legrand and Pedro Velho based on the results obtained with the GTNets simulator for
432 * onelink and dogbone sharing scenarios. See comments in the code for more information.
434 * \see surf_host_model_init_LegrandVelho()
436 XBT_PUBLIC(void) surf_network_model_init_LegrandVelho();
438 /** \ingroup SURF_models
439 * \brief Initializes the platform with the network model 'Constant'
441 * In this model, the communication time between two network cards is constant, hence no need for a routing table.
442 * This is particularly useful when simulating huge distributed algorithms where scalability is really an issue. This
443 * function is called in conjunction with surf_host_model_init_compound.
445 * \see surf_host_model_init_compound()
447 XBT_PUBLIC(void) surf_network_model_init_Constant();
449 /** \ingroup SURF_models
450 * \brief Initializes the platform with the network model CM02
452 * You sould call this function by yourself only if you plan using surf_host_model_init_compound.
453 * See comments in the code for more information.
455 XBT_PUBLIC(void) surf_network_model_init_CM02();
457 /** \ingroup SURF_models
458 * \brief Initializes the platform with the network model NS3
460 * This function is called by surf_host_model_init_NS3 or by yourself only if you plan using
461 * surf_host_model_init_compound
463 * \see surf_host_model_init_NS3()
465 XBT_PUBLIC(void) surf_network_model_init_NS3();
467 /** \ingroup SURF_models
468 * \brief Initializes the platform with the network model Reno
470 * The problem is related to max( sum( arctan(C * Df * xi) ) ).
473 * [LOW03] S. H. Low. A duality model of TCP and queue management algorithms.
474 * IEEE/ACM Transaction on Networking, 11(4):525-536, 2003.
476 * Call this function only if you plan using surf_host_model_init_compound.
478 XBT_PUBLIC(void) surf_network_model_init_Reno();
480 /** \ingroup SURF_models
481 * \brief Initializes the platform with the network model Reno2
483 * The problem is related to max( sum( arctan(C * Df * xi) ) ).
486 * [LOW01] S. H. Low. A duality model of TCP and queue management algorithms.
487 * IEEE/ACM Transaction on Networking, 11(4):525-536, 2003.
489 * Call this function only if you plan using surf_host_model_init_compound.
491 XBT_PUBLIC(void) surf_network_model_init_Reno2();
493 /** \ingroup SURF_models
494 * \brief Initializes the platform with the network model Vegas
496 * This problem is related to max( sum( a * Df * ln(xi) ) ) which is equivalent to the proportional fairness.
499 * [LOW03] S. H. Low. A duality model of TCP and queue management algorithms.
500 * IEEE/ACM Transaction on Networking, 11(4):525-536, 2003.
502 * Call this function only if you plan using surf_host_model_init_compound.
504 XBT_PUBLIC(void) surf_network_model_init_Vegas();
506 /** \ingroup SURF_models
507 * \brief The list of all available network model models
509 XBT_PUBLIC_DATA(s_surf_model_description_t) surf_network_model_description[];
511 /** \ingroup SURF_models
512 * \brief The storage model
514 XBT_PUBLIC(void) surf_storage_model_init_default();
516 /** \ingroup SURF_models
517 * \brief The list of all available storage modes.
518 * This storage mode can be set using --cfg=storage/model:...
520 XBT_PUBLIC_DATA(s_surf_model_description_t) surf_storage_model_description[];
522 XBT_PUBLIC_DATA(surf_storage_model_t) surf_storage_model;
524 /** \ingroup SURF_models
525 * \brief The host model
527 * Note that when you create an API on top of SURF, the host model should be the only one you use
528 * because depending on the platform model, the network model and the CPU model may not exist.
530 XBT_PUBLIC_DATA(surf_host_model_t) surf_host_model;
532 /** \ingroup SURF_models
533 * \brief Initializes the platform with a compound host model
535 * This function should be called after a cpu_model and a network_model have been set up.
537 XBT_PUBLIC(void) surf_host_model_init_compound();
539 /** \ingroup SURF_models
540 * \brief Initializes the platform with the current best network and cpu models at hand
542 * This platform model separates the host model and the network model.
543 * The host model will be initialized with the model compound, the network model with the model LV08 (with cross
544 * traffic support) and the CPU model with the model Cas01.
545 * Such model is subject to modification with warning in the ChangeLog so monitor it!
547 XBT_PUBLIC(void) surf_host_model_init_current_default();
549 /** \ingroup SURF_models
550 * \brief Initializes the platform with the model L07
552 * With this model, only parallel tasks can be used. Resource sharing is done by identifying bottlenecks and giving an
553 * equal share of the model to each action.
555 XBT_PUBLIC(void) surf_host_model_init_ptask_L07();
557 /** \ingroup SURF_models
558 * \brief The list of all available host model models
560 XBT_PUBLIC_DATA(s_surf_model_description_t) surf_host_model_description[];
562 /** \ingroup SURF_models
563 * \brief Initializes the platform with the current best network and cpu models at hand
565 * This platform model seperates the host model and the network model.
566 * The host model will be initialized with the model compound, the network model with the model LV08 (with cross
567 * traffic support) and the CPU model with the model Cas01.
568 * Such model is subject to modification with warning in the ChangeLog so monitor it!
570 XBT_PUBLIC(void) surf_vm_model_init_HL13();
572 /** \ingroup SURF_simulation
573 * \brief List of hosts for which one want to be notified if they ever restart.
575 XBT_PUBLIC_DATA(xbt_dict_t) watched_hosts_lib;
577 /*** SURF Globals **************************/
579 /** \ingroup SURF_simulation
580 * \brief Initialize SURF
581 * \param argc argument number
582 * \param argv arguments
584 * This function has to be called to initialize the common structures. Then you will have to create the environment by
585 * calling e.g. surf_host_model_init_CM02()
587 * \see surf_host_model_init_CM02(), surf_host_model_init_compound(), surf_exit()
589 XBT_PUBLIC(void) surf_init(int *argc, char **argv); /* initialize common structures */
591 /** \ingroup SURF_simulation
592 * \brief Finish simulation initialization
594 * This function must be called before the first call to surf_solve()
596 XBT_PUBLIC(void) surf_presolve();
598 /** \ingroup SURF_simulation
599 * \brief Performs a part of the simulation
600 * \param max_date Maximum date to update the simulation to, or -1
601 * \return the elapsed time, or -1.0 if no event could be executed
603 * This function execute all possible events, update the action states and returns the time elapsed.
604 * When you call execute or communicate on a model, the corresponding actions are not executed immediately but only
605 * when you call surf_solve.
606 * Note that the returned elapsed time can be zero.
608 XBT_PUBLIC(double) surf_solve(double max_date);
610 /** \ingroup SURF_simulation
611 * \brief Return the current time
613 * Return the current time in millisecond.
615 XBT_PUBLIC(double) surf_get_clock();
617 /** \ingroup SURF_simulation
624 XBT_PUBLIC(void) surf_exit();
626 /* Prototypes of the functions that handle the properties */
627 XBT_PUBLIC_DATA(xbt_dict_t) current_property_set;// the prop set for the currently parsed element (also used in SIMIX)
628 /* The same for model_prop set*/
629 XBT_PUBLIC_DATA(xbt_dict_t) current_model_property_set;
631 /* surf parse file related (public because called from a test suite) */
632 XBT_PUBLIC(void) parse_platform_file(const char *file);
634 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
635 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
638 * Returns the initial path. On Windows the initial path is the current directory for the current process in the other
639 * case the function returns "./" that represents the current directory on Unix/Linux platforms.
641 const char *__surf_get_initial_path();
643 /********** Tracing **********/
644 /* from surf_instr.c */
645 void TRACE_surf_action(surf_action_t surf_action, const char *category);
646 void TRACE_surf_alloc();
647 void TRACE_surf_release();
649 /* instr_routing.c */
650 void instr_routing_define_callbacks ();
651 void instr_new_variable_type (const char *new_typename, const char *color);
652 void instr_new_user_variable_type (const char *father_type, const char *new_typename, const char *color);
653 void instr_new_user_state_type (const char *father_type, const char *new_typename);
654 void instr_new_value_for_user_state_type (const char *_typename, const char *value, const char *color);
655 int instr_platform_traced ();
656 xbt_graph_t instr_routing_platform_graph ();
657 void instr_routing_platform_graph_export_graphviz (xbt_graph_t g, const char *filename);