Statistics
| Branch: | Tag: | Revision:

one / src / scheduler / include / HostXML.h @ 621a1869

History | View | Annotate | Download (6.65 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
    /**
51
     *  Tests whether a new VM can be hosted by the host or not
52
     *    @param cpu needed by the VM (percentage)
53
     *    @param mem needed by the VM (in KB)
54
     *    @param pci devices needed by the VM
55
     *    @param error error message
56
     *    @return true if the share can host the VM
57
     */
58
    bool test_capacity(long long cpu, long long mem, vector<VectorAttribute *> &pci,
59
        string & error);
60

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

    
74
    /**
75
     *  Adds a new VM to the given share by incrementing the cpu,mem and disk
76
     *  counters
77
     *    @param cpu needed by the VM (percentage)
78
     *    @param mem needed by the VM (in KB)
79
     *    @return 0 on success
80
     */
81
    void add_capacity(int vmid, long long cpu, long long mem,
82
        vector<VectorAttribute *> &p)
83
    {
84
        cpu_usage  += cpu;
85
        mem_usage  += mem;
86

    
87
        pci.add(p, vmid);
88

    
89
        running_vms++;
90
    };
91

    
92
    /**
93
     *  Deletes a VM to the given host by updating the cpu,mem and disk
94
     *  counters
95
     *    @param cpu needed by the VM (percentage)
96
     *    @param mem needed by the VM (in KB)
97
     *    @return 0 on success
98
     */
99
    void del_capacity(int cpu, int mem)
100
    {
101
        cpu_usage  -= cpu;
102
        mem_usage  -= mem;
103

    
104
        running_vms--;
105
    };
106

    
107
    /**
108
     *  Tests whether a new VM can be hosted by the local system DS or not
109
     *    @param dsid DS id
110
     *    @param vm_disk_mb System disk needed by the VM (in MB)
111
     *    @return true if the share can host the VM
112
     */
113
    bool test_ds_capacity(int dsid, long long vm_disk_mb);
114

    
115
    /**
116
     *  Adds a new VM to the given local sytem DS share by incrementing the disk
117
     *  counter
118
     *    @param dsid DS id
119
     *    @param vm_disk_mb System disk needed by the VM (in MB)
120
     */
121
    void add_ds_capacity(int dsid, long long vm_disk_mb);
122

    
123
    /**
124
     *  Search the Object for a given attribute in a set of object specific
125
     *  routes.
126
     *    @param name of the attribute
127
     *    @param value of the attribute
128
     *
129
     *    @return -1 if the element was not found
130
     */
131
    virtual int search(const char *name, std::string& value)
132
    {
133
        return __search(name, value);
134
    }
135

    
136
    virtual int search(const char *name, int& value)
137
    {
138
        return __search(name, value);
139
    }
140

    
141
    virtual int search(const char *name, float& value)
142
    {
143
        return __search(name, value);
144
    }
145

    
146
    /**
147
     *  Checks if the host is a remote public cloud
148
     *    @return true if the host is a remote public cloud
149
     */
150
    bool is_public_cloud() const
151
    {
152
        return public_cloud;
153
    }
154

    
155
    /**
156
     *  Prints the Host information to an output stream. This function is used
157
     *  for logging purposes.
158
     */
159
    friend ostream& operator<<(ostream& o, const HostXML& p);
160

    
161
private:
162
    int oid;
163
    set<int> cluster_ids;
164

    
165
    // Host share values
166
    long long mem_usage;  /**< Memory allocated to VMs (in KB)       */
167
    long long cpu_usage;  /**< CPU  allocated to VMs (in percentage) */
168

    
169
    long long max_mem;    /**< Total memory capacity (in KB)         */
170
    long long max_cpu;    /**< Total cpu capacity (in percentage)    */
171

    
172
    long long free_disk;  /**< Free disk capacity (in MB)            */
173

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

    
176
    long long running_vms; /**< Number of running VMs in this Host   */
177

    
178
    bool public_cloud;
179

    
180
    HostSharePCI pci;
181

    
182
    // Configuration attributes
183
    static const char *host_paths[]; /**< paths for search function */
184

    
185
    static int host_num_paths; /**< number of paths*/
186

    
187
    void init_attributes();
188

    
189
    /**
190
     *  Search the Object for a given attribute in a set of object specific
191
     *  routes. Overrite ObjectXML function to deal with pseudo-attributes
192
     *    - CURRENT_VMS. value is the VM ID to search in the set of VMS
193
     *    running VMs in the host. If the VM_ID is found value is not modified
194
     *    otherwise is set to -1
195
     */
196
    template<typename T>
197
    int __search(const char *name, T& value)
198
    {
199
        string s_name(name);
200

    
201
        if (s_name == "CURRENT_VMS")
202
        {
203
            typename std::vector<T>::iterator it;
204
            std::vector<T> results;
205

    
206
            xpaths(results, "/HOST/VMS/ID");
207

    
208
            for (it=results.begin(); it!=results.end(); it++)
209
            {
210
                if (*it == value)
211
                {
212
                    return 0; //VMID found in VMS value is VMID
213
                }
214
            }
215

    
216
            value = -1; //VMID not found in VMS value is -1
217

    
218
            return 0;
219
        }
220
        else
221
        {
222
            return ObjectXML::search(name, value);
223
        }
224
    };
225
};
226

    
227
#endif /* HOST_XML_H_ */