~ubuntu-branches/ubuntu/precise/ceph/precise-proposed

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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software 
 * Foundation.  See file COPYING.
 * 
 */



#ifndef CEPH_LRU_H
#define CEPH_LRU_H

#include <stdint.h>

#include "common/config.h"



class LRUObject {
 private:
  LRUObject *lru_next, *lru_prev;
  bool lru_pinned;
  class LRU *lru;
  class LRUList *lru_list;

 public:
  LRUObject() {
    lru_next = lru_prev = NULL;
    lru_list = 0;
    lru_pinned = false;
    lru = 0;
  }

  // pin/unpin item in cache
  void lru_pin(); 
  void lru_unpin();
  bool lru_is_expireable() { return !lru_pinned; }

  friend class LRU;
  friend class LRUList;
};


class LRUList {
 private:
  LRUObject *head, *tail;
  uint32_t len;

 public:
  LRUList() {
    head = tail = 0;
    len = 0;
  }
  
  uint32_t  get_length() { return len; }

  LRUObject *get_head() {
    return head;
  }
  LRUObject *get_tail() {
    return tail;
  }

  void clear() {
    while (len > 0) {
      remove(get_head());
    }
  }

  void insert_head(LRUObject *o) {
    o->lru_next = head;
    o->lru_prev = NULL;
    if (head) {
      head->lru_prev = o;
    } else {
      tail = o;
    }
    head = o;
    o->lru_list = this;
    len++;
  }
  void insert_tail(LRUObject *o) {
    o->lru_next = NULL;
    o->lru_prev = tail;
    if (tail) {
      tail->lru_next = o;
    } else {
      head = o;
    }
    tail = o;
    o->lru_list = this;
    len++;
  }

  void remove(LRUObject *o) {
    assert(o->lru_list == this);
    if (o->lru_next)
      o->lru_next->lru_prev = o->lru_prev;
    else
      tail = o->lru_prev;
    if (o->lru_prev)
      o->lru_prev->lru_next = o->lru_next;
    else
      head = o->lru_next;
    o->lru_next = o->lru_prev = NULL;
    o->lru_list = 0;
    assert(len>0);
    len--;
  }
  
};


class LRU {
 protected:
  LRUList lru_top, lru_bot, lru_pintail;
  uint32_t lru_num, lru_num_pinned;
  uint32_t lru_max;   // max items
  double lru_midpoint;

  friend class LRUObject;
  //friend class MDCache; // hack
  
 public:
  LRU(int max = 0) {
    lru_num = 0;
    lru_num_pinned = 0;
    lru_midpoint = .6;
    lru_max = max;
  }

  uint32_t lru_get_size() { return lru_num; }
  uint32_t lru_get_top() { return lru_top.get_length(); }
  uint32_t lru_get_bot() { return lru_bot.get_length(); }
  uint32_t lru_get_pintail() { return lru_pintail.get_length(); }
  uint32_t lru_get_max() { return lru_max; }
  uint32_t lru_get_num_pinned() { return lru_num_pinned; }

  void lru_set_max(uint32_t m) { lru_max = m; }
  void lru_set_midpoint(float f) { lru_midpoint = f; }
  
  void lru_clear() {
    lru_top.clear();
    lru_bot.clear();
    lru_pintail.clear();
  }

  // insert at top of lru
  void lru_insert_top(LRUObject *o) {
    //assert(!o->lru_in_lru);
    //o->lru_in_lru = true;
    assert(!o->lru);
    o->lru = this;
    lru_top.insert_head( o );
    lru_num++;
    if (o->lru_pinned) lru_num_pinned++;
    lru_adjust();
  }

  // insert at mid point in lru
  void lru_insert_mid(LRUObject *o) {
    //assert(!o->lru_in_lru);
    //o->lru_in_lru = true;
    assert(!o->lru);
    o->lru = this;
    lru_bot.insert_head(o);
    lru_num++;
    if (o->lru_pinned) lru_num_pinned++;
  }

