• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "worker_manager.h"
17 
18 #include <mutex>
19 
20 #include "utils/log.h"
21 #ifdef OHOS_PLATFORM
22 #include "parameters.h"
23 #endif
24 
25 static constexpr size_t MAX_JS_THREAD_COUNT = 80;
26 static std::mutex g_jsThreadCountMutex;
27 static uint32_t g_jsThreadCount = 0;
28 
29 static constexpr size_t MAX_ARK_RUNTIME_COUNT = 64;
30 static std::mutex g_arkRuntimeCountMutex;
31 static uint32_t g_arkRuntimeCount = 0;
32 
33 static constexpr size_t MAX_THREADWORKER_COUNT = 64;
34 static std::mutex g_threadWorkerCountMutex;
35 static uint32_t g_threadWorkerCount = 0;
36 
37 static constexpr size_t MAX_LIMITWORKER_COUNT = 16;
38 static std::mutex g_limitWorkerCountMutex;
39 static uint32_t g_limitWorkerCount = 0;
40 
41 static constexpr size_t MAX_OLDWORKER_COUNT = 8;
42 static std::mutex g_oldWorkerCountMutex;
43 static uint32_t g_oldWorkerCount = 0;
44 
IncrementArkRuntimeCount()45 bool WorkerManager::IncrementArkRuntimeCount()
46 {
47     bool success = IncrementJSThreadCount();
48     if (!success) {
49         HILOG_ERROR("reach max JSThread count limit");
50         return false;
51     }
52 
53     std::lock_guard<std::mutex> lock(g_arkRuntimeCountMutex);
54     if (g_arkRuntimeCount >= MAX_ARK_RUNTIME_COUNT) {
55         HILOG_ERROR("reach MAX_ARK_RUNTIME_COUNT");
56         DecrementJSThreadCount();
57         return false;
58     } else {
59         g_arkRuntimeCount++;
60         return true;
61     }
62 }
63 
DecrementArkRuntimeCount()64 void WorkerManager::DecrementArkRuntimeCount()
65 {
66     std::lock_guard<std::mutex> lock(g_arkRuntimeCountMutex);
67     if (g_arkRuntimeCount > 0) {
68         g_arkRuntimeCount--;
69     } else {
70         HILOG_ERROR("DecrementArkRuntimeCount, already is 0");
71     }
72     DecrementJSThreadCount();
73 }
74 
IncrementWorkerCount(WorkerType type)75 bool WorkerManager::IncrementWorkerCount(WorkerType type)
76 {
77     if (type == THREAD_WORKER) {
78         return IncrementThreadWorkerCount();
79     } else if (type == LIMITED_WORKER) {
80         return IncrementLimitWorkerCount();
81     } else {
82         return IncrementOldWorkerCount();
83     }
84 }
85 
DecrementWorkerCount(WorkerType type)86 void WorkerManager::DecrementWorkerCount(WorkerType type)
87 {
88     if (type == THREAD_WORKER) {
89         DecrementThreadWorkerCount();
90     } else if (type == LIMITED_WORKER) {
91         DecrementLimitWorkerCount();
92     } else {
93         DecrementOldWorkerCount();
94     }
95 }
96 
IncrementJSThreadCount()97 bool WorkerManager::IncrementJSThreadCount()
98 {
99     std::lock_guard<std::mutex> lock(g_jsThreadCountMutex);
100     if (g_jsThreadCount >= MAX_JS_THREAD_COUNT) {
101         HILOG_ERROR("reach MAX_JS_THREAD_COUNT");
102         return false;
103     } else {
104         g_jsThreadCount++;
105         return true;
106     }
107 }
108 
DecrementJSThreadCount()109 void WorkerManager::DecrementJSThreadCount()
110 {
111     std::lock_guard<std::mutex> lock(g_jsThreadCountMutex);
112     if (g_jsThreadCount > 0) {
113         g_jsThreadCount--;
114     } else {
115         HILOG_ERROR("DecrementJSThreadCount, already is 0");
116     }
117 }
118 
IncrementThreadWorkerCount()119 bool WorkerManager::IncrementThreadWorkerCount()
120 {
121     bool success = IncrementJSThreadCount();
122     if (!success) {
123         HILOG_ERROR("reach max JSThread count limit");
124         return false;
125     }
126 
127     std::lock_guard<std::mutex> lock(g_threadWorkerCountMutex);
128     uint32_t maxWorkers = MAX_THREADWORKER_COUNT;
129 #if defined(OHOS_PLATFORM)
130     maxWorkers = OHOS::system::GetUintParameter<uint32_t>("persist.commonlibrary.maxworkers", maxWorkers);
131 #endif
132     if (g_threadWorkerCount >= maxWorkers) {
133         HILOG_ERROR("reach MAX_THREADWORKER_COUNT");
134         DecrementJSThreadCount();
135         return false;
136     } else {
137         g_threadWorkerCount++;
138         return true;
139     }
140 }
141 
DecrementThreadWorkerCount()142 void WorkerManager::DecrementThreadWorkerCount()
143 {
144     std::lock_guard<std::mutex> lock(g_threadWorkerCountMutex);
145     if (g_threadWorkerCount > 0) {
146         g_threadWorkerCount--;
147     } else {
148         HILOG_ERROR("DecrementThreadWorkerCount, already is 0");
149     }
150     DecrementJSThreadCount();
151 }
152 
IncrementLimitWorkerCount()153 bool WorkerManager::IncrementLimitWorkerCount()
154 {
155     bool success = IncrementJSThreadCount();
156     if (!success) {
157         HILOG_ERROR("reach max JSThread count limit");
158         return false;
159     }
160 
161     std::lock_guard<std::mutex> lock(g_limitWorkerCountMutex);
162     if (g_limitWorkerCount >= MAX_LIMITWORKER_COUNT) {
163         HILOG_ERROR("reach MAX_LIMITWORKER_COUNT");
164         DecrementJSThreadCount();
165         return false;
166     } else {
167         g_limitWorkerCount++;
168         return true;
169     }
170 }
171 
DecrementLimitWorkerCount()172 void WorkerManager::DecrementLimitWorkerCount()
173 {
174     std::lock_guard<std::mutex> lock(g_limitWorkerCountMutex);
175     if (g_limitWorkerCount > 0) {
176         g_limitWorkerCount--;
177     } else {
178         HILOG_ERROR("DecrementLimitWorkerCount, already is 0");
179     }
180     DecrementJSThreadCount();
181 }
182 
IncrementOldWorkerCount()183 bool WorkerManager::IncrementOldWorkerCount()
184 {
185     bool success = IncrementJSThreadCount();
186     if (!success) {
187         HILOG_ERROR("reach max JSThread count limit");
188         return false;
189     }
190 
191     std::lock_guard<std::mutex> lock(g_oldWorkerCountMutex);
192     uint32_t maxWorkers = MAX_OLDWORKER_COUNT;
193 #if defined(OHOS_PLATFORM)
194     maxWorkers = OHOS::system::GetUintParameter<uint32_t>("persist.commonlibrary.maxworkers", maxWorkers);
195 #endif
196     if (g_oldWorkerCount >= maxWorkers) {
197         HILOG_ERROR("reach MAX_OLDWORKER_COUNT");
198         DecrementJSThreadCount();
199         return false;
200     } else {
201         g_oldWorkerCount++;
202         return true;
203     }
204 }
205 
DecrementOldWorkerCount()206 void WorkerManager::DecrementOldWorkerCount()
207 {
208     std::lock_guard<std::mutex> lock(g_oldWorkerCountMutex);
209     if (g_oldWorkerCount > 0) {
210         g_oldWorkerCount--;
211     } else {
212         HILOG_ERROR("DecrementOldWorkerCount, already is 0");
213     }
214     DecrementJSThreadCount();
215 }
216