1 /**
2 * Copyright (c) 2021-2024 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 int res = os::thread::SetThreadName(updateThread_->native_handle(), "UpdateRemset");
48
49 LOG_IF(res != 0, ERROR, RUNTIME) << "Failed to set a name for the UpdateRemset thread";
50 }
51
52 template <class LanguageConfig>
DestroyWorkerImpl()53 void UpdateRemsetThread<LanguageConfig>::DestroyWorkerImpl()
54 {
55 LOG(DEBUG, GC) << "Starting destroy UpdateRemsetThread";
56 {
57 os::memory::LockHolder holder(this->updateRemsetLock_);
58 threadCondVar_.SignalAll(); // wake up updateremset worker & pause method
59 }
60 ASSERT(updateThread_ != nullptr);
61 updateThread_->join();
62 auto allocator = this->GetGC()->GetInternalAllocator();
63 ASSERT(allocator != nullptr);
64 allocator->Delete(updateThread_);
65 updateThread_ = nullptr;
66 LOG(DEBUG, GC) << "UpdateRemsetThread was destroyed";
67 }
68
69 template <class LanguageConfig>
ThreadLoop()70 void UpdateRemsetThread<LanguageConfig>::ThreadLoop()
71 {
72 LOG(DEBUG, GC) << "Entering UpdateRemsetThread ThreadLoop";
73
74 this->updateRemsetLock_.Lock();
75 while (true) {
76 // Do one atomic load before checks
77 auto iterationFlag = this->GetFlag();
78 if (iterationFlag == UpdateRemsetWorker<LanguageConfig>::UpdateRemsetWorkerFlags::IS_STOP_WORKER) {
79 LOG(DEBUG, GC) << "exit UpdateRemsetThread loop, because thread was stopped";
80 break;
81 }
82 if (iterationFlag == UpdateRemsetWorker<LanguageConfig>::UpdateRemsetWorkerFlags::IS_PAUSED_BY_GC_THREAD) {
83 // UpdateRemsetThread is paused by GC, wait until GC notifies to continue work
84 threadCondVar_.Wait(&this->updateRemsetLock_);
85 continue;
86 }
87 if (iterationFlag == UpdateRemsetWorker<LanguageConfig>::UpdateRemsetWorkerFlags::IS_INVALIDATE_REGIONS) {
88 // wait until GC-Thread invalidates regions
89 threadCondVar_.Wait(&this->updateRemsetLock_);
90 continue;
91 }
92 ASSERT(!this->pausedByGcThread_);
93 ASSERT(iterationFlag == UpdateRemsetWorker<LanguageConfig>::UpdateRemsetWorkerFlags::IS_PROCESS_CARD);
94 auto processedCards = this->ProcessAllCards();
95 if (processedCards < this->GetMinConcurrentCardsToProcess()) {
96 Sleep();
97 }
98 }
99 this->updateRemsetLock_.Unlock();
100 LOG(DEBUG, GC) << "Exiting UpdateRemsetThread ThreadLoop";
101 }
102
103 template <class LanguageConfig>
ContinueProcessCards()104 void UpdateRemsetThread<LanguageConfig>::ContinueProcessCards()
105 {
106 // Signal to continue UpdateRemsetThread
107 threadCondVar_.Signal();
108 }
109
110 TEMPLATE_CLASS_LANGUAGE_CONFIG(UpdateRemsetThread);
111 } // namespace ark::mem
112