Skip to content

Commit b01f636

Browse files
author
lijiachen
committed
hotness management for gc
1 parent b53b23a commit b01f636

File tree

12 files changed

+431
-2
lines changed

12 files changed

+431
-2
lines changed

ucm/store/infra/file/ifile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class IFile {
7070
virtual Status MMap(void*& addr, size_t size, bool write, bool read, bool shared) = 0;
7171
virtual void MUnmap(void* addr, size_t size) = 0;
7272
virtual void ShmUnlink() = 0;
73+
virtual Status UpdateTime() = 0;
7374

7475
private:
7576
std::string path_;

ucm/store/infra/file/posix_file.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <sys/mman.h>
2626
#include <sys/stat.h>
2727
#include <sys/xattr.h>
28+
#include <utime.h>
2829
#include <unistd.h>
2930
#include "logger/logger.h"
3031

@@ -230,4 +231,15 @@ void PosixFile::ShmUnlink()
230231
}
231232
}
232233

234+
Status PosixFile::UpdateTime()
235+
{
236+
auto ret = utime(this->Path().c_str(), nullptr);
237+
auto eno = errno;
238+
if (ret != 0) {
239+
UC_ERROR("Failed({},{}) to update time file({}).", ret, eno, this->Path());
240+
return Status::OsApiError();
241+
}
242+
return Status::OK();
243+
}
244+
233245
} // namespace UC

ucm/store/infra/file/posix_file.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class PosixFile : public IFile {
4747
Status MMap(void*& addr, size_t size, bool write, bool read, bool shared) override;
4848
void MUnmap(void* addr, size_t size) override;
4949
void ShmUnlink() override;
50+
Status UpdateTime() override;
5051

5152
private:
5253
int32_t handle_;

ucm/store/infra/template/timer.h

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
* */
24+
#ifndef UNIFIEDCACHE_TIMER_H
25+
#define UNIFIEDCACHE_TIMER_H
26+
27+
#include <chrono>
28+
#include <thread>
29+
#include <mutex>
30+
#include <atomic>
31+
#include <condition_variable>
32+
#include "logger/logger.h"
33+
#include "status/status.h"
34+
35+
namespace UC {
36+
37+
template <typename Callable>
38+
class Timer {
39+
public:
40+
Timer(const std::chrono::seconds& interval, Callable&& callable)
41+
: interval_(interval), callable_(callable), running_(false) {}
42+
~Timer() {
43+
{
44+
std::lock_guard<std::mutex> lg(this->mutex_);
45+
this->running_ = false;
46+
}
47+
48+
this->cv_.notify_one();
49+
if (this->thread_.joinable()) { this->thread_.join(); }
50+
}
51+
Status Start()
52+
{
53+
{
54+
std::lock_guard<std::mutex> lg(this->mutex_);
55+
if (this->running_) { return Status::OK(); }
56+
}
57+
try {
58+
this->running_ = true;
59+
this->thread_ = std::thread(&Timer::Runner, this);
60+
return Status::OK();
61+
} catch (const std::exception& e) {
62+
UC_ERROR("Failed({}) to start timer.", e.what());
63+
return Status::OutOfMemory();
64+
}
65+
}
66+
67+
private:
68+
void Runner()
69+
{
70+
while (this->running_) {
71+
try {
72+
{
73+
std::unique_lock<std::mutex> lg(this->mutex_);
74+
this->cv_.wait_for(lg, this->interval_, [this] { return !this->running_; });
75+
if (!this->running_) { break; }
76+
}
77+
this->callable_();
78+
} catch (const std::exception& e) { UC_ERROR("Failed({}) to run timer.", e.what()); }
79+
}
80+
}
81+
82+
private:
83+
std::chrono::seconds interval_;
84+
Callable callable_;
85+
std::thread thread_;
86+
std::mutex mutex_;
87+
std::condition_variable cv_;
88+
std::atomic<bool> running_;
89+
};
90+
91+
} // namespace UC
92+
93+
#endif

ucm/store/nfsstore/cc/api/nfsstore.cc

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "logger/logger.h"
2727
#include "space/space_manager.h"
2828
#include "trans/trans_manager.h"
29+
#include "hotness/hotness_manager.h"
2930

3031
namespace UC {
3132

@@ -49,14 +50,27 @@ class NFSStoreImpl : public NFSStore {
4950
return status.Underlying();
5051
}
5152
}
53+
if (config.hotnessEnable) {
54+
status = this->hotnessMgr_.Setup(config.hotnessInterval, this->spaceMgr_.GetSpaceLayout());
55+
if (status.Failure()) {
56+
UC_ERROR("Failed({}) to setup HotnessManager.", status);
57+
return status.Underlying();
58+
}
59+
this->hotnessSetup_ = true;
60+
}
5261
this->ShowConfig(config);
5362
return Status::OK().Underlying();
5463
}
5564
int32_t Alloc(const std::string& block) override
5665
{
5766
return this->spaceMgr_.NewBlock(block).Underlying();
5867
}
59-
bool Lookup(const std::string& block) override { return this->spaceMgr_.LookupBlock(block); }
68+
bool Lookup(const std::string& block) override
69+
{
70+
auto found = this->spaceMgr_.LookupBlock(block);
71+
if (this->hotnessSetup_ && found) { this->hotnessMgr_.Visit(block); }
72+
return found;
73+
}
6074
void Commit(const std::string& block, const bool success) override
6175
{
6276
this->spaceMgr_.CommitBlock(block, success);
@@ -105,11 +119,15 @@ class NFSStoreImpl : public NFSStore {
105119
UC_INFO("Set UC::BufferNumber to {}.", config.transferBufferNumber);
106120
UC_INFO("Set UC::TimeoutMs to {}.", config.transferTimeoutMs);
107121
UC_INFO("Set UC::TempDumpDirEnable to {}.", config.tempDumpDirEnable);
122+
UC_INFO("Set UC::HotnessInterval to {}.", config.hotnessInterval);
123+
UC_INFO("Set UC::HotnessEnabled to {}.", config.hotnessEnable);
108124
}
109125

110126
private:
111127
SpaceManager spaceMgr_;
112128
TransManager transMgr_;
129+
HotnessManager hotnessMgr_;
130+
bool hotnessSetup_{false};
113131
};
114132

