Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(235)

Side by Side Diff: base/process/process_metrics_unittest.cc

Issue 2558043007: Fix free memory calculation. (Closed)
Patch Set: Fix integer overflow in unittest. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/process/process_metrics_mac.cc ('k') | base/process/process_metrics_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/process/process_metrics.h" 5 #include "base/process/process_metrics.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <sstream> 10 #include <sstream>
11 #include <string> 11 #include <string>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/command_line.h" 14 #include "base/command_line.h"
15 #include "base/files/file.h" 15 #include "base/files/file.h"
16 #include "base/files/file_util.h" 16 #include "base/files/file_util.h"
17 #include "base/files/scoped_temp_dir.h" 17 #include "base/files/scoped_temp_dir.h"
18 #include "base/macros.h" 18 #include "base/macros.h"
19 #include "base/strings/string_number_conversions.h" 19 #include "base/strings/string_number_conversions.h"
20 #include "base/sys_info.h"
20 #include "base/test/multiprocess_test.h" 21 #include "base/test/multiprocess_test.h"
21 #include "base/threading/thread.h" 22 #include "base/threading/thread.h"
22 #include "build/build_config.h" 23 #include "build/build_config.h"
23 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
24 #include "testing/multiprocess_func_list.h" 25 #include "testing/multiprocess_func_list.h"
25 26
26 namespace base { 27 namespace base {
27 namespace debug { 28 namespace debug {
28 29
29 #if defined(OS_LINUX) || defined(OS_CHROMEOS) 30 #if defined(OS_LINUX) || defined(OS_CHROMEOS)
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 "Inactive: 21221496 kB\n" 100 "Inactive: 21221496 kB\n"
100 "Active(anon): 5674352 kB\n" 101 "Active(anon): 5674352 kB\n"
101 "Inactive(anon): 633992 kB\n"; 102 "Inactive(anon): 633992 kB\n";
102 EXPECT_FALSE(ParseProcMeminfo(invalid_input1, &meminfo)); 103 EXPECT_FALSE(ParseProcMeminfo(invalid_input1, &meminfo));
103 EXPECT_FALSE(ParseProcMeminfo(invalid_input2, &meminfo)); 104 EXPECT_FALSE(ParseProcMeminfo(invalid_input2, &meminfo));
104 EXPECT_FALSE(ParseProcMeminfo(invalid_input3, &meminfo)); 105 EXPECT_FALSE(ParseProcMeminfo(invalid_input3, &meminfo));
105 106
106 std::string valid_input1 = 107 std::string valid_input1 =
107 "MemTotal: 3981504 kB\n" 108 "MemTotal: 3981504 kB\n"
108 "MemFree: 140764 kB\n" 109 "MemFree: 140764 kB\n"
110 "MemAvailable: 535413 kB\n"
109 "Buffers: 116480 kB\n" 111 "Buffers: 116480 kB\n"
110 "Cached: 406160 kB\n" 112 "Cached: 406160 kB\n"
111 "SwapCached: 21304 kB\n" 113 "SwapCached: 21304 kB\n"
112 "Active: 3152040 kB\n" 114 "Active: 3152040 kB\n"
113 "Inactive: 472856 kB\n" 115 "Inactive: 472856 kB\n"
114 "Active(anon): 2972352 kB\n" 116 "Active(anon): 2972352 kB\n"
115 "Inactive(anon): 270108 kB\n" 117 "Inactive(anon): 270108 kB\n"
116 "Active(file): 179688 kB\n" 118 "Active(file): 179688 kB\n"
117 "Inactive(file): 202748 kB\n" 119 "Inactive(file): 202748 kB\n"
118 "Unevictable: 0 kB\n" 120 "Unevictable: 0 kB\n"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 "VmallocTotal: 3874808 kB\n" 166 "VmallocTotal: 3874808 kB\n"
165 "VmallocUsed: 1416 kB\n" 167 "VmallocUsed: 1416 kB\n"
166 "VmallocChunk: 3872908 kB\n" 168 "VmallocChunk: 3872908 kB\n"
167 "HugePages_Total: 0\n" 169 "HugePages_Total: 0\n"
168 "HugePages_Free: 0\n" 170 "HugePages_Free: 0\n"
169 "Hugepagesize: 4096 kB\n"; 171 "Hugepagesize: 4096 kB\n";
170 172
171 EXPECT_TRUE(ParseProcMeminfo(valid_input1, &meminfo)); 173 EXPECT_TRUE(ParseProcMeminfo(valid_input1, &meminfo));
172 EXPECT_EQ(meminfo.total, 3981504); 174 EXPECT_EQ(meminfo.total, 3981504);
173 EXPECT_EQ(meminfo.free, 140764); 175 EXPECT_EQ(meminfo.free, 140764);
176 EXPECT_EQ(meminfo.available, 535413);
174 EXPECT_EQ(meminfo.buffers, 116480); 177 EXPECT_EQ(meminfo.buffers, 116480);
175 EXPECT_EQ(meminfo.cached, 406160); 178 EXPECT_EQ(meminfo.cached, 406160);
176 EXPECT_EQ(meminfo.active_anon, 2972352); 179 EXPECT_EQ(meminfo.active_anon, 2972352);
177 EXPECT_EQ(meminfo.active_file, 179688); 180 EXPECT_EQ(meminfo.active_file, 179688);
178 EXPECT_EQ(meminfo.inactive_anon, 270108); 181 EXPECT_EQ(meminfo.inactive_anon, 270108);
179 EXPECT_EQ(meminfo.inactive_file, 202748); 182 EXPECT_EQ(meminfo.inactive_file, 202748);
180 EXPECT_EQ(meminfo.swap_total, 5832280); 183 EXPECT_EQ(meminfo.swap_total, 5832280);
181 EXPECT_EQ(meminfo.swap_free, 3672368); 184 EXPECT_EQ(meminfo.swap_free, 3672368);
182 EXPECT_EQ(meminfo.dirty, 184); 185 EXPECT_EQ(meminfo.dirty, 184);
186 EXPECT_EQ(meminfo.reclaimable, 30936);
183 #if defined(OS_CHROMEOS) 187 #if defined(OS_CHROMEOS)
184 EXPECT_EQ(meminfo.shmem, 140204); 188 EXPECT_EQ(meminfo.shmem, 140204);
185 EXPECT_EQ(meminfo.slab, 54212); 189 EXPECT_EQ(meminfo.slab, 54212);
186 #endif 190 #endif
191 EXPECT_EQ(355725,
192 base::SysInfo::AmountOfAvailablePhysicalMemory(meminfo) / 1024);
193 // Simulate as if there is no MemAvailable.
194 meminfo.available = 0;
195 EXPECT_EQ(374448,
196 base::SysInfo::AmountOfAvailablePhysicalMemory(meminfo) / 1024);
197 meminfo = {};
187 EXPECT_TRUE(ParseProcMeminfo(valid_input2, &meminfo)); 198 EXPECT_TRUE(ParseProcMeminfo(valid_input2, &meminfo));
188 EXPECT_EQ(meminfo.total, 255908); 199 EXPECT_EQ(meminfo.total, 255908);
189 EXPECT_EQ(meminfo.free, 69936); 200 EXPECT_EQ(meminfo.free, 69936);
201 EXPECT_EQ(meminfo.available, 0);
190 EXPECT_EQ(meminfo.buffers, 15812); 202 EXPECT_EQ(meminfo.buffers, 15812);
191 EXPECT_EQ(meminfo.cached, 115124); 203 EXPECT_EQ(meminfo.cached, 115124);
192 EXPECT_EQ(meminfo.swap_total, 524280); 204 EXPECT_EQ(meminfo.swap_total, 524280);
193 EXPECT_EQ(meminfo.swap_free, 524200); 205 EXPECT_EQ(meminfo.swap_free, 524200);
194 EXPECT_EQ(meminfo.dirty, 4); 206 EXPECT_EQ(meminfo.dirty, 4);
207 EXPECT_EQ(69936,
208 base::SysInfo::AmountOfAvailablePhysicalMemory(meminfo) / 1024);
195 } 209 }
196 210
197 TEST_F(SystemMetricsTest, ParseVmstat) { 211 TEST_F(SystemMetricsTest, ParseVmstat) {
198 struct SystemMemoryInfoKB meminfo; 212 struct SystemMemoryInfoKB meminfo;
199 // part of vmstat from a 3.2 kernel with numa enabled 213 // part of vmstat from a 3.2 kernel with numa enabled
200 std::string valid_input1 = 214 std::string valid_input1 =
201 "nr_free_pages 905104\n" 215 "nr_free_pages 905104\n"
202 "nr_inactive_anon 142478" 216 "nr_inactive_anon 142478"
203 "nr_active_anon 1520046\n" 217 "nr_active_anon 1520046\n"
204 "nr_inactive_file 4481001\n" 218 "nr_inactive_file 4481001\n"
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 348
335 thread2.Stop(); 349 thread2.Stop();
336 EXPECT_GE(metrics->GetCPUUsage(), 0.0); 350 EXPECT_GE(metrics->GetCPUUsage(), 0.0);
337 351
338 thread3.Stop(); 352 thread3.Stop();
339 EXPECT_GE(metrics->GetCPUUsage(), 0.0); 353 EXPECT_GE(metrics->GetCPUUsage(), 0.0);
340 } 354 }
341 355
342 #endif // defined(OS_LINUX) || defined(OS_CHROMEOS) 356 #endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
343 357
344 #if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) || \ 358 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) || \
345 defined(OS_LINUX) || defined(OS_ANDROID) 359 defined(OS_ANDROID)
346 TEST(SystemMetrics2Test, GetSystemMemoryInfo) { 360 TEST(SystemMetrics2Test, GetSystemMemoryInfo) {
347 SystemMemoryInfoKB info; 361 SystemMemoryInfoKB info;
348 EXPECT_TRUE(GetSystemMemoryInfo(&info)); 362 EXPECT_TRUE(GetSystemMemoryInfo(&info));
349 363
350 // Ensure each field received a value. 364 // Ensure each field received a value.
351 EXPECT_GT(info.total, 0); 365 EXPECT_GT(info.total, 0);
366 #if defined(OS_WIN)
367 EXPECT_GT(info.avail_phys, 0);
368 #else
352 EXPECT_GT(info.free, 0); 369 EXPECT_GT(info.free, 0);
370 #endif
353 #if defined(OS_LINUX) || defined(OS_ANDROID) 371 #if defined(OS_LINUX) || defined(OS_ANDROID)
354 EXPECT_GT(info.buffers, 0); 372 EXPECT_GT(info.buffers, 0);
355 EXPECT_GT(info.cached, 0); 373 EXPECT_GT(info.cached, 0);
356 EXPECT_GT(info.active_anon, 0); 374 EXPECT_GT(info.active_anon, 0);
357 EXPECT_GT(info.inactive_anon, 0); 375 EXPECT_GT(info.inactive_anon, 0);
358 EXPECT_GT(info.active_file, 0); 376 EXPECT_GT(info.active_file, 0);
359 EXPECT_GT(info.inactive_file, 0); 377 EXPECT_GT(info.inactive_file, 0);
360 #endif // defined(OS_LINUX) || defined(OS_ANDROID) 378 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
361 379
362 // All the values should be less than the total amount of memory. 380 // All the values should be less than the total amount of memory.
381 #if !defined(OS_WIN)
363 EXPECT_LT(info.free, info.total); 382 EXPECT_LT(info.free, info.total);
383 #endif
364 #if defined(OS_LINUX) || defined(OS_ANDROID) 384 #if defined(OS_LINUX) || defined(OS_ANDROID)
365 EXPECT_LT(info.buffers, info.total); 385 EXPECT_LT(info.buffers, info.total);
366 EXPECT_LT(info.cached, info.total); 386 EXPECT_LT(info.cached, info.total);
367 EXPECT_LT(info.active_anon, info.total); 387 EXPECT_LT(info.active_anon, info.total);
368 EXPECT_LT(info.inactive_anon, info.total); 388 EXPECT_LT(info.inactive_anon, info.total);
369 EXPECT_LT(info.active_file, info.total); 389 EXPECT_LT(info.active_file, info.total);
370 EXPECT_LT(info.inactive_file, info.total); 390 EXPECT_LT(info.inactive_file, info.total);
371 #endif // defined(OS_LINUX) || defined(OS_ANDROID) 391 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
372 392
393 #if defined(OS_MACOSX) || defined(OS_IOS)
394 EXPECT_GT(info.file_backed, 0);
395 #endif
396
373 #if defined(OS_CHROMEOS) 397 #if defined(OS_CHROMEOS)
374 // Chrome OS exposes shmem. 398 // Chrome OS exposes shmem.
375 EXPECT_GT(info.shmem, 0); 399 EXPECT_GT(info.shmem, 0);
376 EXPECT_LT(info.shmem, info.total); 400 EXPECT_LT(info.shmem, info.total);
377 // Chrome unit tests are not run on actual Chrome OS hardware, so gem_objects 401 // Chrome unit tests are not run on actual Chrome OS hardware, so gem_objects
378 // and gem_size cannot be tested here. 402 // and gem_size cannot be tested here.
379 #endif 403 #endif
380 } 404 }
381 #endif // defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) || 405 #endif // defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) ||
382 // defined(OS_LINUX) || defined(OS_ANDROID) 406 // defined(OS_ANDROID)
383 407
384 #if defined(OS_LINUX) || defined(OS_ANDROID) 408 #if defined(OS_LINUX) || defined(OS_ANDROID)
385 TEST(ProcessMetricsTest, ParseProcStatCPU) { 409 TEST(ProcessMetricsTest, ParseProcStatCPU) {
386 // /proc/self/stat for a process running "top". 410 // /proc/self/stat for a process running "top".
387 const char kTopStat[] = "960 (top) S 16230 960 16230 34818 960 " 411 const char kTopStat[] = "960 (top) S 16230 960 16230 34818 960 "
388 "4202496 471 0 0 0 " 412 "4202496 471 0 0 0 "
389 "12 16 0 0 " // <- These are the goods. 413 "12 16 0 0 " // <- These are the goods.
390 "20 0 1 0 121946157 15077376 314 18446744073709551615 4194304 " 414 "20 0 1 0 121946157 15077376 314 18446744073709551615 4194304 "
391 "4246868 140733983044336 18446744073709551615 140244213071219 " 415 "4246868 140733983044336 18446744073709551615 140244213071219 "
392 "0 0 0 138047495 0 0 0 17 1 0 0 0 0 0"; 416 "0 0 0 138047495 0 0 0 17 1 0 0 0 0 0";
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 522
499 std::unique_ptr<ProcessMetrics> metrics( 523 std::unique_ptr<ProcessMetrics> metrics(
500 ProcessMetrics::CreateProcessMetrics(child.Handle())); 524 ProcessMetrics::CreateProcessMetrics(child.Handle()));
501 EXPECT_EQ(0, metrics->GetOpenFdCount()); 525 EXPECT_EQ(0, metrics->GetOpenFdCount());
502 ASSERT_TRUE(child.Terminate(0, true)); 526 ASSERT_TRUE(child.Terminate(0, true));
503 } 527 }
504 #endif // defined(OS_LINUX) 528 #endif // defined(OS_LINUX)
505 529
506 } // namespace debug 530 } // namespace debug
507 } // namespace base 531 } // namespace base
OLDNEW
« no previous file with comments | « base/process/process_metrics_mac.cc ('k') | base/process/process_metrics_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698