1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2013 Inktank Storage, Inc.
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #ifndef CEPH_FDCACHE_H
16 #define CEPH_FDCACHE_H
21 #include "common/hobject.h"
22 #include "common/Mutex.h"
23 #include "common/Cond.h"
24 #include "common/shared_cache.hpp"
25 #include "include/compat.h"
26 #include "include/intarith.h"
31 class FDCache : public md_config_obs_t {
36 * Wrapper for an fd. Destructor closes the fd.
41 explicit FD(int _fd) : fd(_fd) {
44 int operator*() const {
48 VOID_TEMP_FAILURE_RETRY(::close(fd));
54 const int registry_shards;
55 SharedLRU<ghobject_t, FD> *registry;
58 explicit FDCache(CephContext *cct) : cct(cct),
59 registry_shards(MAX(cct->_conf->filestore_fd_cache_shards, 1)) {
61 cct->_conf->add_observer(this);
62 registry = new SharedLRU<ghobject_t, FD>[registry_shards];
63 for (int i = 0; i < registry_shards; ++i) {
64 registry[i].set_cct(cct);
66 MAX((cct->_conf->filestore_fd_cache_size / registry_shards), 1));
70 cct->_conf->remove_observer(this);
73 typedef ceph::shared_ptr<FD> FDRef;
75 FDRef lookup(const ghobject_t &hoid) {
76 int registry_id = hoid.hobj.get_hash() % registry_shards;
77 return registry[registry_id].lookup(hoid);
80 FDRef add(const ghobject_t &hoid, int fd, bool *existed) {
81 int registry_id = hoid.hobj.get_hash() % registry_shards;
82 return registry[registry_id].add(hoid, new FD(fd), existed);
85 /// clear cached fd for hoid, subsequent lookups will get an empty FD
86 void clear(const ghobject_t &hoid) {
87 int registry_id = hoid.hobj.get_hash() % registry_shards;
88 registry[registry_id].purge(hoid);
92 const char** get_tracked_conf_keys() const override {
93 static const char* KEYS[] = {
94 "filestore_fd_cache_size",
99 void handle_conf_change(const md_config_t *conf,
100 const std::set<std::string> &changed) override {
101 if (changed.count("filestore_fd_cache_size")) {
102 for (int i = 0; i < registry_shards; ++i)
103 registry[i].set_size(
104 MAX((conf->filestore_fd_cache_size / registry_shards), 1));
109 typedef FDCache::FDRef FDRef;