[go: nahoru, domu]

More memory stats code cleanup:
Move GetSystemCommitCharge() into bsae\process_util*.
Kill PrintChromeMemoryUsageInfo(), which was only used by
reliability_tests.exe on Windows and whose stats are obsolete.
Delete the now-unnecessary chrome\test\perf\mem_usage* files.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/371025

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@31423 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/base/process_util.h b/base/process_util.h
index 135b92e..85b295b 100644
--- a/base/process_util.h
+++ b/base/process_util.h
@@ -420,6 +420,10 @@
   DISALLOW_EVIL_CONSTRUCTORS(ProcessMetrics);
 };
 
+// Returns the memory commited by the system in KBytes.
+// Returns 0 if it can't compute the commit charge.
+size_t GetSystemCommitCharge();
+
 // Enables low fragmentation heap (LFH) for every heaps of this process. This
 // won't have any effect on heaps created after this function call. It will not
 // modify data allocated in the heaps before calling this function. So it is
diff --git a/base/process_util_linux.cc b/base/process_util_linux.cc
index 7c78f6f..455af3e 100644
--- a/base/process_util_linux.cc
+++ b/base/process_util_linux.cc
@@ -450,4 +450,50 @@
   return percentage;
 }
 
+namespace {
+
+// The format of /proc/meminfo is:
+//
+// MemTotal:      8235324 kB
+// MemFree:       1628304 kB
+// Buffers:        429596 kB
+// Cached:        4728232 kB
+// ...
+const size_t kMemTotalIndex = 1;
+const size_t kMemFreeIndex = 4;
+const size_t kMemBuffersIndex = 7;
+const size_t kMemCacheIndex = 10;
+
+}  // namespace
+
+size_t GetSystemCommitCharge() {
+  // Used memory is: total - free - buffers - caches
+  FilePath meminfo_file("/proc/meminfo");
+  std::string meminfo_data;
+  if (!file_util::ReadFileToString(meminfo_file, &meminfo_data))
+    LOG(ERROR) << "Failed to open /proc/meminfo.";
+    return 0;
+  std::vector<std::string> meminfo_fields;
+  SplitStringAlongWhitespace(meminfo_data, &meminfo_fields);
+
+  if (meminfo_fields.size() < kMemCacheIndex) {
+    LOG(ERROR) << "Failed to parse /proc/meminfo.  Only found " <<
+      meminfo_fields.size() << " fields.";
+    return 0;
+  }
+
+  DCHECK_EQ(meminfo_fields[kMemTotalIndex-1], "MemTotal:");
+  DCHECK_EQ(meminfo_fields[kMemFreeIndex-1], "MemFree:");
+  DCHECK_EQ(meminfo_fields[kMemBuffersIndex-1], "Buffers:");
+  DCHECK_EQ(meminfo_fields[kMemCacheIndex-1], "Cached:");
+
+  size_t result_in_kb;
+  result_in_kb = StringToInt(meminfo_fields[kMemTotalIndex]);
+  result_in_kb -= StringToInt(meminfo_fields[kMemFreeIndex]);
+  result_in_kb -= StringToInt(meminfo_fields[kMemBuffersIndex]);
+  result_in_kb -= StringToInt(meminfo_fields[kMemCacheIndex]);
+
+  return result_in_kb;
+}
+
 }  // namespace base
diff --git a/base/process_util_mac.mm b/base/process_util_mac.mm
index 61029c01..e1574e5 100644
--- a/base/process_util_mac.mm
+++ b/base/process_util_mac.mm
@@ -7,6 +7,7 @@
 
 #import <Cocoa/Cocoa.h>
 #include <crt_externs.h>
+#include <mach/mach.h>
 #include <mach/mach_init.h>
 #include <mach/task.h>
 #include <spawn.h>
@@ -213,4 +214,25 @@
 
 // ------------------------------------------------------------------------
 
+// Bytes committed by the system.
+size_t GetSystemCommitCharge() {
+  host_name_port_t host = mach_host_self();
+  mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
+  vm_statistics_data_t data;
+  kern_return_t kr = host_statistics(host, HOST_VM_INFO,
+                                     reinterpret_cast<host_info_t>(&data),
+                                     &count);
+  if (kr)
+    LOG(ERROR) << "Failed to fetch host statistics.";
+    return 0;
+
+  vm_size_t page_size;
+  kr = host_page_size(host, &page_size);
+  if (kr)
+    LOG(ERROR) << "Failed to fetch host page size.";
+    return 0;
+
+  return (data.active_count * page_size) / 1024;
+}
+
 }  // namespace base
