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 <jni.h>
18 #include <unistd.h>
19 #include <cstdint>
20 #include <cstdio>
21 #include <cstdlib>
22
23 #include <fstream>
24 #include <limits>
25
26 namespace {
27
28 // Must be kept in sync with heapprofd_test_cts.cc
29 constexpr int kIndividualAllocSz = 4153;
30 constexpr int kAllocationIntervalUs = 10 * 1000;
31
32 // Increments a value in the text file `path`. The file is read by the CTS test
33 // to observe the app progress.
ReportCycle(const char * path)34 void ReportCycle(const char* path) {
35 int64_t value = 0;
36 {
37 // Read the previous value from the file (it might be from a separate
38 // execution of this app).
39 std::ifstream ifs(path);
40 if (ifs) {
41 ifs >> value;
42 }
43 }
44
45 std::string tmppath = std::string(path) + std::string(".tmp");
46 std::ofstream ofs(tmppath, std::ios::trunc);
47 if (value == std::numeric_limits<int64_t>::max()) {
48 value = std::numeric_limits<int64_t>::min();
49 } else {
50 value++;
51 }
52 ofs << value;
53 ofs.close();
54 if (!ofs) {
55 abort();
56 }
57 rename(tmppath.c_str(), path);
58 }
59
perfetto_test_allocations(const char * report_cycle_path)60 __attribute__((noreturn)) void perfetto_test_allocations(
61 const char* report_cycle_path) {
62 for (;;) {
63 for (size_t j = 0; j < 20; j++) {
64 // volatile & use avoids builtin malloc optimizations
65 volatile char* x = static_cast<char*>(malloc(kIndividualAllocSz));
66 if (x) {
67 x[0] = '\0';
68 free(const_cast<char*>(x));
69 }
70 usleep(kAllocationIntervalUs);
71 }
72 ReportCycle(report_cycle_path);
73 }
74 }
75
76 // Runs continuously as a target for the sampling perf profiler tests.
perfetto_busy_wait()77 __attribute__((noreturn)) void perfetto_busy_wait() {
78 for (volatile unsigned i = 0;; i++) {
79 }
80 }
81
82 } // namespace
83
84 extern "C" JNIEXPORT void JNICALL
Java_android_perfetto_cts_app_MainActivity_runNative(JNIEnv * env,jclass,jstring jreport_cycle_path)85 Java_android_perfetto_cts_app_MainActivity_runNative(
86 JNIEnv* env,
87 jclass,
88 jstring jreport_cycle_path) {
89 const char* path = env->GetStringUTFChars(jreport_cycle_path, NULL);
90 perfetto_test_allocations(path);
91 env->ReleaseStringUTFChars(jreport_cycle_path, NULL);
92 }
93
94 extern "C" JNIEXPORT void JNICALL
Java_android_perfetto_cts_app_BusyWaitActivity_runNativeBusyWait(JNIEnv *,jclass)95 Java_android_perfetto_cts_app_BusyWaitActivity_runNativeBusyWait(JNIEnv*,
96 jclass) {
97 perfetto_busy_wait();
98 }
99