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 "simix/smx_private.h"
8 #include "workstation_interface.hpp"
9 #include "vm_workstation_interface.hpp"
10 #include "cpu_cas01.hpp"
11 #include "simgrid/sg_config.h"
13 #include "network_interface.hpp"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
16 "Logging specific to the SURF workstation module");
18 WorkstationModelPtr surf_workstation_model = NULL;
24 surf_callback(void, WorkstationPtr) workstationCreatedCallbacks;
25 surf_callback(void, WorkstationPtr) workstationDestructedCallbacks;
26 surf_callback(void, WorkstationPtr, e_surf_resource_state_t, e_surf_resource_state_t) workstationStateChangedCallbacks;
27 surf_callback(void, WorkstationActionPtr, e_surf_action_state_t, e_surf_action_state_t) workstationActionStateChangedCallbacks;
29 void workstation_parse_init(sg_platf_host_cbarg_t host)
31 surf_workstation_model->createWorkstation(host->id);
34 void workstation_add_traces(){
35 surf_workstation_model->addTraces();
41 WorkstationModel::WorkstationModel(const char *name)
44 p_cpuModel = surf_cpu_model_pm;
47 WorkstationModel::WorkstationModel()
48 : Model("Workstation") {
49 p_cpuModel = surf_cpu_model_pm;
52 WorkstationModel::~WorkstationModel() {
55 /* Each VM has a dummy CPU action on the PM layer. This CPU action works as the
56 * constraint (capacity) of the VM in the PM layer. If the VM does not have any
57 * active task, the dummy CPU action must be deactivated, so that the VM does
58 * not get any CPU share in the PM layer. */
59 void WorkstationModel::adjustWeightOfDummyCpuActions()
61 /* iterate for all virtual machines */
62 for (WorkstationVMModel::vm_list_t::iterator iter =
63 WorkstationVMModel::ws_vms.begin();
64 iter != WorkstationVMModel::ws_vms.end(); ++iter) {
66 WorkstationVMPtr ws_vm = &*iter;
67 CpuCas01Ptr cpu_cas01 = static_cast<CpuCas01Ptr>(ws_vm->p_cpu);
68 xbt_assert(cpu_cas01, "cpu-less workstation");
70 int is_active = lmm_constraint_used(cpu_cas01->getModel()->getMaxminSystem(), cpu_cas01->getConstraint());
71 // int is_active_old = constraint_is_active(cpu_cas01);
74 // xbt_assert(is_active == is_active_old, "%d %d", is_active, is_active_old);
78 /* some tasks exist on this VM */
79 XBT_DEBUG("set the weight of the dummy CPU action on PM to 1");
81 /* FIXME: we shoud use lmm_update_variable_weight() ? */
82 /* FIXME: If we assgign 1.05 and 0.05, the system makes apparently wrong values. */
83 ws_vm->p_action->setPriority(1);
86 /* no task exits on this VM */
87 XBT_DEBUG("set the weight of the dummy CPU action on PM to 0");
89 ws_vm->p_action->setPriority(0);
97 Workstation::Workstation()
99 surf_callback_emit(workstationCreatedCallbacks, this);
102 Workstation::Workstation(ModelPtr model, const char *name, xbt_dict_t props,
103 xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
104 : Resource(model, name, props)
105 , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
107 p_params.ramsize = 0;
108 surf_callback_emit(workstationCreatedCallbacks, this);
111 Workstation::Workstation(ModelPtr model, const char *name, xbt_dict_t props, lmm_constraint_t constraint,
112 xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
113 : Resource(model, name, props, constraint)
114 , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
116 p_params.ramsize = 0;
117 surf_callback_emit(workstationCreatedCallbacks, this);
120 Workstation::~Workstation(){
121 surf_callback_emit(workstationDestructedCallbacks, this);
124 void Workstation::setState(e_surf_resource_state_t state){
125 e_surf_resource_state_t old = Resource::getState();
126 Resource::setState(state);
127 surf_callback_emit(workstationStateChangedCallbacks, this, old, state);
130 int Workstation::getCore(){
131 return p_cpu->getCore();
134 double Workstation::getSpeed(double load){
135 return p_cpu->getSpeed(load);
138 double Workstation::getAvailableSpeed(){
139 return p_cpu->getAvailableSpeed();
142 double Workstation::getCurrentPowerPeak()
144 return p_cpu->getCurrentPowerPeak();
147 double Workstation::getPowerPeakAt(int pstate_index)
149 return p_cpu->getPowerPeakAt(pstate_index);
152 int Workstation::getNbPstates()
154 return p_cpu->getNbPstates();
157 void Workstation::setPowerPeakAt(int pstate_index)
159 p_cpu->setPowerPeakAt(pstate_index);
162 xbt_dict_t Workstation::getProperties()
164 return p_cpu->getProperties();
167 StoragePtr Workstation::findStorageOnMountList(const char* mount)
169 StoragePtr st = NULL;
173 XBT_DEBUG("Search for storage name '%s' on '%s'", mount, getName());
174 xbt_dynar_foreach(p_storage,cursor,mnt)
176 XBT_DEBUG("See '%s'",mnt.name);
177 if(!strcmp(mount,mnt.name)){
178 st = static_cast<StoragePtr>(mnt.storage);
182 if(!st) xbt_die("Can't find mount '%s' for '%s'", mount, getName());
186 xbt_dict_t Workstation::getMountedStorageList()
190 xbt_dict_t storage_list = xbt_dict_new_homogeneous(NULL);
191 char *storage_name = NULL;
193 xbt_dynar_foreach(p_storage,i,mnt){
194 storage_name = (char *)static_cast<StoragePtr>(mnt.storage)->getName();
195 xbt_dict_set(storage_list,mnt.name,storage_name,NULL);
200 xbt_dynar_t Workstation::getAttachedStorageList()
202 xbt_lib_cursor_t cursor;
205 xbt_dynar_t result = xbt_dynar_new(sizeof(void*), NULL);
206 xbt_lib_foreach(storage_lib, cursor, key, data) {
207 if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != NULL) {
208 StoragePtr storage = static_cast<StoragePtr>(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL));
209 if(!strcmp((const char*)storage->p_attach,this->getName())){
210 xbt_dynar_push_as(result, void *, (void*)storage->getName());
217 ActionPtr Workstation::open(const char* fullpath) {
219 StoragePtr st = NULL;
222 size_t longest_prefix_length = 0;
224 char *file_mount_name = NULL;
225 char *mount_name = NULL;
227 XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath, getName());
228 xbt_dynar_foreach(p_storage,cursor,mnt)
230 XBT_DEBUG("See '%s'",mnt.name);
231 file_mount_name = (char *) xbt_malloc ((strlen(mnt.name)+1));
232 strncpy(file_mount_name,fullpath,strlen(mnt.name)+1);
233 file_mount_name[strlen(mnt.name)] = '\0';
235 if(!strcmp(file_mount_name,mnt.name) && strlen(mnt.name)>longest_prefix_length)
236 {/* The current mount name is found in the full path and is bigger than the previous*/
237 longest_prefix_length = strlen(mnt.name);
238 st = static_cast<StoragePtr>(mnt.storage);
240 free(file_mount_name);
242 if(longest_prefix_length>0)
243 { /* Mount point found, split fullpath into mount_name and path+filename*/
244 path = (char *) xbt_malloc ((strlen(fullpath)-longest_prefix_length+1));
245 mount_name = (char *) xbt_malloc ((longest_prefix_length+1));
246 strncpy(mount_name, fullpath, longest_prefix_length+1);
247 strncpy(path, fullpath+longest_prefix_length, strlen(fullpath)-longest_prefix_length+1);
248 path[strlen(fullpath)-longest_prefix_length] = '\0';
249 mount_name[longest_prefix_length] = '\0';
252 xbt_die("Can't find mount point for '%s' on '%s'", fullpath, getName());
254 ActionPtr action = st->open((const char*)mount_name, (const char*)path);
256 free((char*)mount_name);
260 ActionPtr Workstation::close(surf_file_t fd) {
261 StoragePtr st = findStorageOnMountList(fd->mount);
262 XBT_DEBUG("CLOSE on disk '%s'",st->getName());
263 return st->close(fd);
266 ActionPtr Workstation::read(surf_file_t fd, sg_size_t size) {
267 StoragePtr st = findStorageOnMountList(fd->mount);
268 XBT_DEBUG("READ on disk '%s'",st->getName());
269 return st->read(fd, size);
272 ActionPtr Workstation::write(surf_file_t fd, sg_size_t size) {
273 StoragePtr st = findStorageOnMountList(fd->mount);
274 XBT_DEBUG("WRITE on disk '%s'",st->getName());
275 return st->write(fd, size);
278 int Workstation::unlink(surf_file_t fd) {
280 XBT_WARN("No such file descriptor. Impossible to unlink");
284 StoragePtr st = findStorageOnMountList(fd->mount);
285 /* Check if the file is on this storage */
286 if (!xbt_dict_get_or_null(st->p_content, fd->name)){
287 XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
291 XBT_DEBUG("UNLINK on disk '%s'",st->getName());
292 st->m_usedSize -= fd->size;
294 // Remove the file from storage
295 xbt_dict_remove(st->p_content, fd->name);
305 sg_size_t Workstation::getSize(surf_file_t fd){
309 xbt_dynar_t Workstation::getInfo( surf_file_t fd)
311 StoragePtr st = findStorageOnMountList(fd->mount);
312 sg_size_t *psize = xbt_new(sg_size_t, 1);
314 xbt_dynar_t info = xbt_dynar_new(sizeof(void*), NULL);
315 xbt_dynar_push_as(info, sg_size_t *, psize);
316 xbt_dynar_push_as(info, void *, fd->mount);
317 xbt_dynar_push_as(info, void *, (void *)st->getName());
318 xbt_dynar_push_as(info, void *, st->p_typeId);
319 xbt_dynar_push_as(info, void *, st->p_contentType);
324 sg_size_t Workstation::fileTell(surf_file_t fd){
325 return fd->current_position;
328 int Workstation::fileSeek(surf_file_t fd, sg_size_t offset, int origin){
332 fd->current_position = offset;
335 fd->current_position += offset;
338 fd->current_position = fd->size + offset;
345 int Workstation::fileMove(surf_file_t fd, const char* fullpath){
346 /* Check if the new full path is on the same mount point */
347 if(!strncmp((const char*)fd->mount, fullpath, strlen(fd->mount))) {
348 sg_size_t *psize, *new_psize;
350 xbt_dict_get_or_null(findStorageOnMountList(fd->mount)->p_content,
352 new_psize = xbt_new(sg_size_t, 1);
354 if (psize){// src file exists
355 xbt_dict_remove(findStorageOnMountList(fd->mount)->p_content, fd->name);
356 char *path = (char *) xbt_malloc ((strlen(fullpath)-strlen(fd->mount)+1));;
357 strncpy(path, fullpath+strlen(fd->mount),
358 strlen(fullpath)-strlen(fd->mount)+1);
359 xbt_dict_set(findStorageOnMountList(fd->mount)->p_content, path,
361 XBT_DEBUG("Move file from %s to %s, size '%llu'",fd->name, fullpath, *psize);
365 XBT_WARN("File %s doesn't exist", fd->name);
369 XBT_WARN("New full path %s is not on the same mount point: %s. Action has been canceled.",
370 fullpath, fd->mount);
375 xbt_dynar_t Workstation::getVms()
377 xbt_dynar_t dyn = xbt_dynar_new(sizeof(smx_host_t), NULL);
379 /* iterate for all virtual machines */
380 for (WorkstationVMModel::vm_list_t::iterator iter =
381 WorkstationVMModel::ws_vms.begin();
382 iter != WorkstationVMModel::ws_vms.end(); ++iter) {
384 WorkstationVMPtr ws_vm = &*iter;
385 if (this == ws_vm-> p_subWs)
386 xbt_dynar_push(dyn, &ws_vm->p_subWs);
392 void Workstation::getParams(ws_params_t params)
397 void Workstation::setParams(ws_params_t params)
399 /* may check something here. */
407 void WorkstationAction::setState(e_surf_action_state_t state){
408 e_surf_action_state_t old = getState();
409 Action::setState(state);
410 surf_callback_emit(workstationActionStateChangedCallbacks, this, old, state);