Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/arc/metrics/oom_kills_monitor.h" | 5 #include "components/arc/metrics/oom_kills_monitor.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <stdio.h> | 9 #include <stdio.h> |
| 10 | 10 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 #include "third_party/re2/src/re2/re2.h" | 26 #include "third_party/re2/src/re2/re2.h" |
| 27 #include "third_party/re2/src/re2/stringpiece.h" | 27 #include "third_party/re2/src/re2/stringpiece.h" |
| 28 | 28 |
| 29 namespace arc { | 29 namespace arc { |
| 30 | 30 |
| 31 using base::StringPiece; | 31 using base::StringPiece; |
| 32 | 32 |
| 33 using base::SequencedWorkerPool; | 33 using base::SequencedWorkerPool; |
| 34 using base::TimeDelta; | 34 using base::TimeDelta; |
| 35 | 35 |
| 36 OomKillsMonitor::OomKillsMonitor() | 36 namespace { |
| 37 : worker_pool_( | |
| 38 new SequencedWorkerPool(1, "oom_kills_monitor")) {} | |
| 39 | 37 |
| 40 OomKillsMonitor::~OomKillsMonitor() { | 38 base::LazyInstance<OomKillsMonitor>::Leaky g_instance = |
| 41 Stop(); | 39 LAZY_INSTANCE_INITIALIZER; |
| 42 } | |
| 43 | |
| 44 void OomKillsMonitor::Start() { | |
| 45 auto task_runner = worker_pool_->GetTaskRunnerWithShutdownBehavior( | |
| 46 SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | |
| 47 task_runner->PostTask( | |
| 48 FROM_HERE, base::Bind(&OomKillsMonitor::Run, worker_pool_)); | |
| 49 } | |
| 50 | |
| 51 void OomKillsMonitor::Stop() { | |
| 52 worker_pool_->Shutdown(); | |
| 53 } | |
| 54 | |
| 55 namespace { | |
| 56 | 40 |
| 57 int64_t GetTimestamp(const StringPiece& line) { | 41 int64_t GetTimestamp(const StringPiece& line) { |
| 58 std::vector<StringPiece> fields = base::SplitStringPiece( | 42 std::vector<StringPiece> fields = base::SplitStringPiece( |
| 59 line, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 43 line, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 60 | 44 |
| 61 int64_t timestamp = -1; | 45 int64_t timestamp = -1; |
| 62 // Timestamp is the third field in a line of /dev/kmsg. | 46 // Timestamp is the third field in a line of /dev/kmsg. |
| 63 if (fields.size() < 3 || !base::StringToInt64(fields[2], ×tamp)) | 47 if (fields.size() < 3 || !base::StringToInt64(fields[2], ×tamp)) |
| 64 return -1; | 48 return -1; |
| 65 return timestamp; | 49 return timestamp; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 | 131 |
| 148 if (GetTimeDelta(line, &last_timestamp, &time_delta)) { | 132 if (GetTimeDelta(line, &last_timestamp, &time_delta)) { |
| 149 UMA_HISTOGRAM_OOM_KILL_TIME_INTERVAL( | 133 UMA_HISTOGRAM_OOM_KILL_TIME_INTERVAL( |
| 150 "Arc.LowMemoryKiller.TimeDelta", time_delta); | 134 "Arc.LowMemoryKiller.TimeDelta", time_delta); |
| 151 } | 135 } |
| 152 } | 136 } |
| 153 } | 137 } |
| 154 | 138 |
| 155 } // namespace | 139 } // namespace |
| 156 | 140 |
| 141 OomKillsMonitor::OomKillsMonitorHandle::OomKillsMonitorHandle( | |
| 142 OomKillsMonitor* outer) : outer_(outer) { DCHECK(outer_); } | |
| 143 | |
| 144 OomKillsMonitor::OomKillsMonitorHandle::~OomKillsMonitorHandle() { | |
| 145 outer_->is_shutting_down_.Set(); | |
| 146 } | |
| 147 | |
| 148 OomKillsMonitor::OomKillsMonitor() | |
| 149 : worker_thread_("oom_kills_monitor") {} | |
| 150 | |
| 157 // static | 151 // static |
| 158 void OomKillsMonitor::Run( | 152 void OomKillsMonitor::StartMonitoring() { |
| 159 scoped_refptr<base::SequencedWorkerPool> worker_pool) { | 153 OomKillsMonitor* instance = g_instance.Pointer(); |
| 154 | |
| 155 DCHECK(!instance->IsRunning()); | |
| 156 | |
| 157 base::Thread::Options thread_options; | |
| 158 thread_options.joinable = false; | |
| 159 instance->StartWithOptions(thread_options); | |
| 160 } | |
|
Luis Héctor Chávez
2016/08/01 15:48:14
return OomKillsMonitorHandle(instance); ?
gab
2016/08/04 22:42:36
Yea I was trying to do this on my Windows machine.
| |
| 161 | |
| 162 void OomKillsMonitor::Run() { | |
| 160 base::ScopedFILE kmsg_handle( | 163 base::ScopedFILE kmsg_handle( |
| 161 base::OpenFile(base::FilePath("/dev/kmsg"), "r")); | 164 base::OpenFile(base::FilePath("/dev/kmsg"), "r")); |
| 162 if (!kmsg_handle) { | 165 if (!kmsg_handle) { |
| 163 LOG(WARNING) << "Open /dev/kmsg failed: " << base::safe_strerror(errno); | 166 LOG(WARNING) << "Open /dev/kmsg failed: " << base::safe_strerror(errno); |
| 164 return; | 167 return; |
| 165 } | 168 } |
| 166 // Skip kernel messages prior to the instantiation of this object to avoid | 169 // Skip kernel messages prior to the instantiation of this object to avoid |
| 167 // double reporting. | 170 // double reporting. |
| 168 fseek(kmsg_handle.get(), 0, SEEK_END); | 171 fseek(kmsg_handle.get(), 0, SEEK_END); |
| 169 | 172 |
| 170 static const int kMaxBufSize = 512; | 173 static const int kMaxBufSize = 512; |
| 171 char buf[kMaxBufSize]; | 174 char buf[kMaxBufSize]; |
| 172 | 175 |
| 173 while (fgets(buf, kMaxBufSize, kmsg_handle.get())) { | 176 while (fgets(buf, kMaxBufSize, kmsg_handle.get())) { |
| 174 if (worker_pool->IsShutdownInProgress()) { | 177 if (is_shutting_down_.IsSet()) { |
| 175 DVLOG(1) << "Chrome is shutting down, exit now."; | 178 DVLOG(1) << "Chrome is shutting down, exit now."; |
| 176 break; | 179 break; |
| 177 } | 180 } |
| 178 const StringPiece buf_string(buf); | 181 const StringPiece buf_string(buf); |
| 179 LogOOMKill(buf_string); | 182 LogOOMKill(buf_string); |
| 180 LogLowMemoryKill(buf_string); | 183 LogLowMemoryKill(buf_string); |
| 181 } | 184 } |
| 182 } | 185 } |
| 183 | 186 |
| 184 } // namespace arc | 187 } // namespace arc |
| OLD | NEW |