1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <debugstore/debugstore_cxx_bridge.rs.h>
18 #include <log/log.h>
19 #include <nativehelper/JNIHelp.h>
20 #include <nativehelper/ScopedLocalRef.h>
21 #include <nativehelper/ScopedUtfChars.h>
22
23 #include <iterator>
24 #include <sstream>
25 #include <vector>
26
27 #include "core_jni_helpers.h"
28
29 namespace android {
30
31 static struct {
32 jmethodID mGet;
33 jmethodID mSize;
34 } gListClassInfo;
35
list_to_vector(JNIEnv * env,jobject jList)36 static std::vector<std::string> list_to_vector(JNIEnv* env, jobject jList) {
37 std::vector<std::string> vec;
38 jint size = env->CallIntMethod(jList, gListClassInfo.mSize);
39 if (size % 2 != 0) {
40 std::ostringstream oss;
41
42 std::copy(vec.begin(), vec.end(), std::ostream_iterator<std::string>(oss, ", "));
43 ALOGW("DebugStore list size is odd: %d, elements: %s", size, oss.str().c_str());
44
45 return vec;
46 }
47
48 vec.reserve(size);
49
50 for (jint i = 0; i < size; i++) {
51 ScopedLocalRef<jstring> jEntry(env,
52 reinterpret_cast<jstring>(
53 env->CallObjectMethod(jList, gListClassInfo.mGet,
54 i)));
55 ScopedUtfChars cEntry(env, jEntry.get());
56 vec.emplace_back(cEntry.c_str());
57 }
58 return vec;
59 }
60
com_android_internal_os_DebugStore_endEvent(JNIEnv * env,jclass clazz,jlong eventId,jobject jAttributeList)61 static void com_android_internal_os_DebugStore_endEvent(JNIEnv* env, jclass clazz, jlong eventId,
62 jobject jAttributeList) {
63 auto attributes = list_to_vector(env, jAttributeList);
64 debugstore::debug_store_end(static_cast<uint64_t>(eventId), attributes);
65 }
66
com_android_internal_os_DebugStore_beginEvent(JNIEnv * env,jclass clazz,jstring jeventName,jobject jAttributeList)67 static jlong com_android_internal_os_DebugStore_beginEvent(JNIEnv* env, jclass clazz,
68 jstring jeventName,
69 jobject jAttributeList) {
70 ScopedUtfChars eventName(env, jeventName);
71 auto attributes = list_to_vector(env, jAttributeList);
72 jlong eventId =
73 static_cast<jlong>(debugstore::debug_store_begin(eventName.c_str(), attributes));
74 return eventId;
75 }
76
com_android_internal_os_DebugStore_recordEvent(JNIEnv * env,jclass clazz,jstring jeventName,jobject jAttributeList)77 static void com_android_internal_os_DebugStore_recordEvent(JNIEnv* env, jclass clazz,
78 jstring jeventName,
79 jobject jAttributeList) {
80 ScopedUtfChars eventName(env, jeventName);
81 auto attributes = list_to_vector(env, jAttributeList);
82 debugstore::debug_store_record(eventName.c_str(), attributes);
83 }
84
85 static const JNINativeMethod gDebugStoreMethods[] = {
86 /* name, signature, funcPtr */
87 {"beginEventNative", "(Ljava/lang/String;Ljava/util/List;)J",
88 (void*)com_android_internal_os_DebugStore_beginEvent},
89 {"endEventNative", "(JLjava/util/List;)V",
90 (void*)com_android_internal_os_DebugStore_endEvent},
91 {"recordEventNative", "(Ljava/lang/String;Ljava/util/List;)V",
92 (void*)com_android_internal_os_DebugStore_recordEvent},
93 };
94
register_com_android_internal_os_DebugStore(JNIEnv * env)95 int register_com_android_internal_os_DebugStore(JNIEnv* env) {
96 int res = RegisterMethodsOrDie(env, "com/android/internal/os/DebugStore", gDebugStoreMethods,
97 NELEM(gDebugStoreMethods));
98 jclass listClass = FindClassOrDie(env, "java/util/List");
99 gListClassInfo.mGet = GetMethodIDOrDie(env, listClass, "get", "(I)Ljava/lang/Object;");
100 gListClassInfo.mSize = GetMethodIDOrDie(env, listClass, "size", "()I");
101
102 return res;
103 }
104
105 } // namespace android