Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix msg-start-kill-time
[simgrid.git] / src / surf / workstation.cpp
1 #include "workstation.hpp"
2 #include "simgrid/sg_config.h"
3
4 extern "C" {
5 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
6                                 "Logging specific to the SURF workstation module");
7 }
8
9 WorkstationModelPtr surf_workstation_model = NULL;
10
11 //FIXME:Faire hériter ou composer de cup et network
12
13 /*************
14  * CallBacks *
15  *************/
16
17 static void workstation_new(sg_platf_host_cbarg_t host){
18   surf_workstation_model->createResource(host->id);
19 }
20
21 /*********
22  * Model *
23  *********/
24
25 void surf_workstation_model_init_current_default(void)
26 {
27   surf_workstation_model = new WorkstationModel();
28   xbt_cfg_setdefault_boolean(_sg_cfg_set, "network/crosstraffic", xbt_strdup("yes"));
29   surf_cpu_model_init_Cas01();
30   surf_network_model_init_LegrandVelho();
31
32   ModelPtr model = static_cast<ModelPtr>(surf_workstation_model);
33   xbt_dynar_push(model_list, &model);
34   sg_platf_host_add_cb(workstation_new);
35 }
36
37 void surf_workstation_model_init_compound()
38 {
39
40   xbt_assert(surf_cpu_model, "No CPU model defined yet!");
41   xbt_assert(surf_network_model, "No network model defined yet!");
42   surf_workstation_model = new WorkstationModel();
43
44   ModelPtr model = static_cast<ModelPtr>(surf_workstation_model);
45   xbt_dynar_push(model_list, &model);
46   sg_platf_host_add_cb(workstation_new);
47 }
48
49 WorkstationModel::WorkstationModel() : Model("Workstation") {
50 }
51
52 WorkstationModel::~WorkstationModel() {
53 }
54
55 void WorkstationModel::parseInit(sg_platf_host_cbarg_t host){
56   createResource(host->id);
57 }
58
59 WorkstationCLM03Ptr WorkstationModel::createResource(string name){
60
61   WorkstationCLM03Ptr workstation = new WorkstationCLM03(surf_workstation_model, name.c_str(), NULL,
62                   (xbt_dynar_t)xbt_lib_get_or_null(storage_lib, name.c_str(), ROUTING_STORAGE_HOST_LEVEL),
63                   (RoutingEdgePtr)xbt_lib_get_or_null(host_lib, name.c_str(), ROUTING_HOST_LEVEL),
64                   dynamic_cast<CpuPtr>(static_cast<ResourcePtr>(xbt_lib_get_or_null(host_lib, name.c_str(), SURF_CPU_LEVEL))));
65   XBT_DEBUG("Create workstation %s with %ld mounted disks", name.c_str(), xbt_dynar_length(workstation->p_storage));
66   xbt_lib_set(host_lib, name.c_str(), SURF_WKS_LEVEL, static_cast<ResourcePtr>(workstation));
67   return workstation;
68 }
69
70 double WorkstationModel::shareResources(double now){
71   return -1.0;
72 }
73
74 void WorkstationModel::updateActionsState(double now, double delta){
75   return;
76 }
77
78 ActionPtr WorkstationModel::executeParallelTask(int workstation_nb,
79                                         void **workstation_list,
80                                         double *computation_amount,
81                                         double *communication_amount,
82                                         double rate){
83 #define cost_or_zero(array,pos) ((array)?(array)[pos]:0.0)
84   if ((workstation_nb == 1)
85       && (cost_or_zero(communication_amount, 0) == 0.0))
86     return ((WorkstationCLM03Ptr)workstation_list[0])->execute(computation_amount[0]);
87   else if ((workstation_nb == 1)
88            && (cost_or_zero(computation_amount, 0) == 0.0))
89     return communicate((WorkstationCLM03Ptr)workstation_list[0], (WorkstationCLM03Ptr)workstation_list[0],communication_amount[0], rate);
90   else if ((workstation_nb == 2)
91              && (cost_or_zero(computation_amount, 0) == 0.0)
92              && (cost_or_zero(computation_amount, 1) == 0.0)) {
93     int i,nb = 0;
94     double value = 0.0;
95
96     for (i = 0; i < workstation_nb * workstation_nb; i++) {
97       if (cost_or_zero(communication_amount, i) > 0.0) {
98         nb++;
99         value = cost_or_zero(communication_amount, i);
100       }
101     }
102     if (nb == 1)
103       return communicate((WorkstationCLM03Ptr)workstation_list[0], (WorkstationCLM03Ptr)workstation_list[1],value, rate);
104   }
105 #undef cost_or_zero
106
107   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
108   return NULL;
109 }
110
111 /* returns an array of network_link_CM02_t */
112 xbt_dynar_t WorkstationModel::getRoute(WorkstationCLM03Ptr src, WorkstationCLM03Ptr dst)
113 {
114   XBT_DEBUG("ws_get_route");
115   return surf_network_model->getRoute(src->p_netElm, dst->p_netElm);
116 }
117
118 ActionPtr WorkstationModel::communicate(WorkstationCLM03Ptr src, WorkstationCLM03Ptr dst, double size, double rate){
119   return surf_network_model->communicate(src->p_netElm, dst->p_netElm, size, rate);
120 }
121
122
123
124 /************
125  * Resource *
126  ************/
127 WorkstationCLM03::WorkstationCLM03(WorkstationModelPtr model, const char* name, xbt_dict_t properties, xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
128   : Resource(model, name, properties), p_storage(storage), p_netElm(netElm), p_cpu(cpu) {}
129
130 bool WorkstationCLM03::isUsed(){
131   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
132   return -1;
133 }
134
135 void WorkstationCLM03::updateState(tmgr_trace_event_t event_type, double value, double date){
136   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
137 }
138
139 ActionPtr WorkstationCLM03::execute(double size) {
140   return p_cpu->execute(size);
141 }
142
143 ActionPtr WorkstationCLM03::sleep(double duration) {
144   return p_cpu->sleep(duration);
145 }
146
147 e_surf_resource_state_t WorkstationCLM03::getState() {
148   return p_cpu->getState();
149 }
150
151 int WorkstationCLM03::getCore(){
152   return p_cpu->getCore();
153 }
154
155 double WorkstationCLM03::getSpeed(double load){
156   return p_cpu->getSpeed(load);
157 }
158
159 double WorkstationCLM03::getAvailableSpeed(){
160   return p_cpu->getAvailableSpeed();
161 }
162
163 xbt_dict_t WorkstationCLM03::getProperties()
164 {
165   return p_cpu->m_properties;
166 }
167
168
169 StoragePtr WorkstationCLM03::findStorageOnMountList(const char* storage)
170 {
171   StoragePtr st = NULL;
172   s_mount_t mnt;
173   unsigned int cursor;
174
175   XBT_DEBUG("Search for storage name '%s' on '%s'",storage,m_name);
176   xbt_dynar_foreach(p_storage,cursor,mnt)
177   {
178     XBT_DEBUG("See '%s'",mnt.name);
179     if(!strcmp(storage,mnt.name)){
180       st = (StoragePtr)mnt.id;
181       break;
182     }
183   }
184   if(!st) xbt_die("Can't find mount '%s' for '%s'",storage,m_name);
185   return st;
186 }
187
188 ActionPtr WorkstationCLM03::open(const char* mount, const char* path) {
189   StoragePtr st = findStorageOnMountList(mount);
190   XBT_DEBUG("OPEN on disk '%s'", st->m_name);
191   return st->open(mount, path);
192 }
193
194 ActionPtr WorkstationCLM03::close(surf_file_t fd) {
195   StoragePtr st = findStorageOnMountList(fd->storage);
196   XBT_DEBUG("CLOSE on disk '%s'",st->m_name);
197   return st->close(fd);
198 }
199
200 ActionPtr WorkstationCLM03::read(void* ptr, size_t size, surf_file_t fd) {
201   StoragePtr st = findStorageOnMountList(fd->storage);
202   XBT_DEBUG("READ on disk '%s'",st->m_name);
203   return st->read(ptr, size, fd);
204 }
205
206 ActionPtr WorkstationCLM03::write(const void* ptr, size_t size, surf_file_t fd) {
207   StoragePtr st = findStorageOnMountList(fd->storage);
208   XBT_DEBUG("WRITE on disk '%s'",st->m_name);
209   return st->write(ptr, size, fd);
210 }
211
212 int WorkstationCLM03::unlink(surf_file_t fd) {
213   if (!fd){
214     XBT_WARN("No such file descriptor. Impossible to unlink");
215     return 0;
216   } else {
217 //    XBT_INFO("%s %zu", fd->storage, fd->size);
218     StoragePtr st = findStorageOnMountList(fd->storage);
219     /* Check if the file is on this storage */
220     if (!xbt_dict_get_or_null(st->p_content, fd->name)){
221       XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
222           st->m_name);
223       return 0;
224     } else {
225       XBT_DEBUG("UNLINK on disk '%s'",st->m_name);
226       st->m_usedSize -= fd->size;
227
228       // Remove the file from storage
229       xbt_dict_remove(st->p_content, fd->name);
230
231       free(fd->name);
232       free(fd->storage);
233       xbt_free(fd);
234       return 1;
235     }
236   }
237 }
238
239 ActionPtr WorkstationCLM03::ls(const char* mount, const char *path){
240   XBT_DEBUG("LS on mount '%s' and file '%s'", mount, path);
241   StoragePtr st = findStorageOnMountList(mount);
242   return st->ls(path);
243 }
244
245 size_t WorkstationCLM03::getSize(surf_file_t fd){
246   return fd->size;
247 }
248
249 e_surf_resource_state_t WorkstationCLM03Lmm::getState() {
250   return WorkstationCLM03::getState();
251 }
252 /**********
253  * Action *
254  **********/