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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_MEMORY_OOM_PRIORITY_MANAGER_DELEGATE_CHROMEOS_H_
6 #define CHROME_BROWSER_MEMORY_OOM_PRIORITY_MANAGER_DELEGATE_CHROMEOS_H_
11 #include "base/containers/hash_tables.h"
12 #include "base/process/process.h"
13 #include "base/timer/timer.h"
14 #include "chrome/browser/memory/oom_priority_manager.h"
15 #include "chrome/browser/memory/tab_stats.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
21 // The Chrome OS OomPriorityManagerDelegate is responsible for keeping the
22 // renderers' scores up to date in /proc/<pid>/oom_score_adj.
24 // Note that AdjustOomPriorities will be called on the UI thread by
25 // OomPriorityManager, but the actual work will take place on the file thread
26 // (see implementation of AdjustOomPriorities).
27 class OomPriorityManagerDelegate
: public content::NotificationObserver
{
29 OomPriorityManagerDelegate();
30 ~OomPriorityManagerDelegate() override
;
32 // Return the score of a process.
33 int GetOomScore(int child_process_host_id
);
35 // Called when the timer fires, sets oom_adjust_score for all renderers.
36 void AdjustOomPriorities(const TabStatsList
& stats_list
);
39 FRIEND_TEST_ALL_PREFIXES(OomPriorityManagerDelegateTest
, GetProcessHandles
);
41 // content::NotificationObserver:
42 void Observe(int type
,
43 const content::NotificationSource
& source
,
44 const content::NotificationDetails
& details
) override
;
46 // Pair to hold child process host id and ProcessHandle.
47 typedef std::pair
<int, base::ProcessHandle
> ProcessInfo
;
49 // Returns a list of child process host ids and ProcessHandles from
50 // |stats_list| with unique pids. If multiple tabs use the same process,
51 // returns the first child process host id and corresponding pid. This implies
52 // that the processes are selected based on their "most important" tab.
53 static std::vector
<ProcessInfo
> GetChildProcessInfos(
54 const TabStatsList
& stats_list
);
56 // Called by AdjustOomPriorities.
57 void AdjustOomPrioritiesOnFileThread(TabStatsList stats_list
);
59 // Posts AdjustFocusedTabScore task to the file thread.
60 void OnFocusTabScoreAdjustmentTimeout();
62 // Sets the score of the focused tab to the least value.
63 void AdjustFocusedTabScoreOnFileThread();
65 // Registrar to receive renderer notifications.
66 content::NotificationRegistrar registrar_
;
67 // Timer to guarantee that the tab is focused for a certain amount of time.
68 base::OneShotTimer
<OomPriorityManagerDelegate
> focus_tab_score_adjust_timer_
;
69 // This lock is for |oom_score_map_| and |focused_tab_process_info_|.
70 base::Lock oom_score_lock_
;
71 // Map maintaining the child process host id - oom_score mapping.
72 typedef base::hash_map
<int, int> ProcessScoreMap
;
73 ProcessScoreMap oom_score_map_
;
74 // Holds the focused tab's child process host id.
75 ProcessInfo focused_tab_process_info_
;
77 DISALLOW_COPY_AND_ASSIGN(OomPriorityManagerDelegate
);
82 #endif // CHROME_BROWSER_MEMORY_OOM_PRIORITY_MANAGER_DELEGATE_CHROMEOS_H_