1 /*
2  * Copyright (C) 2016 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 "base/time_utils.h"
18 #include "jni.h"
19 #include "runtime.h"
20 #include "thread_list.h"
21 
22 namespace art {
23 
Java_Main_suspendAndResume(JNIEnv *,jclass)24 extern "C" JNIEXPORT void JNICALL Java_Main_suspendAndResume(JNIEnv*, jclass) {
25   static constexpr size_t kInitialSleepUS = 100 * 1000;  // 100ms.
26   usleep(kInitialSleepUS);  // Leave some time for threads to get in here before we start suspending.
27   enum Operation {
28     kOPSuspendAll,
29     kOPDumpStack,
30     kOPSuspendAllDumpStack,
31     // Total number of operations.
32     kOPNumber,
33   };
34   const uint64_t start_time = NanoTime();
35   size_t iterations = 0;
36   // Run for a fixed period of 10 seconds.
37   while (NanoTime() - start_time < MsToNs(10 * 1000)) {
38     switch (static_cast<Operation>(iterations % kOPNumber)) {
39       case kOPSuspendAll: {
40         ScopedSuspendAll ssa(__FUNCTION__);
41         usleep(500);
42         break;
43       }
44       case kOPDumpStack: {
45         Runtime::Current()->GetThreadList()->Dump(LOG_STREAM(INFO));
46         usleep(500);
47         break;
48       }
49       case kOPSuspendAllDumpStack: {
50         // Not yet supported.
51         if ((false)) {
52           ScopedSuspendAll ssa(__FUNCTION__);
53           Runtime::Current()->GetThreadList()->Dump(LOG_STREAM(INFO));
54         }
55         break;
56       }
57       case kOPNumber:
58         break;
59     }
60     ++iterations;
61   }
62   LOG(INFO) << "Did " << iterations << " iterations";
63 }
64 
65 }  // namespace art
66