1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
// -*- encoding: utf-8; -*- 
 
#pike __REAL_VERSION__ 
 
protected inherit .Heap; 
 
//! This class implements a quantized resource scheduler. 
//! 
//! Weighted consumers are added to the scheduler with @[add()], 
//! which returns a @[Consumer] object. 
//! 
//! When there's some of the resource available to be consumed 
//! the resource owner calls @[get()], which returns the 
//! @[Consumer] that is to use the resource. @[Consumer()->consume()] 
//! is then called with the fraction of the quanta that was consumed 
//! (typically @expr{1.0@}). The amount of resources allocated to a 
//! consumer is proportional to the weight of the consumer. 
//! 
//! A consumer may be temporarily deactivated (in which case it won't 
//! be returned by @[get()], but still retain its share of the resource 
//! which will be provided later by @[get()] when it has been reactivated.  
 
// This is an offset from the "true" priority value that is used to 
// keep the priority values small enough to save on the mantissa. 
protected int normalization_offset = 0; 
 
protected void renormalize_priorities() 
{ 
  int i; 
  normalization_offset += 256; 
  for (i = 0; i < Heap::num_values; i++) { 
    Heap::values[i]->pri -= 256.0; 
    Heap::values[i]->offset = normalization_offset; 
  } 
} 
 
protected enum ConsumerState 
{ 
  STATE_ACTIVE          = 1, 
} 
 
//! A resource consumer. 
//! 
//! Active consumers are kept in a (min-)@[Heap]. 
class Consumer { 
  inherit Element; 
 
  protected int|float weight_; 
 
  // 
  // We distribute quotas according to the Sainte-laguë method, 
  // using an inverted and scaled quotient, and with fractional 
  // actual quotas. 
  // 
 
  float pri = 0.0; 
  //! Accumulated deltas and initial priority. 
  //! 
  //! Typically in the range @expr{0.0 .. 2.0@}, but may temporarily 
  //! be outside of the range. 
 
  int offset; 
 
  ConsumerState state; 
 
  // NB: Negative and zero quanta indicate that it should be recalculated. 
  float quanta = 0.0; 
 
  //! 
  protected void create(int|float weight, mixed v) 
  { 
    weight_ = weight; 
    value = v; 
    quanta = 1.0/weight; 
 
    // Initialize to half a quanta. 
    pri = quanta/2.0; 
    if (Heap::_sizeof()) { 
      // Adjust the priority as if we've been active from the beginning. 
      // Otherwise we'll get an unfair amount of the resource until 
      // we reach this point. The element on the top of the heap is 
      // representative of the accumulated consumption so far. 
      Consumer c = Heap::low_peek(); 
      pri += c->pri - c->quanta/2.0; 
    } else if (normalization_offset) { 
      pri -= (float)normalization_offset; 
    } 
 
    offset = normalization_offset; 
  } 
 
  protected void adjust() 
  { 
    if (!(state & STATE_ACTIVE)) return; 
    Heap::adjust(this); 
  } 
 
  //! Consume some of the resource. 
  //! 
  //! @param delta 
  //!   Share of the resource quanta that was actually consumed. 
  //!   Typically @expr{1.0@}, but other values are supported. 
  //! 
  //! This causes the consumer to be reprioritized. 
  void consume(float delta) 
  { 
    pri += delta * quanta; 
    adjust(); 
    if (pri > 256.0) { 
      renormalize_priorities(); 
    } 
  } 
 
  //! The weight of the consumer. 
  void `weight=(int|float weight) 
  { 
    int|float old_quanta = quanta; 
    weight_ = weight; 
    quanta = 1.0/weight; 
    pri += (quanta - old_quanta)/2.0; 
    adjust(); 
  } 
 
  //! Get the weight of the consumer. 
  int|float `weight() { return weight_; } 
 
  protected int `<(object o) { return pri<o->pri; } 
  protected int `>(object o) { return pri>o->pri; } 
 
  protected string _sprintf(int c) 
  { 
    return sprintf("Consumer(%O [pri: %O, q: %O, s:%s], %O)", 
                   weight_, pri, quanta, (state & STATE_ACTIVE)?"A":"", value); 
  } 
} 
 
//! (Re-)activate a @[Consumer]. 
Consumer add(Consumer c) 
{ 
  if (c->state & STATE_ACTIVE) { 
    return c; 
  } 
  c->pri -= (float)(normalization_offset - c->offset); 
  c->offset = normalization_offset; 
  c->state |= STATE_ACTIVE; 
  Heap::push(c); 
  return c; 
} 
 
//! Create a @[Consumer] with the weight @[weight] for the value @[val], 
//! and add it to the Scheduler. 
variant Consumer add(int|float weight, mixed val) 
{ 
  return add(Consumer(weight, val)); 
} 
 
//! Adjust the weight value @[new_weight] of the @[Consumer] @[c] in the 
//! scheduling table. 
void adjust_weight(Consumer c, int new_weight) 
{ 
  c->set_weight(new_weight); 
} 
 
//! Remove the @[Consumer] @[c] from the set of active consumers. 
//! 
//! The consumer may be reactivated by calling @[add()]. 
void remove(Consumer c) 
{ 
  if (c->state & STATE_ACTIVE) { 
    Heap::remove(c); 
    c->state &= ~STATE_ACTIVE; 
  } 
} 
 
//! Returns the next @[Consumer] to consume some of the resource. 
//! 
//! @returns 
//!   Returns a @[Consumer] if there are any active @[Consumers] 
//!   and @[UNDEFINED] otherwise. 
//! 
//! @note 
//!   The same @[Consumer] will be returned until it has either 
//!   consumed some of the resource, been removed or another 
//!   @[Consumer] with lower priority has been added. 
Consumer get() { 
  return Heap::low_peek(); 
}