Statistics
| Branch: | Tag: | Revision:

one / src / scheduler / include / SchedulerPolicy.h @ 1f571426

History | View | Annotate | Download (3.62 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2017, 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
#ifndef SCHEDULER_POLICY_H_
18
#define SCHEDULER_POLICY_H_
19

    
20
#include "ObjectXML.h"
21

    
22
#include <cmath>
23
#include <algorithm>
24

    
25
using namespace std;
26

    
27
/**
28
 *  Abstract class that represents a Scheduling policy
29
 */
30
class SchedulerPolicy
31
{
32
public:
33
    SchedulerPolicy(float w=1.0):sw(w){};
34

    
35
    virtual ~SchedulerPolicy(){};
36

    
37
    /**
38
     *  Main interface for the class schedule the objects applying the policy.
39
     *  It returns a reference to a vector of priorities for each "schedulable"
40
     *  object.
41
     *    @param obj, pointer to the object to schedule
42
     *
43
     */
44
    const void schedule(ObjectXML * obj)
45
    {
46
        vector<float> priority;
47
        const vector<Resource *> resources = get_match_resources(obj);
48

    
49
        if (resources.empty())
50
        {
51
            return;
52
        }
53

    
54
        //1. Compute priorities
55
        policy(obj, priority);
56

    
57
        //2. Scale priorities
58
        sw.max = fabs(*max_element(priority.begin(), priority.end(), abs_cmp));
59

    
60
        transform(priority.begin(), priority.end(), priority.begin(), sw);
61

    
62
        //3. Aggregate to other policies
63
        for (unsigned int i=0; i< resources.size(); i++)
64
        {
65
            resources[i]->priority += priority[i];
66
        }
67
    };
68

    
69
protected:
70

    
71
    /**
72
     *  Get the vector of matched resources for the Object being schedule
73
     *    @param obj pointer to the object
74
     *    @return a reference to the vector
75
     */
76
     virtual const vector<Resource *> get_match_resources(ObjectXML *obj) = 0;
77

    
78
    /**
79
     *  Implements the actual schedule by computing the priority of each
80
     *  matching resource.
81
     */
82
    virtual void policy(ObjectXML * obj, vector<float>& priority) = 0;
83

    
84
private:
85
    /**
86
     *  ABS compare to sort priorities
87
     */
88
    static bool abs_cmp(float fl1, float fl2)
89
    {
90
        return fabs(fl1)<fabs(fl2);
91
    };
92

    
93
    /**
94
     *  Private class to scale priorities on resources. Each resource has a
95
     *  priority assgined by a policy, in order to sort and combine policies
96
     *  priorities are scaled to 1.0 and weighted.
97
     */
98
    class ScaleWeight
99
    {
100
    public:
101
        ScaleWeight(float _weight):weight(_weight){};
102

    
103
        ~ScaleWeight(){};
104

    
105
        float operator() (float pr)
106
        {
107
            if ( max == 0 )
108
            {
109
                return 0;
110
            }
111
            else
112
            {
113
                return weight * pr / max;
114
            }
115
        };
116

    
117
        float weight;
118

    
119
        float max;
120
    } sw;
121
};
122

    
123
#endif /*SCHEDULER_POLICY_H_*/