Commit a7b5e669 authored by droger's avatar droger Committed by Commit bot

[iOS] Upstream //ios/chrome/browser/memory

Review URL: https://codereview.chromium.org/1057933002

Cr-Commit-Position: refs/heads/master@{#324216}
parent 303b4f84
// Copyright 2014 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 IOS_CHROME_BROWSER_MEMORY_MEMORY_DEBUGGER_H_
#define IOS_CHROME_BROWSER_MEMORY_MEMORY_DEBUGGER_H_
#import <UIKit/UIKit.h>
// A view that contains memory information (e.g. amount of free memory) and
// tools (e.g. trigger memory warning) to help investigate memory issues and
// performance.
//
// The debugger ensures that it remains visible by continuously calling
// bringSubviewToFront on it's parent so it should be added as a subview of the
// the application's window in order to stay visible all the times.
//
// The debugger owns some timers that must be invalidated before it can be
// deallocated so the owner must call |invalidateTimers| before a MemoryDebugger
// instance can be deallocated.
@interface MemoryDebugger : UIView<UITextFieldDelegate>
// Must be called before the object can be deallocated!
- (void)invalidateTimers;
@end
#endif // IOS_CHROME_BROWSER_MEMORY_MEMORY_DEBUGGER_H_
This diff is collapsed.
// Copyright 2014 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 IOS_CHROME_BROWSER_MEMORY_MEMORY_DEBUGGER_MANAGER_H_
#define IOS_CHROME_BROWSER_MEMORY_MEMORY_DEBUGGER_MANAGER_H_
#import <Foundation/Foundation.h>
class PrefRegistrySimple;
class PrefService;
@class UIView;
// A class to manage the life cycle of a MemoryDebugger instance.
//
// A MemoryDebugger's existence is controlled by a pref in local state, so the
// MemoryDebuggerManager listens for changes to that pref and instantiates or
// frees the debugger as appropriate.
@interface MemoryDebuggerManager : NSObject
// Designated initializer.
- (instancetype)initWithView:(UIView*)view prefs:(PrefService*)prefs;
// Registers local state preferences.
+ (void)registerLocalState:(PrefRegistrySimple*)registry;
@end
#endif // IOS_CHROME_BROWSER_MEMORY_MEMORY_DEBUGGER_MANAGER_H_
// Copyright 2014 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.
#import "ios/chrome/browser/memory/memory_debugger_manager.h"
#include "base/ios/weak_nsobject.h"
#import "base/mac/bind_objc_block.h"
#include "base/mac/scoped_nsobject.h"
#include "base/prefs/pref_member.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_service.h"
#import "ios/chrome/browser/memory/memory_debugger.h"
#import "ios/chrome/browser/pref_names.h"
@implementation MemoryDebuggerManager {
UIView* debuggerParentView_; // weak
base::scoped_nsobject<MemoryDebugger> memoryDebugger_;
BooleanPrefMember showMemoryDebugger_;
}
- (instancetype)initWithView:(UIView*)debuggerParentView
prefs:(PrefService*)prefs {
if (self = [super init]) {
debuggerParentView_ = debuggerParentView;
// Set up the callback for when the pref to show/hide the debugger changes.
base::WeakNSObject<MemoryDebuggerManager> weakSelf(self);
base::Closure callback = base::BindBlock(^{
base::scoped_nsobject<MemoryDebuggerManager> strongSelf(
[weakSelf retain]);
if (strongSelf) {
[self onShowMemoryDebuggingToolsChange];
}
});
showMemoryDebugger_.Init(prefs::kShowMemoryDebuggingTools, prefs, callback);
// Invoke the pref change callback once to show the debugger on start up,
// if necessary.
[self onShowMemoryDebuggingToolsChange];
}
return self;
}
- (void)dealloc {
[self tearDownDebugger];
[super dealloc];
}
#pragma mark - Pref-handling methods
// Registers local state prefs.
+ (void)registerLocalState:(PrefRegistrySimple*)registry {
registry->RegisterBooleanPref(prefs::kShowMemoryDebuggingTools, false);
}
// Shows or hides the debugger when the pref changes.
- (void)onShowMemoryDebuggingToolsChange {
if (showMemoryDebugger_.GetValue()) {
memoryDebugger_.reset([[MemoryDebugger alloc] init]);
[debuggerParentView_ addSubview:memoryDebugger_];
} else {
[self tearDownDebugger];
}
}
// Tears down the debugger so it can be deallocated.
- (void)tearDownDebugger {
[memoryDebugger_ invalidateTimers];
[memoryDebugger_ removeFromSuperview];
memoryDebugger_.reset();
}
@end
// Copyright 2014 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 "ios/chrome/browser/memory/memory_metrics.h"
#include <mach/mach.h>
#include "base/logging.h"
#include "base/process/process_handle.h"
#include "base/process/process_metrics.h"
#ifdef ARCH_CPU_64_BITS
#define cr_vm_region vm_region_64
#else
#define cr_vm_region vm_region
#endif
namespace {
// The number of pages returned by host_statistics and vm_region are a count
// of pages of 4096 bytes even when running on arm64 but the constants that
// are exposed (vm_page_size, VM_PAGE_SIZE, host_page_size) are all equals to
// 16384 bytes. So we define our own constant here to convert from page count
// to bytes.
const uint64_t kVMPageSize = 4096;
}
namespace memory_util {
uint64 GetFreePhysicalBytes() {
vm_statistics_data_t vmstat;
mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
kern_return_t result =
host_statistics(mach_host_self(), HOST_VM_INFO,
reinterpret_cast<host_info_t>(&vmstat), &count);
if (result != KERN_SUCCESS) {
LOG(ERROR) << "Calling host_statistics failed.";
return 0;
}
return vmstat.free_count * kVMPageSize;
}
uint64 GetRealMemoryUsedInBytes() {
base::ProcessHandle process_handle = base::GetCurrentProcessHandle();
scoped_ptr<base::ProcessMetrics> process_metrics(
base::ProcessMetrics::CreateProcessMetrics(process_handle));
return static_cast<uint64>(process_metrics->GetWorkingSetSize());
}
uint64 GetDirtyVMBytes() {
// Iterate over all VM regions and sum their dirty pages.
unsigned int total_dirty_pages = 0;
vm_size_t vm_size = 0;
kern_return_t result;
for (vm_address_t address = MACH_VM_MIN_ADDRESS;; address += vm_size) {
vm_region_extended_info_data_t info;
mach_msg_type_number_t info_count = VM_REGION_EXTENDED_INFO_COUNT;
mach_port_t object_name;
result = cr_vm_region(
mach_task_self(), &address, &vm_size, VM_REGION_EXTENDED_INFO,
reinterpret_cast<vm_region_info_t>(&info), &info_count, &object_name);
if (result == KERN_INVALID_ADDRESS) {
// The end of the address space has been reached.
break;
} else if (result != KERN_SUCCESS) {
LOG(ERROR) << "Calling vm_region failed with code: " << result;
break;
} else {
total_dirty_pages += info.pages_dirtied;
}
}
return total_dirty_pages * kVMPageSize;
}
uint64 GetInternalVMBytes() {
task_vm_info_data_t task_vm_info;
mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
kern_return_t result =
task_info(mach_task_self(), TASK_VM_INFO,
reinterpret_cast<task_info_t>(&task_vm_info), &count);
if (result != KERN_SUCCESS) {
LOG(ERROR) << "Calling task_info failed.";
return 0;
}
return static_cast<uint64>(task_vm_info.internal);
}
} // namespace memory_util
// Copyright 2014 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 IOS_CHROME_BROWSER_MEMORY_MEMORY_METRICS_H_
#define IOS_CHROME_BROWSER_MEMORY_MEMORY_METRICS_H_
#include "base/basictypes.h"
namespace memory_util {
// "Physical Free" memory metric. This corresponds to the "Physical Memory Free"
// value reported by the Memory Monitor in Instruments.
uint64 GetFreePhysicalBytes();
// "Real Memory Used" memory metric. This corresponds to the "Real Memory" value
// reported for the app by the Memory Monitor in Instruments.
uint64 GetRealMemoryUsedInBytes();
// "Xcode Gauge" memory metric. This corresponds to the "Memory" value reported
// for the app by the Debug Navigator in Xcode. Only supported in iOS 7 and
// later.
uint64 GetInternalVMBytes();
// "Dirty VM" memory metric. This corresponds to the "Dirty Size" value reported
// for the app by the VM Tracker in Instruments.
uint64 GetDirtyVMBytes();
} // namespace memory_util
#endif // IOS_CHROME_BROWSER_MEMORY_MEMORY_METRICS_H_
......@@ -33,4 +33,8 @@ const char kIosBookmarkPromoAlreadySeen[] = "ios.bookmark.promo_already_seen";
// The preferred SSO user for wallet payments.
const char kPaymentsPreferredUserId[] = "ios.payments.preferred_user_id";
// True if the memory debugging tools should be visible.
extern const char kShowMemoryDebuggingTools[] =
"ios.memory.show_debugging_tools";
} // namespace prefs
......@@ -20,6 +20,7 @@ namespace prefs {
extern const char kIosBookmarkFolderDefault[];
extern const char kIosBookmarkPromoAlreadySeen[];
extern const char kPaymentsPreferredUserId[];
extern const char kShowMemoryDebuggingTools[];
} // namespace prefs
......
......@@ -105,6 +105,12 @@
'browser/infobars/infobar_manager_impl.h',
'browser/infobars/infobar_utils.h',
'browser/infobars/infobar_utils.mm',
'browser/memory/memory_debugger.h',
'browser/memory/memory_debugger.mm',
'browser/memory/memory_debugger_manager.h',
'browser/memory/memory_debugger_manager.mm',
'browser/memory/memory_metrics.cc',
'browser/memory/memory_metrics.h',
'browser/net/chrome_cookie_store_ios_client.h',
'browser/net/chrome_cookie_store_ios_client.mm',
'browser/net/image_fetcher.h',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment