1 /* Copyright (c) 2013-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "workstation_interface.hpp"
8 #include "vm_workstation_interface.hpp"
9 #include "cpu_cas01.hpp"
10 #include "simgrid/sg_config.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
13 "Logging specific to the SURF workstation module");
15 WorkstationModelPtr surf_workstation_model = NULL;
21 surf_callback(void, WorkstationPtr) workstationCreatedCallbacks;
22 surf_callback(void, WorkstationPtr) workstationDestructedCallbacks;
23 surf_callback(void, WorkstationPtr) workstationStateChangedCallbacks;
24 surf_callback(void, WorkstationActionPtr) workstationActionStateChangedCallbacks;
29 WorkstationModel::WorkstationModel(const char *name)
32 p_cpuModel = surf_cpu_model_pm;
35 WorkstationModel::WorkstationModel()
36 : Model("Workstation") {
37 p_cpuModel = surf_cpu_model_pm;
40 WorkstationModel::~WorkstationModel() {
43 /* Each VM has a dummy CPU action on the PM layer. This CPU action works as the
44 * constraint (capacity) of the VM in the PM layer. If the VM does not have any
45 * active task, the dummy CPU action must be deactivated, so that the VM does
46 * not get any CPU share in the PM layer. */
47 void WorkstationModel::adjustWeightOfDummyCpuActions()
49 /* iterate for all virtual machines */
50 for (WorkstationVMModel::vm_list_t::iterator iter =
51 WorkstationVMModel::ws_vms.begin();
52 iter != WorkstationVMModel::ws_vms.end(); ++iter) {
54 WorkstationVMPtr ws_vm = &*iter;
55 CpuCas01Ptr cpu_cas01 = static_cast<CpuCas01Ptr>(ws_vm->p_cpu);
56 xbt_assert(cpu_cas01, "cpu-less workstation");
58 int is_active = lmm_constraint_used(cpu_cas01->getModel()->getMaxminSystem(), cpu_cas01->getConstraint());
59 // int is_active_old = constraint_is_active(cpu_cas01);
62 // xbt_assert(is_active == is_active_old, "%d %d", is_active, is_active_old);
66 /* some tasks exist on this VM */
67 XBT_DEBUG("set the weight of the dummy CPU action on PM to 1");
69 /* FIXME: we shoud use lmm_update_variable_weight() ? */
70 /* FIXME: If we assgign 1.05 and 0.05, the system makes apparently wrong values. */
71 ws_vm->p_action->setPriority(1);
74 /* no task exits on this VM */
75 XBT_DEBUG("set the weight of the dummy CPU action on PM to 0");
77 ws_vm->p_action->setPriority(0);
85 Workstation::Workstation()
87 surf_callback_emit(workstationCreatedCallbacks, this);
90 Workstation::Workstation(ModelPtr model, const char *name, xbt_dict_t props,
91 xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
92 : Resource(model, name, props)
93 , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
96 surf_callback_emit(workstationCreatedCallbacks, this);
99 Workstation::Workstation(ModelPtr model, const char *name, xbt_dict_t props, lmm_constraint_t constraint,
100 xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
101 : Resource(model, name, props, constraint)
102 , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
104 p_params.ramsize = 0;
105 surf_callback_emit(workstationCreatedCallbacks, this);
108 Workstation::~Workstation(){
109 surf_callback_emit(workstationDestructedCallbacks, this);
112 void Workstation::setState(e_surf_resource_state_t state){
113 Resource::setState(state);
114 surf_callback_emit(workstationStateChangedCallbacks, this);
117 int Workstation::getCore(){
118 return p_cpu->getCore();
121 double Workstation::getSpeed(double load){
122 return p_cpu->getSpeed(load);
125 double Workstation::getAvailableSpeed(){
126 return p_cpu->getAvailableSpeed();
129 double Workstation::getCurrentPowerPeak()
131 return p_cpu->getCurrentPowerPeak();
134 double Workstation::getPowerPeakAt(int pstate_index)
136 return p_cpu->getPowerPeakAt(pstate_index);
139 int Workstation::getNbPstates()
141 return p_cpu->getNbPstates();
144 void Workstation::setPowerPeakAt(int pstate_index)
146 p_cpu->setPowerPeakAt(pstate_index);
149 xbt_dict_t Workstation::getProperties()
151 return p_cpu->getProperties();
154 StoragePtr Workstation::findStorageOnMountList(const char* mount)
156 StoragePtr st = NULL;
160 XBT_DEBUG("Search for storage name '%s' on '%s'", mount, getName());
161 xbt_dynar_foreach(p_storage,cursor,mnt)
163 XBT_DEBUG("See '%s'",mnt.name);
164 if(!strcmp(mount,mnt.name)){
165 st = static_cast<StoragePtr>(mnt.storage);
169 if(!st) xbt_die("Can't find mount '%s' for '%s'", mount, getName());
173 xbt_dict_t Workstation::getMountedStorageList()
177 xbt_dict_t storage_list = xbt_dict_new_homogeneous(NULL);
178 char *storage_name = NULL;
180 xbt_dynar_foreach(p_storage,i,mnt){
181 storage_name = (char *)static_cast<StoragePtr>(mnt.storage)->getName();
182 xbt_dict_set(storage_list,mnt.name,storage_name,NULL);
187 xbt_dynar_t Workstation::getAttachedStorageList()
189 xbt_lib_cursor_t cursor;
192 xbt_dynar_t result = xbt_dynar_new(sizeof(void*), NULL);
193 xbt_lib_foreach(storage_lib, cursor, key, data) {
194 if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != NULL) {
195 StoragePtr storage = static_cast<StoragePtr>(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL));
196 if(!strcmp((const char*)storage->p_attach,this->getName())){
197 xbt_dynar_push_as(result, void *,(void *)static_cast<ResourcePtr>(storage)->getName());
204 ActionPtr Workstation::open(const char* fullpath) {
206 StoragePtr st = NULL;
209 size_t longest_prefix_length = 0;
211 char *file_mount_name = NULL;
212 char *mount_name = NULL;
214 XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath, getName());
215 xbt_dynar_foreach(p_storage,cursor,mnt)
217 XBT_DEBUG("See '%s'",mnt.name);
218 file_mount_name = (char *) xbt_malloc ((strlen(mnt.name)+1));
219 strncpy(file_mount_name,fullpath,strlen(mnt.name)+1);
220 file_mount_name[strlen(mnt.name)] = '\0';
222 if(!strcmp(file_mount_name,mnt.name) && strlen(mnt.name)>longest_prefix_length)
223 {/* The current mount name is found in the full path and is bigger than the previous*/
224 longest_prefix_length = strlen(mnt.name);
225 st = static_cast<StoragePtr>(mnt.storage);
227 free(file_mount_name);
229 if(longest_prefix_length>0)
230 { /* Mount point found, split fullpath into mount_name and path+filename*/
231 path = (char *) xbt_malloc ((strlen(fullpath)-longest_prefix_length+1));
232 mount_name = (char *) xbt_malloc ((longest_prefix_length+1));
233 strncpy(mount_name, fullpath, longest_prefix_length+1);
234 strncpy(path, fullpath+longest_prefix_length, strlen(fullpath)-longest_prefix_length+1);
235 path[strlen(fullpath)-longest_prefix_length] = '\0';
236 mount_name[longest_prefix_length] = '\0';
239 xbt_die("Can't find mount point for '%s' on '%s'", fullpath, getName());
241 ActionPtr action = st->open((const char*)mount_name, (const char*)path);
243 free((char*)mount_name);
247 ActionPtr Workstation::close(surf_file_t fd) {
248 StoragePtr st = findStorageOnMountList(fd->mount);
249 XBT_DEBUG("CLOSE on disk '%s'",st->getName());
250 return st->close(fd);
253 ActionPtr Workstation::read(surf_file_t fd, sg_size_t size) {
254 StoragePtr st = findStorageOnMountList(fd->mount);
255 XBT_DEBUG("READ on disk '%s'",st->getName());
256 return st->read(fd, size);
259 ActionPtr Workstation::write(surf_file_t fd, sg_size_t size) {
260 StoragePtr st = findStorageOnMountList(fd->mount);
261 XBT_DEBUG("WRITE on disk '%s'",st->getName());
262 return st->write(fd, size);
265 int Workstation::unlink(surf_file_t fd) {
267 XBT_WARN("No such file descriptor. Impossible to unlink");
271 StoragePtr st = findStorageOnMountList(fd->mount);
272 /* Check if the file is on this storage */
273 if (!xbt_dict_get_or_null(st->p_content, fd->name)){
274 XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
278 XBT_DEBUG("UNLINK on disk '%s'",st->getName());
279 st->m_usedSize -= fd->size;
281 // Remove the file from storage
282 xbt_dict_remove(st->p_content, fd->name);
292 ActionPtr Workstation::ls(const char* mount, const char *path){
293 XBT_DEBUG("LS on mount '%s' and file '%s'", mount, path);
294 StoragePtr st = findStorageOnMountList(mount);
298 sg_size_t Workstation::getSize(surf_file_t fd){
302 xbt_dynar_t Workstation::getInfo( surf_file_t fd)
304 StoragePtr st = findStorageOnMountList(fd->mount);
305 sg_size_t *psize = xbt_new(sg_size_t, 1);
307 xbt_dynar_t info = xbt_dynar_new(sizeof(void*), NULL);
308 xbt_dynar_push_as(info, sg_size_t *, psize);
309 xbt_dynar_push_as(info, void *, fd->mount);
310 xbt_dynar_push_as(info, void *, (void *)st->getName());
311 xbt_dynar_push_as(info, void *, st->p_typeId);
312 xbt_dynar_push_as(info, void *, st->p_contentType);
317 sg_size_t Workstation::fileTell(surf_file_t fd){
318 return fd->current_position;
321 int Workstation::fileSeek(surf_file_t fd, sg_size_t offset, int origin){
325 fd->current_position = 0;
328 if(offset > fd->size)
330 fd->current_position = offset;
333 fd->current_position = fd->size;
336 return MSG_TASK_CANCELED;
340 int Workstation::fileMove(surf_file_t fd, const char* fullpath){
342 /* Check if the new full path is on the same mount point */
343 if(!strncmp((const char*)fd->mount, fullpath, strlen(fd->mount)))
345 sg_size_t *psize, *new_psize;
346 psize = (sg_size_t*) xbt_dict_get_or_null(findStorageOnMountList(fd->mount)->p_content,fd->name);
347 new_psize = xbt_new(sg_size_t, 1);
349 if (psize){// src file exists
350 xbt_dict_remove(findStorageOnMountList(fd->mount)->p_content, fd->name);
352 char *path = (char *) xbt_malloc ((strlen(fullpath)-strlen(fd->mount)+1));;
353 strncpy(path, fullpath+strlen(fd->mount), strlen(fullpath)-strlen(fd->mount)+1);
354 xbt_dict_set(findStorageOnMountList(fd->mount)->p_content, path, new_psize,NULL);
355 XBT_DEBUG("Move file from %s to %s, size '%llu'",fd->name, fullpath, *psize);
360 XBT_WARN("File %s doesn't exist", fd->name);
361 return MSG_TASK_CANCELED;
365 XBT_WARN("New full path %s is not on the same mount point: %s. Action has been canceled.", fullpath, fd->mount);
366 return MSG_TASK_CANCELED;
370 int Workstation::fileRcopy(surf_file_t fd, surf_resource_t host_dest, const char* fullpath){
372 XBT_INFO("FILE %s WKS %s FULLPATH %s",fd->name, host_dest->key, fullpath);
374 /* Find the host src where the file is located */
375 StoragePtr storage = findStorageOnMountList(fd->mount);
376 const char* host_name_src = (const char*)storage->p_attach;
378 /* Find the host dest where the file will be stored */
381 StoragePtr storage_dest = NULL;
382 const char* host_name_dest;
383 char *file_mount_name = NULL;
384 size_t longest_prefix_length = 0;
385 xbt_dynar_foreach(((WorkstationPtr)host_dest)->p_storage,cursor,mnt)
387 file_mount_name = (char *) xbt_malloc ((strlen(mnt.name)+1));
388 strncpy(file_mount_name,fullpath,strlen(mnt.name)+1);
389 file_mount_name[strlen(mnt.name)] = '\0';
391 if(!strcmp(file_mount_name,mnt.name) && strlen(mnt.name)>longest_prefix_length)
392 {/* The current mount name is found in the full path and is bigger than the previous*/
393 longest_prefix_length = strlen(mnt.name);
394 storage_dest = static_cast<StoragePtr>(mnt.storage);
396 free(file_mount_name);
398 if(longest_prefix_length>0)
399 { /* Mount point found */
400 host_name_dest = storage_dest->p_attach;
404 XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, host_dest->key);
405 return MSG_TASK_CANCELED;
407 XBT_INFO("SRC %s DEST %s", host_name_src, host_name_dest);
411 // /* Check that file to copy is local to the src workstation (storage is attached to src workstation) */
412 // StoragePtr storage = findStorageOnMountList(fd->mount);
413 // if(!strcmp((const char*)storage->p_attach, this->getName()))
415 // /* Check that there is a route between src and dest workstations */
416 // xbt_dynar_t route = NULL;
417 // routing_get_route_and_latency(this->p_netElm, ((WorkstationPtr)host_dest)->p_netElm, &route, NULL);
420 // ATTENTION DISCUSSION AVEC FRED !
425 // XBT_WARN("There is no route between %s and %s. Action has been canceled", this->getName(), host_dest->key);
426 // return MSG_TASK_CANCELED;
431 // XBT_WARN("File %s is not local to %s but to %s. Action has been canceled", fd->name,this->getName(), storage->p_attach);
432 // return MSG_TASK_CANCELED;
436 sg_size_t Workstation::getFreeSize(const char* name)
438 StoragePtr st = findStorageOnMountList(name);
439 return st->m_size - st->m_usedSize;
442 sg_size_t Workstation::getUsedSize(const char* name)
444 StoragePtr st = findStorageOnMountList(name);
445 return st->m_usedSize;
448 xbt_dynar_t Workstation::getVms()
450 xbt_dynar_t dyn = xbt_dynar_new(sizeof(smx_host_t), NULL);
452 /* iterate for all virtual machines */
453 for (WorkstationVMModel::vm_list_t::iterator iter =
454 WorkstationVMModel::ws_vms.begin();
455 iter != WorkstationVMModel::ws_vms.end(); ++iter) {
457 WorkstationVMPtr ws_vm = &*iter;
458 if (this == ws_vm-> p_subWs)
459 xbt_dynar_push(dyn, &ws_vm->p_subWs);
465 void Workstation::getParams(ws_params_t params)
470 void Workstation::setParams(ws_params_t params)
472 /* may check something here. */
480 void WorkstationAction::setState(e_surf_action_state_t state){
481 Action::setState(state);
482 surf_callback_emit(workstationActionStateChangedCallbacks, this);