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