• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 使用JSVM-API接口进行Trace相关开发
2<!--Kit: NDK Development-->
3<!--Subsystem: arkcompiler-->
4<!--Owner: @yuanxiaogou; @string_sz-->
5<!--Designer: @knightaoko-->
6<!--Tester: @test_lzz-->
7<!--Adviser: @fang-jinxu-->
8
9## 简介
10
11JSVM-API中trace相关接口用于在运行时采集并输出各种类型运行时信息。该能力可用于在JSVM模块中定位问题与性能分析。
12
13## 基本概念
14
15JSVM-API中的Trace相关接口能够通过指定一系列分类,采集这些分类所属的JSVM内部事件信息,并以JSON格式通过用户指定的回调函数输出。
16
17在JSVM-API中,通过支持Trace相关能力,JSVM模块能够更紧密地与JavaScript环境集成,提高复杂问题定位和性能分析能力。
18
19## 接口说明
20
21| 接口                       | 功能说明                       |
22|----------------------------|--------------------------------|
23| OH_JSVM_TraceStart           | 以指定事件类型与事件数量限制,开始采集运行时信息。  |
24| OH_JSVM_TraceStop        | 停止采集信息,并以JSON格式在用户指定的回调函数中进行输出。  |
25
26## 使用示例
27
28JSVM-API接口开发流程参考[使用JSVM-API实现JS与C/C++语言交互开发流程](use-jsvm-process.md),本文仅对接口对应C++相关代码进行展示。
29
30### 使用Trace接口进行数据采集
31
32cpp 部分代码:
33
34```cpp
35#include <vector>
36#include <string>
37
38bool OutputStream(const char *data, int size, void *streamData) {
39    std::string value(data, size);
40    *(std::string *)streamData = std::string(data, size);
41    return true;
42}
43
44// OH_JSVM_TraceStart/OH_JSVM_TraceStop样例方法
45static JSVM_Value Trace(JSVM_Env env, JSVM_CallbackInfo info) {
46    // start trace with categories
47    std::vector<JSVM_TraceCategory> categories = {JSVM_TRACE_VM};
48    JSVM_CALL(OH_JSVM_TraceStart(categories.size(), categories.data(), "trace test", 0));
49
50    // run and trace
51    JSVM_Script script;
52    JSVM_Value jsSrc;
53    JSVM_Value result;
54    const char* trace_js = R"JS(
55      function map(x, y) {
56        return {"a": x, "b": y};
57      }
58      for (var i = 0; i < 80000; ++i) {
59        var x = map(i, i);
60      }
61    )JS";
62    JSVM_CALL(OH_JSVM_CreateStringUtf8(env, trace_js, JSVM_AUTO_LENGTH, &jsSrc));
63    JSVM_CALL(OH_JSVM_CompileScript(env, jsSrc, nullptr, 0, true, nullptr, &script));
64    JSVM_CALL(OH_JSVM_RunScript(env, script, &result));
65
66    // stop trace and write file
67    std::string data;
68    JSVM_CALL(OH_JSVM_TraceStop(OutputStream, (void *)&data));
69
70    OH_LOG_INFO(LOG_APP, "JSVM Trace success:%{public}s", data.c_str());
71    return nullptr;
72}
73// Trace注册回调
74static JSVM_CallbackStruct param[] = {
75    {.data = nullptr, .callback = Trace},
76};
77static JSVM_CallbackStruct *method = param;
78// Trace方法别名,供JS调用
79static JSVM_PropertyDescriptor descriptor[] = {
80    {"trace", nullptr, method, nullptr, nullptr, nullptr, JSVM_DEFAULT},
81};
82// 样例测试js
83const char *srcCallNative = R"JS(trace())JS";
84```
85预计的输出结果:
86```
87JSVM Trace success:{"trace test":[{"pid":54485,"tid":54485,"ts":341364468711,"tts":139452,"ph":"B","cat":"devtools.timeline,v8","name":"MinorGC","dur":0,"tdur":0,"args":{"usedHeapSizeBefore":1592824,"type":"allocation failure"}},{"pid":54485,"tid":54485,"ts":341364468727,"tts":139463,"ph":"X","cat":"v8","name":"V8.GCScavenger","dur":203,"tdur":203,"args":{}},{"pid":54485,"tid":54485,"ts":341364468949,"tts":139684,"ph":"E","cat":"devtools.timeline,v8","name":"MinorGC","dur":0,"tdur":0,"args":{"usedHeapSizeAfter":173472}},{"pid":54485,"tid":54485,"ts":341364471055,"tts":141792,"ph":"B","cat":"devtools.timeline,v8","name":"MinorGC","dur":0,"tdur":0,"args":{"usedHeapSizeBefore":1208672,"type":"allocation failure"}},{"pid":54485,"tid":54485,"ts":341364471060,"tts":141796,"ph":"X","cat":"v8","name":"V8.GCScavenger","dur":110,"tdur":110,"args":{}},{"pid":54485,"tid":54485,"ts":341364471182,"tts":141918,"ph":"E","cat":"devtools.timeline,v8","name":"MinorGC","dur":0,"tdur":0,"args":{"usedHeapSizeAfter":173472}},{"pid":54485,"tid":54485,"ts":341364472901,"tts":143638,"ph":"B","cat":"devtools.timeline,v8","name":"MinorGC","dur":0,"tdur":0,"args":{"usedHeapSizeBefore":1221944,"type":"allocation failure"}},{"pid":54485,"tid":54485,"ts":341364472905,"tts":143641,"ph":"X","cat":"v8","name":"V8.GCScavenger","dur":26,"tdur":26,"args":{}},{"pid":54485,"tid":54485,"ts":341364472940,"tts":143675,"ph":"E","cat":"devtools.timeline,v8","name":"MinorGC","dur":0,"tdur":0,"args":{"usedHeapSizeAfter":173472}},{"pid":54485,"tid":54485,"ts":341364474583,"tts":145319,"ph":"B","cat":"devtools.timeline,v8","name":"MinorGC","dur":0,"tdur":0,"args":{"usedHeapSizeBefore":1221944,"type":"allocation failure"}},{"pid":54485,"tid":54485,"ts":341364474585,"tts":145321,"ph":"X","cat":"v8","name":"V8.GCScavenger","dur":17,"tdur":16,"args":{}},{"pid":54485,"tid":54485,"ts":341364474608,"tts":145343,"ph":"E","cat":"devtools.timeline,v8","name":"MinorGC","dur":0,"tdur":0,"args":{"usedHeapSizeAfter":173472}}]}
88```
89注:用户运行demo输出的结果不一定与给出参考结果相等,id的分配方式和用户环境相关