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