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 hosts including virtual machines */
50 xbt_lib_cursor_t cursor;
54 xbt_lib_foreach(host_lib, cursor, key, ind_host) {
55 WorkstationPtr ws = static_cast<WorkstationPtr>(ind_host[SURF_WKS_LEVEL]);
56 CpuCas01Ptr cpu_cas01 = static_cast<CpuCas01Ptr>(ind_host[SURF_CPU_LEVEL]);
60 /* skip if it is not a virtual machine */
61 if (ws->getModel() != static_cast<ModelPtr>(surf_vm_workstation_model))
63 xbt_assert(cpu_cas01, "cpu-less workstation");
65 /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
66 WorkstationVMPtr ws_vm = static_cast<WorkstationVMPtr>(ws);
68 int is_active = lmm_constraint_used(cpu_cas01->getModel()->getMaxminSystem(), cpu_cas01->getConstraint());
69 // int is_active_old = constraint_is_active(cpu_cas01);
72 // xbt_assert(is_active == is_active_old, "%d %d", is_active, is_active_old);
76 /* some tasks exist on this VM */
77 XBT_DEBUG("set the weight of the dummy CPU action on PM to 1");
79 /* FIXME: we shoud use lmm_update_variable_weight() ? */
80 /* FIXME: If we assgign 1.05 and 0.05, the system makes apparently wrong values. */
81 ws_vm->p_action->setPriority(1);
84 /* no task exits on this VM */
85 XBT_DEBUG("set the weight of the dummy CPU action on PM to 0");
87 ws_vm->p_action->setPriority(0);
95 Workstation::Workstation()
97 surf_callback_emit(workstationCreatedCallbacks, this);
100 Workstation::Workstation(ModelPtr model, const char *name, xbt_dict_t props,
101 xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
102 : Resource(model, name, props)
103 , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
105 surf_callback_emit(workstationCreatedCallbacks, this);
108 Workstation::Workstation(ModelPtr model, const char *name, xbt_dict_t props, lmm_constraint_t constraint,
109 xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
110 : Resource(model, name, props, constraint)
111 , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
113 surf_callback_emit(workstationCreatedCallbacks, this);
116 Workstation::~Workstation(){
117 surf_callback_emit(workstationDestructedCallbacks, this);
120 void Workstation::setState(e_surf_resource_state_t state){
121 Resource::setState(state);
122 surf_callback_emit(workstationStateChangedCallbacks, this);
125 int Workstation::getCore(){
126 return p_cpu->getCore();
129 double Workstation::getSpeed(double load){
130 return p_cpu->getSpeed(load);
133 double Workstation::getAvailableSpeed(){
134 return p_cpu->getAvailableSpeed();
137 double Workstation::getCurrentPowerPeak()
139 return p_cpu->getCurrentPowerPeak();
142 double Workstation::getPowerPeakAt(int pstate_index)
144 return p_cpu->getPowerPeakAt(pstate_index);
147 int Workstation::getNbPstates()
149 return p_cpu->getNbPstates();
152 void Workstation::setPowerPeakAt(int pstate_index)
154 p_cpu->setPowerPeakAt(pstate_index);
157 xbt_dict_t Workstation::getProperties()
159 return p_cpu->getProperties();
162 StoragePtr Workstation::findStorageOnMountList(const char* mount)
164 StoragePtr st = NULL;
168 XBT_DEBUG("Search for storage name '%s' on '%s'", mount, getName());
169 xbt_dynar_foreach(p_storage,cursor,mnt)
171 XBT_DEBUG("See '%s'",mnt.name);
172 if(!strcmp(mount,mnt.name)){
173 st = static_cast<StoragePtr>(mnt.storage);
177 if(!st) xbt_die("Can't find mount '%s' for '%s'", mount, getName());
181 xbt_dict_t Workstation::getStorageList()
185 xbt_dict_t storage_list = xbt_dict_new_homogeneous(NULL);
186 char *storage_name = NULL;
188 xbt_dynar_foreach(p_storage,i,mnt){
189 storage_name = (char *)static_cast<StoragePtr>(mnt.storage)->getName();
190 xbt_dict_set(storage_list,mnt.name,storage_name,NULL);
195 ActionPtr Workstation::open(const char* mount, const char* path) {
196 StoragePtr st = findStorageOnMountList(mount);
197 XBT_DEBUG("OPEN on disk '%s'", st->getName());
198 return st->open(mount, path);
201 ActionPtr Workstation::close(surf_file_t fd) {
202 StoragePtr st = findStorageOnMountList(fd->mount);
203 XBT_DEBUG("CLOSE on disk '%s'",st->getName());
204 return st->close(fd);
207 ActionPtr Workstation::read(surf_file_t fd, sg_size_t size) {
208 StoragePtr st = findStorageOnMountList(fd->mount);
209 XBT_DEBUG("READ on disk '%s'",st->getName());
210 return st->read(fd, size);
213 ActionPtr Workstation::write(surf_file_t fd, sg_size_t size) {
214 StoragePtr st = findStorageOnMountList(fd->mount);
215 XBT_DEBUG("WRITE on disk '%s'",st->getName());
216 return st->write(fd, size);
219 int Workstation::unlink(surf_file_t fd) {
221 XBT_WARN("No such file descriptor. Impossible to unlink");
224 // XBT_INFO("%s %zu", fd->storage, fd->size);
225 StoragePtr st = findStorageOnMountList(fd->mount);
226 /* Check if the file is on this storage */
227 if (!xbt_dict_get_or_null(st->p_content, fd->name)){
228 XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
232 XBT_DEBUG("UNLINK on disk '%s'",st->getName());
233 st->m_usedSize -= fd->size;
235 // Remove the file from storage
236 xbt_dict_remove(st->p_content, fd->name);
246 ActionPtr Workstation::ls(const char* mount, const char *path){
247 XBT_DEBUG("LS on mount '%s' and file '%s'", mount, path);
248 StoragePtr st = findStorageOnMountList(mount);
252 sg_size_t Workstation::getSize(surf_file_t fd){
256 xbt_dynar_t Workstation::getInfo( surf_file_t fd)
258 StoragePtr st = findStorageOnMountList(fd->mount);
259 sg_size_t *psize = xbt_new(sg_size_t, 1);
261 xbt_dynar_t info = xbt_dynar_new(sizeof(void*), NULL);
262 xbt_dynar_push_as(info, sg_size_t *, psize);
263 xbt_dynar_push_as(info, void *, fd->mount);
264 xbt_dynar_push_as(info, void *, (void *)st->getName());
265 xbt_dynar_push_as(info, void *, st->p_typeId);
266 xbt_dynar_push_as(info, void *, st->p_contentType);
271 sg_size_t Workstation::fileTell(surf_file_t fd){
272 return fd->current_position;
275 int Workstation::fileSeek(surf_file_t fd, sg_size_t offset, int origin){
279 fd->current_position = 0;
282 if(offset > fd->size)
284 fd->current_position = offset;
287 fd->current_position = fd->size;
290 return MSG_TASK_CANCELED;
294 sg_size_t Workstation::getFreeSize(const char* name)
296 StoragePtr st = findStorageOnMountList(name);
297 return st->m_size - st->m_usedSize;
300 sg_size_t Workstation::getUsedSize(const char* name)
302 StoragePtr st = findStorageOnMountList(name);
303 return st->m_usedSize;
306 xbt_dynar_t Workstation::getVms()
308 xbt_dynar_t dyn = xbt_dynar_new(sizeof(smx_host_t), NULL);
310 /* iterate for all hosts including virtual machines */
311 xbt_lib_cursor_t cursor;
314 xbt_lib_foreach(host_lib, cursor, key, ind_host) {
315 WorkstationPtr ws = static_cast<WorkstationPtr>(ind_host[SURF_WKS_LEVEL]);
318 /* skip if it is not a virtual machine */
319 if (ws->getModel() != static_cast<ModelPtr>(surf_vm_workstation_model))
322 /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
323 WorkstationVMPtr ws_vm = static_cast<WorkstationVMPtr>(ws);
324 if (this == ws_vm-> p_subWs)
325 xbt_dynar_push(dyn, &ws_vm->p_subWs);
331 void Workstation::getParams(ws_params_t params)
333 memcpy(params, &p_params, sizeof(s_ws_params_t));
336 void Workstation::setParams(ws_params_t params)
338 /* may check something here. */
339 memcpy(&p_params, params, sizeof(s_ws_params_t));
346 void WorkstationAction::setState(e_surf_action_state_t state){
347 Action::setState(state);
348 surf_callback_emit(workstationActionStateChangedCallbacks, this);