Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further cleaning in this rotten realm
[simgrid.git] / src / simdag / sd_workstation.cpp
1 /* Copyright (c) 2006-2016. 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 "src/surf/host_interface.hpp"
8 #include "src/simdag/simdag_private.h"
9 #include "simgrid/simdag.h"
10 #include "simgrid/host.h"
11 #include <simgrid/s4u/host.hpp>
12 #include "xbt/dict.h"
13 #include "xbt/lib.h"
14 #include "xbt/sysdep.h"
15 #include "surf/surf.h"
16
17 /* Creates a storage and registers it in SD.
18  */
19 SD_storage_t __SD_storage_create(void *surf_storage, void *data)
20 {
21
22   SD_storage_priv_t storage;
23   const char *name;
24
25   storage = xbt_new(s_SD_storage_priv_t, 1);
26   storage->data = data;     /* user data */
27   name = surf_resource_name((surf_cpp_resource_t)surf_storage);
28   storage->host = (const char*)surf_storage_get_host( (surf_resource_t )surf_storage_resource_by_name(name));
29   xbt_lib_set(storage_lib,name, SD_STORAGE_LEVEL, storage);
30   return xbt_lib_get_elm_or_null(storage_lib, name);
31 }
32
33 /* Destroys a storage.
34  */
35 void __SD_storage_destroy(void *storage)
36 {
37   SD_storage_priv_t s;
38
39   s = (SD_storage_priv_t) storage;
40   xbt_free(s);
41 }
42
43 /** @brief Returns the route between two workstations
44  *
45  * Use SD_route_get_size() to know the array size.
46  *
47  * \param src a host
48  * \param dst another host
49  * \return an array of the \ref SD_link_t composing the route
50  * \see SD_route_get_size(), SD_link_t
51  */
52 const SD_link_t *SD_route_get_list(sg_host_t src, sg_host_t dst)
53 {
54   xbt_dynar_t surf_route;
55   SD_link_t* list;
56   void *surf_link;
57   unsigned int cpt;
58   surf_route = surf_host_model_get_route((surf_host_model_t)surf_host_model,
59                                          src, dst);
60
61   list = xbt_new(SD_link_t, xbt_dynar_length(surf_route));
62   xbt_dynar_foreach(surf_route, cpt, surf_link) {
63     list[cpt] = (SD_link_t)surf_link;
64   }
65   return list;
66 }
67
68 /**
69  * \brief Returns the number of links on the route between two workstations
70  *
71  * \param src a workstation
72  * \param dst another workstation
73  * \return the number of links on the route between these two workstations
74  * \see SD_route_get_list()
75  */
76 int SD_route_get_size(sg_host_t src, sg_host_t dst)
77 {
78   return xbt_dynar_length(surf_host_model_get_route(
79       (surf_host_model_t)surf_host_model, src, dst));
80 }
81
82 /**
83  * \brief Returns the latency of the route between two workstations.
84  *
85  * \param src the first workstation
86  * \param dst the second workstation
87  * \return the latency of the route between the two workstations (in seconds)
88  * \see SD_route_get_bandwidth()
89  */
90 double SD_route_get_latency(sg_host_t src, sg_host_t dst)
91 {
92   xbt_dynar_t route = NULL;
93   double latency = 0;
94
95   routing_platf->getRouteAndLatency(src->pimpl_netcard, dst->pimpl_netcard,
96                                     &route, &latency);
97
98   return latency;
99 }
100
101 /**
102  * \brief Returns the bandwidth of the route between two workstations,
103  * i.e. the minimum link bandwidth of all between the workstations.
104  *
105  * \param src the first workstation
106  * \param dst the second workstation
107  * \return the bandwidth of the route between the two workstations
108  * (in bytes/second)
109  * \see SD_route_get_latency()
110  */
111 double SD_route_get_bandwidth(sg_host_t src, sg_host_t dst)
112 {
113   xbt_dynar_t route = NULL;
114   unsigned int cpt;
115   double latency = 0;
116   double bandwidth;
117   double min_bandwidth = -1.0;
118   SD_link_t link;
119
120   routing_platf->getRouteAndLatency(src->pimpl_netcard, dst->pimpl_netcard,
121                                     &route, &latency);
122
123   xbt_dynar_foreach(route, cpt, link){
124     bandwidth = sg_link_bandwidth(link);
125     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
126       min_bandwidth = bandwidth;
127   }
128
129   return min_bandwidth;
130 }
131
132 /**
133  * \brief Returns an approximative estimated time for the given
134  * communication amount between two hosts
135  *
136  * \param src the first host
137  * \param dst the second host
138  * \param bytes_amount the communication amount you want to evaluate (in bytes)
139  * \return an approximative estimated communication time for the given bytes amount
140  * between the workstations (in seconds)
141  */
142 double SD_route_get_communication_time(sg_host_t src,sg_host_t dst,
143                                        double bytes_amount)
144 {
145   /* total time = latency + transmission time of the slowest link
146      transmission time of a link = communication amount / link bandwidth */
147
148   xbt_assert(bytes_amount >= 0, "bytes_amount must be greater than or equal to zero");
149
150
151   if (bytes_amount == 0.0)
152     return 0.0;
153
154   return SD_route_get_latency(src, dst) +
155           (bytes_amount / SD_route_get_bandwidth(src,dst));
156 }
157
158 /**
159  * \brief Returns the host name the storage is attached to
160  *
161  * This functions checks whether a storage is a valid pointer or not and return its name.
162  */
163 const char *SD_storage_get_host(msg_storage_t storage) {
164   xbt_assert((storage != NULL), "Invalid parameters");
165   SD_storage_priv_t priv = SD_storage_priv(storage);
166   return priv->host;
167 }