GCC Code Coverage Report


Directory: src/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 59.0% 46 / 0 / 78
Functions: 50.0% 9 / 0 / 18
Branches: 23.1% 30 / 0 / 130

memory/allocators/r2_slab_allocator.h
Line Branch Exec Source
1 #pragma once
2
3 #include <memory>
4 #include <string>
5 #include <vector>
6
7 #include "memory/malloc.h"
8 #include "memory/shm_file.h"
9 #include "base/factory.h"
10 #include "memory/allocators/r2/allocator_master.hh"
11
12 namespace base {
13 class R2alloc : public MallocApi {
14 public:
15 // filename: 文件名, 内存大小
16 368 ~R2alloc() override { LOG(INFO) << "~R2alloc() called"; }
17 184 R2alloc(const std::string& filename,
18 int64 memory_size,
19 std::string medium = "DRAM")
20 184 : memory_size_(memory_size) {
21
6/12
✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 184 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 184 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 184 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 184 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 184 times.
✗ Branch 17 not taken.
368 LOG(INFO) << "filename:" << filename << ", memory_size:" << memory_size
22
2/4
✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 184 times.
✗ Branch 5 not taken.
184 << ", medium:" << medium;
23
1/2
✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
184 bool file_exists = base::file_util::PathExists(filename);
24 shm_file_ =
25
2/4
✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 184 times.
✗ Branch 5 not taken.
184 ShmFile::New(ShmFile::ConfigForMedium(medium, filename, memory_size));
26
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 184 times.
184 if (!shm_file_) {
27 file_exists = false;
28 CHECK(base::file_util::Delete(filename, false));
29 shm_file_ =
30 ShmFile::New(ShmFile::ConfigForMedium(medium, filename, memory_size));
31 CHECK(shm_file_) << filename << " " << memory_size;
32 }
33
34 184 data_ = shm_file_->Data();
35
4/8
✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 184 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 184 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 184 times.
✗ Branch 11 not taken.
184 LOG(INFO) << "init data_ addr:" << static_cast<void*>(data_);
36 // Use the full mapped budget for the allocator. Using half here can make
37 // concurrent arena creation fail under moderate write concurrency.
38
1/2
✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
184 R2AllocMaster.init(data_, memory_size);
39 184 healthy_used_ = memory_size;
40 184 load_success_ = file_exists;
41 184 Initialize();
42
1/2
✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
184 if (!file_exists) {
43
3/6
✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 184 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 184 times.
✗ Branch 8 not taken.
184 LOG(INFO) << "R2malloc: first initialization.";
44 } else {
45 LOG(INFO) << "R2malloc: Recovery from shutdown.";
46 }
47 184 }
48
49 int64 DataBaseOffset() const { return 0; }
50 char* BackingData() const override { return data_; }
51 int64 BackingSize() const override { return memory_size_; }
52
53 32738 char* New(int memory_size) {
54 32738 r2::Allocator* alloc = R2AllocMaster.get_thread_allocator();
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32738 times.
32738 if (!alloc) {
56 return nullptr;
57 }
58 32738 void* p = alloc->alloc(memory_size);
59 32738 return static_cast<char*>(p);
60 }
61
62 10462 bool Free(void* memory_data) {
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
10462 if (!memory_data)
64 return true;
65 10462 r2::Allocator* alloc = R2AllocMaster.get_thread_allocator();
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
10462 if (!alloc)
67 return false;
68 10462 alloc->dealloc(memory_data);
69 10462 return true;
70 }
71
72 bool load_success() const { return load_success_; }
73 void GetMallocsAppend(std::vector<char*>* mallocs_data) const {
74 // TODO
75 LOG(INFO) << "NOT IMPLEMENT!";
76 return;
77 }
78 void GetMallocsAppend(std::vector<int64>* mallocs_offset) const {
79 // TODO
80 LOG(INFO) << " NOT IMPLEMENT!";
81 return;
82 }
83 std::string GetInfo() const {
84 // TODO
85 std::string info;
86 info.append(base::StringPrintf("total_malloc: %ld\n", total_malloc_));
87 return info;
88 }
89 368 void Initialize() {
90 368 total_used_ = 0;
91 368 total_malloc_ = 0;
92 368 }
93 // 一共使用了多少 8 字节的 block
94 int64 total_used() const { return total_used_; }
95 // 一共分配了多少块内存, 和 GetUsedBlockAppend 对应
96 uint64 total_malloc() const { return total_malloc_; }
97 bool Healthy() const { return total_used_ <= healthy_used_; }
98
99 103006 char* GetMallocData(int64 offset) const {
100
2/4
✓ Branch 0 taken 103006 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 103006 times.
103006 if (offset > memory_size_ || offset < 0) {
101 LOG(INFO) << "GetMallocData offset invalid:" << offset;
102 return NULL;
103 }
104 103006 return data_ + offset;
105 }
106
107 33164 int GetMallocSize(int64 offset) const {
108 33164 char* data = GetMallocData(offset);
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33164 times.
33164 if (data == NULL)
110 return -1;
111 else
112 33164 return GetMallocSize(data);
113 }
114
115 32738 int64 GetMallocOffset(const char* data) const {
116 32738 int64 offset = data - data_;
117
3/6
✓ Branch 0 taken 32738 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32738 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 32738 times.
32738 if (offset < 0 || offset > memory_size_ || (offset & 7) != 0) {
118 LOG(INFO) << "GetMallocOffset offset invalid:" << offset;
119 return -1;
120 }
121 32738 return offset;
122 }
123
124 33164 int GetMallocSize(const char* data) const {
125 33164 int malloc_size = static_cast<int>(je_malloc_usable_size((void*)data));
126 33164 return malloc_size;
127 }
128
129 void AddMallocs4Recovery(int64_t shm_offset) {
130 LOG(INFO) << " NOT IMPLEMENT!";
131 return;
132 }
133
134 private:
135 std::unique_ptr<ShmFile> shm_file_;
136 char* data_;
137 int64 memory_size_;
138 int64 total_used_;
139 int64 total_malloc_;
140 int64 healthy_used_;
141 bool load_success_;
142
143 DISALLOW_COPY_AND_ASSIGN(R2alloc);
144 r2::AllocatorMaster R2AllocMaster;
145 };
146 FACTORY_REGISTER(MallocApi,
147 R2ShmMalloc, // 注册名(通常同类名)
148 R2alloc, // 具体类型
149 const std::string&,
150 int64,
151 const std::string&);
152 FACTORY_REGISTER(
153 MallocApi, R2_SLAB, R2alloc, const std::string&, int64, const std::string&);
154 } // namespace base
155