mirror of
https://github.com/ceph/ceph
synced 2025-02-19 08:57:27 +00:00
*** empty log message ***
git-svn-id: https://ceph.svn.sf.net/svnroot/ceph@4 29311d96-e01e-0410-9327-a35deaab8ce9
This commit is contained in:
parent
0ac5aed04a
commit
fa8f4df134
@ -8,6 +8,9 @@ using namespace std;
|
||||
|
||||
// ====== CInode =======
|
||||
|
||||
CInode::~CInode() {
|
||||
if (dir) { delete dir; dir = 0; }
|
||||
}
|
||||
|
||||
void CInode::add_parent(CDentry *p) {
|
||||
nparents++;
|
||||
@ -107,7 +110,9 @@ void CDir::dump(int depth) {
|
||||
map<string,CDentry*>::iterator iter = items.begin();
|
||||
while (iter != items.end()) {
|
||||
CDentry* d = iter->second;
|
||||
cout << ind << d->name << endl;
|
||||
char isdir = ' ';
|
||||
if (d->inode->dir != NULL) isdir = '/';
|
||||
cout << ind << d->inode->inode.ino << " " << d->name << isdir << endl;
|
||||
d->inode->dump(depth+1);
|
||||
iter++;
|
||||
}
|
||||
@ -125,6 +130,47 @@ void CDir::dump(int depth) {
|
||||
|
||||
// DentryCache
|
||||
|
||||
bool DentryCache::add_inode(CInode *o)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
bool DentryCache::remove_inode(CInode *o)
|
||||
{
|
||||
// detach from parents
|
||||
if (o->nparents == 1) {
|
||||
CDentry *dn = o->parent;
|
||||
dn->dir->remove_child(dn);
|
||||
delete dn;
|
||||
}
|
||||
else if (o->nparents > 1) {
|
||||
throw 1;
|
||||
}
|
||||
|
||||
// remove from map
|
||||
inode_map.erase(o->inode.ino);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DentryCache::trim(__int32_t max) {
|
||||
if (max < 0)
|
||||
max = lru->lru_get_max();
|
||||
|
||||
while (lru->lru_get_num() > max) {
|
||||
CInode *o = (CInode*)lru->lru_expire();
|
||||
if (!o) return false;
|
||||
|
||||
remove_inode(o);
|
||||
delete o;
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
CInode* DentryCache::get_file(string& fn) {
|
||||
int off = 1;
|
||||
CInode *cur = root;
|
||||
@ -149,6 +195,9 @@ CInode* DentryCache::get_file(string& fn) {
|
||||
off = slash+1;
|
||||
}
|
||||
|
||||
dump();
|
||||
lru->lru_status();
|
||||
|
||||
return cur;
|
||||
}
|
||||
|
||||
@ -184,4 +233,11 @@ void DentryCache::add_file(string& fn, CInode *in) {
|
||||
CDentry* dn = new CDentry(file, in);
|
||||
in->add_parent(dn);
|
||||
idir->dir->add_child(dn);
|
||||
lru->lru_insert_top(in);
|
||||
inode_map[ in->inode.ino ] = in;
|
||||
cout << " map size now " << inode_map.size() << endl;
|
||||
|
||||
// trim
|
||||
trim();
|
||||
|
||||
}
|
||||
|
@ -2,21 +2,11 @@
|
||||
#ifndef __MDS_H
|
||||
#define __MDS_H
|
||||
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
// raw inode
|
||||
struct inode_t {
|
||||
__uint64_t ino;
|
||||
|
||||
__uint64_t size;
|
||||
__uint32_t mode;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#include "dcache.h"
|
||||
|
||||
//
|
||||
class CInode;
|
||||
|
||||
class CMDS {
|
||||
protected:
|
||||
@ -24,13 +14,17 @@ class CMDS {
|
||||
int num_nodes;
|
||||
|
||||
// cache
|
||||
CInode *root;
|
||||
DentryCache *dc;
|
||||
|
||||
|
||||
public:
|
||||
CMDS() {
|
||||
|
||||
root = NULL;
|
||||
CMDS(int id, int num) {
|
||||
nodeid = id;
|
||||
num_nodes = num;
|
||||
dc = NULL;
|
||||
}
|
||||
~CMDS() {
|
||||
if (dc) { delete dc; dc = NULL; }
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#include "inode.h"
|
||||
#include "lru.h"
|
||||
|
||||
using namespace std;
|
||||
@ -16,8 +17,9 @@ class CDir;
|
||||
|
||||
// cached inode wrapper
|
||||
class CInode : LRUObject {
|
||||
protected:
|
||||
public:
|
||||
inode_t inode; // the inode itself
|
||||
protected:
|
||||
CDir *dir; // directory entries, if we're a directory
|
||||
|
||||
int ref; // reference count (???????)
|
||||
@ -30,7 +32,8 @@ class CInode : LRUObject {
|
||||
// dcache lru
|
||||
CInode *lru_next, *lru_prev;
|
||||
|
||||
friend class DCache;
|
||||
friend class DentryCache;
|
||||
friend class CDir;
|
||||
|
||||
public:
|
||||
CInode() : LRUObject() {
|
||||
@ -44,6 +47,9 @@ class CInode : LRUObject {
|
||||
lru_next = lru_prev = NULL;
|
||||
|
||||
}
|
||||
~CInode();
|
||||
|
||||
|
||||
|
||||
// --- reference counting
|
||||
void put() {
|
||||
@ -75,7 +81,7 @@ class CDentry {
|
||||
CInode *inode;
|
||||
CDir *dir;
|
||||
|
||||
friend class DCache;
|
||||
friend class DentryCache;
|
||||
|
||||
public:
|
||||
// cons
|
||||
@ -115,7 +121,7 @@ class CDir {
|
||||
protected:
|
||||
CInode *inode;
|
||||
|
||||
map<string, CDentry*> items;
|
||||
map<string, CDentry*> items; // use map; ordered list
|
||||
__uint64_t nitems;
|
||||
bool complete;
|
||||
|
||||
@ -141,19 +147,18 @@ class CDir {
|
||||
|
||||
class DentryCache {
|
||||
protected:
|
||||
CInode *root;
|
||||
|
||||
LRU *lru;
|
||||
|
||||
CInode *root; // root inode
|
||||
LRU *lru; // lru for expiring items
|
||||
hash_map<__uint64_t, CInode*> inode_map; // map of inodes by ino
|
||||
|
||||
public:
|
||||
DentryCache() {
|
||||
root = NULL;
|
||||
lru = new LRU();
|
||||
lru = new LRU(25);
|
||||
}
|
||||
DentryCache(CInode *r) {
|
||||
root = r;
|
||||
lru = new LRU();
|
||||
lru = new LRU(25);
|
||||
}
|
||||
~DentryCache() {
|
||||
if (lru) { delete lru; lru = NULL; }
|
||||
@ -164,9 +169,22 @@ class DentryCache {
|
||||
return root;
|
||||
}
|
||||
|
||||
// fns
|
||||
// fn
|
||||
bool trim(__int32_t max = -1); // trim cache
|
||||
bool clear() { // clear cache
|
||||
return trim(0);
|
||||
}
|
||||
|
||||
bool remove_inode(CInode *ino);
|
||||
bool add_inode(CInode *ino);
|
||||
|
||||
// crap fns
|
||||
CInode* get_file(string& fn);
|
||||
void add_file(string& fn, CInode* in);
|
||||
|
||||
void dump() {
|
||||
if (root) root->dump();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
15
ceph/include/inode.h
Normal file
15
ceph/include/inode.h
Normal file
@ -0,0 +1,15 @@
|
||||
|
||||
#ifndef __INODE_H
|
||||
#define __INODE_H
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
// raw inode
|
||||
struct inode_t {
|
||||
__uint64_t ino;
|
||||
|
||||
__uint64_t size;
|
||||
__uint32_t mode;
|
||||
};
|
||||
|
||||
#endif
|
@ -2,6 +2,8 @@
|
||||
#ifndef __LRU_H
|
||||
#define __LRU_H
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class LRUObject {
|
||||
protected:
|
||||
@ -34,13 +36,23 @@ class LRU {
|
||||
LRUObject *lru_tophead, *lru_toptail, *lru_bothead, *lru_bottail;
|
||||
__uint32_t lru_ntop, lru_nbot, lru_num;
|
||||
double lru_midpoint;
|
||||
__uint32_t lru_max; // max items
|
||||
|
||||
public:
|
||||
LRU() {
|
||||
LRU(int max) {
|
||||
lru_ntop = lru_nbot = lru_num = 0;
|
||||
lru_tophead = lru_toptail = NULL;
|
||||
lru_bothead = lru_bottail = NULL;
|
||||
lru_midpoint = .9;
|
||||
lru_max = max;
|
||||
}
|
||||
|
||||
__uint32_t lru_get_num() {
|
||||
return lru_num;
|
||||
}
|
||||
|
||||
__uint32_t lru_get_max() {
|
||||
return lru_max;
|
||||
}
|
||||
|
||||
// insert at top of lru
|
||||
@ -53,7 +65,9 @@ class LRU {
|
||||
} else {
|
||||
lru_toptail = o;
|
||||
}
|
||||
lru_tophead = o;
|
||||
lru_ntop++;
|
||||
lru_num++;
|
||||
|
||||
lru_adjust();
|
||||
}
|
||||
@ -68,7 +82,9 @@ class LRU {
|
||||
} else {
|
||||
lru_bottail = o;
|
||||
}
|
||||
lru_bothead = o;
|
||||
lru_nbot++;
|
||||
lru_num++;
|
||||
}
|
||||
|
||||
|
||||
@ -76,7 +92,7 @@ class LRU {
|
||||
|
||||
// adjust top/bot balance, as necessary
|
||||
void lru_adjust() {
|
||||
__uint32_t topwant = (__uint32_t)(lru_midpoint * (double)lru_num);
|
||||
__uint32_t topwant = (__uint32_t)(lru_midpoint * (double)lru_max);
|
||||
while (lru_ntop > 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.
|
||||
@ -88,6 +104,7 @@ class LRU {
|
||||
// remove an item
|
||||
LRUObject *lru_remove(LRUObject *o) {
|
||||
if (o->lru_in_top) {
|
||||
//cout << "removing " << o << " from top" << endl;
|
||||
// top
|
||||
if (o->lru_next)
|
||||
o->lru_next->lru_prev = o->lru_prev;
|
||||
@ -99,6 +116,7 @@ class LRU {
|
||||
lru_tophead = o->lru_next;
|
||||
lru_ntop--;
|
||||
} else {
|
||||
//cout << "removing " << o << " from bot" << endl;
|
||||
// bot
|
||||
if (o->lru_next)
|
||||
o->lru_next->lru_prev = o->lru_prev;
|
||||
@ -110,6 +128,7 @@ class LRU {
|
||||
lru_bothead = o->lru_next;
|
||||
lru_nbot--;
|
||||
}
|
||||
lru_num--;
|
||||
o->lru_next = o->lru_prev = NULL;
|
||||
return o;
|
||||
}
|
||||
@ -132,22 +151,24 @@ class LRU {
|
||||
|
||||
|
||||
// expire -- expire a single item
|
||||
LRUObject *expire() {
|
||||
LRUObject *lru_expire() {
|
||||
LRUObject *p;
|
||||
|
||||
// look through tail of bot
|
||||
p = lru_bottail;
|
||||
while (p) {
|
||||
if (p->lru_expireable)
|
||||
return lru_remove( lru_bottail );
|
||||
return lru_remove( p );
|
||||
//cout << "p " << p << " no expireable" << endl;
|
||||
p = p->lru_prev;
|
||||
}
|
||||
|
||||
// ok, try head then
|
||||
p = lru_headtail;
|
||||
p = lru_toptail;
|
||||
while (p) {
|
||||
if (p->lru_expireable)
|
||||
return lru_remove( lru_bottail );
|
||||
return lru_remove( p );
|
||||
//cout << "p " << p << " no expireable" << endl;
|
||||
p = p->lru_prev;
|
||||
}
|
||||
|
||||
@ -156,6 +177,10 @@ class LRU {
|
||||
}
|
||||
|
||||
|
||||
void lru_status() {
|
||||
cout << "lru: " << lru_num << " items, " << lru_ntop << " top, " << lru_nbot << " bot" << endl;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
@ -9,15 +9,33 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
__uint64_t ino = 1;
|
||||
|
||||
|
||||
// this parses find output
|
||||
|
||||
DentryCache *readfiles() {
|
||||
DentryCache *dc = new DentryCache(new CInode());
|
||||
|
||||
string fn;
|
||||
int offs = -1;
|
||||
while (getline(cin, fn)) {
|
||||
string relfn;
|
||||
|
||||
if (offs < 0) {
|
||||
if (fn == "/")
|
||||
offs = 0;
|
||||
else
|
||||
offs = fn.length();
|
||||
//cout << "offs is " << offs << " from " << fn << endl;
|
||||
relfn = "/";
|
||||
} else
|
||||
relfn = fn.substr(offs);
|
||||
|
||||
CInode *in = new CInode();
|
||||
dc->add_file(fn, in);
|
||||
cout << "added " << fn << endl;
|
||||
in->inode.ino = ino++;
|
||||
dc->add_file(relfn, in);
|
||||
cout << "added " << relfn << endl;
|
||||
}
|
||||
|
||||
return dc;
|
||||
@ -27,7 +45,15 @@ int main(char **argv, int argc) {
|
||||
cout << "hi there" << endl;
|
||||
|
||||
DentryCache *dc = readfiles();
|
||||
dc->get_root()->dump();
|
||||
dc->dump();
|
||||
|
||||
if (dc->clear()) {
|
||||
cout << "clean shutdown" << endl;
|
||||
dc->dump();
|
||||
delete dc;
|
||||
} else {
|
||||
throw "can't empty cache";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user