diff --git a/base/process_util_win.cc b/base/process_util_win.cc
index f6157dbc..18de37f 100644
--- a/base/process_util_win.cc
+++ b/base/process_util_win.cc
@@ -799,4 +799,43 @@
   SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
 }
 
+// GetPerformanceInfo is not available on WIN2K.  So we'll
+// load it on-the-fly.
+const wchar_t kPsapiDllName[] = L"psapi.dll";
+typedef BOOL (WINAPI *GetPerformanceInfoFunction) (
+    PPERFORMANCE_INFORMATION pPerformanceInformation,
+    DWORD cb);
+
+// Beware of races if called concurrently from multiple threads.
+static BOOL InternalGetPerformanceInfo(
+    PPERFORMANCE_INFORMATION pPerformanceInformation, DWORD cb) {
+  static GetPerformanceInfoFunction GetPerformanceInfo_func = NULL;
+  if (!GetPerformanceInfo_func) {
+    HMODULE psapi_dll = ::GetModuleHandle(kPsapiDllName);
+    if (psapi_dll)
+      GetPerformanceInfo_func = reinterpret_cast<GetPerformanceInfoFunction>(
+          GetProcAddress(psapi_dll, "GetPerformanceInfo"));
+
+    if (!GetPerformanceInfo_func) {
+      // The function could be loaded!
+      memset(pPerformanceInformation, 0, cb);
+      return FALSE;
+    }
+  }
+  return GetPerformanceInfo_func(pPerformanceInformation, cb);
+}
+
+size_t GetSystemCommitCharge() {
+  // Get the System Page Size.
+  SYSTEM_INFO system_info;
+  GetSystemInfo(&system_info);
+
+  PERFORMANCE_INFORMATION info;
+  if (! InternalGetPerformanceInfo(&info, sizeof(info))) {
+    LOG(ERROR) << "Failed to fetch internal performance info.";
+    return 0;
+  }
+  return (info.CommitTotal * system_info.dwPageSize) / 1024;
+}
+
 }  // namespace base
diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp
index 51dc100..d5b7a5c 100755
--- a/chrome/chrome.gyp
+++ b/chrome/chrome.gyp
@@ -4078,10 +4078,6 @@
         'test/chrome_process_util_linux.cc',
         'test/chrome_process_util_mac.cc',
         'test/chrome_process_util_win.cc',
-        'test/perf/mem_usage_linux.cc',
-        'test/perf/mem_usage_mac.cc',
-        'test/perf/mem_usage_win.cc',
-        'test/perf/mem_usage.h',
         'test/test_browser_window.h',
         'test/testing_profile.cc',
         'test/testing_profile.h',
diff --git a/chrome/test/memory_test/memory_test.cc b/chrome/test/memory_test/memory_test.cc
index 3c55d9a..8134c80 100644
--- a/chrome/test/memory_test/memory_test.cc
+++ b/chrome/test/memory_test/memory_test.cc
@@ -18,7 +18,6 @@
 #include "chrome/test/automation/window_proxy.h"
 #include "chrome/test/chrome_process_util.h"
 #include "chrome/test/ui/ui_test.h"
-#include "chrome/test/perf/mem_usage.h"
 #include "googleurl/src/gurl.h"
 #include "net/base/net_util.h"
 #include "testing/gtest/include/gtest/gtest.h"
@@ -135,7 +134,7 @@
     // Record the initial CommitCharge.  This is a system-wide measurement,
     // so if other applications are running, they can create variance in this
     // test.
-    size_t start_size = GetSystemCommitCharge();
+    size_t start_size = base::GetSystemCommitCharge();
 
     // Cycle through the URLs.
     scoped_refptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
@@ -221,8 +220,8 @@
       PlatformThread::Sleep(100);
     }
 
