• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 <android/log.h>
18 #include <jni.h>
19 #include <pthread.h>
20 #include <simpleperf.h>
21 #include <unistd.h>
22 
23 #include <atomic>
24 #include <string>
25 
log(const char * msg)26 static void log(const char* msg) {
27   __android_log_print(ANDROID_LOG_INFO, "simpleperf", "%s", msg);
28 }
29 
30 static std::atomic_bool profile_thread_exited(false);
31 
ProfileThreadFunc(void *)32 void* ProfileThreadFunc(void*) {
33   pthread_setname_np(pthread_self(), "ProfileThread");
34   simpleperf::RecordOptions options;
35   options.RecordDwarfCallGraph().SetEvent("cpu-clock");
36   simpleperf::ProfileSession session;
37   log("start recording");
38   session.StartRecording(options);
39   for (int i = 0; i < 1; i++) {
40     sleep(1);
41     log("pause recording");
42     session.PauseRecording();
43     sleep(1);
44     log("resume recording");
45     session.ResumeRecording();
46   }
47   sleep(1);
48   log("stop recording");
49   session.StopRecording();
50   log("stop recording successfully");
51   profile_thread_exited = true;
52   return nullptr;
53 };
54 
CallFunction(int a)55 int CallFunction(int a) {
56   return a + 1;
57 }
58 
59 static std::atomic_int64_t count;
60 
BusyThreadFunc(void *)61 void* BusyThreadFunc(void*) {
62   pthread_setname_np(pthread_self(), "BusyThread");
63   count = 0;
64   volatile int i;
65   while (!profile_thread_exited) {
66     for (i = 0; i < 1000000; ) {
67       i = CallFunction(i);
68     }
69     timespec ts;
70     ts.tv_sec = 0;
71     ts.tv_nsec = 1000000;
72     nanosleep(&ts, nullptr);
73     count++;
74   }
75   // Exit after recording. So we can build test apk, and expect profiling data file after app exits.
76   exit(0);
77   return nullptr;
78 }
79 
80 extern "C" JNIEXPORT void JNICALL
Java_simpleperf_demo_cpp_1api_MainActivity_runNativeCode(JNIEnv * env,jobject jobj)81 Java_simpleperf_demo_cpp_1api_MainActivity_runNativeCode(
82     JNIEnv *env,
83     jobject jobj) {
84   static bool threadsStarted = false;
85   if (!threadsStarted) {
86     pthread_t profile_thread;
87     if (pthread_create(&profile_thread, nullptr, ProfileThreadFunc, nullptr) != 0) {
88       log("failed to create profile thread");
89       return;
90     }
91     pthread_t busy_thread;
92     if (pthread_create(&busy_thread, nullptr, BusyThreadFunc, nullptr) != 0) {
93       log("failed to create busy thread");
94     }
95     threadsStarted = true;
96   }
97 }
98 
99 extern "C" JNIEXPORT jlong JNICALL
Java_simpleperf_demo_cpp_1api_MainActivity_getBusyThreadCount(JNIEnv * env,jobject jobj)100 Java_simpleperf_demo_cpp_1api_MainActivity_getBusyThreadCount(
101     JNIEnv *env,
102     jobject jobj) {
103   return static_cast<jlong>(count);
104 }
105