| 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 |
|
| 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 |
|
| 32 |
void* ProfileThreadFunc(void*) { |
| 33 |
pthread_setname_np(pthread_self(), "ProfileThread"); |
| 34 |
simpleperf::RecordOptions options; |
| 35 |
options.RecordDwarfCallGraph(); |
| 36 |
simpleperf::ProfileSession session; |
| 37 |
log("start recording"); |
| 38 |
session.StartRecording(options); |
| 39 |
for (int i = 0; i < 3; 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 |
|
| 55 |
int CallFunction(int a) { |
| 56 |
return a + 1; |
| 57 |
} |
| 58 |
|
| 59 |
static std::atomic_int64_t count; |
| 60 |
|
| 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 |
return nullptr; |
| 76 |
} |
| 77 |
|
| 78 |
extern "C" JNIEXPORT void JNICALL |
| 79 |
Java_simpleperf_demo_cpp_1api_MainActivity_runNativeCode( |
| 80 |
JNIEnv *env, |
| 81 |
jobject jobj) { |
| 82 |
pthread_t profile_thread; |
| 83 |
if (pthread_create(&profile_thread, nullptr, ProfileThreadFunc, nullptr) != 0) { |
| 84 |
log("failed to create profile thread"); |
| 85 |
return; |
| 86 |
} |
| 87 |
pthread_t busy_thread; |
| 88 |
if (pthread_create(&busy_thread, nullptr, BusyThreadFunc, nullptr) != 0) { |
| 89 |
log("failed to create busy thread"); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
extern "C" JNIEXPORT jlong JNICALL |
| 94 |
Java_simpleperf_demo_cpp_1api_MainActivity_getBusyThreadCount( |
| 95 |
JNIEnv *env, |
| 96 |
jobject jobj) { |
| 97 |
return static_cast<jlong>(count); |
| 98 |
} |