• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 static constexpr uint64_t NSEC_PER_MSEC = 1000000;
24 
copyVecsToArray(JNIEnv * env,std::vector<std::vector<uint64_t>> & vec)25 static jlongArray copyVecsToArray(JNIEnv *env, std::vector<std::vector<uint64_t>> &vec) {
26     jsize s = 0;
27     for (const auto &subVec : vec) s += subVec.size();
28     jlongArray ar = env->NewLongArray(s);
29     jsize start = 0;
30     for (auto &subVec : vec) {
31         for (uint32_t i = 0; i < subVec.size(); ++i) subVec[i] /= NSEC_PER_MSEC;
32         env->SetLongArrayRegion(ar, start, subVec.size(),
33                                 reinterpret_cast<const jlong*>(subVec.data()));
34         start += subVec.size();
35     }
36     return ar;
37 }
38 
getUidCpuFreqTimeMs(JNIEnv * env,jclass,jint uid)39 static jlongArray getUidCpuFreqTimeMs(JNIEnv *env, jclass, jint uid) {
40     auto out = android::bpf::getUidCpuFreqTimes(uid);
41     if (!out) return env->NewLongArray(0);
42     return copyVecsToArray(env, out.value());
43 }
44 
45 static const JNINativeMethod g_single_methods[] = {
46     {"readBpfData", "(I)[J", (void *)getUidCpuFreqTimeMs},
47 };
48 
register_com_android_internal_os_KernelSingleUidTimeReader(JNIEnv * env)49 int register_com_android_internal_os_KernelSingleUidTimeReader(JNIEnv *env) {
50     return RegisterMethodsOrDie(env, "com/android/internal/os/KernelSingleUidTimeReader$Injector",
51                                 g_single_methods, NELEM(g_single_methods));
52 }
53 
54 }
55