blob: 724111e8c485e5a107d795d605de395bc8ca5126 [file] [log] [blame]
Yi Konga86c7e32020-06-03 23:24:36 +08001/*
2 * Copyright (C) 2020 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
Yabin Cui0d539c12022-02-17 09:57:42 -080017#include <wakelock/wakelock.h>
Yi Kongf8abfdb2021-02-18 16:23:37 +080018#include <include/simpleperf_profcollect.hpp>
Yi Konga86c7e32020-06-03 23:24:36 +080019
Yabin Cui0b2673c2020-07-15 14:09:34 -070020#include "ETMRecorder.h"
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +020021#include "command.h"
Yi Konga86c7e32020-06-03 23:24:36 +080022#include "event_attr.h"
23#include "event_fd.h"
24#include "event_type.h"
25
Yi Kongf8abfdb2021-02-18 16:23:37 +080026using namespace simpleperf;
Yi Konga86c7e32020-06-03 23:24:36 +080027
Yabin Cuif158a752022-01-10 15:35:59 -080028bool HasDriverSupport() {
29 return ETMRecorder::GetInstance().IsETMDriverAvailable();
30}
31
32bool HasDeviceSupport() {
33 auto result = ETMRecorder::GetInstance().CheckEtmSupport();
34 if (!result.ok()) {
35 LOG(DEBUG) << result.error();
Yabin Cui0b2673c2020-07-15 14:09:34 -070036 return false;
37 }
Yi Kong937d0452020-07-14 16:50:44 +080038 const EventType* type = FindEventTypeByName("cs-etm", false);
39 if (type == nullptr) {
40 return false;
41 }
Yi Konga86c7e32020-06-03 23:24:36 +080042 return IsEventAttrSupported(CreateDefaultPerfEventAttr(*type), type->name);
43}
44
Yabin Cui4abcd872021-03-23 15:51:08 -070045bool Record(const char* event_name, const char* output, float duration) {
Yabin Cui0d539c12022-02-17 09:57:42 -080046 // The kernel may panic when trying to hibernate or hotplug CPUs while collecting
47 // ETM data. So get wakelock to keep the CPUs on.
48 auto wakelock = android::wakelock::WakeLock::tryGet("profcollectd");
49 if (!wakelock) {
50 LOG(ERROR) << "Failed to request wakelock.";
51 return false;
52 }
Yi Konga86c7e32020-06-03 23:24:36 +080053 auto recordCmd = CreateCommandInstance("record");
54 std::vector<std::string> args;
55 args.push_back("-a");
Yabin Cui4abcd872021-03-23 15:51:08 -070056 args.insert(args.end(), {"-e", event_name});
Yi Kongf8abfdb2021-02-18 16:23:37 +080057 args.insert(args.end(), {"--duration", std::to_string(duration)});
Yi Konga86c7e32020-06-03 23:24:36 +080058 args.insert(args.end(), {"-o", output});
59 return recordCmd->Run(args);
60}
61
Yi Kong49eb6ff2021-11-17 00:09:02 +080062bool Inject(const char* traceInput, const char* profileOutput, const char* binary_filter) {
Yi Konga86c7e32020-06-03 23:24:36 +080063 auto injectCmd = CreateCommandInstance("inject");
64 std::vector<std::string> args;
65 args.insert(args.end(), {"-i", traceInput});
Yi Kongf8abfdb2021-02-18 16:23:37 +080066 args.insert(args.end(), {"-o", profileOutput});
Yi Kong49eb6ff2021-11-17 00:09:02 +080067 if (binary_filter) {
68 args.insert(args.end(), {"--binary", binary_filter});
69 }
Yi Konga86c7e32020-06-03 23:24:36 +080070 args.insert(args.end(), {"--output", "branch-list"});
Yabin Cui561bf1b2020-11-03 12:11:07 -080071 args.emplace_back("--exclude-perf");
Yi Konga86c7e32020-06-03 23:24:36 +080072 return injectCmd->Run(args);
73}