xref: /aosp_15_r20/art/test/925-threadgroups/threadgroups.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include <stdio.h>
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "android-base/logging.h"
20*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
21*795d594fSAndroid Build Coastguard Worker #include "jni.h"
22*795d594fSAndroid Build Coastguard Worker #include "jvmti.h"
23*795d594fSAndroid Build Coastguard Worker #include "scoped_local_ref.h"
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker // Test infrastructure
26*795d594fSAndroid Build Coastguard Worker #include "jni_helper.h"
27*795d594fSAndroid Build Coastguard Worker #include "jvmti_helper.h"
28*795d594fSAndroid Build Coastguard Worker #include "test_env.h"
29*795d594fSAndroid Build Coastguard Worker #include "ti_macros.h"
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker namespace art {
32*795d594fSAndroid Build Coastguard Worker namespace Test925ThreadGroups {
33*795d594fSAndroid Build Coastguard Worker 
34*795d594fSAndroid Build Coastguard Worker //   private static native Object[] getThreadGroupInfo();
35*795d594fSAndroid Build Coastguard Worker //   // Returns an array where element 0 is an array of threads and element 1 is an array of groups.
36*795d594fSAndroid Build Coastguard Worker //   private static native Object[] getThreadGroupChildren();
37*795d594fSAndroid Build Coastguard Worker 
Java_art_Test925_getTopThreadGroups(JNIEnv * env,jclass Main_klass)38*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test925_getTopThreadGroups(
39*795d594fSAndroid Build Coastguard Worker     JNIEnv* env, [[maybe_unused]] jclass Main_klass) {
40*795d594fSAndroid Build Coastguard Worker   jthreadGroup* groups;
41*795d594fSAndroid Build Coastguard Worker   jint group_count;
42*795d594fSAndroid Build Coastguard Worker   jvmtiError result = jvmti_env->GetTopThreadGroups(&group_count, &groups);
43*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env, jvmti_env, result)) {
44*795d594fSAndroid Build Coastguard Worker     return nullptr;
45*795d594fSAndroid Build Coastguard Worker   }
46*795d594fSAndroid Build Coastguard Worker 
47*795d594fSAndroid Build Coastguard Worker   auto callback = [&](jint index) -> jobject {
48*795d594fSAndroid Build Coastguard Worker     return groups[index];
49*795d594fSAndroid Build Coastguard Worker   };
50*795d594fSAndroid Build Coastguard Worker   jobjectArray ret = CreateObjectArray(env, group_count, "java/lang/ThreadGroup", callback);
51*795d594fSAndroid Build Coastguard Worker 
52*795d594fSAndroid Build Coastguard Worker   jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(groups));
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker   return ret;
55*795d594fSAndroid Build Coastguard Worker }
56*795d594fSAndroid Build Coastguard Worker 
Java_art_Test925_getThreadGroupInfo(JNIEnv * env,jclass Main_klass,jthreadGroup group)57*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test925_getThreadGroupInfo(
58*795d594fSAndroid Build Coastguard Worker     JNIEnv* env, [[maybe_unused]] jclass Main_klass, jthreadGroup group) {
59*795d594fSAndroid Build Coastguard Worker   jvmtiThreadGroupInfo info;
60*795d594fSAndroid Build Coastguard Worker   jvmtiError result = jvmti_env->GetThreadGroupInfo(group, &info);
61*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env, jvmti_env, result)) {
62*795d594fSAndroid Build Coastguard Worker     return nullptr;
63*795d594fSAndroid Build Coastguard Worker   }
64*795d594fSAndroid Build Coastguard Worker 
65*795d594fSAndroid Build Coastguard Worker   auto callback = [&](jint index) -> jobject {
66*795d594fSAndroid Build Coastguard Worker     switch (index) {
67*795d594fSAndroid Build Coastguard Worker       // The parent.
68*795d594fSAndroid Build Coastguard Worker       case 0:
69*795d594fSAndroid Build Coastguard Worker         return info.parent;
70*795d594fSAndroid Build Coastguard Worker 
71*795d594fSAndroid Build Coastguard Worker       // The name.
72*795d594fSAndroid Build Coastguard Worker       case 1:
73*795d594fSAndroid Build Coastguard Worker         return (info.name == nullptr) ? nullptr : env->NewStringUTF(info.name);
74*795d594fSAndroid Build Coastguard Worker 
75*795d594fSAndroid Build Coastguard Worker       // The priority. Use a string for simplicity of construction.
76*795d594fSAndroid Build Coastguard Worker       case 2:
77*795d594fSAndroid Build Coastguard Worker         return env->NewStringUTF(android::base::StringPrintf("%d", info.max_priority).c_str());
78*795d594fSAndroid Build Coastguard Worker 
79*795d594fSAndroid Build Coastguard Worker       // Whether it's a daemon. Use a string for simplicity of construction.
80*795d594fSAndroid Build Coastguard Worker       case 3:
81*795d594fSAndroid Build Coastguard Worker         return env->NewStringUTF(info.is_daemon == JNI_TRUE ? "true" : "false");
82*795d594fSAndroid Build Coastguard Worker     }
83*795d594fSAndroid Build Coastguard Worker     LOG(FATAL) << "Should not reach here";
84*795d594fSAndroid Build Coastguard Worker     UNREACHABLE();
85*795d594fSAndroid Build Coastguard Worker   };
86*795d594fSAndroid Build Coastguard Worker   return CreateObjectArray(env, 4, "java/lang/Object", callback);
87*795d594fSAndroid Build Coastguard Worker }
88*795d594fSAndroid Build Coastguard Worker 
Java_art_Test925_getThreadGroupChildren(JNIEnv * env,jclass Main_klass,jthreadGroup group)89*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test925_getThreadGroupChildren(
90*795d594fSAndroid Build Coastguard Worker     JNIEnv* env, [[maybe_unused]] jclass Main_klass, jthreadGroup group) {
91*795d594fSAndroid Build Coastguard Worker   jint thread_count;
92*795d594fSAndroid Build Coastguard Worker   jthread* threads;
93*795d594fSAndroid Build Coastguard Worker   jint threadgroup_count;
94*795d594fSAndroid Build Coastguard Worker   jthreadGroup* groups;
95*795d594fSAndroid Build Coastguard Worker 
96*795d594fSAndroid Build Coastguard Worker   jvmtiError result = jvmti_env->GetThreadGroupChildren(group,
97*795d594fSAndroid Build Coastguard Worker                                                         &thread_count,
98*795d594fSAndroid Build Coastguard Worker                                                         &threads,
99*795d594fSAndroid Build Coastguard Worker                                                         &threadgroup_count,
100*795d594fSAndroid Build Coastguard Worker                                                         &groups);
101*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env, jvmti_env, result)) {
102*795d594fSAndroid Build Coastguard Worker     return nullptr;
103*795d594fSAndroid Build Coastguard Worker   }
104*795d594fSAndroid Build Coastguard Worker 
105*795d594fSAndroid Build Coastguard Worker   auto callback = [&](jint component_index) -> jobject {
106*795d594fSAndroid Build Coastguard Worker     if (component_index == 0) {
107*795d594fSAndroid Build Coastguard Worker       // Threads.
108*795d594fSAndroid Build Coastguard Worker       auto inner_callback = [&](jint index) {
109*795d594fSAndroid Build Coastguard Worker         return threads[index];
110*795d594fSAndroid Build Coastguard Worker       };
111*795d594fSAndroid Build Coastguard Worker       return CreateObjectArray(env, thread_count, "java/lang/Thread", inner_callback);
112*795d594fSAndroid Build Coastguard Worker     } else {
113*795d594fSAndroid Build Coastguard Worker       // Groups.
114*795d594fSAndroid Build Coastguard Worker       auto inner_callback = [&](jint index) {
115*795d594fSAndroid Build Coastguard Worker         return groups[index];
116*795d594fSAndroid Build Coastguard Worker       };
117*795d594fSAndroid Build Coastguard Worker       return CreateObjectArray(env, threadgroup_count, "java/lang/ThreadGroup", inner_callback);
118*795d594fSAndroid Build Coastguard Worker     }
119*795d594fSAndroid Build Coastguard Worker   };
120*795d594fSAndroid Build Coastguard Worker   jobjectArray ret = CreateObjectArray(env, 2, "java/lang/Object", callback);
121*795d594fSAndroid Build Coastguard Worker 
122*795d594fSAndroid Build Coastguard Worker   jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(threads));
123*795d594fSAndroid Build Coastguard Worker   jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(groups));
124*795d594fSAndroid Build Coastguard Worker 
125*795d594fSAndroid Build Coastguard Worker   return ret;
126*795d594fSAndroid Build Coastguard Worker }
127*795d594fSAndroid Build Coastguard Worker 
128*795d594fSAndroid Build Coastguard Worker }  // namespace Test925ThreadGroups
129*795d594fSAndroid Build Coastguard Worker }  // namespace art
130