1 /*
2 * Copyright (c) 2021 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 #include "hiperf_client_napi.h"
16 #include <cstdio>
17 #include <sstream>
18 #include <string>
19 #include "hiperf_hilog.h"
20 #include "hiperf_client.h"
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23
24 namespace OHOS {
25 namespace Developtools {
26 namespace HiPerf {
27 namespace HiperfClient {
28 static std::unique_ptr<HiperfClient::Client> g_hiperfClient =
29 std::make_unique<HiperfClient::Client>();
30
31 static std::unique_ptr<HiperfClient::RecordOption> g_hiperfRecordOption =
32 std::make_unique<HiperfClient::RecordOption>();
33
StringSplit(const std::string & text,char delimiter=',')34 static std::vector<std::string> StringSplit(const std::string& text, char delimiter = ',')
35 {
36 std::vector<std::string> tokens;
37 std::string token;
38 std::istringstream tokenStream(text);
39 while (std::getline(tokenStream, token, delimiter)) {
40 if (!token.empty()) {
41 tokens.push_back(token);
42 }
43 }
44 return tokens;
45 }
46
IsNumeric(const std::string & str)47 static bool IsNumeric(const std::string& str)
48 {
49 std::istringstream iss(str);
50 int number;
51 char trailingCharacter;
52 if (!(iss >> number)) {
53 return false;
54 }
55 if (iss >> trailingCharacter) {
56 return false;
57 }
58 return true;
59 }
60
StringSplitToInt(const std::string & text,char delimiter=',')61 static std::vector<int> StringSplitToInt(const std::string& text, char delimiter = ',')
62 {
63 std::vector<int> tokens;
64 std::string token;
65 std::istringstream tokenStream(text);
66 while (std::getline(tokenStream, token, delimiter)) {
67 if (IsNumeric(token)) {
68 tokens.push_back(std::stoi(token));
69 }
70 }
71 return tokens;
72 }
73
GetJsStringFromOption(const napi_env & env,const napi_callback_info & info)74 static std::string GetJsStringFromOption(const napi_env &env, const napi_callback_info &info)
75 {
76 size_t argc = 1;
77 napi_value args[1] = {0};
78 NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr), "");
79 NAPI_ASSERT_BASE(env, argc == 1, "requires 1 parameter", "");
80
81 napi_valuetype inputType = napi_undefined;
82 napi_status status = napi_typeof(env, args[0], &inputType);
83 if (status != napi_ok) {
84 HILOG_ERROR(LOG_CORE, "GetJsStringFromOption failed to napi_typeof.");
85 return "";
86 }
87 NAPI_ASSERT_BASE(env, inputType == napi_string, "type mismatch for parameter path", "");
88
89 char value[PATH_MAX] = {0};
90 size_t valueLen = 0;
91 if (napi_get_value_string_utf8(env, args[0], value, sizeof(value), &valueLen) != napi_ok) {
92 HIPERF_HILOGE(MODULE_JS_NAPI, "napi_get_value_string_utf8 failed.");
93 return "";
94 }
95 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}s", value);
96 return std::string(value);
97 }
98
GetBoolFromOption(const napi_env & env,const napi_callback_info & info)99 static bool GetBoolFromOption(const napi_env &env, const napi_callback_info &info)
100 {
101 size_t argc = 1;
102 napi_value args[1] = {0};
103 NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr), false);
104 NAPI_ASSERT_BASE(env, argc == 1, "requires 1 parameter", false);
105
106 napi_valuetype inputType = napi_undefined;
107 napi_status status = napi_typeof(env, args[0], &inputType);
108 if (status != napi_ok) {
109 HILOG_ERROR(LOG_CORE, "GetBoolFromOption failed to napi_typeof.");
110 return false;
111 }
112 NAPI_ASSERT_BASE(env, (inputType == napi_boolean), "type mismatch for parameter path", false);
113
114 bool result = false;
115 napi_get_value_bool(env, args[0], &result);
116 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
117 return result;
118 }
119
GetUintFromOption(const napi_env & env,const napi_callback_info & info)120 static uint32_t GetUintFromOption(const napi_env &env, const napi_callback_info &info)
121 {
122 size_t argc = 1;
123 napi_value args[1] = {0};
124 NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr), 0);
125 NAPI_ASSERT_BASE(env, argc == 1, "requires 1 parameter", 0);
126
127 napi_valuetype inputType = napi_undefined;
128 napi_status status = napi_typeof(env, args[0], &inputType);
129 if (status != napi_ok) {
130 HILOG_ERROR(LOG_CORE, "GetUintFromOption failed to napi_typeof.");
131 return 0;
132 }
133 NAPI_ASSERT_BASE(env, (inputType == napi_number), "type mismatch for parameter path", false);
134
135 uint32_t result = 0;
136 napi_get_value_uint32(env, args[0], &result);
137 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
138 return result;
139 }
140
ResetOption(napi_env env,napi_callback_info info)141 static napi_value ResetOption(napi_env env, napi_callback_info info)
142 {
143 napi_value napiValue = nullptr;
144 bool result = true;
145 g_hiperfRecordOption = std::make_unique<HiperfClient::RecordOption>();
146
147 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
148 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
149 return napiValue;
150 }
151
SetOutputFilename(napi_env env,napi_callback_info info)152 static napi_value SetOutputFilename(napi_env env, napi_callback_info info)
153 {
154 napi_value napiValue = nullptr;
155 bool result = true;
156 const std::string option = GetJsStringFromOption(env, info);
157 g_hiperfRecordOption->SetOutputFilename(option);
158
159 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
160 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
161 return napiValue;
162 }
163
GetOutputFileName(napi_env env,napi_callback_info info)164 static napi_value GetOutputFileName(napi_env env, napi_callback_info info)
165 {
166 napi_value napiValue = nullptr;
167 std::string result = g_hiperfRecordOption->GetOutputFileName();
168 NAPI_CALL(env, napi_create_string_utf8(env, result.c_str(), result.size(), &napiValue));
169
170 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}s", result.c_str());
171 return napiValue;
172 }
173
SetTargetSystemWide(napi_env env,napi_callback_info info)174 static napi_value SetTargetSystemWide(napi_env env, napi_callback_info info)
175 {
176 napi_value napiValue = nullptr;
177 bool result = true;
178 bool enable = GetBoolFromOption(env, info);
179 g_hiperfRecordOption->SetTargetSystemWide(enable);
180
181 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
182 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
183 return napiValue;
184 }
185
SetCompressData(napi_env env,napi_callback_info info)186 static napi_value SetCompressData(napi_env env, napi_callback_info info)
187 {
188 napi_value napiValue = nullptr;
189 bool result = true;
190 bool enable = GetBoolFromOption(env, info);
191 g_hiperfRecordOption->SetCompressData(enable);
192
193 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
194 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
195 return napiValue;
196 }
197
SetSelectCpus(napi_env env,napi_callback_info info)198 static napi_value SetSelectCpus(napi_env env, napi_callback_info info)
199 {
200 napi_value napiValue = nullptr;
201 bool result = true;
202 std::string option = GetJsStringFromOption(env, info);
203 g_hiperfRecordOption->SetSelectCpus(StringSplitToInt(option));
204
205 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
206 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
207 return napiValue;
208 }
209
SetTimeStopSec(napi_env env,napi_callback_info info)210 static napi_value SetTimeStopSec(napi_env env, napi_callback_info info)
211 {
212 napi_value napiValue = nullptr;
213 bool result = true;
214 uint32_t option = GetUintFromOption(env, info);
215 g_hiperfRecordOption->SetTimeStopSec(option);
216
217 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
218 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
219 return napiValue;
220 }
221
SetFrequency(napi_env env,napi_callback_info info)222 static napi_value SetFrequency(napi_env env, napi_callback_info info)
223 {
224 napi_value napiValue = nullptr;
225 bool result = true;
226 uint32_t option = GetUintFromOption(env, info);
227 g_hiperfRecordOption->SetFrequency(option);
228
229 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
230 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
231 return napiValue;
232 }
233
SetPeriod(napi_env env,napi_callback_info info)234 static napi_value SetPeriod(napi_env env, napi_callback_info info)
235 {
236 napi_value napiValue = nullptr;
237 bool result = true;
238 uint32_t option = GetUintFromOption(env, info);
239 g_hiperfRecordOption->SetPeriod(option);
240
241 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
242 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
243 return napiValue;
244 }
245
SetSelectEvents(napi_env env,napi_callback_info info)246 static napi_value SetSelectEvents(napi_env env, napi_callback_info info)
247 {
248 napi_value napiValue = nullptr;
249 bool result = true;
250 std::string option = GetJsStringFromOption(env, info);
251 g_hiperfRecordOption->SetSelectEvents(StringSplit(option));
252
253 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
254 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
255 return napiValue;
256 }
SetSelectGroups(napi_env env,napi_callback_info info)257 static napi_value SetSelectGroups(napi_env env, napi_callback_info info)
258 {
259 napi_value napiValue = nullptr;
260 bool result = true;
261 std::string option = GetJsStringFromOption(env, info);
262 g_hiperfRecordOption->SetSelectGroups(StringSplit(option));
263
264 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
265 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
266 return napiValue;
267 }
SetNoInherit(napi_env env,napi_callback_info info)268 static napi_value SetNoInherit(napi_env env, napi_callback_info info)
269 {
270 napi_value napiValue = nullptr;
271 bool result = true;
272 bool enable = GetBoolFromOption(env, info);
273 g_hiperfRecordOption->SetNoInherit(enable);
274
275 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
276 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
277 return napiValue;
278 }
SetSelectPids(napi_env env,napi_callback_info info)279 static napi_value SetSelectPids(napi_env env, napi_callback_info info)
280 {
281 napi_value napiValue = nullptr;
282 bool result = true;
283 std::string option = GetJsStringFromOption(env, info);
284 g_hiperfRecordOption->SetSelectPids(StringSplitToInt(option));
285
286 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
287 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
288 return napiValue;
289 };
SetCallStackSamplingConfigs(napi_env env,napi_callback_info info)290 static napi_value SetCallStackSamplingConfigs(napi_env env, napi_callback_info info)
291 {
292 napi_value napiValue = nullptr;
293 bool result = true;
294 uint32_t option = GetUintFromOption(env, info);
295 g_hiperfRecordOption->SetCallStackSamplingConfigs(option);
296
297 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
298 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
299 return napiValue;
300 }
301
SetSelectTids(napi_env env,napi_callback_info info)302 static napi_value SetSelectTids(napi_env env, napi_callback_info info)
303 {
304 napi_value napiValue = nullptr;
305 bool result = true;
306 std::string option = GetJsStringFromOption(env, info);
307 g_hiperfRecordOption->SetSelectTids(StringSplitToInt(option));
308
309 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
310 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
311 return napiValue;
312 }
313
SetExcludePerf(napi_env env,napi_callback_info info)314 static napi_value SetExcludePerf(napi_env env, napi_callback_info info)
315 {
316 napi_value napiValue = nullptr;
317 bool result = true;
318 bool enable = GetBoolFromOption(env, info);
319 g_hiperfRecordOption->SetExcludePerf(enable);
320
321 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
322 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
323 return napiValue;
324 }
325
SetCpuPercent(napi_env env,napi_callback_info info)326 static napi_value SetCpuPercent(napi_env env, napi_callback_info info)
327 {
328 napi_value napiValue = nullptr;
329 bool result = true;
330 uint32_t option = GetUintFromOption(env, info);
331 g_hiperfRecordOption->SetCpuPercent(option);
332
333 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
334 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
335 return napiValue;
336 }
337
SetOffCPU(napi_env env,napi_callback_info info)338 static napi_value SetOffCPU(napi_env env, napi_callback_info info)
339 {
340 napi_value napiValue = nullptr;
341 bool result = true;
342 bool enable = GetBoolFromOption(env, info);
343 g_hiperfRecordOption->SetOffCPU(enable);
344
345 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
346 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
347 return napiValue;
348 }
349
SetCallGraph(napi_env env,napi_callback_info info)350 static napi_value SetCallGraph(napi_env env, napi_callback_info info)
351 {
352 napi_value napiValue = nullptr;
353 bool result = true;
354 std::string option = GetJsStringFromOption(env, info);
355 g_hiperfRecordOption->SetCallGraph((option));
356
357 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
358 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
359 return napiValue;
360 }
361
SetDelayUnwind(napi_env env,napi_callback_info info)362 static napi_value SetDelayUnwind(napi_env env, napi_callback_info info)
363 {
364 napi_value napiValue = nullptr;
365 bool result = true;
366 bool enable = GetBoolFromOption(env, info);
367 g_hiperfRecordOption->SetDelayUnwind(enable);
368
369 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
370 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
371 return napiValue;
372 }
373
SetDisableUnwind(napi_env env,napi_callback_info info)374 static napi_value SetDisableUnwind(napi_env env, napi_callback_info info)
375 {
376 napi_value napiValue = nullptr;
377 bool result = true;
378 bool enable = GetBoolFromOption(env, info);
379 g_hiperfRecordOption->SetDisableUnwind(enable);
380
381 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
382 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
383 return napiValue;
384 }
385
SetDisableCallstackMerge(napi_env env,napi_callback_info info)386 static napi_value SetDisableCallstackMerge(napi_env env, napi_callback_info info)
387 {
388 napi_value napiValue = nullptr;
389 bool result = true;
390 bool enable = GetBoolFromOption(env, info);
391 g_hiperfRecordOption->SetDisableCallstackMerge(enable);
392
393 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
394 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
395 return napiValue;
396 }
397
SetSymbolDir(napi_env env,napi_callback_info info)398 static napi_value SetSymbolDir(napi_env env, napi_callback_info info)
399 {
400 napi_value napiValue = nullptr;
401 bool result = true;
402 std::string option = GetJsStringFromOption(env, info);
403 g_hiperfRecordOption->SetSymbolDir(option);
404
405 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
406 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
407 return napiValue;
408 }
409
SetDataLimit(napi_env env,napi_callback_info info)410 static napi_value SetDataLimit(napi_env env, napi_callback_info info)
411 {
412 napi_value napiValue = nullptr;
413 bool result = true;
414 std::string option = GetJsStringFromOption(env, info);
415 g_hiperfRecordOption->SetDataLimit(option);
416
417 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
418 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
419 return napiValue;
420 }
421
SetAppPackage(napi_env env,napi_callback_info info)422 static napi_value SetAppPackage(napi_env env, napi_callback_info info)
423 {
424 napi_value napiValue = nullptr;
425 bool result = true;
426 std::string option = GetJsStringFromOption(env, info);
427 g_hiperfRecordOption->SetAppPackage(option);
428
429 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
430 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
431 return napiValue;
432 }
433
SetClockId(napi_env env,napi_callback_info info)434 static napi_value SetClockId(napi_env env, napi_callback_info info)
435 {
436 napi_value napiValue = nullptr;
437 bool result = true;
438 std::string option = GetJsStringFromOption(env, info);
439 g_hiperfRecordOption->SetClockId(option);
440
441 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
442 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
443 return napiValue;
444 }
445
SetVecBranchSampleTypes(napi_env env,napi_callback_info info)446 static napi_value SetVecBranchSampleTypes(napi_env env, napi_callback_info info)
447 {
448 napi_value napiValue = nullptr;
449 bool result = true;
450 std::string option = GetJsStringFromOption(env, info);
451 g_hiperfRecordOption->SetVecBranchSampleTypes(StringSplit(option));
452
453 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
454 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
455 return napiValue;
456 }
457
SetMmapPages(napi_env env,napi_callback_info info)458 static napi_value SetMmapPages(napi_env env, napi_callback_info info)
459 {
460 napi_value napiValue = nullptr;
461 bool result = true;
462 uint32_t option = GetUintFromOption(env, info);
463 g_hiperfRecordOption->SetMmapPages(option);
464
465 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
466 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
467 return napiValue;
468 }
469
GetOptionVecString(napi_env env,napi_callback_info info)470 static napi_value GetOptionVecString(napi_env env, napi_callback_info info)
471 {
472 napi_value napiValue = nullptr;
473 const std::vector<std::string> items = g_hiperfRecordOption->GetOptionVecString();
474 std::string result;
475 const std::string split = ",";
476 for (auto item : items) {
477 if (!result.empty())
478 result.append(split);
479 result.append(item);
480 }
481 if (result.empty()) {
482 result.append("<empty>");
483 }
484
485 NAPI_CALL(env, napi_create_string_utf8(env, result.c_str(), result.size(), &napiValue));
486
487 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}s", result.c_str());
488 return napiValue;
489 }
490
StartWithOption(napi_env env,napi_callback_info info)491 static napi_value StartWithOption(napi_env env, napi_callback_info info)
492 {
493 napi_value napiValue = nullptr;
494
495 // for js api , we always use hilog
496 g_hiperfClient->EnableHilog();
497
498 bool result = g_hiperfClient->Setup(g_hiperfRecordOption->GetOutputFileName());
499 if (result) {
500 const HiperfClient::RecordOption *option = g_hiperfRecordOption.get();
501 result = g_hiperfClient->Start(*option);
502 }
503 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
504 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
505 return napiValue;
506 }
507
Start(napi_env env,napi_callback_info info)508 static napi_value Start(napi_env env, napi_callback_info info)
509 {
510 napi_value napiValue = nullptr;
511
512 // for js api , we always use hilog
513 g_hiperfClient->EnableHilog();
514
515 bool result = g_hiperfClient->Start();
516
517 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
518 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
519 return napiValue;
520 }
521
Setup(napi_env env,napi_callback_info info)522 static napi_value Setup(napi_env env, napi_callback_info info)
523 {
524 napi_value napiValue = nullptr;
525
526 std::string outputPath = GetJsStringFromOption(env, info);
527
528 // for js api , we always use hilog
529 g_hiperfClient->EnableHilog();
530 bool result = g_hiperfClient->Setup(outputPath);
531
532 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
533 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
534 return napiValue;
535 }
536
IsReady(napi_env env,napi_callback_info info)537 static napi_value IsReady(napi_env env, napi_callback_info info)
538 {
539 napi_value napiValue = nullptr;
540 bool result = g_hiperfClient->IsReady();
541
542 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
543
544 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
545 return napiValue;
546 }
547
Stop(napi_env env,napi_callback_info info)548 static napi_value Stop(napi_env env, napi_callback_info info)
549 {
550 napi_value napiValue = nullptr;
551 bool result = g_hiperfClient->Stop();
552
553 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
554
555 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
556 return napiValue;
557 }
558
Pause(napi_env env,napi_callback_info info)559 static napi_value Pause(napi_env env, napi_callback_info info)
560 {
561 napi_value napiValue = nullptr;
562 bool result = g_hiperfClient->Pause();
563
564 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
565
566 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
567 return napiValue;
568 }
569
Resume(napi_env env,napi_callback_info info)570 static napi_value Resume(napi_env env, napi_callback_info info)
571 {
572 napi_value napiValue = nullptr;
573 bool result = g_hiperfClient->Resume();
574
575 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
576
577 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
578 return napiValue;
579 }
580
GetOutputDir(napi_env env,napi_callback_info info)581 static napi_value GetOutputDir(napi_env env, napi_callback_info info)
582 {
583 napi_value napiValue = nullptr;
584 std::string result = g_hiperfClient->GetOutputDir();
585
586 NAPI_CALL(env, napi_create_string_utf8(env, result.c_str(), result.size(), &napiValue));
587
588 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}s", result.c_str());
589 return napiValue;
590 }
591
GetCommandPath(napi_env env,napi_callback_info info)592 static napi_value GetCommandPath(napi_env env, napi_callback_info info)
593 {
594 napi_value napiValue = nullptr;
595 std::string result = g_hiperfClient->GetCommandPath();
596
597 NAPI_CALL(env, napi_create_string_utf8(env, result.c_str(), result.size(), &napiValue));
598
599 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}s", result.c_str());
600 return napiValue;
601 }
602
GetOutputPerfDataPath(napi_env env,napi_callback_info info)603 static napi_value GetOutputPerfDataPath(napi_env env, napi_callback_info info)
604 {
605 napi_value napiValue = nullptr;
606 std::string result = g_hiperfClient->GetOutputPerfDataPath();
607
608 NAPI_CALL(env, napi_create_string_utf8(env, result.c_str(), result.size(), &napiValue));
609
610 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}s", result.c_str());
611 return napiValue;
612 }
613
SetDebugMode(napi_env env,napi_callback_info info)614 static napi_value SetDebugMode(napi_env env, napi_callback_info info)
615 {
616 napi_value napiValue = nullptr;
617 bool result = true;
618
619 g_hiperfClient->SetDebugMode();
620
621 NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
622
623 HIPERF_HILOGD(MODULE_JS_NAPI, "%{public}d", result);
624 return napiValue;
625 }
626 } // namespace HiperfClient
627 } // namespace HiPerf
628 } // namespace Developtools
629 } // namespace OHOS
630
631 using namespace OHOS::Developtools::HiPerf::HiperfClient;
632
633 EXTERN_C_START
634 /*
635 * function for module exports
636 */
HiperfClientInit(napi_env env,napi_value exports)637 static napi_value HiperfClientInit(napi_env env, napi_value exports)
638 {
639 HIPERF_HILOGD(MODULE_JS_NAPI, "enter");
640
641 napi_property_descriptor desc[] = {
642 DECLARE_NAPI_FUNCTION("isReady", IsReady),
643 DECLARE_NAPI_FUNCTION("setup", Setup),
644 DECLARE_NAPI_FUNCTION("start", Start),
645 DECLARE_NAPI_FUNCTION("stop", Stop),
646 DECLARE_NAPI_FUNCTION("pause", Pause),
647 DECLARE_NAPI_FUNCTION("resume", Resume),
648 DECLARE_NAPI_FUNCTION("getOutputDir", GetOutputDir),
649 DECLARE_NAPI_FUNCTION("getOutputPerfDataPath", GetOutputPerfDataPath),
650 DECLARE_NAPI_FUNCTION("getCommandPath", GetCommandPath),
651 DECLARE_NAPI_FUNCTION("setDebugMode", SetDebugMode),
652 // Option:
653 DECLARE_NAPI_FUNCTION("startWithOption", StartWithOption),
654 DECLARE_NAPI_FUNCTION("resetOption", ResetOption),
655 DECLARE_NAPI_FUNCTION("setOutputFilename", SetOutputFilename),
656 DECLARE_NAPI_FUNCTION("getOutputFileName", GetOutputFileName),
657 DECLARE_NAPI_FUNCTION("setTargetSystemWide", SetTargetSystemWide),
658 DECLARE_NAPI_FUNCTION("setCompressData", SetCompressData),
659 DECLARE_NAPI_FUNCTION("setSelectCpus", SetSelectCpus),
660 DECLARE_NAPI_FUNCTION("setTimeStopSec", SetTimeStopSec),
661 DECLARE_NAPI_FUNCTION("setFrequency", SetFrequency),
662 DECLARE_NAPI_FUNCTION("setPeriod", SetPeriod),
663 DECLARE_NAPI_FUNCTION("setSelectEvents", SetSelectEvents),
664 DECLARE_NAPI_FUNCTION("setSelectGroups", SetSelectGroups),
665 DECLARE_NAPI_FUNCTION("setNoInherit", SetNoInherit),
666 DECLARE_NAPI_FUNCTION("setSelectPids", SetSelectPids),
667 DECLARE_NAPI_FUNCTION("setCallStackSamplingConfigs", SetCallStackSamplingConfigs),
668 DECLARE_NAPI_FUNCTION("setSelectTids", SetSelectTids),
669 DECLARE_NAPI_FUNCTION("setExcludePerf", SetExcludePerf),
670 DECLARE_NAPI_FUNCTION("setCpuPercent", SetCpuPercent),
671 DECLARE_NAPI_FUNCTION("setOffCPU", SetOffCPU),
672 DECLARE_NAPI_FUNCTION("setCallGraph", SetCallGraph),
673 DECLARE_NAPI_FUNCTION("setDelayUnwind", SetDelayUnwind),
674 DECLARE_NAPI_FUNCTION("setDisableUnwind", SetDisableUnwind),
675 DECLARE_NAPI_FUNCTION("setDisableCallstackMerge", SetDisableCallstackMerge),
676 DECLARE_NAPI_FUNCTION("setSymbolDir", SetSymbolDir),
677 DECLARE_NAPI_FUNCTION("setDataLimit", SetDataLimit),
678 DECLARE_NAPI_FUNCTION("setAppPackage", SetAppPackage),
679 DECLARE_NAPI_FUNCTION("setClockId", SetClockId),
680 DECLARE_NAPI_FUNCTION("setVecBranchSampleTypes", SetVecBranchSampleTypes),
681 DECLARE_NAPI_FUNCTION("setMmapPages", SetMmapPages),
682 DECLARE_NAPI_FUNCTION("getOptionVecString", GetOptionVecString),
683 };
684 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
685 HIPERF_HILOGD(MODULE_JS_NAPI, "exit");
686 return exports;
687 }
688 EXTERN_C_END
689
690 /*
691 * Module definition
692 */
693 static napi_module g_module = {
694 .nm_version = 1,
695 .nm_flags = 0,
696 .nm_filename = nullptr,
697 .nm_register_func = HiperfClientInit,
698 .nm_modname = "hiperf",
699 .nm_priv = ((void *)0),
700 .reserved = {0},
701 };
702
703 /*
704 * Module registration
705 */
RegisterModule(void)706 extern "C" __attribute__((constructor)) void RegisterModule(void)
707 {
708 napi_module_register(&g_module);
709 }
710