Statistics
| Branch: | Tag: | Revision:

one / src / scheduler / src / pool / HostXML.cc @ 621a1869

History | View | Annotate | Download (6.77 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 =  4;
30

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

    
37
/* -------------------------------------------------------------------------- */
38
/* -------------------------------------------------------------------------- */
39

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

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

    
47
    cluster_ids = cluster_collection.clone();
48

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

    
56
    string public_cloud_st;
57

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

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

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

    
68
    int id;
69
    long long disk;
70

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

    
76
        ds_free_disk[id] = disk;
77
    }
78

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

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

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

    
88
        free_nodes(content);
89

    
90
        content.clear();
91
    }
92

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

    
98
/* -------------------------------------------------------------------------- */
99
/* -------------------------------------------------------------------------- */
100

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

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

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

    
144
    return fits;
145
}
146

    
147
/* -------------------------------------------------------------------------- */
148
/* -------------------------------------------------------------------------- */
149

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

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

    
160
/* -------------------------------------------------------------------------- */
161
/* -------------------------------------------------------------------------- */
162

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

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

    
173
/* -------------------------------------------------------------------------- */
174
/* -------------------------------------------------------------------------- */
175

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

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

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

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

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

    
203
    return o;
204
}