-    size_t stop_size = GetSystemCommitCharge();
-    PrintResults(test_name, stop_size - start_size);
+    size_t stop_size = base::GetSystemCommitCharge();
+    PrintResults(test_name, (stop_size - start_size) / 1024);
   }
 
   void PrintResults(const char* test_name, size_t commit_size) {
diff --git a/chrome/test/perf/mem_usage.h b/chrome/test/perf/mem_usage.h
deleted file mode 100644
index 90a4824..0000000
--- a/chrome/test/perf/mem_usage.h
+++ /dev/null
@@ -1,17 +0,0 @@
-// Copyright (c) 2006-2008 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.
-
-#ifndef CHROME_TEST_PERF_MEM_USAGE_H_
-#define CHROME_TEST_PERF_MEM_USAGE_H_
-
-#include "base/basictypes.h"
-
-// Get the number of bytes currently committed by the system.
-// Returns -1 on failure.
-size_t GetSystemCommitCharge();
-
-// Get and print memory usage information for running chrome processes.
-void PrintChromeMemoryUsageInfo();
-
-#endif  // CHROME_TEST_PERF_MEM_USAGE_H_
diff --git a/chrome/test/perf/mem_usage_linux.cc b/chrome/test/perf/mem_usage_linux.cc
deleted file mode 100644
index 568b4fc9..0000000
--- a/chrome/test/perf/mem_usage_linux.cc
+++ /dev/null
@@ -1,60 +0,0 @@
-// Copyright (c) 2009 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 "chrome/test/perf/mem_usage.h"
-
-#include "base/file_util.h"
-#include "base/logging.h"
-#include "base/process_util.h"
-#include "base/string_util.h"
-
-namespace {
-
-// The format of /proc/meminfo is:
-//
-// MemTotal:      8235324 kB
-// MemFree:       1628304 kB
-// Buffers:        429596 kB
-// Cached:        4728232 kB
-// ...
-const size_t kMemTotalIndex = 1;
-const size_t kMemFreeIndex = 4;
-const size_t kMemBuffersIndex = 7;
-const size_t kMemCacheIndex = 10;
-
-}  // namespace
-
-size_t GetSystemCommitCharge() {
-  // Used memory is: total - free - buffers - caches
-  FilePath meminfo_file("/proc/meminfo");
-  std::string meminfo_data;
-  if (!file_util::ReadFileToString(meminfo_file, &meminfo_data))
-    return 0;
-  std::vector<std::string> meminfo_fields;
-  SplitStringAlongWhitespace(meminfo_data, &meminfo_fields);
-
-  if (meminfo_fields.size() < kMemCacheIndex) {
-    LOG(ERROR) << "Failed to parse /proc/meminfo.  Only found " <<
-      meminfo_fields.size() << " fields.";
-    return 0;
-  }
-
-  DCHECK_EQ(meminfo_fields[kMemTotalIndex-1], "MemTotal:");
-  DCHECK_EQ(meminfo_fields[kMemFreeIndex-1], "MemFree:");
-  DCHECK_EQ(meminfo_fields[kMemBuffersIndex-1], "Buffers:");
-  DCHECK_EQ(meminfo_fields[kMemCacheIndex-1], "Cached:");
-
-  int result_in_kb;
-  result_in_kb = StringToInt(meminfo_fields[kMemTotalIndex]);
-  result_in_kb -= StringToInt(meminfo_fields[kMemFreeIndex]);
-  result_in_kb -= StringToInt(meminfo_fields[kMemBuffersIndex]);
-  result_in_kb -= StringToInt(meminfo_fields[kMemCacheIndex]);
-
-  return result_in_kb * 1024;
-}
-
-void PrintChromeMemoryUsageInfo() {
-  NOTIMPLEMENTED();
-}
-
diff --git a/chrome/test/perf/mem_usage_mac.cc b/chrome/test/perf/mem_usage_mac.cc
deleted file mode 100644
index 90d87e5..0000000
--- a/chrome/test/perf/mem_usage_mac.cc
+++ /dev/null
@@ -1,37 +0,0 @@
-// Copyright (c) 2009 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 "chrome/test/perf/mem_usage.h"
-
-#include <mach/mach.h>
-
-#include <vector>
-
-#include "base/logging.h"
-#include "base/process_util.h"
-#include "chrome/test/chrome_process_util.h"
-
-// Bytes committed by the system.
-size_t GetSystemCommitCharge() {
-  host_name_port_t host = mach_host_self();
-  mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
-  vm_statistics_data_t data;
-  kern_return_t kr = host_statistics(host, HOST_VM_INFO,
-                                     reinterpret_cast<host_info_t>(&data),
-                                     &count);
-  if (kr)
-    return 0;
-
-  vm_size_t page_size;
-  kr = host_page_size(host, &page_size);
-  if (kr)
-    return 0;
-
-  return data.active_count * page_size;
-}
-
-void PrintChromeMemoryUsageInfo() {
-  NOTIMPLEMENTED();
-}
-
diff --git a/chrome/test/perf/mem_usage_win.cc b/chrome/test/perf/mem_usage_win.cc
deleted file mode 100644
index 465b6024..0000000
--- a/chrome/test/perf/mem_usage_win.cc
+++ /dev/null
@@ -1,97 +0,0 @@
-// Copyright (c) 2006-2008 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 "chrome/test/perf/mem_usage.h"
-
-#include <windows.h>
-#include <psapi.h>
-
-#include "base/file_path.h"
-#include "base/path_service.h"
-#include "base/process_util.h"
-#include "base/scoped_ptr.h"
-#include "chrome/common/chrome_constants.h"
-#include "chrome/common/chrome_paths.h"
-#include "chrome/test/chrome_process_util.h"
-
-// GetPerformanceInfo is not available on WIN2K.  So we'll
-// load it on-the-fly.
-const wchar_t kPsapiDllName[] = L"psapi.dll";
-typedef BOOL (WINAPI *GetPerformanceInfoFunction) (
-    PPERFORMANCE_INFORMATION pPerformanceInformation,
-    DWORD cb);
-
-static BOOL InternalGetPerformanceInfo(
-    PPERFORMANCE_INFORMATION pPerformanceInformation, DWORD cb) {
-  static GetPerformanceInfoFunction GetPerformanceInfo_func = NULL;
-  if (!GetPerformanceInfo_func) {
-    HMODULE psapi_dll = ::GetModuleHandle(kPsapiDllName);
-    if (psapi_dll)
-      GetPerformanceInfo_func = reinterpret_cast<GetPerformanceInfoFunction>(
-          GetProcAddress(psapi_dll, "GetPerformanceInfo"));
-
-    if (!GetPerformanceInfo_func) {
-      // The function could be loaded!
-      memset(pPerformanceInformation, 0, cb);
-      return FALSE;
-    }
-  }
-  return GetPerformanceInfo_func(pPerformanceInformation, cb);
-}
-
-
-size_t GetSystemCommitCharge() {
-  // Get the System Page Size.
-  SYSTEM_INFO system_info;
-  GetSystemInfo(&system_info);
-
-  PERFORMANCE_INFORMATION info;
-  if (InternalGetPerformanceInfo(&info, sizeof(info)))
-    return info.CommitTotal * system_info.dwPageSize;
-  return -1;
-}
-
-void PrintChromeMemoryUsageInfo() {
-  printf("\n");
-
-  FilePath data_dir;
-  PathService::Get(chrome::DIR_USER_DATA, &data_dir);
-  int browser_process_pid = ChromeBrowserProcessId(data_dir);
-  ChromeProcessList chrome_processes(GetRunningChromeProcesses(data_dir));
-
-  ChromeProcessList::const_iterator it;
-  for (it = chrome_processes.begin(); it != chrome_processes.end(); ++it) {
-    base::ProcessHandle process_handle;
-    if (!base::OpenPrivilegedProcessHandle(*it, &process_handle)) {
-      NOTREACHED();
-    }
-
-    // TODO(sgk):  if/when base::ProcessMetrics can return real memory
-    // stats on mac, convert to:
-    //
-    // scoped_ptr<base::ProcessMetrics> process_metrics;
-    // process_metrics.reset(
-    //     base::ProcessMetrics::CreateProcessMetrics(process_handle));
-    scoped_ptr<ChromeTestProcessMetrics> process_metrics;
-    process_metrics.reset(
-        ChromeTestProcessMetrics::CreateProcessMetrics(process_handle));
-
-    size_t peak_virtual_size = process_metrics->GetPeakPagefileUsage();
-    size_t current_virtual_size = process_metrics->GetPagefileUsage();
-    size_t peak_working_set_size = process_metrics->GetPeakWorkingSetSize();
-    size_t current_working_set_size = process_metrics->GetWorkingSetSize();
-
-    if (*it == browser_process_pid) {
-      wprintf(L"browser_vm_peak = %d\n", peak_virtual_size);
-      wprintf(L"browser_vm_current = %d\n", current_virtual_size);
-      wprintf(L"browser_ws_peak = %d\n", peak_working_set_size);
-      wprintf(L"browser_ws_final = %d\n", current_working_set_size);
-    } else {
-      wprintf(L"render_vm_peak = %d\n", peak_virtual_size);
-      wprintf(L"render_vm_current = %d\n", current_virtual_size);
-      wprintf(L"render_ws_peak = %d\n", peak_working_set_size);
-      wprintf(L"render_ws_final = %d\n", current_working_set_size);
-    }
-  };
-}
diff --git a/chrome/test/reliability/page_load_test.cc b/chrome/test/reliability/page_load_test.cc
index ffaa63a..ef7f66da 100644
--- a/chrome/test/reliability/page_load_test.cc
+++ b/chrome/test/reliability/page_load_test.cc
@@ -60,7 +60,6 @@
 #include "chrome/test/automation/tab_proxy.h"
 #include "chrome/test/automation/window_proxy.h"
 #include "chrome/test/ui/ui_test.h"
-#include "chrome/test/perf/mem_usage.h"
 #include "chrome/test/reliability/page_load_test.h"
 #include "net/base/net_util.h"
 
@@ -608,12 +607,6 @@
     } else {
       NavigateThroughURLList(log_file);
     }
