0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MemoryTracker.h
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 
27 #ifndef Hypertable_RangeServer_MemoryTracker_h
28 #define Hypertable_RangeServer_MemoryTracker_h
29 
32 
33 #include <memory>
34 #include <mutex>
35 
36 namespace Hypertable {
37 
40 
42  class MemoryTracker {
43  public:
44 
48  MemoryTracker(FileBlockCache *block_cache, QueryCachePtr query_cache)
49  : m_block_cache(block_cache), m_query_cache(query_cache) { }
50 
53  void add(int64_t amount) {
54  std::lock_guard<std::mutex> lock(m_mutex);
55  m_memory_used += amount;
56  }
57 
60  void subtract(int64_t amount) {
61  std::lock_guard<std::mutex> lock(m_mutex);
62  m_memory_used -= amount;
63  }
64 
70  int64_t balance() {
71  std::lock_guard<std::mutex> lock(m_mutex);
73  (m_query_cache ? m_query_cache->memory_used() : 0);
74  }
75 
76  private:
77 
80 
82  int64_t m_memory_used {};
83 
86 
89  };
90 
92 
93 }
94 
95 #endif // Hypertable_RangeServer_MemoryTracker_h
FileBlockCache * m_block_cache
Pointer to block cache.
Definition: MemoryTracker.h:85
static std::mutex mutex
Definition: Logger.cc:43
MemoryTracker(FileBlockCache *block_cache, QueryCachePtr query_cache)
Constructor.
Definition: MemoryTracker.h:48
int64_t m_memory_used
Current range server memory used.
Definition: MemoryTracker.h:82
Tracks range server memory used.
Definition: MemoryTracker.h:42
QueryCachePtr m_query_cache
Pointer to query cache.
Definition: MemoryTracker.h:88
std::mutex m_mutex
Mutex to serialize concurrent access
Definition: MemoryTracker.h:79
std::shared_ptr< QueryCache > QueryCachePtr
Smart pointer to QueryCache.
Definition: QueryCache.h:252
Hypertable definitions
void subtract(int64_t amount)
Subtract to memory used.
Definition: MemoryTracker.h:60
void add(int64_t amount)
Add to memory used.
Definition: MemoryTracker.h:53
int64_t balance()
Return total range server memory used.
Definition: MemoryTracker.h:70