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 
16 #include "napi/native_api.h"
17 #include "hicollie/hicollie.h"
18 #include <thread>
19 #include <string>
20 #include <unistd.h>
21 #include <atomic>
22 
23 #undef LOG_TAG
24 #define LOG_TAG "testTag"
25 
26 static OH_HiCollie_BeginFunc beginFunc_;
27 static OH_HiCollie_EndFunc endFunc_;
28 HiCollie_DetectionParam param {.sampleStackTriggerTime = 150, .reserved = 0};
29 int64_t lastWatchTime = 0;
30 const int64_t CHECK_INTERNAL_TIME = 3000;
31 std::shared_ptr<std::atomic<bool>> isReport = std::make_shared<std::atomic<bool>>(true);
32 int g_count = 0;
33 bool g_needReport = true;
34 int g_initResult = -1;
35 
InitBeginFunc(const char* eventName)36 void InitBeginFunc(const char* eventName)
37 {
38     std::string str(eventName);
39 }
InitEndFunc(const char* eventName)40 void InitEndFunc(const char* eventName)
41 {
42     std::string str(eventName);
43 }
44 
TestJankDetection()45 void TestJankDetection()
46 {
47     beginFunc_ = InitBeginFunc;
48     endFunc_ = InitEndFunc;
49     int initResult = OH_HiCollie_Init_JankDetection(&beginFunc_, &endFunc_, param);
50     int initcount = 0;
51     while (initcount < 2) { //as of 2
52         beginFunc_("TestBegin");
53         usleep(350 * 1000); //350ms转换为350*1000微秒
54         endFunc_("TestEnd");
55         initcount++;
56     }
57 }
58 
TestHiCollieJankC(napi_env env, napi_callback_info info)59 static napi_value TestHiCollieJankC(napi_env env, napi_callback_info info)
60 {
61     std::thread threadObj(TestJankDetection);
62     threadObj.join();
63     napi_value sum;
64     napi_create_double(env, 0, &sum);
65     return sum;
66 }
67 
GetCurrentTime()68 int64_t GetCurrentTime()
69 {
70     return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::
71     system_clock::now().time_since_epoch()).count();
72 }
73 
ReportEvent()74 bool ReportEvent()
75 {
76     if ((GetCurrentTime() - lastWatchTime) > CHECK_INTERNAL_TIME) {
77         return true;
78     }
79     return true;
80 }
81 
TestTask()82 void TestTask()
83 {
84     if (g_needReport && ReportEvent()) {
85         bool temp = isReport->load();
86         int reportResult = OH_HiCollie_Report(&temp);
87         g_needReport = false;
88     }
89     int64_t now = GetCurrentTime();
90     if ((now - lastWatchTime) >= (CHECK_INTERNAL_TIME / 2)) { //as of 2
91         lastWatchTime = now;
92     }
93 }
94 
TestStuckDetection()95 int  TestStuckDetection()
96 {
97     int initResult = -1;
98     if (g_count == 0) {
99         initResult = OH_HiCollie_Init_StuckDetection(TestTask);
100         TestTask();
101         g_count++;
102     }
103     return initResult;
104 }
105 
TestHiCollieStuckC(napi_env env, napi_callback_info info)106 static napi_value TestHiCollieStuckC(napi_env env, napi_callback_info info)
107 {
108     std::thread threadObj(TestStuckDetection);
109     threadObj.join();
110     napi_value sum;
111     napi_create_double(env, 0, &sum);
112     return sum;
113 }
114 
TestStuckCMThread(napi_env env, napi_callback_info info)115 static napi_value TestStuckCMThread(napi_env env, napi_callback_info info)
116 {
117     napi_value sum;
118     int initResult = OH_HiCollie_Init_StuckDetection(nullptr);
119     napi_create_int32(env, initResult, &sum);
120     return sum;
121 }
122 
TestJankCMThread(napi_env env, napi_callback_info info)123 static napi_value TestJankCMThread(napi_env env, napi_callback_info info)
124 {
125     napi_value sum;
126     int initResult = OH_HiCollie_Init_JankDetection(nullptr, &endFunc_, param);
127     napi_create_int32(env, initResult, &sum);
128     return sum;
129 }
130 
131 
TestReportCMThread(napi_env env, napi_callback_info info)132 static napi_value TestReportCMThread(napi_env env, napi_callback_info info)
133 {
134     napi_value sum;
135     int initResult = OH_HiCollie_Report(nullptr);
136     napi_create_int32(env, initResult, &sum);
137     return sum;
138 }
139 
140 
Test001()141 void Test001()
142 {
143     g_initResult = OH_HiCollie_Init_JankDetection(nullptr, &endFunc_, param);
144 }
145 
TestJankCerr401(napi_env env, napi_callback_info info)146 static napi_value TestJankCerr401(napi_env env, napi_callback_info info)
147 {
148     napi_value sum;
149     std::thread threadObj(Test001);
150     threadObj.join();
151     napi_create_int32(env, g_initResult, &sum);
152     return sum;
153 }
154 
155 
Add(napi_env env, napi_callback_info info)156 static napi_value Add(napi_env env, napi_callback_info info)
157 {
158     size_t argc = 2;
159     napi_value args[2] = {nullptr};
160 
161     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
162 
163     napi_valuetype valuetype0;
164     napi_typeof(env, args[0], &valuetype0);
165 
166     napi_valuetype valuetype1;
167     napi_typeof(env, args[1], &valuetype1);
168 
169     double value0;
170     napi_get_value_double(env, args[0], &value0);
171 
172     double value1;
173     napi_get_value_double(env, args[1], &value1);
174 
175     napi_value sum;
176     napi_create_double(env, value0 + value1, &sum);
177     return sum;
178 }
179 
180 EXTERN_C_START
Init(napi_env env, napi_value exports)181 static napi_value Init(napi_env env, napi_value exports)
182 {
183     napi_property_descriptor desc[] = {
184         { "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr },
185         { "testHiCollieJankC", nullptr, TestHiCollieJankC, nullptr, nullptr, nullptr, napi_default, nullptr },
186         { "testHiCollieStuckC", nullptr, TestHiCollieStuckC, nullptr, nullptr, nullptr, napi_default, nullptr },
187         { "testJankCMThread", nullptr, TestJankCMThread, nullptr, nullptr, nullptr, napi_default, nullptr },
188         { "testStuckCMThread", nullptr, TestStuckCMThread, nullptr, nullptr, nullptr, napi_default, nullptr },
189         { "testReportCMThread", nullptr, TestReportCMThread, nullptr, nullptr, nullptr, napi_default, nullptr },
190         { "testJankCerr401", nullptr,  TestJankCerr401, nullptr, nullptr, nullptr, napi_default, nullptr },
191     };
192     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
193     return exports;
194 }
195 EXTERN_C_END
196 
197 static napi_module demoModule = {
198     .nm_version = 1,
199     .nm_flags = 0,
200     .nm_filename = nullptr,
201     .nm_register_func = Init,
202     .nm_modname = "hicollie",
203     .nm_priv = ((void*)0),
204     .reserved = { 0 },
205 };
206 
RegisterEntryModule(void)207 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
208 {
209     napi_module_register(&demoModule);
210 }
211