1 /*
2 * Copyright (C) 2020 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 "core_jni_helpers.h"
18
19 #include <cputimeinstate.h>
20
21 namespace android {
22
KernelCpuTotalBpfMapReader_readInternal(JNIEnv * env,jobject)23 static jlongArray KernelCpuTotalBpfMapReader_readInternal(JNIEnv *env, jobject) {
24 auto freqTimes = android::bpf::getTotalCpuFreqTimes();
25 if (!freqTimes) return nullptr;
26
27 std::vector<uint64_t> allTimes;
28 for (const auto &vec : *freqTimes) {
29 for (const auto &timeNs : vec) {
30 allTimes.push_back(timeNs / 1000000);
31 }
32 }
33
34 auto ar = env->NewLongArray(allTimes.size());
35 if (ar != nullptr) {
36 env->SetLongArrayRegion(ar, 0, allTimes.size(),
37 reinterpret_cast<const jlong *>(allTimes.data()));
38 }
39 return ar;
40 }
41
42 static const JNINativeMethod methods[] = {
43 {"readInternal", "()[J", (void *)KernelCpuTotalBpfMapReader_readInternal},
44 };
45
register_com_android_internal_os_KernelCpuTotalBpfMapReader(JNIEnv * env)46 int register_com_android_internal_os_KernelCpuTotalBpfMapReader(JNIEnv *env) {
47 return RegisterMethodsOrDie(env, "com/android/internal/os/KernelCpuTotalBpfMapReader", methods,
48 NELEM(methods));
49 }
50
51 } // namespace android
52