1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

Feature #1678: Method to calculate VM storage usage in scheduler

This commit is contained in:
Carlos Martín 2013-10-16 13:38:52 +02:00 committed by Javi Fontan
parent 4f916854a0
commit 887a4191a5
4 changed files with 152 additions and 8 deletions

View File

@ -517,6 +517,9 @@ int Image::disk_attribute( VectorAttribute * disk,
disk->replace("IMAGE_ID", iid.str());
disk->replace("SOURCE", source);
// TODO: move all size values (image, volatile disk, quotas) to long long
disk->replace("SIZE", (int)size_mb);
if (driver.empty() && !template_driver.empty())//DRIVER in Image,not in DISK
{
disk->replace("DRIVER",template_driver);

View File

@ -48,6 +48,11 @@ public:
{
delete vm_template;
}
if (user_template != 0)
{
delete user_template;
}
}
//--------------------------------------------------------------------------
@ -106,6 +111,8 @@ public:
void get_requirements (int& cpu, int& memory, int& disk);
map<int,float> get_storage_usage();
//--------------------------------------------------------------------------
// Matched Resources Interface
//--------------------------------------------------------------------------
@ -189,9 +196,9 @@ public:
*/
string& get_template(string& xml_str)
{
if (vm_template != 0)
if (user_template != 0)
{
vm_template->to_xml(xml_str);
user_template->to_xml(xml_str);
}
else
{
@ -210,7 +217,7 @@ public:
{
attributes.clear();
vm_template->remove("SCHED_ACTION", attributes);
user_template->remove("SCHED_ACTION", attributes);
}
/**
@ -220,7 +227,7 @@ public:
*/
void set_attribute(Attribute* att)
{
return vm_template->set(att);
return user_template->set(att);
}
/**
@ -274,7 +281,8 @@ protected:
string ds_requirements;
string ds_rank;
VirtualMachineTemplate * vm_template; /**< The VM user template */
VirtualMachineTemplate * vm_template; /**< The VM template */
VirtualMachineTemplate * user_template; /**< The VM user template */
};
#endif /* VM_XML_H_ */

View File

@ -42,6 +42,31 @@ int VirtualMachinePoolXML::set_up()
}
NebulaLog::log("VM",Log::DEBUG,oss);
// TODO: remove debug log
oss.str("");
oss << "Storage usage:" << endl;
for (it=objects.begin();it!=objects.end();it++)
{
VirtualMachineXML * vm = static_cast<VirtualMachineXML *>(it->second);
map<int,float> ds_usage = vm->get_storage_usage();
map<int,float>::iterator ds_it;
oss << "VM " << vm->get_oid() << ": ";
for (ds_it = ds_usage.begin(); ds_it != ds_usage.end(); ds_it++)
{
oss << "DS " << ds_it->first << " " << ds_it->second << "MB; ";
}
oss << endl;
}
NebulaLog::log("VM",Log::DEBUG,oss);
}
return rc;

View File

@ -168,7 +168,7 @@ void VirtualMachineXML::init_attributes()
resched = 0;
}
if (get_nodes("/VM/USER_TEMPLATE", nodes) > 0)
if (get_nodes("/VM/TEMPLATE", nodes) > 0)
{
vm_template = new VirtualMachineTemplate;
@ -180,6 +180,21 @@ void VirtualMachineXML::init_attributes()
{
vm_template = 0;
}
nodes.clear();
if (get_nodes("/VM/USER_TEMPLATE", nodes) > 0)
{
user_template = new VirtualMachineTemplate;
user_template->from_xml_node(nodes[0]);
free_nodes(nodes);
}
else
{
user_template = 0;
}
}
/* -------------------------------------------------------------------------- */
@ -245,9 +260,102 @@ void VirtualMachineXML::get_requirements (int& cpu, int& memory, int& disk)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
// TODO: use VirtualMachine::isVolatile(disk)
bool isVolatile(const VectorAttribute * disk)
{
string type = disk->vector_value("TYPE");
one_util::toupper(type);
return ( type == "SWAP" || type == "FS");
}
map<int,float> VirtualMachineXML::get_storage_usage()
{
map<int, float> ds_usage;
vector<Attribute *> disks;
vector<Attribute*>::iterator it;
float size;
string st;
int ds_id;
bool clone;
// Usage for the system DS will be stored in the index 0, although
// it may not be DS 0
ds_usage[0] = 0;
vm_template->remove("DISK", disks);
for (it=disks.begin(); it != disks.end(); it++)
{
const VectorAttribute * disk = dynamic_cast<const VectorAttribute*>(*it);
if (disk == 0)
{
continue;
}
if (disk->vector_value("SIZE", size) != 0)
{
// TODO: report error?
continue;
}
if (isVolatile(disk))
{
ds_usage[0] += size;
}
else
{
if (disk->vector_value("DATASTORE_ID", ds_id) != 0)
{
// TODO: report error?
continue;
}
if (ds_usage.count(ds_id) == 0)
{
ds_usage[ds_id] = 0;
}
if (disk->vector_value("CLONE", clone) != 0)
{
continue;
}
if (clone)
{
st = disk->vector_value("CLONE_TARGET");
}
else
{
st = disk->vector_value("LN_TARGET");
}
// TODO: define constants or enum in Datastore.h ?
if (st == "SELF")
{
ds_usage[ds_id] += size;
}
else if (st == "SYSTEM")
{
ds_usage[0] += size;
} // else st == NONE
}
}
return ds_usage;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void VirtualMachineXML::log(const string &st)
{
if (vm_template == 0 || st.empty())
if (user_template == 0 || st.empty())
{
return;
}
@ -255,7 +363,7 @@ void VirtualMachineXML::log(const string &st)
oss << one_util::log_time() << " : " << st;
vm_template->replace("SCHED_MESSAGE", oss.str());
user_template->replace("SCHED_MESSAGE", oss.str());
}
/* -------------------------------------------------------------------------- */