-
-// TODO(estade): port.
-#if defined(OS_WIN)
-    if (g_memory_usage)
-      PrintChromeMemoryUsageInfo();
-#endif  // defined(OS_WIN)
   }
 
   if (!g_end_url.empty()) {
diff --git a/chrome_frame/chrome_frame.gyp b/chrome_frame/chrome_frame.gyp
index 37436b7..b6a144f 100644
--- a/chrome_frame/chrome_frame.gyp
+++ b/chrome_frame/chrome_frame.gyp
@@ -404,7 +404,6 @@
             '../chrome/installer/installer.gyp:installer_util',
           ],
           'sources': [
-            '../chrome/test/perf/mem_usage_win.cc',
             '../chrome/test/chrome_process_util_win.cc',
             '../base/test/test_file_util_win.cc',
           ]
diff --git a/chrome_frame/test/perf/chrome_frame_perftest.cc b/chrome_frame/test/perf/chrome_frame_perftest.cc
index 968bb6d..d93ebd5 100644
--- a/chrome_frame/test/perf/chrome_frame_perftest.cc
+++ b/chrome_frame/test/perf/chrome_frame_perftest.cc
@@ -23,7 +23,6 @@
 #include "chrome/common/chrome_paths.h"
 #include "chrome/common/chrome_paths_internal.h"
 #include "chrome/test/chrome_process_util.h"
