Florian Mayer | b433400 | 2018-02-01 11:10:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Primiano Tucci | ee41877 | 2018-09-24 23:01:03 +0100 | [diff] [blame] | 17 | #include "perfetto/base/build_config.h" |
| 18 | |
| 19 | // Watchdog is currently not supported on Mac. This ifdef-based exclusion is |
| 20 | // here only for the Mac build in AOSP. The standalone and chromium builds |
| 21 | // exclude this file at the GN level. However, propagating the per-os exclusion |
| 22 | // through our GN -> BP build file translator is not worth the effort for a |
| 23 | // one-off case. |
| 24 | #if !PERFETTO_BUILDFLAG(PERFETTO_OS_MACOSX) |
| 25 | |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame^] | 26 | #include "perfetto/ext/base/watchdog_posix.h" |
Florian Mayer | b433400 | 2018-02-01 11:10:36 +0000 | [diff] [blame] | 27 | |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 28 | #include <fcntl.h> |
Anna Zappone | 78fbbf3 | 2018-03-26 18:29:05 +0100 | [diff] [blame] | 29 | #include <inttypes.h> |
Florian Mayer | b433400 | 2018-02-01 11:10:36 +0000 | [diff] [blame] | 30 | #include <signal.h> |
| 31 | #include <stdint.h> |
Primiano Tucci | 5e6bc99 | 2018-12-12 15:04:36 +0000 | [diff] [blame] | 32 | |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 33 | #include <fstream> |
| 34 | #include <thread> |
Florian Mayer | b433400 | 2018-02-01 11:10:36 +0000 | [diff] [blame] | 35 | |
Primiano Tucci | 5e6bc99 | 2018-12-12 15:04:36 +0000 | [diff] [blame] | 36 | #include "perfetto/base/build_config.h" |
| 37 | #include "perfetto/base/logging.h" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame^] | 38 | #include "perfetto/ext/base/scoped_file.h" |
| 39 | #include "perfetto/ext/base/thread_utils.h" |
Primiano Tucci | 5e6bc99 | 2018-12-12 15:04:36 +0000 | [diff] [blame] | 40 | |
Primiano Tucci | 7c7f5f5 | 2019-01-15 21:47:04 +0000 | [diff] [blame] | 41 | #if PERFETTO_BUILDFLAG(PERFETTO_EMBEDDER_BUILD) |
| 42 | #error perfetto::base::Watchdog should not be used in Chromium or embedders |
Primiano Tucci | 808d6df | 2018-03-31 13:24:18 +0100 | [diff] [blame] | 43 | #endif |
| 44 | |
Florian Mayer | b433400 | 2018-02-01 11:10:36 +0000 | [diff] [blame] | 45 | namespace perfetto { |
| 46 | namespace base { |
| 47 | |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 48 | namespace { |
| 49 | |
Florian Mayer | 22e4b39 | 2018-03-08 10:20:11 +0000 | [diff] [blame] | 50 | constexpr uint32_t kDefaultPollingInterval = 30 * 1000; |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 51 | |
| 52 | bool IsMultipleOf(uint32_t number, uint32_t divisor) { |
| 53 | return number >= divisor && number % divisor == 0; |
| 54 | } |
| 55 | |
Florian Mayer | 22e4b39 | 2018-03-08 10:20:11 +0000 | [diff] [blame] | 56 | double MeanForArray(const uint64_t array[], size_t size) { |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 57 | uint64_t total = 0; |
| 58 | for (size_t i = 0; i < size; i++) { |
| 59 | total += array[i]; |
| 60 | } |
| 61 | return total / size; |
| 62 | } |
| 63 | |
| 64 | } // namespace |
| 65 | |
| 66 | Watchdog::Watchdog(uint32_t polling_interval_ms) |
| 67 | : polling_interval_ms_(polling_interval_ms) {} |
| 68 | |
| 69 | Watchdog::~Watchdog() { |
Lalit Maganti | e419ccb | 2018-03-06 11:48:03 +0000 | [diff] [blame] | 70 | if (!thread_.joinable()) { |
Primiano Tucci | 0d3bd5e | 2018-10-07 20:49:35 +0200 | [diff] [blame] | 71 | PERFETTO_DCHECK(!enabled_); |
Lalit Maganti | e419ccb | 2018-03-06 11:48:03 +0000 | [diff] [blame] | 72 | return; |
| 73 | } |
Primiano Tucci | 0d3bd5e | 2018-10-07 20:49:35 +0200 | [diff] [blame] | 74 | PERFETTO_DCHECK(enabled_); |
| 75 | enabled_ = false; |
Lalit Maganti | e419ccb | 2018-03-06 11:48:03 +0000 | [diff] [blame] | 76 | exit_signal_.notify_one(); |
| 77 | thread_.join(); |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | Watchdog* Watchdog::GetInstance() { |
| 81 | static Watchdog* watchdog = new Watchdog(kDefaultPollingInterval); |
| 82 | return watchdog; |
| 83 | } |
| 84 | |
| 85 | Watchdog::Timer Watchdog::CreateFatalTimer(uint32_t ms) { |
Primiano Tucci | 0d3bd5e | 2018-10-07 20:49:35 +0200 | [diff] [blame] | 86 | if (!enabled_.load(std::memory_order_relaxed)) |
| 87 | return Watchdog::Timer(0); |
| 88 | |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 89 | return Watchdog::Timer(ms); |
| 90 | } |
| 91 | |
Lalit Maganti | e419ccb | 2018-03-06 11:48:03 +0000 | [diff] [blame] | 92 | void Watchdog::Start() { |
| 93 | std::lock_guard<std::mutex> guard(mutex_); |
| 94 | if (thread_.joinable()) { |
Primiano Tucci | 0d3bd5e | 2018-10-07 20:49:35 +0200 | [diff] [blame] | 95 | PERFETTO_DCHECK(enabled_); |
Lalit Maganti | e419ccb | 2018-03-06 11:48:03 +0000 | [diff] [blame] | 96 | } else { |
Primiano Tucci | 0d3bd5e | 2018-10-07 20:49:35 +0200 | [diff] [blame] | 97 | PERFETTO_DCHECK(!enabled_); |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 98 | |
Lalit Maganti | e419ccb | 2018-03-06 11:48:03 +0000 | [diff] [blame] | 99 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \ |
| 100 | PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) |
| 101 | // Kick the thread to start running but only on Android or Linux. |
Primiano Tucci | 0d3bd5e | 2018-10-07 20:49:35 +0200 | [diff] [blame] | 102 | enabled_ = true; |
Lalit Maganti | e419ccb | 2018-03-06 11:48:03 +0000 | [diff] [blame] | 103 | thread_ = std::thread(&Watchdog::ThreadMain, this); |
| 104 | #endif |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 105 | } |
Lalit Maganti | e419ccb | 2018-03-06 11:48:03 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 108 | void Watchdog::SetMemoryLimit(uint64_t bytes, uint32_t window_ms) { |
Lalit Maganti | e419ccb | 2018-03-06 11:48:03 +0000 | [diff] [blame] | 109 | // Update the fields under the lock. |
| 110 | std::lock_guard<std::mutex> guard(mutex_); |
| 111 | |
| 112 | PERFETTO_CHECK(IsMultipleOf(window_ms, polling_interval_ms_) || bytes == 0); |
| 113 | |
| 114 | size_t size = bytes == 0 ? 0 : window_ms / polling_interval_ms_ + 1; |
| 115 | memory_window_bytes_.Reset(size); |
| 116 | memory_limit_bytes_ = bytes; |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void Watchdog::SetCpuLimit(uint32_t percentage, uint32_t window_ms) { |
Lalit Maganti | e419ccb | 2018-03-06 11:48:03 +0000 | [diff] [blame] | 120 | std::lock_guard<std::mutex> guard(mutex_); |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 121 | |
Lalit Maganti | e419ccb | 2018-03-06 11:48:03 +0000 | [diff] [blame] | 122 | PERFETTO_CHECK(percentage <= 100); |
| 123 | PERFETTO_CHECK(IsMultipleOf(window_ms, polling_interval_ms_) || |
| 124 | percentage == 0); |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 125 | |
Lalit Maganti | e419ccb | 2018-03-06 11:48:03 +0000 | [diff] [blame] | 126 | size_t size = percentage == 0 ? 0 : window_ms / polling_interval_ms_ + 1; |
| 127 | cpu_window_time_ticks_.Reset(size); |
| 128 | cpu_limit_percentage_ = percentage; |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void Watchdog::ThreadMain() { |
Florian Mayer | b03fd28 | 2018-10-03 16:05:16 +0100 | [diff] [blame] | 132 | base::ScopedFile stat_fd(base::OpenFile("/proc/self/stat", O_RDONLY)); |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 133 | if (!stat_fd) { |
| 134 | PERFETTO_ELOG("Failed to open stat file to enforce resource limits."); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | std::unique_lock<std::mutex> guard(mutex_); |
| 139 | for (;;) { |
| 140 | exit_signal_.wait_for(guard, |
| 141 | std::chrono::milliseconds(polling_interval_ms_)); |
Primiano Tucci | 0d3bd5e | 2018-10-07 20:49:35 +0200 | [diff] [blame] | 142 | if (!enabled_) |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 143 | return; |
| 144 | |
| 145 | lseek(stat_fd.get(), 0, SEEK_SET); |
| 146 | |
| 147 | char c[512]; |
| 148 | if (read(stat_fd.get(), c, sizeof(c)) < 0) { |
| 149 | PERFETTO_ELOG("Failed to read stat file to enforce resource limits."); |
| 150 | return; |
| 151 | } |
| 152 | c[sizeof(c) - 1] = '\0'; |
| 153 | |
| 154 | unsigned long int utime = 0l; |
| 155 | unsigned long int stime = 0l; |
| 156 | long int rss_pages = -1l; |
| 157 | PERFETTO_CHECK( |
| 158 | sscanf(c, |
| 159 | "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu" |
| 160 | "%lu %*d %*d %*d %*d %*d %*d %*u %*u %ld", |
| 161 | &utime, &stime, &rss_pages) == 3); |
| 162 | |
| 163 | uint64_t cpu_time = utime + stime; |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 164 | uint64_t rss_bytes = static_cast<uint64_t>(rss_pages) * base::kPageSize; |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 165 | |
| 166 | CheckMemory(rss_bytes); |
| 167 | CheckCpu(cpu_time); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void Watchdog::CheckMemory(uint64_t rss_bytes) { |
| 172 | if (memory_limit_bytes_ == 0) |
| 173 | return; |
| 174 | |
| 175 | // Add the current stat value to the ring buffer and check that the mean |
| 176 | // remains under our threshold. |
| 177 | if (memory_window_bytes_.Push(rss_bytes)) { |
| 178 | if (memory_window_bytes_.Mean() > memory_limit_bytes_) { |
Anna Zappone | 78fbbf3 | 2018-03-26 18:29:05 +0100 | [diff] [blame] | 179 | PERFETTO_ELOG( |
| 180 | "Memory watchdog trigger. Memory window of %f bytes is above the " |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 181 | "%" PRIu64 " bytes limit.", |
Anna Zappone | 78fbbf3 | 2018-03-26 18:29:05 +0100 | [diff] [blame] | 182 | memory_window_bytes_.Mean(), memory_limit_bytes_); |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 183 | kill(getpid(), SIGABRT); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void Watchdog::CheckCpu(uint64_t cpu_time) { |
| 189 | if (cpu_limit_percentage_ == 0) |
| 190 | return; |
| 191 | |
| 192 | // Add the cpu time to the ring buffer. |
| 193 | if (cpu_window_time_ticks_.Push(cpu_time)) { |
| 194 | // Compute the percentage over the whole window and check that it remains |
| 195 | // under the threshold. |
| 196 | uint64_t difference_ticks = cpu_window_time_ticks_.NewestWhenFull() - |
| 197 | cpu_window_time_ticks_.OldestWhenFull(); |
| 198 | double window_interval_ticks = |
| 199 | (static_cast<double>(WindowTimeForRingBuffer(cpu_window_time_ticks_)) / |
| 200 | 1000.0) * |
| 201 | sysconf(_SC_CLK_TCK); |
| 202 | double percentage = static_cast<double>(difference_ticks) / |
| 203 | static_cast<double>(window_interval_ticks) * 100; |
| 204 | if (percentage > cpu_limit_percentage_) { |
Anna Zappone | 78fbbf3 | 2018-03-26 18:29:05 +0100 | [diff] [blame] | 205 | PERFETTO_ELOG("CPU watchdog trigger. %f%% CPU use is above the %" PRIu32 |
| 206 | "%% CPU limit.", |
| 207 | percentage, cpu_limit_percentage_); |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 208 | kill(getpid(), SIGABRT); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 213 | uint32_t Watchdog::WindowTimeForRingBuffer(const WindowedInterval& window) { |
| 214 | return static_cast<uint32_t>(window.size() - 1) * polling_interval_ms_; |
| 215 | } |
| 216 | |
| 217 | bool Watchdog::WindowedInterval::Push(uint64_t sample) { |
| 218 | // Add the sample to the current position in the ring buffer. |
| 219 | buffer_[position_] = sample; |
| 220 | |
| 221 | // Update the position with next one circularily. |
| 222 | position_ = (position_ + 1) % size_; |
| 223 | |
| 224 | // Set the filled flag the first time we wrap. |
| 225 | filled_ = filled_ || position_ == 0; |
| 226 | return filled_; |
| 227 | } |
| 228 | |
| 229 | double Watchdog::WindowedInterval::Mean() const { |
| 230 | return MeanForArray(buffer_.get(), size_); |
| 231 | } |
| 232 | |
| 233 | void Watchdog::WindowedInterval::Clear() { |
| 234 | position_ = 0; |
| 235 | buffer_.reset(new uint64_t[size_]()); |
| 236 | } |
| 237 | |
| 238 | void Watchdog::WindowedInterval::Reset(size_t new_size) { |
| 239 | position_ = 0; |
| 240 | size_ = new_size; |
| 241 | buffer_.reset(new_size == 0 ? nullptr : new uint64_t[new_size]()); |
| 242 | } |
| 243 | |
| 244 | Watchdog::Timer::Timer(uint32_t ms) { |
Primiano Tucci | 0d3bd5e | 2018-10-07 20:49:35 +0200 | [diff] [blame] | 245 | if (!ms) |
| 246 | return; // No-op timer created when the watchdog is disabled. |
| 247 | |
Florian Mayer | f5b8d72 | 2018-02-01 13:12:17 +0000 | [diff] [blame] | 248 | struct sigevent sev = {}; |
Primiano Tucci | 5e6bc99 | 2018-12-12 15:04:36 +0000 | [diff] [blame] | 249 | sev.sigev_notify = SIGEV_THREAD_ID; |
| 250 | sev._sigev_un._tid = base::GetThreadId(); |
Florian Mayer | b433400 | 2018-02-01 11:10:36 +0000 | [diff] [blame] | 251 | sev.sigev_signo = SIGABRT; |
Florian Mayer | b433400 | 2018-02-01 11:10:36 +0000 | [diff] [blame] | 252 | PERFETTO_CHECK(timer_create(CLOCK_MONOTONIC, &sev, &timerid_) != -1); |
Florian Mayer | f5b8d72 | 2018-02-01 13:12:17 +0000 | [diff] [blame] | 253 | struct itimerspec its = {}; |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 254 | its.it_value.tv_sec = ms / 1000; |
| 255 | its.it_value.tv_nsec = 1000000L * (ms % 1000); |
Florian Mayer | b433400 | 2018-02-01 11:10:36 +0000 | [diff] [blame] | 256 | PERFETTO_CHECK(timer_settime(timerid_, 0, &its, nullptr) != -1); |
| 257 | } |
| 258 | |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 259 | Watchdog::Timer::~Timer() { |
| 260 | if (timerid_ != nullptr) { |
| 261 | timer_delete(timerid_); |
| 262 | } |
| 263 | } |
| 264 | |
Hector Dearman | a0eb1a4 | 2018-06-20 10:48:12 +0100 | [diff] [blame] | 265 | Watchdog::Timer::Timer(Timer&& other) noexcept { |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 266 | timerid_ = other.timerid_; |
| 267 | other.timerid_ = nullptr; |
Florian Mayer | b433400 | 2018-02-01 11:10:36 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | } // namespace base |
| 271 | } // namespace perfetto |
Primiano Tucci | ee41877 | 2018-09-24 23:01:03 +0100 | [diff] [blame] | 272 | |
| 273 | #endif // PERFETTO_OS_MACOSX |