]> git.proxmox.com Git - ceph.git/blob - ceph/src/os/filestore/FDCache.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / os / filestore / FDCache.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2013 Inktank Storage, Inc.
7 *
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.
12 *
13 */
14
15 #ifndef CEPH_FDCACHE_H
16 #define CEPH_FDCACHE_H
17
18 #include <memory>
19 #include <errno.h>
20 #include <cstdio>
21 #include "common/config_obs.h"
22 #include "common/hobject.h"
23 #include "common/shared_cache.hpp"
24 #include "include/compat.h"
25 #include "include/intarith.h"
26
27 /**
28 * FD Cache
29 */
30 class FDCache : public md_config_obs_t {
31 public:
32 /**
33 * FD
34 *
35 * Wrapper for an fd. Destructor closes the fd.
36 */
37 class FD {
38 public:
39 const int fd;
40 explicit FD(int _fd) : fd(_fd) {
41 ceph_assert(_fd >= 0);
42 }
43 int operator*() const {
44 return fd;
45 }
46 ~FD() {
47 VOID_TEMP_FAILURE_RETRY(::close(fd));
48 }
49 };
50
51 private:
52 CephContext *cct;
53 const int registry_shards;
54 SharedLRU<ghobject_t, FD> *registry;
55
56 public:
57 explicit FDCache(CephContext *cct) : cct(cct),
58 registry_shards(std::max<int64_t>(cct->_conf->filestore_fd_cache_shards, 1)) {
59 ceph_assert(cct);
60 cct->_conf.add_observer(this);
61 registry = new SharedLRU<ghobject_t, FD>[registry_shards];
62 for (int i = 0; i < registry_shards; ++i) {
63 registry[i].set_cct(cct);
64 registry[i].set_size(
65 std::max<int64_t>((cct->_conf->filestore_fd_cache_size / registry_shards), 1));
66 }
67 }
68 ~FDCache() override {
69 cct->_conf.remove_observer(this);
70 delete[] registry;
71 }
72 typedef std::shared_ptr<FD> FDRef;
73
74 FDRef lookup(const ghobject_t &hoid) {
75 int registry_id = hoid.hobj.get_hash() % registry_shards;
76 return registry[registry_id].lookup(hoid);
77 }
78
79 FDRef add(const ghobject_t &hoid, int fd, bool *existed) {
80 int registry_id = hoid.hobj.get_hash() % registry_shards;
81 return registry[registry_id].add(hoid, new FD(fd), existed);
82 }
83
84 /// clear cached fd for hoid, subsequent lookups will get an empty FD
85 void clear(const ghobject_t &hoid) {
86 int registry_id = hoid.hobj.get_hash() % registry_shards;
87 registry[registry_id].purge(hoid);
88 }
89
90 /// md_config_obs_t
91 const char** get_tracked_conf_keys() const override {
92 static const char* KEYS[] = {
93 "filestore_fd_cache_size",
94 NULL
95 };
96 return KEYS;
97 }
98 void handle_conf_change(const ConfigProxy& conf,
99 const std::set<std::string> &changed) override {
100 if (changed.count("filestore_fd_cache_size")) {
101 for (int i = 0; i < registry_shards; ++i)
102 registry[i].set_size(
103 std::max<int64_t>((conf->filestore_fd_cache_size / registry_shards), 1));
104 }
105 }
106
107 };
108 typedef FDCache::FDRef FDRef;
109
110 #endif