Coverage Report

Created: 2024-11-21 18:14

/root/doris/be/src/olap/page_cache.cpp
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "olap/page_cache.h"
19
20
#include <glog/logging.h>
21
22
#include <ostream>
23
24
#include "runtime/exec_env.h"
25
26
namespace doris {
27
template <typename TAllocator>
28
PageBase<TAllocator>::PageBase(size_t b, bool use_cache, segment_v2::PageTypePB page_type)
29
32.0k
        : LRUCacheValueBase(), _size(b), _capacity(b) {
30
32.0k
    if (use_cache) {
31
5.66k
        _mem_tracker_by_allocator = StoragePageCache::instance()->mem_tracker(page_type);
32
26.3k
    } else {
33
26.3k
        _mem_tracker_by_allocator = thread_context()->thread_mem_tracker_mgr->limiter_mem_tracker();
34
26.3k
    }
35
32.0k
    {
36
32.0k
        SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_mem_tracker_by_allocator);
37
32.0k
        _data = reinterpret_cast<char*>(TAllocator::alloc(_capacity, ALLOCATOR_ALIGNMENT_16));
38
32.0k
    }
39
32.0k
}
Unexecuted instantiation: _ZN5doris8PageBaseI9AllocatorILb1ELb0ELb0E22DefaultMemoryAllocatorEEC2EmbNS_10segment_v210PageTypePBE
_ZN5doris8PageBaseI9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorEEC2EmbNS_10segment_v210PageTypePBE
Line
Count
Source
29
32.0k
        : LRUCacheValueBase(), _size(b), _capacity(b) {
30
32.0k
    if (use_cache) {
31
5.66k
        _mem_tracker_by_allocator = StoragePageCache::instance()->mem_tracker(page_type);
32
26.3k
    } else {
33
26.3k
        _mem_tracker_by_allocator = thread_context()->thread_mem_tracker_mgr->limiter_mem_tracker();
34
26.3k
    }
35
32.0k
    {
36
32.0k
        SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_mem_tracker_by_allocator);
37
32.0k
        _data = reinterpret_cast<char*>(TAllocator::alloc(_capacity, ALLOCATOR_ALIGNMENT_16));
38
32.0k
    }
39
32.0k
}
40
41
template <typename TAllocator>
42
27.3k
PageBase<TAllocator>::~PageBase() {
43
27.3k
    if (_data != nullptr) {
44
27.3k
        DCHECK(_capacity != 0 && _size != 0);
45
27.3k
        SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_mem_tracker_by_allocator);
46
27.3k
        TAllocator::free(_data, _capacity);
47
27.3k
    }
48
27.3k
}
Unexecuted instantiation: _ZN5doris8PageBaseI9AllocatorILb1ELb0ELb0E22DefaultMemoryAllocatorEED2Ev
_ZN5doris8PageBaseI9AllocatorILb0ELb0ELb0E22DefaultMemoryAllocatorEED2Ev
Line
Count
Source
42
27.3k
PageBase<TAllocator>::~PageBase() {
43
27.3k
    if (_data != nullptr) {
44
27.3k
        DCHECK(_capacity != 0 && _size != 0);
45
27.3k
        SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_mem_tracker_by_allocator);
46
27.3k
        TAllocator::free(_data, _capacity);
47
27.3k
    }
48
27.3k
}
49
50
template class PageBase<Allocator<true>>;
51
template class PageBase<Allocator<false>>;
52
53
StoragePageCache* StoragePageCache::create_global_cache(size_t capacity,
54
                                                        int32_t index_cache_percentage,
55
                                                        int64_t pk_index_cache_capacity,
56
1
                                                        uint32_t num_shards) {
57
1
    return new StoragePageCache(capacity, index_cache_percentage, pk_index_cache_capacity,
58
1
                                num_shards);
59
1
}
60
61
StoragePageCache::StoragePageCache(size_t capacity, int32_t index_cache_percentage,
62
                                   int64_t pk_index_cache_capacity, uint32_t num_shards)
63
4
        : _index_cache_percentage(index_cache_percentage) {
64
4
    if (index_cache_percentage == 0) {
65
1
        _data_page_cache = std::make_unique<DataPageCache>(capacity, num_shards);
66
3
    } else if (index_cache_percentage == 100) {
67
1
        _index_page_cache = std::make_unique<IndexPageCache>(capacity, num_shards);
68
2
    } else if (index_cache_percentage > 0 && index_cache_percentage < 100) {
69
2
        _data_page_cache = std::make_unique<DataPageCache>(
70
2
                capacity * (100 - index_cache_percentage) / 100, num_shards);
71
2
        _index_page_cache = std::make_unique<IndexPageCache>(
72
2
                capacity * index_cache_percentage / 100, num_shards);
73
2
    } else {
74
0
        CHECK(false) << "invalid index page cache percentage";
75
0
    }
76
77
4
    _pk_index_page_cache = std::make_unique<PKIndexPageCache>(pk_index_cache_capacity, num_shards);
78
4
}
79
80
bool StoragePageCache::lookup(const CacheKey& key, PageCacheHandle* handle,
81
5.11k
                              segment_v2::PageTypePB page_type) {
82
5.11k
    auto* cache = _get_page_cache(page_type);
83
5.11k
    auto* lru_handle = cache->lookup(key.encode());
84
5.11k
    if (lru_handle == nullptr) {
85
4.90k
        return false;
86
4.90k
    }
87
210
    *handle = PageCacheHandle(cache, lru_handle);
88
210
    return true;
89
5.11k
}
90
91
void StoragePageCache::insert(const CacheKey& key, DataPage* data, PageCacheHandle* handle,
92
5.54k
                              segment_v2::PageTypePB page_type, bool in_memory) {
93
5.54k
    CachePriority priority = CachePriority::NORMAL;
94
5.54k
    if (in_memory) {
95
6
        priority = CachePriority::DURABLE;
96
6
    }
97
98
5.54k
    auto* cache = _get_page_cache(page_type);
99
5.54k
    auto* lru_handle = cache->insert(key.encode(), data, data->capacity(), 0, priority);
100
5.54k
    *handle = PageCacheHandle(cache, lru_handle);
101
5.54k
}
102
103
} // namespace doris