Statistics
| Branch: | Tag: | Revision:

one / src / scheduler / include / HostXML.h @ 4697f1ee

History | View | Annotate | Download (6.95 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

    
18
#ifndef HOST_XML_H_
19
#define HOST_XML_H_
20

    
21
#include <map>
22
#include "ObjectXML.h"
23
#include "HostShare.h"
24

    
25
using namespace std;
26

    
27
class HostXML : public ObjectXML
28
{
29
public:
30
    HostXML(const string &xml_doc):ObjectXML(xml_doc)
31
    {
32
        init_attributes();
33
    };
34

    
35
    HostXML(const xmlNodePtr node):ObjectXML(node)
36
    {
37
        init_attributes();
38
    };
39

    
40
    int get_hid() const
41
    {
42
        return oid;
43
    };
44

    
45
    set<int> get_cids() const
46
    {
47
        return cluster_ids;
48
    };
49

    
50
    bool is_in_cluster(const set<int> &cids) const
51
    {
52
        set<int>::const_iterator i;
53

    
54
        for (i = cids.begin(); i != cids.end(); i++)
55
        {
56
            if (cluster_ids.find(*i) != cluster_ids.end())
57
            {
58
                return true;
59
            }
60
        }
61

    
62
        return false;
63
    };
64

    
65
    /**
66
     *  Tests whether a new VM can be hosted by the host or not
67
     *    @param cpu needed by the VM (percentage)
68
     *    @param mem needed by the VM (in KB)
69
     *    @param pci devices needed by the VM
70
     *    @param error error message
71
     *    @return true if the share can host the VM
72
     */
73
    bool test_capacity(long long cpu, long long mem, vector<VectorAttribute *> &pci,
74
        string & error);
75

    
76
    /**
77
     *  Tests whether a new VM can be hosted by the host or not
78
     *    @param cpu needed by the VM (percentage)
79
     *    @param mem needed by the VM (in KB)
80
     *    @param pci devices needed by the VM
81
     *    @return true if the share can host the VM
82
     */
83
    bool test_capacity(long long cpu,long long mem,vector<VectorAttribute *> &p)
84
    {
85
        string tmp_st;
86
        return test_capacity(cpu, mem, p, tmp_st);
87
    };
88

    
89
    /**
90
     *  Adds a new VM to the given share by incrementing the cpu,mem and disk
91
     *  counters
92
     *    @param cpu needed by the VM (percentage)
93
     *    @param mem needed by the VM (in KB)
94
     *    @return 0 on success
95
     */
96
    void add_capacity(int vmid, long long cpu, long long mem,
97
        vector<VectorAttribute *> &p)
98
    {
99
        cpu_usage  += cpu;
100
        mem_usage  += mem;
101

    
102
        pci.add(p, vmid);
103

    
104
        running_vms++;
105
    };
106

    
107
    /**
108
     *  Deletes a VM to the given host by updating the cpu,mem and disk
109
     *  counters
110
     *    @param cpu needed by the VM (percentage)
111
     *    @param mem needed by the VM (in KB)
112
     *    @return 0 on success
113
     */
114
    void del_capacity(int cpu, int mem)
115
    {
116
        cpu_usage  -= cpu;
117
        mem_usage  -= mem;
118

    
119
        running_vms--;
120
    };
121

    
122
    /**
123
     *  Tests whether a new VM can be hosted by the local system DS or not
124
     *    @param dsid DS id
125
     *    @param vm_disk_mb System disk needed by the VM (in MB)
126
     *    @return true if the share can host the VM
127
     */
128
    bool test_ds_capacity(int dsid, long long vm_disk_mb);
129

    
130
    /**
131
     *  Adds a new VM to the given local sytem DS share by incrementing the disk
132
     *  counter
133
     *    @param dsid DS id
134
     *    @param vm_disk_mb System disk needed by the VM (in MB)
135
     */
136
    void add_ds_capacity(int dsid, long long vm_disk_mb);
137

    
138
    /**
139
     *  Search the Object for a given attribute in a set of object specific
140
     *  routes.
141
     *    @param name of the attribute
142
     *    @param value of the attribute
143
     *
144
     *    @return -1 if the element was not found
145
     */
146
    virtual int search(const char *name, std::string& value)
147
    {
148
        return __search(name, value);
149
    }
150

    
151
    virtual int search(const char *name, int& value)
152
    {
153
        return __search(name, value);
154
    }
155

    
156
    virtual int search(const char *name, float& value)
157
    {
158
        return __search(name, value);
159
    }
160

    
161
    /**
162
     *  Checks if the host is a remote public cloud
163
     *    @return true if the host is a remote public cloud
164
     */
165
    bool is_public_cloud() const
166
    {
167
        return public_cloud;
168
    }
169

    
170
    /**
171
     *  Prints the Host information to an output stream. This function is used
172
     *  for logging purposes.
173
     */
174
    friend ostream& operator<<(ostream& o, const HostXML& p);
175

    
176
private:
177
    int oid;
178
    set<int> cluster_ids;
179

    
180
    // Host share values
181
    long long mem_usage;  /**< Memory allocated to VMs (in KB)       */
182
    long long cpu_usage;  /**< CPU  allocated to VMs (in percentage) */
183

    
184
    long long max_mem;    /**< Total memory capacity (in KB)         */
185
    long long max_cpu;    /**< Total cpu capacity (in percentage)    */
186

    
187
    long long free_disk;  /**< Free disk capacity (in MB)            */
188

    
189
    map<int, long long> ds_free_disk; /**< Free MB for local system DS */
190

    
191
    long long running_vms; /**< Number of running VMs in this Host   */
192

    
193
    bool public_cloud;
194

    
195
    HostSharePCI pci;
196

    
197
    // Configuration attributes
198
    static const char *host_paths[]; /**< paths for search function */
199

    
200
    static int host_num_paths; /**< number of paths*/
201

    
202
    void init_attributes();
203

    
204
    /**
205
     *  Search the Object for a given attribute in a set of object specific
206
     *  routes. Overrite ObjectXML function to deal with pseudo-attributes
207
     *    - CURRENT_VMS. value is the VM ID to search in the set of VMS
208
     *    running VMs in the host. If the VM_ID is found value is not modified
209
     *    otherwise is set to -1
210
     */
211
    template<typename T>
212
    int __search(const char *name, T& value)
213
    {
214
        string s_name(name);
215

    
216
        if (s_name == "CURRENT_VMS")
217
        {
218
            typename std::vector<T>::iterator it;
219
            std::vector<T> results;
220

    
221
            xpaths(results, "/HOST/VMS/ID");
222

    
223
            for (it=results.begin(); it!=results.end(); it++)
224
            {
225
                if (*it == value)
226
                {
227
                    return 0; //VMID found in VMS value is VMID
228
                }
229
            }
230

    
231
            value = -1; //VMID not found in VMS value is -1
232

    
233
            return 0;
234
        }
235
        else
236
        {
237
            return ObjectXML::search(name, value);
238
        }
239
    };
240
};
241

    
242
#endif /* HOST_XML_H_ */