1 /**
2 * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "runtime/mem/gc/g1/update_remset_thread.h"
17
18 #include "libpandabase/utils/logger.h"
19 #include "runtime/include/language_config.h"
20 #include "runtime/mem/gc/g1/g1-gc.h"
21 #include "runtime/mem/gc/g1/update_remset_worker.h"
22
23 namespace ark::mem {
24
25 template <class LanguageConfig>
UpdateRemsetThread(G1GC<LanguageConfig> * gc,GCG1BarrierSet::ThreadLocalCardQueues * queue,os::memory::Mutex * queueLock,size_t regionSize,bool updateConcurrent,size_t minConcurrentCardsToProcess,size_t hotCardsProcessingFrequency)26 UpdateRemsetThread<LanguageConfig>::UpdateRemsetThread(G1GC<LanguageConfig> *gc,
27 GCG1BarrierSet::ThreadLocalCardQueues *queue,
28 os::memory::Mutex *queueLock, size_t regionSize,
29 bool updateConcurrent, size_t minConcurrentCardsToProcess,
30 size_t hotCardsProcessingFrequency)
31 : UpdateRemsetWorker<LanguageConfig>(gc, queue, queueLock, regionSize, updateConcurrent,
32 minConcurrentCardsToProcess, hotCardsProcessingFrequency)
33 {
34 }
35
36 template <class LanguageConfig>
37 UpdateRemsetThread<LanguageConfig>::~UpdateRemsetThread() = default;
38
39 template <class LanguageConfig>
CreateWorkerImpl()40 void UpdateRemsetThread<LanguageConfig>::CreateWorkerImpl()
41 {
42 LOG(DEBUG, GC) << "Start creating UpdateRemsetThread";
43 os::memory::LockHolder lk(this->updateRemsetLock_);
44 auto internalAllocator = this->GetGC()->GetInternalAllocator();
45 ASSERT(updateThread_ == nullptr);
46 updateThread_ = internalAllocator->template New<std::thread>(&UpdateRemsetThread::ThreadLoop, this);
47 ASSERT(updateThread_ != nullptr);
48 int res = os::thread::SetThreadName(updateThread_->native_handle(), "UpdateRemset");
49
50 LOG_IF(res != 0, ERROR, RUNTIME) << "Failed to set a name for the UpdateRemset thread";
51 }
52
53 template <class LanguageConfig>
DestroyWorkerImpl()54 void UpdateRemsetThread<LanguageConfig>::DestroyWorkerImpl()
55 {
56 LOG(DEBUG, GC) << "Starting destroy UpdateRemsetThread";
57 {
58 os::memory::LockHolder holder(this->updateRemsetLock_);
59 threadCondVar_.SignalAll(); // wake up updateremset worker & pause method
60 }
61 ASSERT(updateThread_ != nullptr);
62 updateThread_->join();
63 auto allocator = this->GetGC()->GetInternalAllocator();
64 ASSERT(allocator != nullptr);
65 allocator->Delete(updateThread_);
66 updateThread_ = nullptr;
67 LOG(DEBUG, GC) << "UpdateRemsetThread was destroyed";
68 }
69
70 template <class LanguageConfig>
ThreadLoop()71 void UpdateRemsetThread<LanguageConfig>::ThreadLoop()
72 {
73 LOG(DEBUG, GC) << "Entering UpdateRemsetThread ThreadLoop";
74
75 this->updateRemsetLock_.Lock();
76 while (true) {
77 // Do one atomic load before checks
78 auto iterationFlag = this->GetFlag();
79 if (iterationFlag == UpdateRemsetWorker<LanguageConfig>::UpdateRemsetWorkerFlags::IS_STOP_WORKER) {
80 LOG(DEBUG, GC) << "exit UpdateRemsetThread loop, because thread was stopped";
81 break;
82 }
83 if (iterationFlag == UpdateRemsetWorker<LanguageConfig>::UpdateRemsetWorkerFlags::IS_PAUSED_BY_GC_THREAD) {
84 // UpdateRemsetThread is paused by GC, wait until GC notifies to continue work
85 threadCondVar_.Wait(&this->updateRemsetLock_);
86 continue;
87 }
88 if (iterationFlag == UpdateRemsetWorker<LanguageConfig>::UpdateRemsetWorkerFlags::IS_INVALIDATE_REGIONS) {
89 // wait until GC-Thread invalidates regions
90 threadCondVar_.Wait(&this->updateRemsetLock_);
91 continue;
92 }
93 ASSERT(!this->pausedByGcThread_);
94 ASSERT(iterationFlag == UpdateRemsetWorker<LanguageConfig>::UpdateRemsetWorkerFlags::IS_PROCESS_CARD);
95 auto processedCards = this->ProcessAllCards();
96 if (processedCards < this->GetMinConcurrentCardsToProcess()) {
97 Sleep();
98 }
99 }
100 this->updateRemsetLock_.Unlock();
101 LOG(DEBUG, GC) << "Exiting UpdateRemsetThread ThreadLoop";
102 }
103
104 template <class LanguageConfig>
ContinueProcessCards()105 void UpdateRemsetThread<LanguageConfig>::ContinueProcessCards()
106 {
107 // Signal to continue UpdateRemsetThread
108 threadCondVar_.Signal();
109 }
110
111 TEMPLATE_CLASS_LANGUAGE_CONFIG(UpdateRemsetThread);
112 } // namespace ark::mem
113