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