summaryrefslogtreecommitdiff
path: root/simpleperf/demo/CppApi/app/src/main/cpp/native-lib.cpp
blob: bdf5f468384b3217cec451250bb0454a1c0e0cdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <android/log.h>
#include <jni.h>
#include <pthread.h>
#include <simpleperf.h>
#include <unistd.h>

#include <atomic>
#include <string>

static void log(const char* msg) {
  __android_log_print(ANDROID_LOG_INFO, "simpleperf", "%s", msg);
}

static std::atomic_bool profile_thread_exited(false);

void* ProfileThreadFunc(void*) {
  pthread_setname_np(pthread_self(), "ProfileThread");
  simpleperf::RecordOptions options;
  options.RecordDwarfCallGraph();
  simpleperf::ProfileSession session;
  log("start recording");
  session.StartRecording(options);
  for (int i = 0; i < 3; i++) {
    sleep(1);
    log("pause recording");
    session.PauseRecording();
    sleep(1);
    log("resume recording");
    session.ResumeRecording();
  }
  sleep(1);
  log("stop recording");
  session.StopRecording();
  log("stop recording successfully");
  profile_thread_exited = true;
  return nullptr;
};

int CallFunction(int a) {
  return a + 1;
}

static std::atomic_int64_t count;

void* BusyThreadFunc(void*) {
  pthread_setname_np(pthread_self(), "BusyThread");
  count = 0;
  volatile int i;
  while (!profile_thread_exited) {
    for (i = 0; i < 1000000; ) {
      i = CallFunction(i);
    }
    timespec ts;
    ts.tv_sec = 0;
    ts.tv_nsec = 1000000;
    nanosleep(&ts, nullptr);
    count++;
  }
  return nullptr;
}

extern "C" JNIEXPORT void JNICALL
Java_simpleperf_demo_cpp_1api_MainActivity_runNativeCode(
    JNIEnv *env,
    jobject jobj) {
  static bool threadsStarted = false;
  if (!threadsStarted) {
    pthread_t profile_thread;
    if (pthread_create(&profile_thread, nullptr, ProfileThreadFunc, nullptr) != 0) {
      log("failed to create profile thread");
      return;
    }
    pthread_t busy_thread;
    if (pthread_create(&busy_thread, nullptr, BusyThreadFunc, nullptr) != 0) {
      log("failed to create busy thread");
    }
    threadsStarted = true;
  }
}

extern "C" JNIEXPORT jlong JNICALL
Java_simpleperf_demo_cpp_1api_MainActivity_getBusyThreadCount(
    JNIEnv *env,
    jobject jobj) {
  return static_cast<jlong>(count);
}