blob: de51fc29221c2654a5d36c6e07ac918d0ff02286 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 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.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5#define _CRT_SECURE_NO_WARNINGS
6
7#include "testing/gtest/include/gtest/gtest.h"
8#include "base/process_util.h"
9
10TEST(ProcessUtilTest, EnableLFH) {
11 ASSERT_TRUE(process_util::EnableLowFragmentationHeap());
12 if (IsDebuggerPresent()) {
13 // Under these conditions, LFH can't be enabled. There's no point to test
14 // anything.
15 const char* no_debug_env = getenv("_NO_DEBUG_HEAP");
16 if (!no_debug_env || strcmp(no_debug_env, "1"))
17 return;
18 }
19 HANDLE heaps[1024] = { 0 };
20 unsigned number_heaps = GetProcessHeaps(1024, heaps);
21 EXPECT_GT(number_heaps, 0u);
22 for (unsigned i = 0; i < number_heaps; ++i) {
23 ULONG flag = 0;
24 SIZE_T length;
25 ASSERT_NE(0, HeapQueryInformation(heaps[i],
26 HeapCompatibilityInformation,
27 &flag,
28 sizeof(flag),
29 &length));
30 // If flag is 0, the heap is a standard heap that does not support
31 // look-asides. If flag is 1, the heap supports look-asides. If flag is 2,
32 // the heap is a low-fragmentation heap (LFH). Note that look-asides are not
33 // supported on the LFH.
34
35 // We don't have any documented way of querying the HEAP_NO_SERIALIZE flag.
36 EXPECT_LE(flag, 2u);
37 EXPECT_NE(flag, 1u);
38 }
39}
40
41TEST(ProcessUtilTest, CalcFreeMemory) {
42 process_util::ProcessMetrics* metrics =
43 process_util::ProcessMetrics::CreateProcessMetrics(::GetCurrentProcess());
44 ASSERT_TRUE(NULL != metrics);
45
46 // Typical values here is ~1900 for total and ~1000 for largest. Obviously
47 // it depends in what other tests have done to this process.
48 process_util::FreeMBytes free_mem1 = {0};
49 EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem1));
50 EXPECT_LT(10u, free_mem1.total);
51 EXPECT_LT(10u, free_mem1.largest);
52 EXPECT_GT(2048u, free_mem1.total);
53 EXPECT_GT(2048u, free_mem1.largest);
54 EXPECT_GE(free_mem1.total, free_mem1.largest);
55 EXPECT_TRUE(NULL != free_mem1.largest_ptr);
56
57 // Allocate 20M and check again. It should have gone down.
58 const int kAllocMB = 20;
59 char* alloc = new char[kAllocMB * 1024 * 1024];
60 EXPECT_TRUE(NULL != alloc);
61
62 size_t expected_total = free_mem1.total - kAllocMB;
63 size_t expected_largest = free_mem1.largest;
64
65 process_util::FreeMBytes free_mem2 = {0};
66 EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem2));
67 EXPECT_GE(free_mem2.total, free_mem2.largest);
68 EXPECT_GE(expected_total, free_mem2.total);
69 EXPECT_GE(expected_largest, free_mem2.largest);
70 EXPECT_TRUE(NULL != free_mem2.largest_ptr);
71
72 delete[] alloc;
73 delete metrics;
74}
license.botf003cfe2008-08-24 09:55:55 +090075