]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/PriorityCache.h
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / common / PriorityCache.h
CommitLineData
91327a77
AA
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) 2018 Red Hat
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_PRIORITY_CACHE_H
16#define CEPH_PRIORITY_CACHE_H
17
18#include <stdint.h>
19#include <string>
eafe8130
TL
20#include <vector>
21#include <memory>
22#include <unordered_map>
23#include "common/perf_counters.h"
24#include "include/ceph_assert.h"
91327a77
AA
25
26namespace PriorityCache {
eafe8130
TL
27 // Reserve 16384 slots for PriorityCache perf counters
28 const int PERF_COUNTER_LOWER_BOUND = 1073741824;
29 const int PERF_COUNTER_MAX_BOUND = 1073758208;
30
31 enum MallocStats {
32 M_FIRST = PERF_COUNTER_LOWER_BOUND,
33 M_TARGET_BYTES,
34 M_MAPPED_BYTES,
35 M_UNMAPPED_BYTES,
36 M_HEAP_BYTES,
37 M_CACHE_BYTES,
38 M_LAST,
39 };
40
91327a77 41 enum Priority {
eafe8130
TL
42 PRI0,
43 PRI1,
44 PRI2,
45 PRI3,
46 PRI4,
47 PRI5,
48 PRI6,
49 PRI7,
50 PRI8,
51 PRI9,
52 PRI10,
53 PRI11,
54 LAST = PRI11,
55 };
56
57 enum Extra {
58 E_RESERVED = Priority::LAST+1,
59 E_COMMITTED,
60 E_LAST = E_COMMITTED,
91327a77
AA
61 };
62
11fdf7f2 63 int64_t get_chunk(uint64_t usage, uint64_t total_bytes);
91327a77
AA
64
65 struct PriCache {
66 virtual ~PriCache();
67
11fdf7f2
TL
68 /* Ask the cache to request memory for the given priority. Note that the
69 * cache may ultimately be allocated less memory than it requests here.
91327a77 70 */
11fdf7f2 71 virtual int64_t request_cache_bytes(PriorityCache::Priority pri, uint64_t total_cache) const = 0;
91327a77
AA
72
73 // Get the number of bytes currently allocated to the given priority.
74 virtual int64_t get_cache_bytes(PriorityCache::Priority pri) const = 0;
75
76 // Get the number of bytes currently allocated to all priorities.
77 virtual int64_t get_cache_bytes() const = 0;
78
79 // Allocate bytes for a given priority.
80 virtual void set_cache_bytes(PriorityCache::Priority pri, int64_t bytes) = 0;
81
82 // Allocate additional bytes for a given priority.
83 virtual void add_cache_bytes(PriorityCache::Priority pri, int64_t bytes) = 0;
84
11fdf7f2
TL
85 /* Commit the current number of bytes allocated to the cache. Space is
86 * allocated in chunks based on the allocation size and current total size
87 * of memory available for caches. */
88 virtual int64_t commit_cache_size(uint64_t total_cache) = 0;
89
90 /* Get the current number of bytes allocated to the cache. this may be
91 * larger than the value returned by get_cache_bytes as it includes extra
92 * space for future growth. */
93 virtual int64_t get_committed_size() const = 0;
91327a77
AA
94
95 // Get the ratio of available memory this cache should target.
96 virtual double get_cache_ratio() const = 0;
97
98 // Set the ratio of available memory this cache should target.
99 virtual void set_cache_ratio(double ratio) = 0;
100
101 // Get the name of this cache.
102 virtual std::string get_cache_name() const = 0;
103 };
eafe8130
TL
104
105 class Manager {
106 CephContext* cct = nullptr;
107 PerfCounters* logger;
108 std::unordered_map<std::string, PerfCounters*> loggers;
109 std::unordered_map<std::string, std::vector<int>> indexes;
110 std::unordered_map<std::string, std::shared_ptr<PriCache>> caches;
111
112 // Start perf counter slots after the malloc stats.
113 int cur_index = MallocStats::M_LAST;
114
115 uint64_t min_mem = 0;
116 uint64_t max_mem = 0;
117 uint64_t target_mem = 0;
118 uint64_t tuned_mem = 0;
119 bool reserve_extra;
f67539c2 120 std::string name;
eafe8130
TL
121 public:
122 Manager(CephContext *c, uint64_t min, uint64_t max, uint64_t target,
f67539c2 123 bool reserve_extra, const std::string& name = std::string());
eafe8130
TL
124 ~Manager();
125 void set_min_memory(uint64_t min) {
126 min_mem = min;
127 }
128 void set_max_memory(uint64_t max) {
129 max_mem = max;
130 }
131 void set_target_memory(uint64_t target) {
132 target_mem = target;
133 }
134 uint64_t get_tuned_mem() const {
135 return tuned_mem;
136 }
137 void insert(const std::string& name, const std::shared_ptr<PriCache> c,
138 bool enable_perf_counters);
139 void erase(const std::string& name);
140 void clear();
141 void tune_memory();
142 void balance();
143
144 private:
145 void balance_priority(int64_t *mem_avail, Priority pri);
146 };
91327a77
AA
147}
148
149#endif