1// Copyright (c) 2010 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 CHROME_BROWSER_OOM_PRIORITY_MANAGER_H_
6#define CHROME_BROWSER_OOM_PRIORITY_MANAGER_H_
7
8#include <list>
9
10#include "base/timer.h"
11#include "base/process.h"
12
13namespace browser {
14
15// The OomPriorityManager periodically checks (see
16// ADJUSTMENT_INTERVAL_SECONDS in the source) the status of renderers
17// and adjusts the out of memory (OOM) adjustment value (in
18// /proc/<pid>/oom_adj) of the renderers so that they match the
19// algorithm embedded here for priority in being killed upon OOM
20// conditions.
21//
22// The algorithm used favors killing tabs that are not pinned, have
23// been idle for longest, and take up the most memory, in that order
24// of priority.  We round the idle times to the nearest few minutes
25// (see BUCKET_INTERVAL_MINUTES in the source) so that we can bucket
26// them, as no two tabs will have exactly the same idle time.
27class OomPriorityManager {
28 public:
29  OomPriorityManager();
30  ~OomPriorityManager();
31
32 private:
33  struct RendererStats {
34    bool is_pinned;
35    base::TimeTicks last_selected;
36    size_t memory_used;
37    base::ProcessHandle renderer_handle;
38  };
39  typedef std::list<RendererStats> StatsList;
40
41  void StartTimer();
42  void StopTimer();
43
44  // Posts DoAdjustOomPriorities task to the file thread.  Called when
45  // the timer fires.
46  void AdjustOomPriorities();
47
48  // Called by AdjustOomPriorities.  Runs on the file thread.
49  void DoAdjustOomPriorities(StatsList list);
50
51  static bool CompareRendererStats(RendererStats first, RendererStats second);
52
53  base::RepeatingTimer<OomPriorityManager> timer_;
54
55  DISALLOW_COPY_AND_ASSIGN(OomPriorityManager);
56};
57}  // namespace browser
58
59DISABLE_RUNNABLE_METHOD_REFCOUNT(browser::OomPriorityManager);
60
61#endif  // CHROME_BROWSER_OOM_PRIORITY_MANAGER_H_
62