OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/memory/tab_manager_delegate_chromeos.h" | 5 #include "chrome/browser/memory/tab_manager_delegate_chromeos.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <map> | 10 #include <map> |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
244 return ReadIntFromFile( | 244 return ReadIntFromFile( |
245 kLowMemoryMarginConfig, kDefaultLowMemoryMarginMb) * 1024; | 245 kLowMemoryMarginConfig, kDefaultLowMemoryMarginMb) * 1024; |
246 } | 246 } |
247 | 247 |
248 // The logic of available memory calculation is copied from | 248 // The logic of available memory calculation is copied from |
249 // _is_low_mem_situation() in kernel file include/linux/low-mem-notify.h. | 249 // _is_low_mem_situation() in kernel file include/linux/low-mem-notify.h. |
250 // Maybe we should let kernel report the number directly. | 250 // Maybe we should let kernel report the number directly. |
251 int TabManagerDelegate::MemoryStat::TargetMemoryToFreeKB() { | 251 int TabManagerDelegate::MemoryStat::TargetMemoryToFreeKB() { |
252 static const int kRamVsSwapWeight = 4; | 252 static const int kRamVsSwapWeight = 4; |
253 static const char kMinFilelistConfig[] = "/proc/sys/vm/min_filelist_kbytes"; | 253 static const char kMinFilelistConfig[] = "/proc/sys/vm/min_filelist_kbytes"; |
254 static const char kMinFreeKbytes[] = "/proc/sys/vm/min_free_kbytes"; | |
254 | 255 |
255 base::SystemMemoryInfoKB system_mem; | 256 base::SystemMemoryInfoKB system_mem; |
256 base::GetSystemMemoryInfo(&system_mem); | 257 base::GetSystemMemoryInfo(&system_mem); |
257 const int file_mem_kb = system_mem.active_file + system_mem.inactive_file; | 258 const int file_mem_kb = system_mem.active_file + system_mem.inactive_file; |
258 const int min_filelist_kb = ReadIntFromFile(kMinFilelistConfig, 0); | 259 const int min_filelist_kb = ReadIntFromFile(kMinFilelistConfig, 0); |
260 const int min_free_kb = ReadIntFromFile(kMinFreeKbytes, 0); | |
259 // Calculate current available memory in system. | 261 // Calculate current available memory in system. |
260 // File-backed memory should be easy to reclaim, unless they're dirty. | 262 // File-backed memory should be easy to reclaim, unless they're dirty. |
263 // TODO(cylee): On ChromeOS, kernel reports low memory condition when | |
264 // available memory is low. The following formula duplicates the logic in | |
265 // kernel to calculate how much memory should be released. In the future, | |
266 // kernel should try to report the amount of memory to release directly to | |
267 // eliminate the duplication here. | |
sonnyrao
2017/01/05 00:24:26
that's an interesting idea -- does Chrome have the
cylee1
2017/01/05 20:31:13
Of course it just tries to discard tabs until the
| |
261 const int available_mem_kb = system_mem.free + | 268 const int available_mem_kb = system_mem.free + |
262 file_mem_kb - system_mem.dirty - min_filelist_kb + | 269 file_mem_kb - system_mem.dirty - min_filelist_kb + |
263 system_mem.swap_free / kRamVsSwapWeight; | 270 system_mem.swap_free / kRamVsSwapWeight - |
271 min_free_kb; | |
264 | 272 |
265 return LowMemoryMarginKB() - available_mem_kb; | 273 return LowMemoryMarginKB() - available_mem_kb; |
266 } | 274 } |
267 | 275 |
268 int TabManagerDelegate::MemoryStat::EstimatedMemoryFreedKB( | 276 int TabManagerDelegate::MemoryStat::EstimatedMemoryFreedKB( |
269 base::ProcessHandle pid) { | 277 base::ProcessHandle pid) { |
270 std::unique_ptr<base::ProcessMetrics> process_metrics( | 278 std::unique_ptr<base::ProcessMetrics> process_metrics( |
271 base::ProcessMetrics::CreateProcessMetrics(pid)); | 279 base::ProcessMetrics::CreateProcessMetrics(pid)); |
272 base::WorkingSetKBytes mem_usage; | 280 base::WorkingSetKBytes mem_usage; |
273 process_metrics->GetWorkingSetKBytes(&mem_usage); | 281 process_metrics->GetWorkingSetKBytes(&mem_usage); |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
570 | 578 |
571 | 579 |
572 chromeos::DebugDaemonClient* TabManagerDelegate::GetDebugDaemonClient() { | 580 chromeos::DebugDaemonClient* TabManagerDelegate::GetDebugDaemonClient() { |
573 return chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); | 581 return chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); |
574 } | 582 } |
575 | 583 |
576 void TabManagerDelegate::LowMemoryKillImpl( | 584 void TabManagerDelegate::LowMemoryKillImpl( |
577 const TabStatsList& tab_list, | 585 const TabStatsList& tab_list, |
578 const std::vector<arc::ArcProcess>& arc_processes) { | 586 const std::vector<arc::ArcProcess>& arc_processes) { |
579 | 587 |
580 VLOG(2) << "LowMemoryKilleImpl"; | 588 VLOG(2) << "LowMemoryKillImpl"; |
589 | |
581 const std::vector<TabManagerDelegate::Candidate> candidates = | 590 const std::vector<TabManagerDelegate::Candidate> candidates = |
582 GetSortedCandidates(tab_list, arc_processes); | 591 GetSortedCandidates(tab_list, arc_processes); |
583 | 592 |
584 int target_memory_to_free_kb = mem_stat_->TargetMemoryToFreeKB(); | 593 int target_memory_to_free_kb = mem_stat_->TargetMemoryToFreeKB(); |
585 // Kill processes until the estimated amount of freed memory is sufficient to | 594 // Kill processes until the estimated amount of freed memory is sufficient to |
586 // bring the system memory back to a normal level. | 595 // bring the system memory back to a normal level. |
587 // The list is sorted by descending importance, so we go through the list | 596 // The list is sorted by descending importance, so we go through the list |
588 // backwards. | 597 // backwards. |
589 for (auto it = candidates.rbegin(); it != candidates.rend(); ++it) { | 598 for (auto it = candidates.rbegin(); it != candidates.rend(); ++it) { |
590 VLOG(3) << "Target memory to free: " << target_memory_to_free_kb << " KB"; | 599 VLOG(3) << "Target memory to free: " << target_memory_to_free_kb << " KB"; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
718 } | 727 } |
719 priority += priority_increment; | 728 priority += priority_increment; |
720 } | 729 } |
721 | 730 |
722 if (oom_scores_to_change.size()) | 731 if (oom_scores_to_change.size()) |
723 GetDebugDaemonClient()->SetOomScoreAdj( | 732 GetDebugDaemonClient()->SetOomScoreAdj( |
724 oom_scores_to_change, base::Bind(&OnSetOomScoreAdj)); | 733 oom_scores_to_change, base::Bind(&OnSetOomScoreAdj)); |
725 } | 734 } |
726 | 735 |
727 } // namespace memory | 736 } // namespace memory |
OLD | NEW |