summaryrefslogtreecommitdiff
path: root/chromium/content/browser/memory/memory_monitor_linux.cc
blob: 2901e7f437199e129bb38589b5c17d224b48f980 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/memory/memory_monitor_linux.h"

#include "base/memory/ptr_util.h"
#include "base/process/process_metrics.h"

namespace content {

namespace {

// The number of bits to shift to convert KiB to MiB.
const int kShiftKiBtoMiB = 10;

}  // namespace

MemoryMonitorLinux::MemoryMonitorLinux(MemoryMonitorDelegate* delegate)
    : delegate_(delegate) {}

MemoryMonitorLinux::~MemoryMonitorLinux() {}

int MemoryMonitorLinux::GetFreeMemoryUntilCriticalMB() {
  base::SystemMemoryInfoKB mem_info = {};
  delegate_->GetSystemMemoryInfo(&mem_info);

  // According to kernel commit 34e431b0ae398fc54ea69ff85ec700722c9da773,
  // "available" is the "amount of memory that is available for a new workload
  // without pushing the system into swap"; return that value if it is valid.
  // Old linux kernels (before 3.14) don't support "available" and show zero
  // instead.
  if (mem_info.available > 0)
    return mem_info.available >> kShiftKiBtoMiB;

  // If there is no "available" value, guess at it based on free memory.
  // Though there will be easily discardable memory (buffers and caches), we
  // don't count them because discarding them will affect the overall
  // performance of the OS.
  return mem_info.free >> kShiftKiBtoMiB;
}

// static
std::unique_ptr<MemoryMonitorLinux> MemoryMonitorLinux::Create(
    MemoryMonitorDelegate* delegate) {
  return std::make_unique<MemoryMonitorLinux>(delegate);
}

// Implementation of factory function defined in memory_monitor.h.
std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() {
  return MemoryMonitorLinux::Create(MemoryMonitorDelegate::GetInstance());
}

}  // namespace content