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;
45 class NetworkCm02Link;
50 typedef simgrid::surf::Model surf_Model;
51 typedef simgrid::surf::CpuModel surf_CpuModel;
52 typedef simgrid::surf::Cpu surf_Cpu;
53 typedef simgrid::surf::HostModel surf_HostModel;
54 typedef simgrid::surf::NetworkModel surf_NetworkModel;
55 typedef simgrid::surf::StorageModel surf_StorageModel;
56 typedef simgrid::surf::Resource surf_Resource;
57 typedef simgrid::surf::HostImpl surf_Host;
58 typedef simgrid::surf::Action surf_Action;
62 typedef struct surf_Model surf_Model;
63 typedef struct surf_CpuModel surf_CpuModel;
64 typedef struct surf_Cpu surf_Cpu;
65 typedef struct surf_HostModel surf_HostModel;
66 typedef struct surf_NetworkModel surf_NetworkModel;
67 typedef struct surf_StorageModel surf_StorageModel;
68 typedef struct surf_Resource surf_Resource;
69 typedef struct surf_Host surf_Host;
70 typedef struct surf_Action surf_Action;
74 /** @ingroup SURF_c_bindings
75 * \brief Model datatype
77 * Generic data structure for a model. The hosts,
78 * the CPUs and the network links are examples of models.
80 typedef surf_Model *surf_model_t;
81 typedef surf_CpuModel *surf_cpu_model_t;
82 typedef surf_HostModel *surf_host_model_t;
83 typedef surf_NetworkModel *surf_network_model_t;
84 typedef surf_StorageModel *surf_storage_model_t;
86 typedef xbt_dictelm_t surf_resource_t;
88 /** @ingroup SURF_c_bindings
89 * \brief Action structure
91 * Never create s_surf_action_t by yourself ! The actions are created
92 * on the fly when you call execute or communicate on a model.
94 * \see e_surf_action_state_t
96 typedef surf_Action *surf_action_t;
98 typedef struct surf_file *surf_file_t;
100 /** \brief Resource model description
102 struct surf_model_description {
104 const char *description;
105 void_f_void_t model_init_preparse;
107 typedef struct surf_model_description s_surf_model_description_t;
108 typedef struct surf_model_description* surf_model_description_t;
110 XBT_PUBLIC(int) find_model_description(s_surf_model_description_t * table, const char *name);
111 XBT_PUBLIC(void) model_help(const char *category, s_surf_model_description_t * table);
113 /***************************/
114 /* Generic model object */
115 /***************************/
117 static inline void *surf_storage_resource_priv(const void *storage){
118 return (void*)xbt_lib_get_level((xbt_dictelm_t)storage, SURF_STORAGE_LEVEL);
121 static inline void *surf_storage_resource_by_name(const char *name){
122 return xbt_lib_get_elm_or_null(storage_lib, name);
125 /** @{ @ingroup SURF_c_bindings */
128 * @brief Pop an action from the done actions set
130 * @param model The model from which the action is extracted
131 * @return An action in done state
133 XBT_PUBLIC(surf_action_t) surf_model_extract_done_action_set(surf_model_t model);
136 * @brief Pop an action from the failed actions set
138 * @param model The model from which the action is extracted
139 * @return An action in failed state
141 XBT_PUBLIC(surf_action_t) surf_model_extract_failed_action_set(surf_model_t model);
144 * @brief Get the size of the running action set of a model
146 * @param model The model
147 * @return The size of the running action set
149 XBT_PUBLIC(int) surf_model_running_action_set_size(surf_model_t model);
151 /** @brief Create a file opening action on the given host */
152 XBT_PUBLIC(surf_action_t) surf_host_open(sg_host_t host, const char* fullpath);
154 /** @brief Create a file closing action on the given host */
155 XBT_PUBLIC(surf_action_t) surf_host_close(sg_host_t host, surf_file_t fd);
157 /** @brief Create a file reading action on the given host */
158 XBT_PUBLIC(surf_action_t) surf_host_read(sg_host_t host, surf_file_t fd, sg_size_t size);
160 /** @brief Create a file writing action on the given host */
161 XBT_PUBLIC(surf_action_t) surf_host_write(sg_host_t host, surf_file_t fd, sg_size_t size);
164 * @brief Get the information of a file descriptor
165 * @details The returned xbt_dynar_t contains:
166 * - the size of the file,
168 * - the storage name,
169 * - the storage typeId,
170 * - the storage content type
172 * @param host The surf host
173 * @param fd The file descriptor
174 * @return An xbt_dynar_t with the file information
176 XBT_PUBLIC(xbt_dynar_t) surf_host_get_info(sg_host_t host, surf_file_t fd);
179 * @brief Get the available space of the storage at the mount point
181 * @param resource The surf host
182 * @param name The mount point
183 * @return The amount of available space in bytes
185 XBT_PUBLIC(sg_size_t) surf_host_get_free_size(sg_host_t resource, const char* name);
188 * @brief Get the used space of the storage at the mount point
190 * @param resource The surf host
191 * @param name The mount point
192 * @return The amount of used space in bytes
194 XBT_PUBLIC(sg_size_t) surf_host_get_used_size(sg_host_t resource, const char* name);
197 * @brief Unlink a file descriptor
199 * @param host The surf host
200 * @param fd The file descriptor
202 * @return 0 if failed to unlink, 1 otherwise
204 XBT_PUBLIC(int) surf_host_unlink(sg_host_t host, surf_file_t fd);
207 * @brief Get the size of a file on a host
209 * @param host The surf host
210 * @param fd The file descriptor
212 * @return The size in bytes of the file
214 XBT_PUBLIC(size_t) surf_host_get_size(sg_host_t host, surf_file_t fd);
217 * @brief Get the current position of the file descriptor
219 * @param host The surf host
220 * @param fd The file descriptor
221 * @return The current position of the file descriptor
223 XBT_PUBLIC(size_t) surf_host_file_tell(sg_host_t host, surf_file_t fd);
226 * @brief Move a file to another location on the *same mount point*.
227 * @details [long description]
229 * @param host The surf host
230 * @param fd The file descriptor
231 * @param fullpath The new full path
233 * @return MSG_OK if successful, otherwise MSG_TASK_CANCELED
235 XBT_PUBLIC(int) surf_host_file_move(sg_host_t host, surf_file_t fd, const char* fullpath);
238 * @brief Set the position indictator assiociated with the file descriptor to a new position
239 * @details [long description]
241 * @param host The surf host
242 * @param fd The file descriptor
243 * @param offset The offset from the origin
244 * @param origin Position used as a reference for the offset
245 * - SEEK_SET: beginning of the file
246 * - SEEK_CUR: current position indicator
247 * - SEEK_END: end of the file
248 * @return MSG_OK if successful, otherwise MSG_TASK_CANCELED
250 XBT_PUBLIC(int) surf_host_file_seek(sg_host_t host, surf_file_t fd, sg_offset_t offset, int origin);
253 * @brief Get the content of a storage
255 * @param resource The surf storage
256 * @return A xbt_dict_t with path as keys and size in bytes as values
258 XBT_PUBLIC(xbt_dict_t) surf_storage_get_content(surf_resource_t resource);
261 * @brief Get the size in bytes of a storage
263 * @param resource The surf storage
264 * @return The size in bytes of the storage
266 XBT_PUBLIC(sg_size_t) surf_storage_get_size(surf_resource_t resource);
269 * @brief Get the available size in bytes of a storage
271 * @param resource The surf storage
272 * @return The available size in bytes of the storage
274 XBT_PUBLIC(sg_size_t) surf_storage_get_free_size(surf_resource_t resource);
277 * @brief Get the size in bytes of a storage
279 * @param resource The surf storage
280 * @return The used size in bytes of the storage
282 XBT_PUBLIC(sg_size_t) surf_storage_get_used_size(surf_resource_t resource);
284 /** @brief return the properties set associated to that storage */
285 XBT_PUBLIC(xbt_dict_t) surf_storage_get_properties(surf_resource_t resource);
288 * @brief [brief description]
289 * @details [long description]
291 * @param action The surf cpu action
292 * @param bound [description]
294 XBT_PUBLIC(void) surf_cpu_action_set_bound(surf_action_t action, double bound);
297 * @brief [brief description]
298 * @details [long description]
300 * @param action The surf network action
302 XBT_PUBLIC(double) surf_network_action_get_latency_limited(surf_action_t action);
305 * @brief Get the file associated to a storage action
307 * @param action The surf storage action
308 * @return The file associated to a storage action
310 XBT_PUBLIC(surf_file_t) surf_storage_action_get_file(surf_action_t action);
313 * @brief Get the result dictionary of an ls action
315 * @param action The surf storage action
316 * @return The dictionry listing a path
318 XBT_PUBLIC(xbt_dict_t) surf_storage_action_get_ls_dict(surf_action_t action);
322 * @brief Get the host the storage is attached to
324 * @param resource The surf storage
325 * @return The host name
328 XBT_PUBLIC(const char * ) surf_storage_get_host(surf_resource_t resource);
332 /**************************************/
333 /* Implementations of model object */
334 /**************************************/
336 /** \ingroup SURF_models
337 * \brief The CPU model object for the physical machine layer
339 XBT_PUBLIC_DATA(surf_cpu_model_t) surf_cpu_model_pm;
341 /** \ingroup SURF_models
342 * \brief The CPU model object for the virtual machine layer
344 XBT_PUBLIC_DATA(surf_cpu_model_t) surf_cpu_model_vm;
347 /** \ingroup SURF_models
348 * \brief Initializes the CPU model with the model Cas01
350 * By default, this model uses the lazy optimization mechanism that relies on partial invalidation in LMM and a heap
351 * for lazy action update.
352 * You can change this behavior by setting the cpu/optim configuration variable to a different value.
354 * You shouldn't have to call it by yourself.
356 XBT_PUBLIC(void) surf_cpu_model_init_Cas01();
358 /** \ingroup SURF_models
359 * \brief Initializes the CPU model with trace integration [Deprecated]
361 * You shouldn't have to call it by yourself.
363 XBT_PUBLIC(void) surf_cpu_model_init_ti();
365 /** \ingroup SURF_models
366 * \brief The list of all available optimization modes (both for cpu and networks).
367 * These optimization modes can be set using --cfg=cpu/optim:... and --cfg=network/optim:...
369 XBT_PUBLIC_DATA(s_surf_model_description_t) surf_optimization_mode_description[];
371 /** \ingroup SURF_plugins
372 * \brief The list of all available surf plugins
374 XBT_PUBLIC_DATA(s_surf_model_description_t) surf_plugin_description[];
376 /** \ingroup SURF_models
377 * \brief The list of all available cpu model models
379 XBT_PUBLIC_DATA(s_surf_model_description_t) surf_cpu_model_description[];
381 /** \ingroup SURF_models
382 * \brief The network model
384 * When creating a new API on top on SURF, you shouldn't use the network model unless you know what you are doing.
385 * Only the host model should be accessed because depending on the platform model, the network model can be NULL.
387 XBT_PUBLIC_DATA(surf_network_model_t) surf_network_model;
389 /** \ingroup SURF_models
390 * \brief Same as network model 'LagrangeVelho', only with different correction factors.
392 * This model is proposed by Pierre-Nicolas Clauss and Martin Quinson and Stéphane Génaud based on the model 'LV08' and
393 * different correction factors depending on the communication size (< 1KiB, < 64KiB, >= 64KiB).
394 * See comments in the code for more information.
396 * \see surf_host_model_init_SMPI()
398 XBT_PUBLIC(void) surf_network_model_init_SMPI();
400 /** \ingroup SURF_models
401 * \brief Same as network model 'LagrangeVelho', only with different correction factors.
403 * This model impelments a variant of the contention model on Infinband networks based on
404 * the works of Jérôme Vienne : http://mescal.imag.fr/membres/jean-marc.vincent/index.html/PhD/Vienne.pdf
406 * \see surf_host_model_init_IB()
408 XBT_PUBLIC(void) surf_network_model_init_IB();
410 /** \ingroup SURF_models
411 * \brief Initializes the platform with the network model 'LegrandVelho'
413 * This model is proposed by Arnaud Legrand and Pedro Velho based on the results obtained with the GTNets simulator for
414 * onelink and dogbone sharing scenarios. See comments in the code for more information.
416 * \see surf_host_model_init_LegrandVelho()
418 XBT_PUBLIC(void) surf_network_model_init_LegrandVelho();
420 /** \ingroup SURF_models
421 * \brief Initializes the platform with the network model 'Constant'
423 * In this model, the communication time between two network cards is constant, hence no need for a routing table.
424 * This is particularly useful when simulating huge distributed algorithms where scalability is really an issue. This
425 * function is called in conjunction with surf_host_model_init_compound.
427 * \see surf_host_model_init_compound()
429 XBT_PUBLIC(void) surf_network_model_init_Constant();
431 /** \ingroup SURF_models
432 * \brief Initializes the platform with the network model CM02
434 * You sould call this function by yourself only if you plan using surf_host_model_init_compound.
435 * See comments in the code for more information.
437 XBT_PUBLIC(void) surf_network_model_init_CM02();
439 /** \ingroup SURF_models
440 * \brief Initializes the platform with the network model NS3
442 * This function is called by surf_host_model_init_NS3 or by yourself only if you plan using
443 * surf_host_model_init_compound
445 * \see surf_host_model_init_NS3()
447 XBT_PUBLIC(void) surf_network_model_init_NS3();
449 /** \ingroup SURF_models
450 * \brief Initializes the platform with the network model Reno
452 * The problem is related to max( sum( arctan(C * Df * xi) ) ).
455 * [LOW03] S. H. Low. A duality model of TCP and queue management algorithms.
456 * IEEE/ACM Transaction on Networking, 11(4):525-536, 2003.
458 * Call this function only if you plan using surf_host_model_init_compound.
460 XBT_PUBLIC(void) surf_network_model_init_Reno();
462 /** \ingroup SURF_models
463 * \brief Initializes the platform with the network model Reno2
465 * The problem is related to max( sum( arctan(C * Df * xi) ) ).
468 * [LOW01] S. H. Low. A duality model of TCP and queue management algorithms.
469 * IEEE/ACM Transaction on Networking, 11(4):525-536, 2003.
471 * Call this function only if you plan using surf_host_model_init_compound.
473 XBT_PUBLIC(void) surf_network_model_init_Reno2();
475 /** \ingroup SURF_models
476 * \brief Initializes the platform with the network model Vegas
478 * This problem is related to max( sum( a * Df * ln(xi) ) ) which is equivalent to the proportional fairness.
481 * [LOW03] S. H. Low. A duality model of TCP and queue management algorithms.
482 * IEEE/ACM Transaction on Networking, 11(4):525-536, 2003.
484 * Call this function only if you plan using surf_host_model_init_compound.
486 XBT_PUBLIC(void) surf_network_model_init_Vegas();
488 /** \ingroup SURF_models
489 * \brief The list of all available network model models
491 XBT_PUBLIC_DATA(s_surf_model_description_t) surf_network_model_description[];
493 /** \ingroup SURF_models
494 * \brief The storage model
496 XBT_PUBLIC(void) surf_storage_model_init_default();
498 /** \ingroup SURF_models
499 * \brief The list of all available storage modes.
500 * This storage mode can be set using --cfg=storage/model:...
502 XBT_PUBLIC_DATA(s_surf_model_description_t) surf_storage_model_description[];
504 XBT_PUBLIC_DATA(surf_storage_model_t) surf_storage_model;
506 /** \ingroup SURF_models
507 * \brief The host model
509 * Note that when you create an API on top of SURF, the host model should be the only one you use
510 * because depending on the platform model, the network model and the CPU model may not exist.
512 XBT_PUBLIC_DATA(surf_host_model_t) surf_host_model;
514 /** \ingroup SURF_models
515 * \brief Initializes the platform with a compound host model
517 * This function should be called after a cpu_model and a network_model have been set up.
519 XBT_PUBLIC(void) surf_host_model_init_compound();
521 /** \ingroup SURF_models
522 * \brief Initializes the platform with the current best network and cpu models at hand
524 * This platform model separates the host model and the network model.
525 * The host model will be initialized with the model compound, the network model with the model LV08 (with cross
526 * traffic support) and the CPU model with the model Cas01.
527 * Such model is subject to modification with warning in the ChangeLog so monitor it!
529 XBT_PUBLIC(void) surf_host_model_init_current_default();
531 /** \ingroup SURF_models
532 * \brief Initializes the platform with the model L07
534 * With this model, only parallel tasks can be used. Resource sharing is done by identifying bottlenecks and giving an
535 * equal share of the model to each action.
537 XBT_PUBLIC(void) surf_host_model_init_ptask_L07();
539 /** \ingroup SURF_models
540 * \brief The list of all available host model models
542 XBT_PUBLIC_DATA(s_surf_model_description_t) surf_host_model_description[];
544 /** \ingroup SURF_models
545 * \brief Initializes the platform with the current best network and cpu models at hand
547 * This platform model seperates the host model and the network model.
548 * The host model will be initialized with the model compound, the network model with the model LV08 (with cross
549 * traffic support) and the CPU model with the model Cas01.
550 * Such model is subject to modification with warning in the ChangeLog so monitor it!
552 XBT_PUBLIC(void) surf_vm_model_init_HL13();
554 /** \ingroup SURF_simulation
555 * \brief List of hosts for which one want to be notified if they ever restart.
557 XBT_PUBLIC_DATA(xbt_dict_t) watched_hosts_lib;
559 /*** SURF Globals **************************/
561 /** \ingroup SURF_simulation
562 * \brief Initialize SURF
563 * \param argc argument number
564 * \param argv arguments
566 * This function has to be called to initialize the common structures. Then you will have to create the environment by
567 * calling e.g. surf_host_model_init_CM02()
569 * \see surf_host_model_init_CM02(), surf_host_model_init_compound(), surf_exit()
571 XBT_PUBLIC(void) surf_init(int *argc, char **argv); /* initialize common structures */
573 /** \ingroup SURF_simulation
574 * \brief Finish simulation initialization
576 * This function must be called before the first call to surf_solve()
578 XBT_PUBLIC(void) surf_presolve();
580 /** \ingroup SURF_simulation
581 * \brief Performs a part of the simulation
582 * \param max_date Maximum date to update the simulation to, or -1
583 * \return the elapsed time, or -1.0 if no event could be executed
585 * This function execute all possible events, update the action states and returns the time elapsed.
586 * When you call execute or communicate on a model, the corresponding actions are not executed immediately but only
587 * when you call surf_solve.
588 * Note that the returned elapsed time can be zero.
590 XBT_PUBLIC(double) surf_solve(double max_date);
592 /** \ingroup SURF_simulation
593 * \brief Return the current time
595 * Return the current time in millisecond.
597 XBT_PUBLIC(double) surf_get_clock();
599 /** \ingroup SURF_simulation
606 XBT_PUBLIC(void) surf_exit();
608 /* Prototypes of the functions that handle the properties */
609 XBT_PUBLIC_DATA(xbt_dict_t) current_property_set;// the prop set for the currently parsed element (also used in SIMIX)
611 /* surf parse file related (public because called from a test suite) */
612 XBT_PUBLIC(void) parse_platform_file(const char *file);
614 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
615 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
618 * Returns the initial path. On Windows the initial path is the current directory for the current process in the other
619 * case the function returns "./" that represents the current directory on Unix/Linux platforms.
621 const char *__surf_get_initial_path();
623 /********** Tracing **********/
624 /* from surf_instr.c */
625 void TRACE_surf_action(surf_action_t surf_action, const char *category);
626 void TRACE_surf_alloc();
627 void TRACE_surf_release();
629 /* instr_routing.c */
630 void instr_routing_define_callbacks ();
631 void instr_new_variable_type (const char *new_typename, const char *color);
632 void instr_new_user_variable_type (const char *father_type, const char *new_typename, const char *color);
633 void instr_new_user_state_type (const char *father_type, const char *new_typename);
634 void instr_new_value_for_user_state_type (const char *_typename, const char *value, const char *color);
635 int instr_platform_traced ();
636 xbt_graph_t instr_routing_platform_graph ();
637 void instr_routing_platform_graph_export_graphviz (xbt_graph_t g, const char *filename);