115133
int32_t NFSStore::Setup(const Config& config)

ucm/store/nfsstore/cc/api/nfsstore.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ class NFSStore : public CCStore {
4141
size_t transferBufferNumber;
4242
size_t transferTimeoutMs;
4343
bool tempDumpDirEnable;
44+
bool hotnessEnable;
45+
size_t hotnessInterval;
4446

4547
Config(const std::vector<std::string>& storageBackends, const size_t kvcacheBlockSize,
4648
const bool transferEnable)
4749
: storageBackends{storageBackends}, kvcacheBlockSize{kvcacheBlockSize},
4850
transferEnable{transferEnable}, transferDeviceId{-1}, transferStreamNumber{32},
4951
transferIoSize{262144}, transferBufferNumber{512}, transferTimeoutMs{30000},
50-
tempDumpDirEnable{false}
52+
tempDumpDirEnable{false}, hotnessEnable{true}, hotnessInterval{60}
5153
{
5254
}
5355
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
* */
24+
25+
#ifndef UNIFIEDCACHE_HOTNESS_MANAGER_H
26+
#define UNIFIEDCACHE_HOTNESS_MANAGER_H
27+
28+
#include <atomic>
29+
#include <functional>
30+
#include "hotness_set.h"
31+
#include "hotness_timer.h"
32+
#include "logger/logger.h"
33+
34+
namespace UC {
35+
36+
class HotnessManager {
37+
public:
38+
Status Setup(const size_t interval, const SpaceLayout* spaceLayout)
39+
{
40+
this->hotnessTimer_.SetInterval(interval);
41+
this->layout_ = spaceLayout;
42+
this->setupSuccess_ = true;
43+
return Status::OK();
44+
}
45+
46+
void Visit(const std::string& blockId)
47+
{
48+
if (!this->setupSuccess_) {
49+
UC_ERROR("HotnessManager is not setup.");
50+
return;
51+
}
52+
53+
this->hotnessSet_.Insert(blockId);
54+
auto old = this->serviceRunning_.load(std::memory_order_acquire);
55+
if (old) { return; }
56+
if (this->serviceRunning_.compare_exchange_weak(old, true, std::memory_order_acq_rel)) {
57+
auto updater = std::bind(&HotnessSet::UpdateHotness, &this->hotnessSet_, this->layout_);
58+
if (this->hotnessTimer_.Start(std::move(updater)).Success()) {
59+
UC_INFO("Space hotness service started.");
60+
return;
61+
}
62+
this->serviceRunning_ = old;
63+
}
64+
}
65+
66+
private:
67+
bool setupSuccess_{false};
68+
std::atomic_bool serviceRunning_{false};
69+
const SpaceLayout* layout_;
70+
HotnessSet hotnessSet_;
71+
HotnessTimer hotnessTimer_;
72+
};
73+
74+
} // namespace UC
75+
76+
#endif
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
* */
24+
25+
#include "hotness_set.h"
26+
#include "logger/logger.h"
27+
#include "file/file.h"
28+
#include "template/singleton.h"
29+
namespace UC {
30+
31+
void HotnessSet::Insert(const std::string& blockId)
32+
{
33+
std::lock_guard<std::mutex> lg(this->mutex_);
34+
this->pendingBlocks_.insert(blockId);
35+
}
36+
37+
void HotnessSet::UpdateHotness(const SpaceLayout* spaceLayout)
38+
{
39+
std::unordered_set<std::string> blocksToUpdate;
40+
{
41+
std::lock_guard<std::mutex> lg(this->mutex_);
42+
if (this->pendingBlocks_.empty()) {
43+
return;
44+
}
45+
blocksToUpdate.swap(this->pendingBlocks_);
46+
}
47+
48+
size_t number = 0;
49+
for (const std::string& blockId : blocksToUpdate) {
50+
auto blockPath = spaceLayout->DataFilePath(blockId, false);
51+
auto file = File::Make(blockPath);
52+
if (!file) {
53+
UC_WARN("Failed to make file({}), blockId({}).", blockPath, blockId);
54+
continue;
55+
}
56+
auto status = file->UpdateTime();
57+
if (status.Failure()) {
58+
UC_WARN("Failed({}) to update time({}), blockId({}).", status, blockPath, blockId);
59+
continue;
60+
}
61+
number++;
62+
}
63+
if (blocksToUpdate.size() == number) {
64+
UC_INFO("All blocks are hotness.");
65+
} else {
66+
UC_WARN("{} of {} blocks are hotness.", blocksToUpdate.size() - number, blocksToUpdate.size());
67+
}
68+
}
69+
70+
} // namespace UC

0 commit comments

Comments
 (0)