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