1 /*
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
17 #include <wakelock/wakelock.h>
18 #include <include/simpleperf_profcollect.hpp>
19
20 #include "ETMRecorder.h"
21 #include "command.h"
22 #include "event_attr.h"
23 #include "event_fd.h"
24 #include "event_type.h"
25
26 using namespace simpleperf;
27
HasDriverSupport()28 bool HasDriverSupport() {
29 return ETMRecorder::GetInstance().IsETMDriverAvailable();
30 }
31
HasDeviceSupport()32 bool HasDeviceSupport() {
33 auto result = ETMRecorder::GetInstance().CheckEtmSupport();
34 if (!result.ok()) {
35 LOG(DEBUG) << result.error();
36 return false;
37 }
38 const EventType* type = FindEventTypeByName("cs-etm", false);
39 if (type == nullptr) {
40 return false;
41 }
42 return IsEventAttrSupported(CreateDefaultPerfEventAttr(*type), type->name);
43 }
44
Record(const char * event_name,const char * output,float duration)45 bool Record(const char* event_name, const char* output, float duration) {
46 // 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 }
53 auto recordCmd = CreateCommandInstance("record");
54 std::vector<std::string> args;
55 args.push_back("-a");
56 args.insert(args.end(), {"-e", event_name});
57 args.insert(args.end(), {"--duration", std::to_string(duration)});
58 args.insert(args.end(), {"-o", output});
59 return recordCmd->Run(args);
60 }
61
Inject(const char * traceInput,const char * profileOutput,const char * binary_filter)62 bool Inject(const char* traceInput, const char* profileOutput, const char* binary_filter) {
63 auto injectCmd = CreateCommandInstance("inject");
64 std::vector<std::string> args;
65 args.insert(args.end(), {"-i", traceInput});
66 args.insert(args.end(), {"-o", profileOutput});
67 if (binary_filter) {
68 args.insert(args.end(), {"--binary", binary_filter});
69 }
70 args.insert(args.end(), {"--output", "branch-list"});
71 args.emplace_back("--exclude-perf");
72 return injectCmd->Run(args);
73 }
74