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 #ifndef CONTENT_BROWSER_MEMORY_MEMORY_CONDITION_OBSERVER_H_ |
| 6 #define CONTENT_BROWSER_MEMORY_MEMORY_CONDITION_OBSERVER_H_ |
| 7 |
| 8 #include "base/cancelable_callback.h" |
| 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/time/time.h" |
| 11 #include "content/browser/memory/memory_coordinator_impl.h" |
| 12 #include "content/common/content_export.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 // MemoryConditionObserver is an internal implementation of MemoryCoordinator |
| 17 // which uses a heuristic to determine the current memory condition. The |
| 18 // heuristic is: |
| 19 // * Compute number of renderers which can be created until the system will |
| 20 // be in a critical state. Call this N. |
| 21 // (See memory_monitor.h for the definition of "critical") |
| 22 // * Covert N to memory condition (one of NORMAL/WARNING/CRITICAL) by using some |
| 23 // thresholds and hysteresis. |
| 24 class CONTENT_EXPORT MemoryConditionObserver { |
| 25 public: |
| 26 // |coordinator| must outlive than this instance. |
| 27 MemoryConditionObserver( |
| 28 MemoryCoordinatorImpl* coordinator, |
| 29 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 30 ~MemoryConditionObserver(); |
| 31 |
| 32 // Schedules a task to update memory condition. The task will be executed |
| 33 // after |delay| has passed. |
| 34 void ScheduleUpdateCondition(base::TimeDelta delay); |
| 35 |
| 36 private: |
| 37 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, CalculateNextCondition); |
| 38 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, UpdateCondition); |
| 39 FRIEND_TEST_ALL_PREFIXES(MemoryCoordinatorImplTest, ForceSetMemoryCondition); |
| 40 |
| 41 // Calculates next memory condition from the amount of free memory using |
| 42 // a heuristic. |
| 43 MemoryCondition CalculateNextCondition(); |
| 44 |
| 45 // Periodically called to update the memory condition. |
| 46 void UpdateCondition(); |
| 47 |
| 48 MemoryCoordinatorImpl* coordinator_; |
| 49 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 50 base::CancelableClosure update_condition_closure_; |
| 51 |
| 52 // Sets up parameters for the heuristic. |
| 53 void InitializeParameters(); |
| 54 |
| 55 // Validates parameters defined below. |
| 56 bool ValidateParameters(); |
| 57 |
| 58 // Parameters to control the heuristic. |
| 59 |
| 60 // The median size of a renderer on the current platform. This is used to |
| 61 // convert the amount of free memory to an expected number of new renderers |
| 62 // that could be started before hitting critical memory pressure. |
| 63 int expected_renderer_size_; |
| 64 // When in a NORMAL condition and the potential number of new renderers drops |
| 65 // below this level, the coordinator will transition to a WARNING condition. |
| 66 int new_renderers_until_warning_; |
| 67 // When in a NORMAL/WARNING state and the potential number of new renderers |
| 68 // drops below this level, the coordinator will transition to a CRITICAL |
| 69 // condition. |
| 70 int new_renderers_until_critical_; |
| 71 // When in a WARNING/CRITICAL condition and the potential number of new |
| 72 // renderers rises above this level, the coordinator will transition to a |
| 73 // NORMAL condition. |
| 74 int new_renderers_back_to_normal_; |
| 75 // When in a CRITICAL condition and the potential number of new renderers |
| 76 // rises above this level, the coordinator will transition to a WARNING |
| 77 // condition. |
| 78 int new_renderers_back_to_warning_; |
| 79 // The interval of checking the amount of free memory. |
| 80 base::TimeDelta monitoring_interval_; |
| 81 |
| 82 DISALLOW_COPY_AND_ASSIGN(MemoryConditionObserver); |
| 83 }; |
| 84 |
| 85 } // namespace content |
| 86 |
| 87 #endif // CONTENT_BROWSER_MEMORY_MEMORY_CONDITION_OBSERVER_H_ |
OLD | NEW |