OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "webkit/glue/webkitplatformsupport_impl.h" | 5 #include "webkit/glue/webkitplatformsupport_impl.h" |
6 | 6 |
7 #if defined(OS_LINUX) | 7 #if defined(OS_LINUX) |
8 #include <malloc.h> | 8 #include <malloc.h> |
9 #endif | 9 #endif |
10 | 10 |
11 #include <math.h> | 11 #include <math.h> |
12 | 12 |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #if defined(OS_ANDROID) | |
16 #include <sys/system_properties.h> | |
17 #endif | |
18 | |
15 #include "base/bind.h" | 19 #include "base/bind.h" |
16 #include "base/debug/trace_event.h" | 20 #include "base/debug/trace_event.h" |
17 #include "base/memory/singleton.h" | 21 #include "base/memory/singleton.h" |
18 #include "base/message_loop.h" | 22 #include "base/message_loop.h" |
19 #include "base/metrics/histogram.h" | 23 #include "base/metrics/histogram.h" |
20 #include "base/metrics/stats_counters.h" | 24 #include "base/metrics/stats_counters.h" |
21 #include "base/platform_file.h" | 25 #include "base/platform_file.h" |
22 #include "base/process_util.h" | 26 #include "base/process_util.h" |
23 #include "base/rand_util.h" | 27 #include "base/rand_util.h" |
24 #include "base/string_number_conversions.h" | 28 #include "base/string_number_conversions.h" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 | 109 |
106 // How long the cached value should remain valid. | 110 // How long the cached value should remain valid. |
107 base::TimeDelta cache_valid_time_; | 111 base::TimeDelta cache_valid_time_; |
108 | 112 |
109 // The last time the cached value was updated. | 113 // The last time the cached value was updated. |
110 base::Time last_updated_time_; | 114 base::Time last_updated_time_; |
111 | 115 |
112 base::Lock lock_; | 116 base::Lock lock_; |
113 }; | 117 }; |
114 | 118 |
119 #if defined(OS_ANDROID) | |
120 // A simple class to cache the heap size of the device. | |
121 class DalvikHeapSize { | |
darin (slow to review)
2012/05/09 14:57:35
it seems like this should be factored out into a s
ulan
2012/05/09 17:33:27
Done.
| |
122 public: | |
123 // Retrieves the Singleton. | |
124 static DalvikHeapSize* GetInstance() { | |
125 return Singleton<DalvikHeapSize>::get(); | |
126 } | |
127 | |
128 DalvikHeapSize() : heap_size_mb_(0) { | |
129 char heap_size_str[PROP_VALUE_MAX]; | |
130 __system_property_get("dalvik.vm.heapsize", heap_size_str); | |
131 heap_size_mb_ = ParseHeapSize(std::string(heap_size_str)); | |
132 } | |
133 | |
134 ~DalvikHeapSize() {} | |
135 | |
136 int HeapSizeMB() const { return heap_size_mb_; } | |
137 | |
138 private: | |
139 int ParseHeapSize(const std::string& str) const { | |
140 const int64 KB = 1024; | |
141 const int64 MB = 1024 * KB; | |
142 const int64 GB = 1024 * MB; | |
143 CHECK_GT(str.size(), 0u); | |
144 int64 factor = 1; | |
145 size_t length = str.size(); | |
146 if (str[length - 1] == 'k') { | |
147 factor = KB; | |
148 length--; | |
149 } else if (str[length - 1] == 'm') { | |
150 factor = MB; | |
151 length--; | |
152 } else if (str[length - 1] == 'g') { | |
153 factor = GB; | |
154 length--; | |
155 } else { | |
156 CHECK('0' <= str[length - 1] && str[length - 1] <= '9'); | |
157 } | |
158 int64 result = 0; | |
159 bool parsed = base::StringToInt64(str.substr(0, length), &result); | |
160 CHECK(parsed); | |
161 result = result * factor / MB; | |
162 // dalvik.vm.heapsize property is writable by user, | |
163 // truncate it to reasonable value to avoid overflows later. | |
164 result = std::min<int64>(std::max<int64>(32, result), 1024); | |
165 return static_cast<int>(result); | |
166 } | |
167 | |
168 int heap_size_mb_; | |
169 }; | |
170 #endif | |
171 | |
115 } // anonymous namespace | 172 } // anonymous namespace |
116 | 173 |
117 namespace webkit_glue { | 174 namespace webkit_glue { |
118 | 175 |
119 static int ToMessageID(WebLocalizedString::Name name) { | 176 static int ToMessageID(WebLocalizedString::Name name) { |
120 switch (name) { | 177 switch (name) { |
121 case WebLocalizedString::AXButtonActionVerb: | 178 case WebLocalizedString::AXButtonActionVerb: |
122 return IDS_AX_BUTTON_ACTION_VERB; | 179 return IDS_AX_BUTTON_ACTION_VERB; |
123 case WebLocalizedString::AXCheckedCheckBoxActionVerb: | 180 case WebLocalizedString::AXCheckedCheckBoxActionVerb: |
124 return IDS_AX_CHECKED_CHECK_BOX_ACTION_VERB; | 181 return IDS_AX_CHECKED_CHECK_BOX_ACTION_VERB; |
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
613 return 0; | 670 return 0; |
614 } | 671 } |
615 | 672 |
616 WebKit::WebString WebKitPlatformSupportImpl::signedPublicKeyAndChallengeString( | 673 WebKit::WebString WebKitPlatformSupportImpl::signedPublicKeyAndChallengeString( |
617 unsigned key_size_index, | 674 unsigned key_size_index, |
618 const WebKit::WebString& challenge, | 675 const WebKit::WebString& challenge, |
619 const WebKit::WebURL& url) { | 676 const WebKit::WebURL& url) { |
620 return WebKit::WebString(""); | 677 return WebKit::WebString(""); |
621 } | 678 } |
622 | 679 |
623 #if defined(OS_LINUX) | 680 #if defined(OS_LINUX) || defined(OS_ANDROID) |
624 static size_t memoryUsageMBLinux() { | 681 static size_t memoryUsageMB() { |
625 struct mallinfo minfo = mallinfo(); | 682 struct mallinfo minfo = mallinfo(); |
626 uint64_t mem_usage = | 683 uint64_t mem_usage = |
627 #if defined(USE_TCMALLOC) | 684 #if defined(USE_TCMALLOC) |
628 minfo.uordblks | 685 minfo.uordblks |
629 #else | 686 #else |
630 (minfo.hblkhd + minfo.arena) | 687 (minfo.hblkhd + minfo.arena) |
631 #endif | 688 #endif |
632 >> 20; | 689 >> 20; |
633 | 690 |
634 v8::HeapStatistics stat; | 691 v8::HeapStatistics stat; |
635 v8::V8::GetHeapStatistics(&stat); | 692 v8::V8::GetHeapStatistics(&stat); |
636 return mem_usage + (static_cast<uint64_t>(stat.total_heap_size()) >> 20); | 693 return mem_usage + (static_cast<uint64_t>(stat.total_heap_size()) >> 20); |
637 } | 694 } |
638 #endif | 695 #elif defined(OS_MACOSX) |
639 | 696 static size_t memoryUsageMB() { |
640 #if defined(OS_MACOSX) | |
641 static size_t memoryUsageMBMac() { | |
642 using base::ProcessMetrics; | 697 using base::ProcessMetrics; |
643 static ProcessMetrics* process_metrics = | 698 static ProcessMetrics* process_metrics = |
644 // The default port provider is sufficient to get data for the current | 699 // The default port provider is sufficient to get data for the current |
645 // process. | 700 // process. |
646 ProcessMetrics::CreateProcessMetrics(base::GetCurrentProcessHandle(), | 701 ProcessMetrics::CreateProcessMetrics(base::GetCurrentProcessHandle(), |
647 NULL); | 702 NULL); |
648 DCHECK(process_metrics); | 703 DCHECK(process_metrics); |
649 return process_metrics->GetWorkingSetSize() >> 20; | 704 return process_metrics->GetWorkingSetSize() >> 20; |
650 } | 705 } |
651 #endif | 706 #else |
652 | 707 static size_t memoryUsageMB() { |
653 #if !defined(OS_LINUX) && !defined(OS_MACOSX) | |
654 static size_t memoryUsageMBGeneric() { | |
655 using base::ProcessMetrics; | 708 using base::ProcessMetrics; |
656 static ProcessMetrics* process_metrics = | 709 static ProcessMetrics* process_metrics = |
657 ProcessMetrics::CreateProcessMetrics(base::GetCurrentProcessHandle()); | 710 ProcessMetrics::CreateProcessMetrics(base::GetCurrentProcessHandle()); |
658 DCHECK(process_metrics); | 711 DCHECK(process_metrics); |
659 return process_metrics->GetPagefileUsage() >> 20; | 712 return process_metrics->GetPagefileUsage() >> 20; |
660 } | 713 } |
661 #endif | 714 #endif |
662 | 715 |
663 static size_t getMemoryUsageMB(bool bypass_cache) { | 716 static size_t getMemoryUsageMB(bool bypass_cache) { |
664 size_t current_mem_usage = 0; | 717 size_t current_mem_usage = 0; |
665 MemoryUsageCache* mem_usage_cache_singleton = MemoryUsageCache::GetInstance(); | 718 MemoryUsageCache* mem_usage_cache_singleton = MemoryUsageCache::GetInstance(); |
666 if (!bypass_cache && | 719 if (!bypass_cache && |
667 mem_usage_cache_singleton->IsCachedValueValid(¤t_mem_usage)) | 720 mem_usage_cache_singleton->IsCachedValueValid(¤t_mem_usage)) |
668 return current_mem_usage; | 721 return current_mem_usage; |
669 | 722 |
670 current_mem_usage = | 723 current_mem_usage = memoryUsageMB(); |
671 #if defined(OS_LINUX) | |
672 memoryUsageMBLinux(); | |
673 #elif defined(OS_MACOSX) | |
674 memoryUsageMBMac(); | |
675 #else | |
676 memoryUsageMBGeneric(); | |
677 #endif | |
678 mem_usage_cache_singleton->SetMemoryValue(current_mem_usage); | 724 mem_usage_cache_singleton->SetMemoryValue(current_mem_usage); |
679 return current_mem_usage; | 725 return current_mem_usage; |
680 } | 726 } |
681 | 727 |
682 size_t WebKitPlatformSupportImpl::memoryUsageMB() { | 728 size_t WebKitPlatformSupportImpl::memoryUsageMB() { |
683 return getMemoryUsageMB(false); | 729 return getMemoryUsageMB(false); |
684 } | 730 } |
685 | 731 |
686 size_t WebKitPlatformSupportImpl::actualMemoryUsageMB() { | 732 size_t WebKitPlatformSupportImpl::actualMemoryUsageMB() { |
687 return getMemoryUsageMB(true); | 733 return getMemoryUsageMB(true); |
688 } | 734 } |
689 | 735 |
736 #if defined(OS_ANDROID) | |
737 size_t WebKitPlatformSupportImpl::lowMemoryUsageMB() { | |
738 // If memory usage is below this threshold, do not bother forcing GC. | |
739 // Allow us to use up to our memory class value before V8's GC kicks in. | |
740 // These values have been determined by experimentation. | |
741 DalvikHeapSize* heap_size_singleton = DalvikHeapSize::GetInstance(); | |
742 return heap_size_singleton->HeapSizeMB() / 4; | |
743 } | |
744 | |
745 size_t WebKitPlatformSupportImpl::highMemoryUsageMB() { | |
746 // If memory usage is above this threshold, force GC more aggressively. | |
747 return lowMemoryUsageMB() * 2; | |
748 } | |
749 | |
750 size_t WebKitPlatformSupportImpl::highUsageDeltaMB() { | |
darin (slow to review)
2012/05/09 14:57:35
nit: this function name seems a little strange. s
ulan
2012/05/09 17:33:27
The name is already fixed on WebKit side. Added ex
| |
751 // Threshold of delta of memory usage growth (vs. last working set estimate) | |
752 // to force GC when memory usage is high. | |
753 // Avoid constant V8 GC when memory usage equals to working set estimate. | |
754 return lowMemoryUsageMB() / 2; | |
755 } | |
756 #endif | |
757 | |
690 void WebKitPlatformSupportImpl::SuspendSharedTimer() { | 758 void WebKitPlatformSupportImpl::SuspendSharedTimer() { |
691 ++shared_timer_suspended_; | 759 ++shared_timer_suspended_; |
692 } | 760 } |
693 | 761 |
694 void WebKitPlatformSupportImpl::ResumeSharedTimer() { | 762 void WebKitPlatformSupportImpl::ResumeSharedTimer() { |
695 // The shared timer may have fired or been adjusted while we were suspended. | 763 // The shared timer may have fired or been adjusted while we were suspended. |
696 if (--shared_timer_suspended_ == 0 && !shared_timer_.IsRunning()) { | 764 if (--shared_timer_suspended_ == 0 && !shared_timer_.IsRunning()) { |
697 setSharedTimerFireInterval( | 765 setSharedTimerFireInterval( |
698 shared_timer_fire_time_ - monotonicallyIncreasingTime()); | 766 shared_timer_fire_time_ - monotonicallyIncreasingTime()); |
699 } | 767 } |
(...skipping 12 matching lines...) Expand all Loading... | |
712 worker_task_runner->OnWorkerRunLoopStarted(runLoop); | 780 worker_task_runner->OnWorkerRunLoopStarted(runLoop); |
713 } | 781 } |
714 | 782 |
715 void WebKitPlatformSupportImpl::didStopWorkerRunLoop( | 783 void WebKitPlatformSupportImpl::didStopWorkerRunLoop( |
716 const WebKit::WebWorkerRunLoop& runLoop) { | 784 const WebKit::WebWorkerRunLoop& runLoop) { |
717 WorkerTaskRunner* worker_task_runner = WorkerTaskRunner::Instance(); | 785 WorkerTaskRunner* worker_task_runner = WorkerTaskRunner::Instance(); |
718 worker_task_runner->OnWorkerRunLoopStopped(runLoop); | 786 worker_task_runner->OnWorkerRunLoopStopped(runLoop); |
719 } | 787 } |
720 | 788 |
721 } // namespace webkit_glue | 789 } // namespace webkit_glue |
OLD | NEW |