Logo AND Algorithmique Numérique Distribuée

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