summaryrefslogtreecommitdiff
path: root/chromium/components/memory_pressure/direct_memory_pressure_calculator_win_unittest.cc
blob: a8f0ec5aafceba34d08fdb66286d74b15037d462 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2015 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 "components/memory_pressure/direct_memory_pressure_calculator_win.h"

#include "base/logging.h"
#include "base/process/process_metrics.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace memory_pressure {

#if defined(MEMORY_PRESSURE_IS_POLLING)

namespace {

const int kKBperMB = 1024;

}  // namespace

// This is out of the anonymous namespace space because it is a friend of
// DirectMemoryPressureCalculator.
class TestDirectMemoryPressureCalculator
    : public DirectMemoryPressureCalculator {
 public:
  explicit TestDirectMemoryPressureCalculator(bool large_memory)
      : DirectMemoryPressureCalculator(20, 10) {
    // The values passed to the MemoryPressureCalculator constructor are dummy
    // values that are immediately overwritten by InferTresholds.

    // Generate a plausible amount of memory.
    mem_info_.total = GenerateTotalMemoryMb(large_memory) * kKBperMB;

    // Run InferThresholds using the test fixture's GetSystemMemoryStatus.
    InferThresholds();
  }

  TestDirectMemoryPressureCalculator(int total_memory_mb,
                                     int moderate_threshold_mb,
                                     int critical_threshold_mb)
      : DirectMemoryPressureCalculator(moderate_threshold_mb,
                                       critical_threshold_mb) {
    mem_info_.total = total_memory_mb * kKBperMB;
  }

  // Generates an amount of total memory that is consistent with the requested
  // memory model.
  static int GenerateTotalMemoryMb(bool large_memory) {
    int total_mb = 64;
    while (total_mb < kLargeMemoryThresholdMb)
      total_mb *= 2;
    if (large_memory)
      return total_mb * 2;
    return total_mb / 2;
  }

  // Sets up the memory status to reflect the provided absolute memory left.
  void SetMemoryFree(int phys_left_mb) {
    // |total| is set in the constructor and not modified.

    // Set the amount of free memory.
    mem_info_.avail_phys = phys_left_mb * kKBperMB;
    DCHECK_LT(mem_info_.avail_phys, mem_info_.total);
  }

  void SetNone() { SetMemoryFree(moderate_threshold_mb() + 1); }

  void SetModerate() { SetMemoryFree(moderate_threshold_mb() - 1); }

  void SetCritical() { SetMemoryFree(critical_threshold_mb() - 1); }

 private:
  bool GetSystemMemoryInfo(base::SystemMemoryInfoKB* mem_info) const override {
    // Simply copy the memory information set by the test fixture.
    *mem_info = mem_info_;
    return true;
  }

  base::SystemMemoryInfoKB mem_info_;
};

class DirectMemoryPressureCalculatorTest : public testing::Test {
 public:
  void CalculateCurrentPressureLevelTest(
      TestDirectMemoryPressureCalculator* calc) {
    int mod = calc->moderate_threshold_mb();
    calc->SetMemoryFree(mod + 1);
    EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
              calc->CalculateCurrentPressureLevel());

    calc->SetNone();
    EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
              calc->CalculateCurrentPressureLevel());

    calc->SetMemoryFree(mod);
    EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
              calc->CalculateCurrentPressureLevel());

    calc->SetMemoryFree(mod - 1);
    EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
              calc->CalculateCurrentPressureLevel());

    int crit = calc->critical_threshold_mb();
    calc->SetMemoryFree(crit + 1);
    EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
              calc->CalculateCurrentPressureLevel());

    calc->SetModerate();
    EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
              calc->CalculateCurrentPressureLevel());

    EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
              calc->CalculateCurrentPressureLevel());

    calc->SetMemoryFree(crit);
    EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
              calc->CalculateCurrentPressureLevel());

    calc->SetMemoryFree(crit - 1);
    EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
              calc->CalculateCurrentPressureLevel());

    calc->SetCritical();
    EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
              calc->CalculateCurrentPressureLevel());
  }
};

// Tests the fundamental direct calculation of memory pressure with automatic
// small-memory thresholds.
TEST_F(DirectMemoryPressureCalculatorTest,
       CalculateCurrentMemoryPressureLevelSmall) {
  static const int kModerateMb =
      DirectMemoryPressureCalculator::kSmallMemoryDefaultModerateThresholdMb;
  static const int kCriticalMb =
      DirectMemoryPressureCalculator::kSmallMemoryDefaultCriticalThresholdMb;

  TestDirectMemoryPressureCalculator calc(false);  // Small-memory model.

  EXPECT_EQ(kModerateMb, calc.moderate_threshold_mb());
  EXPECT_EQ(kCriticalMb, calc.critical_threshold_mb());

  ASSERT_NO_FATAL_FAILURE(CalculateCurrentPressureLevelTest(&calc));
}

// Tests the fundamental direct calculation of memory pressure with automatic
// large-memory thresholds.
TEST_F(DirectMemoryPressureCalculatorTest,
       CalculateCurrentMemoryPressureLevelLarge) {
  static const int kModerateMb =
      DirectMemoryPressureCalculator::kLargeMemoryDefaultModerateThresholdMb;
  static const int kCriticalMb =
      DirectMemoryPressureCalculator::kLargeMemoryDefaultCriticalThresholdMb;

  TestDirectMemoryPressureCalculator calc(true);  // Large-memory model.

  EXPECT_EQ(kModerateMb, calc.moderate_threshold_mb());
  EXPECT_EQ(kCriticalMb, calc.critical_threshold_mb());

  ASSERT_NO_FATAL_FAILURE(CalculateCurrentPressureLevelTest(&calc));
}

// Tests the fundamental direct calculation of memory pressure with manually
// specified threshold levels.
TEST_F(DirectMemoryPressureCalculatorTest,
       CalculateCurrentMemoryPressureLevelCustom) {
  static const int kSystemMb = 512;
  static const int kModerateMb = 256;
  static const int kCriticalMb = 128;

  TestDirectMemoryPressureCalculator calc(kSystemMb, kModerateMb, kCriticalMb);

  EXPECT_EQ(kModerateMb, calc.moderate_threshold_mb());
  EXPECT_EQ(kCriticalMb, calc.critical_threshold_mb());

  ASSERT_NO_FATAL_FAILURE(CalculateCurrentPressureLevelTest(&calc));
}

#endif  // defined(MEMORY_PRESSURE_IS_POLLING)

}  // namespace memory_pressure