1 /*
2 * Copyright (c) 2023 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 <fstream>
17 #include <iostream>
18 #include <string>
19 #include <thread>
20 #include <vector>
21
22 #include <securec.h>
23 #include <uv.h>
24
25 #include "ecmascript/base/string_helper.h"
26 #include "ecmascript/js_runtime_options.h"
27 #include "ecmascript/napi/include/jsnapi.h"
28 #include "ecmascript/platform/file.h"
29 #include "tooling/utils/utils.h"
30 #ifdef PANDA_TARGET_MACOS
31 #include <unistd.h>
32 #include <sys/syscall.h>
33 #endif
34 static panda::ecmascript::Mutex g_mutex;
35 static std::list<std::string> g_files;
36 static std::list<std::string>::iterator g_iter;
37 static panda::ecmascript::JSRuntimeOptions g_runtimeOptions;
38 static uv_async_t *g_exitSignal = nullptr;
39 static int g_threadCount = 0;
40 static int g_runningCount = 0;
41
42 static constexpr int MAX_THREAD = 1024;
43
44 namespace OHOS::ArkCompiler::Toolchain {
ExecutePandaFile(panda::ecmascript::EcmaVM * vm,const panda::ecmascript::JSRuntimeOptions & runtimeOptions,const std::string & file,const std::string & entry)45 bool ExecutePandaFile(panda::ecmascript::EcmaVM *vm,
46 const panda::ecmascript::JSRuntimeOptions &runtimeOptions,
47 const std::string &file, const std::string &entry)
48 {
49 panda::LocalScope scope(vm);
50
51 panda::ecmascript::EcmaContext *context1 = nullptr;
52 if (runtimeOptions.IsEnableContext()) {
53 context1 = panda::JSNApi::CreateJSContext(vm);
54 panda::JSNApi::SwitchCurrentContext(vm, context1);
55 }
56
57 if (runtimeOptions.WasAOTOutputFileSet()) {
58 panda::JSNApi::LoadAotFile(vm, "");
59 }
60
61 bool ret = panda::JSNApi::Execute(vm, file, entry);
62
63 if (runtimeOptions.IsEnableContext()) {
64 panda::JSNApi::DestroyJSContext(vm, context1);
65 }
66
67 return ret;
68 }
69
GetNextPara()70 std::pair<std::string, std::string> GetNextPara()
71 {
72 std::string fileName = *g_iter;
73 std::string fileAbc = fileName.substr(fileName.find_last_of('/') + 1);
74 std::string entry = fileAbc.substr(0, fileAbc.size() - 4);
75 g_iter++;
76 g_runningCount++;
77 return {fileName, entry};
78 }
79
GetMsg(int ret,std::string & msg,std::string & fileName)80 std::string GetMsg(int ret, std::string& msg, std::string& fileName)
81 {
82 if (!ret) {
83 #ifdef PANDA_TARGET_MACOS
84 msg = "[FAILED] [" + std::to_string(syscall(SYS_thread_selfid)) + "] Run " +
85 fileName + " failed!";
86 #else
87 msg = "[FAILED] [" + std::to_string(gettid()) + "] Run " + fileName + " failed!";
88 #endif
89 } else {
90 #ifdef PANDA_TARGET_MACOS
91 msg = "[PASS] [" + std::to_string(syscall(SYS_thread_selfid)) + "] Run " +
92 fileName + " success!";
93 #else
94 msg = "[PASS] [" + std::to_string(gettid()) + "] Run " + fileName + " success!";
95 #endif
96 }
97 return msg;
98 }
99
StartThread(uv_loop_t * loop)100 bool StartThread(uv_loop_t *loop)
101 {
102 uv_thread_t tid = 0;
103 int ret = uv_thread_create(&tid, [] (void* arg) -> void {
104 while (true) {
105 g_mutex.Lock();
106 if (g_iter == g_files.end()) {
107 g_threadCount--;
108 if (g_threadCount == 0) {
109 uv_async_send(g_exitSignal);
110 }
111 g_mutex.Unlock();
112 break;
113 }
114 auto [fileName, entry] = GetNextPara();
115 g_mutex.Unlock();
116
117 panda::ecmascript::EcmaVM *vm = panda::JSNApi::CreateEcmaVM(g_runtimeOptions);
118 if (vm == nullptr) {
119 std::cerr << "Cannot create vm." << std::endl;
120 return;
121 }
122 panda::JSNApi::SetBundle(vm, !g_runtimeOptions.GetMergeAbc());
123 bool ret = ExecutePandaFile(vm, g_runtimeOptions, fileName, entry);
124
125 panda::JSNApi::DestroyJSVM(vm);
126 auto loop = static_cast<uv_loop_t *>(arg);
127 auto work = new uv_work_t;
128 std::string msg = GetMsg(ret, msg, fileName);
129 work->data = new char[msg.size() + 1];
130 if (strncpy_s(static_cast<char*>(work->data), msg.size() + 1, msg.data(), msg.size()) != EOK) {
131 std::cerr << "strncpy_s fail." << std::endl;
132 delete[] static_cast<char*>(work->data);
133 delete work;
134 return;
135 }
136 uv_queue_work(loop, work, [] (uv_work_t*) {}, [] (uv_work_t* work, int) {
137 std::cerr << static_cast<char*>(work->data) << std::endl;
138 delete[] static_cast<char*>(work->data);
139 delete work;
140 });
141 }
142 }, loop);
143 return ret != 0;
144 }
145
Main(const int argc,const char ** argv)146 int Main(const int argc, const char **argv)
147 {
148 auto startTime =
149 std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch())
150 .count();
151
152 std::cerr << "Run begin [" << getpid() << "]" << std::endl;
153 if (argc < 3) { // 3: at least have three arguments
154 std::cerr << "At least have three arguments." << std::endl;
155 return -1;
156 }
157
158 std::string countStr = argv[1];
159 int32_t count;
160 if (!Utils::StrToInt32(countStr, count)) {
161 std::cerr << "The argument about the number of threads is incorrect." << std::endl;
162 return -1;
163 }
164 g_threadCount = std::min(count, MAX_THREAD);
165
166 std::string filePath = argv[2];
167 std::string realPath;
168 if (!panda::ecmascript::RealPath(filePath, realPath, true)) {
169 std::cerr << "RealPath return fail";
170 return -1;
171 }
172
173 g_mutex.Lock();
174 std::string line;
175 std::ifstream in(realPath);
176 while (std::getline(in, line)) {
177 if (line.find_last_of(".abc") == std::string::npos) { // endwith
178 std::cerr << "Not endwith .abc" << line << std::endl;
179 return -1;
180 }
181 g_files.emplace_back(line);
182 }
183 g_iter = g_files.begin();
184 g_mutex.Unlock();
185
186 bool retOpt = g_runtimeOptions.ParseCommand(argc - 2, argv + 2);
187 if (!retOpt) {
188 std::cerr << "ParseCommand failed." << std::endl;
189 return -1;
190 }
191 panda::ecmascript::EcmaVM *vm = panda::JSNApi::CreateEcmaVM(g_runtimeOptions);
192 if (vm == nullptr) {
193 std::cerr << "Cannot create vm." << std::endl;
194 return -1;
195 }
196 panda::JSNApi::SetBundle(vm, !g_runtimeOptions.GetMergeAbc());
197
198 uv_loop_t* loop = uv_default_loop();
199 g_exitSignal = new uv_async_t;
200 g_exitSignal->data = loop;
201 uv_async_init(loop, g_exitSignal, []([[maybe_unused]] uv_async_t* handle) {
202 g_mutex.Lock();
203 assert (g_threadCount == 0);
204 g_mutex.Unlock();
205 auto loop = static_cast<uv_loop_t*>(handle->data);
206 uv_stop(loop);
207 });
208
209 int threadCountLocal = g_threadCount;
210 for (int i = 0; i < threadCountLocal; i++) {
211 StartThread(loop);
212 }
213
214 uv_run(loop, UV_RUN_DEFAULT);
215
216 uv_close(reinterpret_cast<uv_handle_t*>(g_exitSignal), [] (uv_handle_t* handle) {
217 if (handle != nullptr) {
218 delete reinterpret_cast<uv_handle_t*>(handle);
219 handle = nullptr;
220 }
221 });
222 panda::JSNApi::DestroyJSVM(vm);
223
224 auto endTime =
225 std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch())
226 .count();
227
228 g_mutex.Lock();
229 const long long timeUnit = 1000'000'000;
230 std::cerr << "Run end, total file count: " << g_runningCount << ", used: "
231 << ((endTime - startTime) / timeUnit) << "s." << std::endl;
232 g_mutex.Unlock();
233 return 0;
234 }
235 } // OHOS::ArkCompiler::Toolchain
236
main(int argc,const char ** argv)237 int main(int argc, const char **argv)
238 {
239 return OHOS::ArkCompiler::Toolchain::Main(argc, argv);
240 }
241