Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 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 | |
Yabin Cui | 0d539c1 | 2022-02-17 09:57:42 -0800 | [diff] [blame] | 17 | #include <wakelock/wakelock.h> |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 18 | #include <include/simpleperf_profcollect.hpp> |
Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 19 | |
Yabin Cui | 0b2673c | 2020-07-15 14:09:34 -0700 | [diff] [blame] | 20 | #include "ETMRecorder.h" |
ThiƩbaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 21 | #include "command.h" |
Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 22 | #include "event_attr.h" |
| 23 | #include "event_fd.h" |
| 24 | #include "event_type.h" |
| 25 | |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 26 | using namespace simpleperf; |
Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 27 | |
Yabin Cui | f158a75 | 2022-01-10 15:35:59 -0800 | [diff] [blame] | 28 | bool HasDriverSupport() { |
| 29 | return ETMRecorder::GetInstance().IsETMDriverAvailable(); |
| 30 | } |
| 31 | |
| 32 | bool HasDeviceSupport() { |
| 33 | auto result = ETMRecorder::GetInstance().CheckEtmSupport(); |
| 34 | if (!result.ok()) { |
| 35 | LOG(DEBUG) << result.error(); |
Yabin Cui | 0b2673c | 2020-07-15 14:09:34 -0700 | [diff] [blame] | 36 | return false; |
| 37 | } |
Yi Kong | 937d045 | 2020-07-14 16:50:44 +0800 | [diff] [blame] | 38 | const EventType* type = FindEventTypeByName("cs-etm", false); |
| 39 | if (type == nullptr) { |
| 40 | return false; |
| 41 | } |
Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 42 | return IsEventAttrSupported(CreateDefaultPerfEventAttr(*type), type->name); |
| 43 | } |
| 44 | |
Yabin Cui | 4abcd87 | 2021-03-23 15:51:08 -0700 | [diff] [blame] | 45 | bool Record(const char* event_name, const char* output, float duration) { |
Yabin Cui | 0d539c1 | 2022-02-17 09:57:42 -0800 | [diff] [blame] | 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 | } |
Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 53 | auto recordCmd = CreateCommandInstance("record"); |
| 54 | std::vector<std::string> args; |
| 55 | args.push_back("-a"); |
Yabin Cui | 4abcd87 | 2021-03-23 15:51:08 -0700 | [diff] [blame] | 56 | args.insert(args.end(), {"-e", event_name}); |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 57 | args.insert(args.end(), {"--duration", std::to_string(duration)}); |
Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 58 | args.insert(args.end(), {"-o", output}); |
| 59 | return recordCmd->Run(args); |
| 60 | } |
| 61 | |
Yi Kong | 49eb6ff | 2021-11-17 00:09:02 +0800 | [diff] [blame] | 62 | bool Inject(const char* traceInput, const char* profileOutput, const char* binary_filter) { |
Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 63 | auto injectCmd = CreateCommandInstance("inject"); |
| 64 | std::vector<std::string> args; |
| 65 | args.insert(args.end(), {"-i", traceInput}); |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 66 | args.insert(args.end(), {"-o", profileOutput}); |
Yi Kong | 49eb6ff | 2021-11-17 00:09:02 +0800 | [diff] [blame] | 67 | if (binary_filter) { |
| 68 | args.insert(args.end(), {"--binary", binary_filter}); |
| 69 | } |
Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 70 | args.insert(args.end(), {"--output", "branch-list"}); |
Yabin Cui | 561bf1b | 2020-11-03 12:11:07 -0800 | [diff] [blame] | 71 | args.emplace_back("--exclude-perf"); |
Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 72 | return injectCmd->Run(args); |
| 73 | } |