2013-03-09 04:29:13 +08:00
|
|
|
//
|
|
|
|
// MemoryGauge.cpp
|
|
|
|
// PerfTestDriver
|
|
|
|
//
|
|
|
|
// Created by Enrico Granata on 3/6/13.
|
|
|
|
// Copyright (c) 2013 Apple Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "MemoryGauge.h"
|
|
|
|
#include <assert.h>
|
2013-03-21 05:18:20 +08:00
|
|
|
#include <mach/mach.h>
|
2013-03-09 04:29:13 +08:00
|
|
|
#include <mach/task.h>
|
2013-03-21 05:18:20 +08:00
|
|
|
#include <mach/mach_traps.h>
|
2013-03-09 04:29:13 +08:00
|
|
|
|
2013-03-19 06:34:00 +08:00
|
|
|
using namespace lldb_perf;
|
2013-03-09 04:29:13 +08:00
|
|
|
|
2013-03-21 11:32:24 +08:00
|
|
|
MemoryStats::MemoryStats (mach_vm_size_t virtual_size,
|
|
|
|
mach_vm_size_t resident_size,
|
|
|
|
mach_vm_size_t max_resident_size) :
|
|
|
|
m_virtual_size (virtual_size),
|
|
|
|
m_resident_size (resident_size),
|
|
|
|
m_max_resident_size (max_resident_size)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryStats::MemoryStats (const MemoryStats& rhs) :
|
|
|
|
m_virtual_size (rhs.m_virtual_size),
|
|
|
|
m_resident_size (rhs.m_resident_size),
|
|
|
|
m_max_resident_size (rhs.m_max_resident_size)
|
|
|
|
{
|
|
|
|
}
|
2013-03-21 05:18:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
MemoryStats&
|
|
|
|
MemoryStats::operator = (const MemoryStats& rhs)
|
|
|
|
{
|
2013-03-21 11:32:24 +08:00
|
|
|
if (this != &rhs)
|
2013-03-21 05:18:20 +08:00
|
|
|
{
|
|
|
|
m_virtual_size = rhs.m_virtual_size;
|
|
|
|
m_resident_size = rhs.m_resident_size;
|
|
|
|
m_max_resident_size = rhs.m_max_resident_size;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryStats&
|
|
|
|
MemoryStats::operator += (const MemoryStats& rhs)
|
|
|
|
{
|
|
|
|
m_virtual_size += rhs.m_virtual_size;
|
|
|
|
m_resident_size += rhs.m_resident_size;
|
|
|
|
m_max_resident_size += rhs.m_max_resident_size;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryStats
|
|
|
|
MemoryStats::operator - (const MemoryStats& rhs)
|
|
|
|
{
|
|
|
|
return MemoryStats(m_virtual_size - rhs.m_virtual_size,
|
|
|
|
m_resident_size - rhs.m_resident_size,
|
|
|
|
m_max_resident_size - rhs.m_max_resident_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryStats&
|
2013-03-21 11:32:24 +08:00
|
|
|
MemoryStats::operator / (size_t n)
|
2013-03-21 05:18:20 +08:00
|
|
|
{
|
2013-03-21 11:32:24 +08:00
|
|
|
m_virtual_size /= n;
|
|
|
|
m_resident_size /= n;
|
|
|
|
m_max_resident_size /= n;
|
2013-03-21 05:18:20 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-03-09 04:29:13 +08:00
|
|
|
MemoryGauge::SizeType
|
2013-03-21 11:32:24 +08:00
|
|
|
MemoryGauge::Now ()
|
2013-03-09 04:29:13 +08:00
|
|
|
{
|
2013-03-21 05:18:20 +08:00
|
|
|
task_t task = mach_task_self();
|
2013-03-09 04:29:13 +08:00
|
|
|
mach_task_basic_info_data_t taskBasicInfo;
|
|
|
|
mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
|
2013-03-21 05:18:20 +08:00
|
|
|
auto task_info_ret = task_info(task, MACH_TASK_BASIC_INFO, (task_info_t) & taskBasicInfo, &count);
|
|
|
|
if (task_info_ret == KERN_SUCCESS) {
|
|
|
|
return MemoryStats(taskBasicInfo.virtual_size, taskBasicInfo.resident_size, taskBasicInfo.resident_size_max);
|
2013-03-09 04:29:13 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryGauge::MemoryGauge () :
|
2013-03-21 11:32:24 +08:00
|
|
|
m_start(),
|
|
|
|
m_state(MemoryGauge::State::eNeverUsed)
|
2013-03-09 04:29:13 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-03-21 11:32:24 +08:00
|
|
|
MemoryGauge::Start ()
|
2013-03-09 04:29:13 +08:00
|
|
|
{
|
2013-03-21 11:32:24 +08:00
|
|
|
m_state = MemoryGauge::State::eCounting;
|
|
|
|
m_start = Now();
|
2013-03-09 04:29:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MemoryGauge::SizeType
|
2013-03-21 11:32:24 +08:00
|
|
|
MemoryGauge::Stop ()
|
2013-03-09 04:29:13 +08:00
|
|
|
{
|
2013-03-21 11:32:24 +08:00
|
|
|
auto stop = Now();
|
|
|
|
assert(m_state == MemoryGauge::State::eCounting && "cannot stop a non-started gauge");
|
|
|
|
m_state = MemoryGauge::State::eStopped;
|
2013-03-09 04:29:13 +08:00
|
|
|
return (m_value = stop-m_start);
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryGauge::SizeType
|
2013-03-21 11:32:24 +08:00
|
|
|
MemoryGauge::GetValue ()
|
2013-03-09 04:29:13 +08:00
|
|
|
{
|
2013-03-21 11:32:24 +08:00
|
|
|
assert(m_state == MemoryGauge::State::eStopped && "gauge must be used before you can evaluate it");
|
2013-03-09 04:29:13 +08:00
|
|
|
return m_value;
|
|
|
|
}
|