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 #include "network_interface.hpp"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
15 "Logging specific to the SURF workstation module");
17 WorkstationModelPtr surf_workstation_model = NULL;
23 surf_callback(void, WorkstationPtr) workstationCreatedCallbacks;
24 surf_callback(void, WorkstationPtr) workstationDestructedCallbacks;
25 surf_callback(void, WorkstationPtr, e_surf_resource_state_t, e_surf_resource_state_t) workstationStateChangedCallbacks;
26 surf_callback(void, WorkstationActionPtr, e_surf_action_state_t, e_surf_action_state_t) workstationActionStateChangedCallbacks;
31 WorkstationModel::WorkstationModel(const char *name)
34 p_cpuModel = surf_cpu_model_pm;
37 WorkstationModel::WorkstationModel()
38 : Model("Workstation") {
39 p_cpuModel = surf_cpu_model_pm;
42 WorkstationModel::~WorkstationModel() {
45 /* Each VM has a dummy CPU action on the PM layer. This CPU action works as the
46 * constraint (capacity) of the VM in the PM layer. If the VM does not have any
47 * active task, the dummy CPU action must be deactivated, so that the VM does
48 * not get any CPU share in the PM layer. */
49 void WorkstationModel::adjustWeightOfDummyCpuActions()
51 /* iterate for all virtual machines */
52 for (WorkstationVMModel::vm_list_t::iterator iter =
53 WorkstationVMModel::ws_vms.begin();
54 iter != WorkstationVMModel::ws_vms.end(); ++iter) {
56 WorkstationVMPtr ws_vm = &*iter;
57 CpuCas01Ptr cpu_cas01 = static_cast<CpuCas01Ptr>(ws_vm->p_cpu);
58 xbt_assert(cpu_cas01, "cpu-less workstation");
60 int is_active = lmm_constraint_used(cpu_cas01->getModel()->getMaxminSystem(), cpu_cas01->getConstraint());
61 // int is_active_old = constraint_is_active(cpu_cas01);
64 // xbt_assert(is_active == is_active_old, "%d %d", is_active, is_active_old);
68 /* some tasks exist on this VM */
69 XBT_DEBUG("set the weight of the dummy CPU action on PM to 1");
71 /* FIXME: we shoud use lmm_update_variable_weight() ? */
72 /* FIXME: If we assgign 1.05 and 0.05, the system makes apparently wrong values. */
73 ws_vm->p_action->setPriority(1);
76 /* no task exits on this VM */
77 XBT_DEBUG("set the weight of the dummy CPU action on PM to 0");
79 ws_vm->p_action->setPriority(0);
87 Workstation::Workstation()
89 surf_callback_emit(workstationCreatedCallbacks, this);
92 Workstation::Workstation(ModelPtr model, const char *name, xbt_dict_t props,
93 xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
94 : Resource(model, name, props)
95 , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
98 surf_callback_emit(workstationCreatedCallbacks, this);
101 Workstation::Workstation(ModelPtr model, const char *name, xbt_dict_t props, lmm_constraint_t constraint,
102 xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
103 : Resource(model, name, props, constraint)
104 , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
106 p_params.ramsize = 0;
107 surf_callback_emit(workstationCreatedCallbacks, this);
110 Workstation::~Workstation(){
111 surf_callback_emit(workstationDestructedCallbacks, this);
114 void Workstation::setState(e_surf_resource_state_t state){
115 e_surf_resource_state_t old = Resource::getState();
116 Resource::setState(state);
117 surf_callback_emit(workstationStateChangedCallbacks, this, old, state);
120 int Workstation::getCore(){
121 return p_cpu->getCore();
124 double Workstation::getSpeed(double load){
125 return p_cpu->getSpeed(load);
128 double Workstation::getAvailableSpeed(){
129 return p_cpu->getAvailableSpeed();
132 double Workstation::getCurrentPowerPeak()
134 return p_cpu->getCurrentPowerPeak();
137 double Workstation::getPowerPeakAt(int pstate_index)
139 return p_cpu->getPowerPeakAt(pstate_index);
142 int Workstation::getNbPstates()
144 return p_cpu->getNbPstates();
147 void Workstation::setPowerPeakAt(int pstate_index)
149 p_cpu->setPowerPeakAt(pstate_index);
152 xbt_dict_t Workstation::getProperties()
154 return p_cpu->getProperties();
157 StoragePtr Workstation::findStorageOnMountList(const char* mount)
159 StoragePtr st = NULL;
163 XBT_DEBUG("Search for storage name '%s' on '%s'", mount, getName());
164 xbt_dynar_foreach(p_storage,cursor,mnt)
166 XBT_DEBUG("See '%s'",mnt.name);
167 if(!strcmp(mount,mnt.name)){
168 st = static_cast<StoragePtr>(mnt.storage);
172 if(!st) xbt_die("Can't find mount '%s' for '%s'", mount, getName());
176 xbt_dict_t Workstation::getMountedStorageList()
180 xbt_dict_t storage_list = xbt_dict_new_homogeneous(NULL);
181 char *storage_name = NULL;
183 xbt_dynar_foreach(p_storage,i,mnt){
184 storage_name = (char *)static_cast<StoragePtr>(mnt.storage)->getName();
185 xbt_dict_set(storage_list,mnt.name,storage_name,NULL);
190 xbt_dynar_t Workstation::getAttachedStorageList()
192 xbt_lib_cursor_t cursor;
195 xbt_dynar_t result = xbt_dynar_new(sizeof(void*), NULL);
196 xbt_lib_foreach(storage_lib, cursor, key, data) {
197 if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != NULL) {
198 StoragePtr storage = static_cast<StoragePtr>(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL));
199 if(!strcmp((const char*)storage->p_attach,this->getName())){
200 xbt_dynar_push_as(result, void *,(void *)static_cast<ResourcePtr>(storage)->getName());
207 ActionPtr Workstation::open(const char* fullpath) {
209 StoragePtr st = NULL;
212 size_t longest_prefix_length = 0;
214 char *file_mount_name = NULL;
215 char *mount_name = NULL;
217 XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath, getName());
218 xbt_dynar_foreach(p_storage,cursor,mnt)
220 XBT_DEBUG("See '%s'",mnt.name);
221 file_mount_name = (char *) xbt_malloc ((strlen(mnt.name)+1));
222 strncpy(file_mount_name,fullpath,strlen(mnt.name)+1);
223 file_mount_name[strlen(mnt.name)] = '\0';
225 if(!strcmp(file_mount_name,mnt.name) && strlen(mnt.name)>longest_prefix_length)
226 {/* The current mount name is found in the full path and is bigger than the previous*/
227 longest_prefix_length = strlen(mnt.name);
228 st = static_cast<StoragePtr>(mnt.storage);
230 free(file_mount_name);
232 if(longest_prefix_length>0)
233 { /* Mount point found, split fullpath into mount_name and path+filename*/
234 path = (char *) xbt_malloc ((strlen(fullpath)-longest_prefix_length+1));
235 mount_name = (char *) xbt_malloc ((longest_prefix_length+1));
236 strncpy(mount_name, fullpath, longest_prefix_length+1);
237 strncpy(path, fullpath+longest_prefix_length, strlen(fullpath)-longest_prefix_length+1);
238 path[strlen(fullpath)-longest_prefix_length] = '\0';
239 mount_name[longest_prefix_length] = '\0';
242 xbt_die("Can't find mount point for '%s' on '%s'", fullpath, getName());
244 ActionPtr action = st->open((const char*)mount_name, (const char*)path);
246 free((char*)mount_name);
250 ActionPtr Workstation::close(surf_file_t fd) {
251 StoragePtr st = findStorageOnMountList(fd->mount);
252 XBT_DEBUG("CLOSE on disk '%s'",st->getName());
253 return st->close(fd);
256 ActionPtr Workstation::read(surf_file_t fd, sg_size_t size) {
257 StoragePtr st = findStorageOnMountList(fd->mount);
258 XBT_DEBUG("READ on disk '%s'",st->getName());
259 return st->read(fd, size);
262 ActionPtr Workstation::write(surf_file_t fd, sg_size_t size) {
263 StoragePtr st = findStorageOnMountList(fd->mount);
264 XBT_DEBUG("WRITE on disk '%s'",st->getName());
265 return st->write(fd, size);
268 int Workstation::unlink(surf_file_t fd) {
270 XBT_WARN("No such file descriptor. Impossible to unlink");
274 StoragePtr st = findStorageOnMountList(fd->mount);
275 /* Check if the file is on this storage */
276 if (!xbt_dict_get_or_null(st->p_content, fd->name)){
277 XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
281 XBT_DEBUG("UNLINK on disk '%s'",st->getName());
282 st->m_usedSize -= fd->size;
284 // Remove the file from storage
285 xbt_dict_remove(st->p_content, fd->name);
295 sg_size_t Workstation::getSize(surf_file_t fd){
299 xbt_dynar_t Workstation::getInfo( surf_file_t fd)
301 StoragePtr st = findStorageOnMountList(fd->mount);
302 sg_size_t *psize = xbt_new(sg_size_t, 1);
304 xbt_dynar_t info = xbt_dynar_new(sizeof(void*), NULL);
305 xbt_dynar_push_as(info, sg_size_t *, psize);
306 xbt_dynar_push_as(info, void *, fd->mount);
307 xbt_dynar_push_as(info, void *, (void *)st->getName());
308 xbt_dynar_push_as(info, void *, st->p_typeId);
309 xbt_dynar_push_as(info, void *, st->p_contentType);
314 sg_size_t Workstation::fileTell(surf_file_t fd){
315 return fd->current_position;
318 int Workstation::fileSeek(surf_file_t fd, sg_size_t offset, int origin){
322 fd->current_position = offset;
325 fd->current_position += offset;
328 fd->current_position = fd->size + offset;
335 int Workstation::fileMove(surf_file_t fd, const char* fullpath){
336 /* Check if the new full path is on the same mount point */
337 if(!strncmp((const char*)fd->mount, fullpath, strlen(fd->mount))) {
338 sg_size_t *psize, *new_psize;
340 xbt_dict_get_or_null(findStorageOnMountList(fd->mount)->p_content,
342 new_psize = xbt_new(sg_size_t, 1);
344 if (psize){// src file exists
345 xbt_dict_remove(findStorageOnMountList(fd->mount)->p_content, fd->name);
346 char *path = (char *) xbt_malloc ((strlen(fullpath)-strlen(fd->mount)+1));;
347 strncpy(path, fullpath+strlen(fd->mount),
348 strlen(fullpath)-strlen(fd->mount)+1);
349 xbt_dict_set(findStorageOnMountList(fd->mount)->p_content, path,
351 XBT_DEBUG("Move file from %s to %s, size '%llu'",fd->name, fullpath, *psize);
355 XBT_WARN("File %s doesn't exist", fd->name);
359 XBT_WARN("New full path %s is not on the same mount point: %s. Action has been canceled.",
360 fullpath, fd->mount);
365 xbt_dynar_t Workstation::getVms()
367 xbt_dynar_t dyn = xbt_dynar_new(sizeof(smx_host_t), NULL);
369 /* iterate for all virtual machines */
370 for (WorkstationVMModel::vm_list_t::iterator iter =
371 WorkstationVMModel::ws_vms.begin();
372 iter != WorkstationVMModel::ws_vms.end(); ++iter) {
374 WorkstationVMPtr ws_vm = &*iter;
375 if (this == ws_vm-> p_subWs)
376 xbt_dynar_push(dyn, &ws_vm->p_subWs);
382 void Workstation::getParams(ws_params_t params)
387 void Workstation::setParams(ws_params_t params)
389 /* may check something here. */
397 void WorkstationAction::setState(e_surf_action_state_t state){
398 e_surf_action_state_t old = getState();
399 Action::setState(state);
400 surf_callback_emit(workstationActionStateChangedCallbacks, this, old, state);