Statistics
| Branch: | Tag: | Revision:

one / src / scheduler / src / pool / HostXML.cc @ 4697f1ee

History | View | Annotate | Download (6.74 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2015, OpenNebula Project, OpenNebula Systems                */
3
/*                                                                            */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
5
/* not use this file except in compliance with the License. You may obtain    */
6
/* a copy of the License at                                                   */
7
/*                                                                            */
8
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
9
/*                                                                            */
10
/* Unless required by applicable law or agreed to in writing, software        */
11
/* distributed under the License is distributed on an "AS IS" BASIS,          */
12
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
13
/* See the License for the specific language governing permissions and        */
14
/* limitations under the License.                                             */
15
/* -------------------------------------------------------------------------- */
16

    
17
#include <math.h>
18
#include <sstream>
19
#include <stdexcept>
20
#include <iomanip>
21

    
22
#include "HostXML.h"
23
#include "NebulaUtil.h"
24
#include "NebulaLog.h"
25
#include "ObjectCollection.h"
26

    
27
/* -------------------------------------------------------------------------- */
28
/* -------------------------------------------------------------------------- */
29
int HostXML::host_num_paths =  3;
30

    
31
const char *HostXML::host_paths[] = {
32
    "/HOST/TEMPLATE/",
33
    "/HOST/HOST_SHARE/",
34
    "/HOST/"};
35

    
36
/* -------------------------------------------------------------------------- */
37
/* -------------------------------------------------------------------------- */
38

    
39
void HostXML::init_attributes()
40
{
41
    xpath(oid,         "/HOST/ID",                     -1);
42

    
43
    ObjectCollection cluster_collection("CLUSTERS");
44
    cluster_collection.from_xml(this, "/HOST/");
45

    
46
    cluster_ids = cluster_collection.clone();
47

    
48
    xpath<long long>(mem_usage, "/HOST/HOST_SHARE/MEM_USAGE",   0);
49
    xpath<long long>(cpu_usage, "/HOST/HOST_SHARE/CPU_USAGE",   0);
50
    xpath<long long>(max_mem,   "/HOST/HOST_SHARE/MAX_MEM",     0);
51
    xpath<long long>(max_cpu,   "/HOST/HOST_SHARE/MAX_CPU",     0);
52
    xpath<long long>(free_disk, "/HOST/HOST_SHARE/FREE_DISK",   0);
53
    xpath<long long>(running_vms, "/HOST/HOST_SHARE/RUNNING_VMS", 0);
54

    
55
    string public_cloud_st;
56

    
57
    xpath(public_cloud_st, "/HOST/TEMPLATE/PUBLIC_CLOUD", "");
58
    public_cloud = (one_util::toupper(public_cloud_st) == "YES");
59

    
60
    //-------------------- HostShare Datastores ------------------------------
61
    vector<string> ds_ids;
62
    vector<string> ds_free;
63

    
64
    xpaths(ds_ids, "/HOST/HOST_SHARE/DATASTORES/DS/ID");
65
    xpaths(ds_free,"/HOST/HOST_SHARE/DATASTORES/DS/FREE_MB");
66

    
67
    int id;
68
    long long disk;
69

    
70
    for (size_t i = 0; i < ds_ids.size() && i < ds_free.size(); i++)
71
    {
72
        id   = atoi(ds_ids[i].c_str());
73
        disk = atoll(ds_free[i].c_str());
74

    
75
        ds_free_disk[id] = disk;
76
    }
77

    
78
    //-------------------- HostShare PCI Devices ------------------------------
79
    vector<xmlNodePtr> content;
80

    
81
    get_nodes("/HOST/HOST_SHARE/PCI_DEVICES", content);
82

    
83
    if( !content.empty())
84
    {
85
        pci.from_xml_node(content[0]);
86

    
87
        free_nodes(content);
88

    
89
        content.clear();
90
    }
91

    
92
    //-------------------- Init search xpath routes ---------------------------
93
    ObjectXML::paths     = host_paths;
94
    ObjectXML::num_paths = host_num_paths;
95
}
96

    
97
/* -------------------------------------------------------------------------- */
98
/* -------------------------------------------------------------------------- */
99

    
100
bool HostXML::test_capacity(long long cpu, long long mem,
101
    vector<VectorAttribute *>& p, string & error)
102
{
103
    bool pci_fits = pci.test(p);
104
    bool fits     = ((max_cpu  - cpu_usage ) >= cpu) &&
105
                    ((max_mem  - mem_usage ) >= mem) &&
106
                    pci_fits;
107
    if (!fits)
108
    {
109
        if (NebulaLog::log_level() >= Log::DDEBUG)
110
        {
111
            if ( pci_fits )
112
            {
113
                ostringstream oss;
114

    
115
                oss << "Not enough capacity. "
116
                    << "Requested: "
117
                    << cpu << " CPU, "
118
                    << mem << " KB MEM; "
119
                    << "Available: "
120
                    << (max_cpu  - cpu_usage ) << " CPU, "
121
                    << (max_mem  - mem_usage ) << " KB MEM";
122

    
123
                error = oss.str();
124
            }
125
            else
126
            {
127
                error = "Unavailable PCI device.";
128
            }
129
        }
130
        else
131
        {
132
            if ( pci_fits )
133
            {
134
                error = "Not enough capacity.";
135
            }
136
            else
137
            {
138
                error = "Unavailable PCI device.";
139
            }
140
        }
141
    }
142

    
143
    return fits;
144
}
145

    
146
/* -------------------------------------------------------------------------- */
147
/* -------------------------------------------------------------------------- */
148

    
149
bool HostXML::test_ds_capacity(int dsid, long long vm_disk_mb)
150
{
151
    if (ds_free_disk.count(dsid) == 0)
152
    {
153
        ds_free_disk[dsid] = free_disk;
154
    }
155

    
156
    return (vm_disk_mb < ds_free_disk[dsid]);
157
}
158

    
159
/* -------------------------------------------------------------------------- */
160
/* -------------------------------------------------------------------------- */
161

    
162
void HostXML::add_ds_capacity(int dsid, long long vm_disk_mb)
163
{
164
    if (ds_free_disk.count(dsid) == 0)
165
    {
166
        ds_free_disk[dsid] = free_disk;
167
    }
168

    
169
    ds_free_disk[dsid] -= vm_disk_mb;
170
}
171

    
172
/* -------------------------------------------------------------------------- */
173
/* -------------------------------------------------------------------------- */
174

    
175
ostream& operator<<(ostream& o, const HostXML& p)
176
{
177
    map<int, long long>::const_iterator it;
178

    
179
    o << "ID          : " << p.oid          << endl;
180
    o << "CLUSTER_IDS : " << one_util::join(p.cluster_ids, ',') << endl;
181
    o << "MEM_USAGE   : " << p.mem_usage    << endl;
182
    o << "CPU_USAGE   : " << p.cpu_usage    << endl;
183
    o << "MAX_MEM     : " << p.max_mem      << endl;
184
    o << "MAX_CPU     : " << p.max_cpu      << endl;
185
    o << "FREE_DISK   : " << p.free_disk    << endl;
186
    o << "RUNNING_VMS : " << p.running_vms  << endl;
187
    o << "PUBLIC      : " << p.public_cloud << endl;
188

    
189
    o << endl
190
      << right << setw(5)  << "DSID" << " "
191
      << right << setw(15) << "FREE_MB" << " "
192
      << endl << setw(30) << setfill('-') << "-" << setfill (' ') << endl;
193

    
194
        for (it = p.ds_free_disk.begin() ; it != p.ds_free_disk.end() ; it++)
195
        {
196
            o << right << setw(5) << it->first << " "
197
              << right << setw(15)<< it->second<< " " <<  endl;
198
        }
199

    
200
    o << endl << p.pci;
201

    
202
    return o;
203
}