  // insert at bottom of lru
  void lru_insert_bot(LRUObject *o) {
    assert(!o->lru);
    o->lru = this;
    lru_bot.insert_tail(o);
    lru_num++;
    if (o->lru_pinned) lru_num_pinned++;
  }

  /*
  // insert at bottom of lru
  void lru_insert_pintail(LRUObject *o) {
    assert(!o->lru);
    o->lru = this;
    
    assert(o->lru_pinned);

    lru_pintail.insert_head(o);
    lru_num++;
    lru_num_pinned += o->lru_pinned;
  }
  */

  


  // adjust top/bot balance, as necessary
  void lru_adjust() {
    if (!lru_max) return;

    unsigned toplen = lru_top.get_length();
    unsigned topwant = (unsigned)(lru_midpoint * ((double)lru_max - lru_num_pinned));
    while (toplen > 0 && 
           toplen > topwant) {
      // remove from tail of top, stick at head of bot
      // FIXME: this could be way more efficient by moving a whole chain of items.

      LRUObject *o = lru_top.get_tail();
      lru_top.remove(o);
      lru_bot.insert_head(o);
      toplen--;
    }
  }


  // remove an item
  LRUObject *lru_remove(LRUObject *o) {
    // not in list
    //assert(o->lru_in_lru);
    //if (!o->lru_in_lru) return o;  // might have expired and been removed that way.
    if (!o->lru) return o;

    assert((o->lru_list == &lru_pintail) ||
           (o->lru_list == &lru_top) ||
           (o->lru_list == &lru_bot));
    o->lru_list->remove(o);

    lru_num--;
    if (o->lru_pinned) lru_num_pinned--;
    o->lru = 0;
    return o;
  }

  // touch item -- move to head of lru
  bool lru_touch(LRUObject *o) {
    lru_remove(o);
    lru_insert_top(o);
    return true;
  }

  // touch item -- move to midpoint (unless already higher)
  bool lru_midtouch(LRUObject *o) {
    if (o->lru_list == &lru_top) return false;
    
    lru_remove(o);
    lru_insert_mid(o);
    return true;
  }

  // touch item -- move to bottom
  bool lru_bottouch(LRUObject *o) {
    lru_remove(o);
    lru_insert_bot(o);
    return true;
  }

  void lru_touch_entire_pintail() {
    // promote entire pintail to the top lru
    while (lru_pintail.get_length() > 0) {
      LRUObject *o = lru_pintail.get_head();
      lru_pintail.remove(o);
      lru_top.insert_tail(o);
    }
  }


  // expire -- expire a single item
  LRUObject *lru_get_next_expire() {
    LRUObject *p;
    
    // look through tail of bot
    while (lru_bot.get_length()) {
      p = lru_bot.get_tail();
      if (!p->lru_pinned) return p;

      // move to pintail
      lru_bot.remove(p);
      lru_pintail.insert_head(p);
    }

    // ok, try head then
    while (lru_top.get_length()) {
      p = lru_top.get_tail();
      if (!p->lru_pinned) return p;

      // move to pintail
      lru_top.remove(p);
      lru_pintail.insert_head(p);
    }
    
    // no luck!
    return NULL;
  }
  
  LRUObject *lru_expire() {
    LRUObject *p = lru_get_next_expire();
    if (p) 
      return lru_remove(p);
    return NULL;
  }


  void lru_status() {
    //generic_dout(10) << "lru: " << lru_num << " items, " << lru_top.get_length() << " top, " << lru_bot.get_length() << " bot, " << lru_pintail.get_length() << " pintail" << dendl;
  }

};


inline void LRUObject::lru_pin() 
{
  lru_pinned = true;
  if (lru) lru->lru_num_pinned++;
}
inline void LRUObject::lru_unpin() {
  lru_pinned = false;
  if (lru) {
    lru->lru_num_pinned--;

    // move from pintail -> bot
    if (lru_list == &lru->lru_pintail) {
      lru->lru_pintail.remove(this);
      lru->lru_bot.insert_tail(this);
    }
  }
}

#endif