-#include "chrome/test/perf/mem_usage.h"
 #include "chrome/test/ui/ui_test.h"
 
 #include "chrome_frame/test_utils.h"
@@ -580,7 +579,7 @@
     // Record the initial CommitCharge.  This is a system-wide measurement,
     // so if other applications are running, they can create variance in this
     // test.
-    start_commit_charge_ = GetSystemCommitCharge();
+    start_commit_charge_ = base::GetSystemCommitCharge();
 
     for (int i = 0; i < total_urls; i++)
       urls_.push_back(urls[i]);
@@ -690,8 +689,8 @@
     // chrome processes which would have exited by now.
     Sleep(200);
 
-    size_t end_commit_charge = GetSystemCommitCharge();
-    size_t commit_size = end_commit_charge - start_commit_charge_;
+    size_t end_commit_charge = base::GetSystemCommitCharge();
+    size_t commit_size = (end_commit_charge - start_commit_charge_) * 1024;
 
     std::string trace_name(test_name);
     trace_name.append("_cc");
@@ -764,7 +763,7 @@
     ASSERT_FALSE(false);
   }
 
-  // Holds the commit charge at the start of the memory test run.
+  // Holds the commit charge in KBytes at the start of the memory test run.
   size_t start_commit_charge_;
 
   // The index of the URL being tested.