1 /*
2 * Copyright (c) 2025 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 <ani.h>
17 #include <array>
18 #include <iostream>
19 #include <algorithm>
20 #include <chrono>
21 #include <future>
22 #include <thread>
23 #include <map>
24 #include <string>
25 #include "frameworks/base/perfmonitor/perf_monitor.h"
26
Begin(ani_env * env,ani_string scene,ani_enum_item startInputType,ani_string note)27 static void Begin([[maybe_unused]] ani_env *env, ani_string scene, ani_enum_item startInputType, ani_string note)
28 {
29 ani_size strSize;
30 env->String_GetUTF8Size(scene, &strSize);
31 std::vector<char> buffer(strSize + 1);
32 char* utf8Buffer = buffer.data();
33 ani_size bytes_written = 0;
34 env->String_GetUTF8(scene, utf8Buffer, strSize + 1, &bytes_written);
35 utf8Buffer[bytes_written] = '\0';
36 std::string sceneId = std::string(utf8Buffer);
37
38 ani_size strSize2;
39 env->String_GetUTF8Size(note, &strSize2);
40 std::vector<char> buffer2(strSize2 + 1);
41 char* utf8Buffer2 = buffer2.data();
42 ani_size bytes_written2 = 0;
43 env->String_GetUTF8(note, utf8Buffer2, strSize2 + 1, &bytes_written2);
44 utf8Buffer2[bytes_written2] = '\0';
45 std::string noteStr = std::string(utf8Buffer2);
46
47 ani_namespace ns;
48 if (ANI_OK != env->FindNamespace("L@ohos/arkui/performanceMonitor/performanceMonitor;", &ns)) {
49 return ;
50 }
51
52 ani_int intValue{};
53 if (ANI_OK != env->EnumItem_GetValue_Int(startInputType, &intValue)) {
54 std::cerr << "Enum_GetEnumItemByIndex FAILD" << std::endl;
55 }
56
57 OHOS::Ace::PerfMonitor* pMonitor = nullptr;
58 pMonitor = OHOS::Ace::PerfMonitor::GetPerfMonitor();
59 if (pMonitor != nullptr) {
60 pMonitor->Start(sceneId, static_cast<OHOS::Ace::PerfActionType>(intValue), noteStr);
61 }
62 }
63
End(ani_env * env,ani_string scene)64 static void End([[maybe_unused]] ani_env *env, ani_string scene)
65 {
66 ani_size strSize;
67 env->String_GetUTF8Size(scene, &strSize);
68 std::vector<char> buffer(strSize + 1);
69 char* utf8Buffer = buffer.data();
70 ani_size bytes_written = 0;
71 env->String_GetUTF8(scene, utf8Buffer, strSize + 1, &bytes_written);
72 utf8Buffer[bytes_written] = '\0';
73 std::string sceneId = std::string(utf8Buffer);
74
75 ani_namespace ns;
76 if (ANI_OK != env->FindNamespace("L@ohos/arkui/performanceMonitor/performanceMonitor;", &ns)) {
77 return ;
78 }
79
80 OHOS::Ace::PerfMonitor* pMonitor = nullptr;
81 pMonitor = OHOS::Ace::PerfMonitor::GetPerfMonitor();
82 if (pMonitor != nullptr) {
83 pMonitor->End(sceneId, true);
84 }
85 }
86
ANI_Constructor(ani_vm * vm,uint32_t * result)87 ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result)
88 {
89 ani_env *env;
90 if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) {
91 std::cerr << "Unsupported ANI_VERSION_1" << std::endl;
92 return ANI_ERROR;
93 }
94
95 ani_namespace ns;
96 if (ANI_OK != env->FindNamespace("L@ohos/arkui/performanceMonitor/performanceMonitor;", &ns)) {
97 return ANI_ERROR;
98 }
99 std::array methods = {
100 ani_native_function {"begin", nullptr, reinterpret_cast<void *>(Begin)},
101 ani_native_function {"end", nullptr, reinterpret_cast<void *>(End)},
102 };
103 if (ANI_OK != env->Namespace_BindNativeFunctions(ns, methods.data(), methods.size())) {
104 return ANI_ERROR;
105 }
106
107 *result = ANI_VERSION_1;
108 return ANI_OK;
109 }
110