Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/arc/arc_low_memory_killer_monitor.h" | |
| 6 | |
| 7 #include <errno.h> | |
| 8 #include <fcntl.h> | |
| 9 #include <stdio.h> | |
| 10 #include <vector> | |
|
Yusuke Sato
2016/03/15 16:59:45
nit: space between line 9 and 10
cylee1
2016/03/15 18:26:57
Done.
| |
| 11 | |
| 12 #include "base/bind.h" | |
| 13 #include "base/files/file_util.h" | |
| 14 #include "base/location.h" | |
| 15 #include "base/logging.h" | |
| 16 #include "base/metrics/histogram_macros.h" | |
| 17 #include "base/posix/safe_strerror.h" | |
| 18 #include "base/sequenced_task_runner.h" | |
| 19 #include "base/strings/string_number_conversions.h" | |
| 20 #include "base/strings/string_split.h" | |
| 21 #include "base/time/time.h" | |
| 22 #include "third_party/re2/src/re2/re2.h" | |
| 23 | |
| 24 namespace arc { | |
| 25 | |
| 26 using base::SequencedWorkerPool; | |
| 27 using base::StringPiece; | |
| 28 using base::TimeDelta; | |
| 29 | |
| 30 #define MAX_LOWMEMORYKILL_TIME_SECS 30 | |
| 31 #define UMA_HISTOGRAM_LOWMEMORYKILL_TIMES(name, sample) \ | |
| 32 UMA_HISTOGRAM_CUSTOM_TIMES( \ | |
| 33 name, sample, base::TimeDelta::FromMilliseconds(1), \ | |
| 34 base::TimeDelta::FromSeconds(MAX_LOWMEMORYKILL_TIME_SECS), 50) | |
| 35 | |
| 36 ArcLowMemoryKillerMonitor::ArcLowMemoryKillerMonitor() | |
| 37 : worker_pool_( | |
| 38 new SequencedWorkerPool(1, "arc_low_memory_killer_monitor")), | |
| 39 weak_ptr_factory_(this) {} | |
| 40 | |
| 41 ArcLowMemoryKillerMonitor::~ArcLowMemoryKillerMonitor() { | |
| 42 Stop(); | |
| 43 } | |
| 44 | |
| 45 void ArcLowMemoryKillerMonitor::Start() { | |
| 46 auto task_runner = worker_pool_->GetTaskRunnerWithShutdownBehavior( | |
| 47 SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | |
| 48 task_runner->PostTask( | |
| 49 FROM_HERE, | |
| 50 base::Bind(&ArcLowMemoryKillerMonitor::Run, | |
| 51 weak_ptr_factory_.GetWeakPtr(), | |
| 52 worker_pool_)); | |
| 53 } | |
| 54 | |
| 55 void ArcLowMemoryKillerMonitor::Stop() { | |
| 56 worker_pool_->Shutdown(); | |
| 57 } | |
| 58 | |
| 59 void ArcLowMemoryKillerMonitor::Run( | |
| 60 scoped_refptr<base::SequencedWorkerPool> worker_pool) { | |
| 61 static const int kMaxBufSize = 512; | |
| 62 | |
| 63 | |
| 64 FILE* kmsg_handle = base::OpenFile(base::FilePath("/dev/kmsg"), "r"); | |
| 65 if (!kmsg_handle) { | |
| 66 LOG(WARNING) << "Open /dev/kmsg failed: " << base::safe_strerror(errno); | |
| 67 return; | |
| 68 } | |
| 69 // Skip kernel messages prior to the instantiation of this object to avoid | |
| 70 // double reporting. | |
| 71 fseek(kmsg_handle, 0, SEEK_END); | |
| 72 | |
| 73 char buf[kMaxBufSize]; | |
| 74 int freed_size; | |
| 75 int64_t timestamp, last_timestamp = -1; | |
| 76 const TimeDelta kMaxTimeDelta = TimeDelta::FromSeconds( | |
| 77 MAX_LOWMEMORYKILL_TIME_SECS); | |
| 78 | |
| 79 while (fgets(buf, kMaxBufSize, kmsg_handle)) { | |
| 80 if (worker_pool->IsShutdownInProgress()) { | |
| 81 LOG(WARNING) << "Chrome is shutting down, exit now."; | |
|
Yusuke Sato
2016/03/15 16:59:45
DLOG(INFO), probably?
cylee1
2016/03/15 18:26:57
DVLOG Added
| |
| 82 break; | |
| 83 } | |
| 84 if (RE2::PartialMatch(buf, | |
| 85 "lowmemorykiller: .* to free (\\d+)kB", | |
| 86 &freed_size)) { | |
| 87 std::vector<StringPiece> fields = SplitStringPiece( | |
| 88 buf, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 89 | |
| 90 // The time to last kill event. Could be |kMaxTimeDelta| in case of the | |
| 91 // first kill event. | |
| 92 TimeDelta time_delta(kMaxTimeDelta); | |
| 93 // Timestamp is the third field in a line of /dev/kmsg. | |
| 94 // | |
| 95 // Sample log line: | |
| 96 // 6,2302,533604004,-;lowmemorykiller: Killing 'externalstorage' (21742), | |
| 97 // adj 1000,\x0a to free 27320kB on behalf of 'kswapd0' (47) because\x0a | |
| 98 // cache 181920kB is below limit 184320kB for oom_score_adj 1000\x0a | |
| 99 // Free memory is 1228kB above reserved | |
| 100 if (fields.size() >= 3) { | |
| 101 base::StringToInt64(fields[2], ×tamp); | |
| 102 if (last_timestamp > 0) { | |
| 103 time_delta = TimeDelta::FromMicroseconds(timestamp - last_timestamp); | |
| 104 } | |
| 105 last_timestamp = timestamp; | |
| 106 | |
| 107 UMA_HISTOGRAM_MEMORY_KB( | |
| 108 "ArcRuntime.LowMemoryKiller.FreedSize", freed_size); | |
| 109 UMA_HISTOGRAM_LOWMEMORYKILL_TIMES( | |
| 110 "ArcRuntime.LowMemoryKiller.TimeDelta", time_delta); | |
| 111 } | |
| 112 } | |
| 113 } | |
| 114 base::CloseFile(kmsg_handle); | |
| 115 } | |
| 116 | |
| 117 } // namespace arc | |
| OLD | NEW |