mirror of https://github.com/trapexit/mergerfs.git
Browse Source
Merge pull request #565 from trapexit/open_cache
Merge pull request #565 from trapexit/open_cache
add policy cache for 'open'pull/567/head
trapexit
6 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
44 changed files with 853 additions and 644 deletions
-
10README.md
-
2libfuse/configure.ac
-
20man/mergerfs.1
-
2src/access.cpp
-
69src/category.cpp
-
105src/category.hpp
-
1src/chmod.cpp
-
1src/chown.cpp
-
4src/config.hpp
-
1src/getattr.cpp
-
2src/getxattr.cpp
-
1src/link.cpp
-
22src/listxattr.cpp
-
12src/open.cpp
-
16src/option_parser.cpp
-
102src/policy.cpp
-
322src/policy.hpp
-
21src/policy_all.cpp
-
128src/policy_cache.cpp
-
61src/policy_cache.hpp
-
35src/policy_epall.cpp
-
36src/policy_epff.cpp
-
36src/policy_eplfs.cpp
-
36src/policy_eplus.cpp
-
36src/policy_epmfs.cpp
-
29src/policy_eprand.cpp
-
17src/policy_erofs.cpp
-
21src/policy_ff.cpp
-
17src/policy_invalid.cpp
-
22src/policy_lfs.cpp
-
22src/policy_lus.cpp
-
22src/policy_mfs.cpp
-
35src/policy_newest.cpp
-
29src/policy_rand.cpp
-
1src/readlink.cpp
-
49src/release.cpp
-
29src/releasedir.cpp
-
1src/removexattr.cpp
-
2src/rename.cpp
-
1src/rmdir.cpp
-
11src/setxattr.cpp
-
1src/truncate.cpp
-
106src/unlink.cpp
-
1src/utimens.cpp
@ -0,0 +1,128 @@ |
|||||
|
#include "policy_cache.hpp"
|
||||
|
|
||||
|
#include <cstdlib>
|
||||
|
#include <map>
|
||||
|
#include <string>
|
||||
|
#include <vector>
|
||||
|
|
||||
|
#include <sys/time.h>
|
||||
|
|
||||
|
using std::map; |
||||
|
using std::string; |
||||
|
using std::vector; |
||||
|
|
||||
|
static const uint64_t DEFAULT_TIMEOUT = 0; |
||||
|
|
||||
|
namespace local |
||||
|
{ |
||||
|
static |
||||
|
uint64_t |
||||
|
get_time(void) |
||||
|
{ |
||||
|
uint64_t rv; |
||||
|
struct timeval now; |
||||
|
|
||||
|
::gettimeofday(&now,NULL); |
||||
|
|
||||
|
rv = now.tv_sec; |
||||
|
|
||||
|
return rv; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
PolicyCache::Value::Value() |
||||
|
: time(0), |
||||
|
path() |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
PolicyCache::PolicyCache(void) |
||||
|
: timeout(DEFAULT_TIMEOUT) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
void |
||||
|
PolicyCache::erase(const char *fusepath_) |
||||
|
{ |
||||
|
pthread_mutex_lock(&_lock); |
||||
|
|
||||
|
_cache.erase(fusepath_); |
||||
|
|
||||
|
pthread_mutex_unlock(&_lock); |
||||
|
} |
||||
|
|
||||
|
void |
||||
|
PolicyCache::cleanup(const int prob_) |
||||
|
{ |
||||
|
uint64_t now; |
||||
|
map<string,Value>::iterator i; |
||||
|
|
||||
|
if(rand() % prob_) |
||||
|
return; |
||||
|
|
||||
|
now = local::get_time(); |
||||
|
|
||||
|
pthread_mutex_lock(&_lock); |
||||
|
|
||||
|
i = _cache.begin(); |
||||
|
while(i != _cache.end()) |
||||
|
{ |
||||
|
if((now - i->second.time) >= timeout) |
||||
|
_cache.erase(i++); |
||||
|
else |
||||
|
++i; |
||||
|
} |
||||
|
|
||||
|
pthread_mutex_unlock(&_lock); |
||||
|
} |
||||
|
|
||||
|
void |
||||
|
PolicyCache::clear(void) |
||||
|
{ |
||||
|
pthread_mutex_lock(&_lock); |
||||
|
|
||||
|
_cache.clear(); |
||||
|
|
||||
|
pthread_mutex_unlock(&_lock); |
||||
|
} |
||||
|
|
||||
|
int |
||||
|
PolicyCache::operator()(Policy::Func::Search &func_, |
||||
|
const Branches &branches_, |
||||
|
const char *fusepath_, |
||||
|
const uint64_t minfreespace_, |
||||
|
std::string *branch_) |
||||
|
{ |
||||
|
int rv; |
||||
|
Value *v; |
||||
|
uint64_t now; |
||||
|
string branch; |
||||
|
|
||||
|
if(timeout == 0) |
||||
|
return func_(branches_,fusepath_,minfreespace_,branch_); |
||||
|
|
||||
|
now = local::get_time(); |
||||
|
|
||||
|
pthread_mutex_lock(&_lock); |
||||
|
v = &_cache[fusepath_]; |
||||
|
|
||||
|
if((now - v->time) >= timeout) |
||||
|
{ |
||||
|
pthread_mutex_unlock(&_lock); |
||||
|
rv = func_(branches_,fusepath_,minfreespace_,&branch); |
||||
|
if(rv == -1) |
||||
|
return -1; |
||||
|
|
||||
|
pthread_mutex_lock(&_lock); |
||||
|
v->time = now; |
||||
|
v->path = branch; |
||||
|
} |
||||
|
|
||||
|
*branch_ = v->path; |
||||
|
|
||||
|
pthread_mutex_unlock(&_lock); |
||||
|
|
||||
|
return 0; |
||||
|
} |
@ -0,0 +1,61 @@ |
|||||
|
/*
|
||||
|
ISC License |
||||
|
|
||||
|
Copyright (c) 2019, Antonio SJ Musumeci <trapexit@spawn.link> |
||||
|
|
||||
|
Permission to use, copy, modify, and/or distribute this software for any |
||||
|
purpose with or without fee is hereby granted, provided that the above |
||||
|
copyright notice and this permission notice appear in all copies. |
||||
|
|
||||
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
||||
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
||||
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
||||
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
||||
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
||||
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
||||
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
||||
|
*/ |
||||
|
|
||||
|
#pragma once
|
||||
|
|
||||
|
#include "policy.hpp"
|
||||
|
|
||||
|
#include <string>
|
||||
|
#include <map>
|
||||
|
|
||||
|
#include <pthread.h>
|
||||
|
#include <stdint.h>
|
||||
|
|
||||
|
class PolicyCache |
||||
|
{ |
||||
|
public: |
||||
|
struct Value |
||||
|
{ |
||||
|
Value(); |
||||
|
|
||||
|
uint64_t time; |
||||
|
std::string path; |
||||
|
}; |
||||
|
|
||||
|
public: |
||||
|
PolicyCache(void); |
||||
|
|
||||
|
public: |
||||
|
void erase(const char *fusepath_); |
||||
|
void cleanup(const int prob_ = 1); |
||||
|
void clear(void); |
||||
|
|
||||
|
public: |
||||
|
int operator()(Policy::Func::Search &func_, |
||||
|
const Branches &branches_, |
||||
|
const char *fusepath_, |
||||
|
const uint64_t minfreespace_, |
||||
|
std::string *branch_); |
||||
|
|
||||
|
public: |
||||
|
uint64_t timeout; |
||||
|
|
||||
|
private: |
||||
|
pthread_mutex_t _lock; |
||||
|
std::map<std::string,Value> _cache; |
||||
|
}; |
Write
Preview
Loading…
Cancel
Save
Reference in new issue