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 | 9eb3093 | 2023-04-27 12:50:59 -0700 | [diff] [blame] | 17 | #include <time.h> |
| 18 | |
| 19 | #include <android-base/file.h> |
| 20 | #include <android-base/stringprintf.h> |
| 21 | |
Yabin Cui | 0d539c1 | 2022-02-17 09:57:42 -0800 | [diff] [blame] | 22 | #include <wakelock/wakelock.h> |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 23 | #include <include/simpleperf_profcollect.hpp> |
Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 24 | |
Yabin Cui | 0b2673c | 2020-07-15 14:09:34 -0700 | [diff] [blame] | 25 | #include "ETMRecorder.h" |
ThiƩbaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 26 | #include "command.h" |
Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 27 | #include "event_attr.h" |
| 28 | #include "event_fd.h" |
| 29 | #include "event_type.h" |
| 30 | |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 31 | using namespace simpleperf; |
Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 32 | |
Yabin Cui | f158a75 | 2022-01-10 15:35:59 -0800 | [diff] [blame] | 33 | bool HasDriverSupport() { |
Yabin Cui | 9eb3093 | 2023-04-27 12:50:59 -0700 | [diff] [blame] | 34 | bool result = ETMRecorder::GetInstance().IsETMDriverAvailable(); |
| 35 | LOG(INFO) << "HasDriverSupport result " << result; |
| 36 | return result; |
Yabin Cui | f158a75 | 2022-01-10 15:35:59 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | bool HasDeviceSupport() { |
| 40 | auto result = ETMRecorder::GetInstance().CheckEtmSupport(); |
| 41 | if (!result.ok()) { |
Yabin Cui | 9eb3093 | 2023-04-27 12:50:59 -0700 | [diff] [blame] | 42 | LOG(INFO) << "HasDeviceSupport check failed: " << result.error(); |
Yabin Cui | 0b2673c | 2020-07-15 14:09:34 -0700 | [diff] [blame] | 43 | return false; |
| 44 | } |
Yi Kong | 937d045 | 2020-07-14 16:50:44 +0800 | [diff] [blame] | 45 | const EventType* type = FindEventTypeByName("cs-etm", false); |
| 46 | if (type == nullptr) { |
Yabin Cui | 9eb3093 | 2023-04-27 12:50:59 -0700 | [diff] [blame] | 47 | LOG(INFO) << "HasDeviceSupport check failed: no etm event"; |
Yi Kong | 937d045 | 2020-07-14 16:50:44 +0800 | [diff] [blame] | 48 | return false; |
| 49 | } |
Yabin Cui | 9eb3093 | 2023-04-27 12:50:59 -0700 | [diff] [blame] | 50 | bool ret = IsEventAttrSupported(CreateDefaultPerfEventAttr(*type), type->name); |
| 51 | LOG(INFO) << "HasDeviceSupport result " << ret; |
| 52 | return ret; |
Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 53 | } |
| 54 | |
Yabin Cui | 4b46790 | 2023-05-02 15:12:32 -0700 | [diff] [blame] | 55 | bool Record(const char* event_name, const char* output, float duration, const char* binary_filter) { |
| 56 | LOG(INFO) << "Record " << event_name << ", duration " << duration << ", output " << output |
| 57 | << ", binary_filter " << binary_filter; |
Yabin Cui | 0d539c1 | 2022-02-17 09:57:42 -0800 | [diff] [blame] | 58 | // The kernel may panic when trying to hibernate or hotplug CPUs while collecting |
| 59 | // ETM data. So get wakelock to keep the CPUs on. |
| 60 | auto wakelock = android::wakelock::WakeLock::tryGet("profcollectd"); |
| 61 | if (!wakelock) { |
Yabin Cui | 9eb3093 | 2023-04-27 12:50:59 -0700 | [diff] [blame] | 62 | LOG(ERROR) << "Record failed: Failed to request wakelock."; |
Yabin Cui | 0d539c1 | 2022-02-17 09:57:42 -0800 | [diff] [blame] | 63 | return false; |
| 64 | } |
Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 65 | auto recordCmd = CreateCommandInstance("record"); |
Yabin Cui | 4b46790 | 2023-05-02 15:12:32 -0700 | [diff] [blame] | 66 | std::vector<std::string> args = {"-a", |
| 67 | "-e", |
| 68 | event_name, |
| 69 | "--duration", |
| 70 | std::to_string(duration), |
| 71 | "--decode-etm", |
| 72 | "--exclude-perf", |
| 73 | "--binary", |
| 74 | binary_filter, |
| 75 | "-o", |
| 76 | output}; |
Yabin Cui | 9eb3093 | 2023-04-27 12:50:59 -0700 | [diff] [blame] | 77 | bool result = recordCmd->Run(args); |
| 78 | LOG(INFO) << "Record result " << result; |
| 79 | return result; |
Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 80 | } |
| 81 | |
Yi Kong | 49eb6ff | 2021-11-17 00:09:02 +0800 | [diff] [blame] | 82 | bool Inject(const char* traceInput, const char* profileOutput, const char* binary_filter) { |
Yabin Cui | 9eb3093 | 2023-04-27 12:50:59 -0700 | [diff] [blame] | 83 | LOG(INFO) << "Inject traceInput " << traceInput << ", profileOutput " << profileOutput |
| 84 | << ", binary_filter " << binary_filter; |
Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 85 | auto injectCmd = CreateCommandInstance("inject"); |
Yabin Cui | 4b46790 | 2023-05-02 15:12:32 -0700 | [diff] [blame] | 86 | std::vector<std::string> args = {"-i", traceInput, "-o", profileOutput, |
| 87 | "--output", "branch-list", "--binary", binary_filter}; |
Yabin Cui | 9eb3093 | 2023-04-27 12:50:59 -0700 | [diff] [blame] | 88 | bool result = injectCmd->Run(args); |
| 89 | LOG(INFO) << "Inject result " << result; |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | static android::base::unique_fd log_fd; |
| 94 | static android::base::LogFunction saved_log_func; |
| 95 | |
| 96 | static void FileLogger(android::base::LogId id, android::base::LogSeverity severity, |
| 97 | const char* tag, const char* file, unsigned int line, const char* message) { |
| 98 | if (log_fd.ok()) { |
| 99 | static const char log_characters[] = "VDIWEFF"; |
| 100 | char severity_char = log_characters[severity]; |
| 101 | struct tm now; |
| 102 | time_t t = time(nullptr); |
| 103 | localtime_r(&t, &now); |
| 104 | char timestamp[32]; |
| 105 | strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now); |
| 106 | std::string s = android::base::StringPrintf("%s %c %s %s:%u] %s\n", tag, severity_char, |
| 107 | timestamp, file, line, message); |
| 108 | WriteStringToFd(s, log_fd); |
| 109 | } |
| 110 | saved_log_func(id, severity, tag, file, line, message); |
| 111 | } |
| 112 | |
| 113 | void SetLogFile(const char* filename) { |
| 114 | int fd = TEMP_FAILURE_RETRY(open(filename, O_APPEND | O_CREAT | O_WRONLY | O_CLOEXEC, 0600)); |
| 115 | if (fd == -1) { |
| 116 | PLOG(ERROR) << "failed to open " << filename; |
| 117 | return; |
| 118 | } |
| 119 | log_fd.reset(fd); |
| 120 | saved_log_func = SetLogger(FileLogger); |
| 121 | } |
| 122 | |
| 123 | void ResetLogFile() { |
| 124 | log_fd.reset(); |
| 125 | SetLogger(std::move(saved_log_func)); |
Yi Kong | a86c7e3 | 2020-06-03 23:24:36 +0800 | [diff] [blame] | 126 | } |