diff --git a/base/util.cc b/base/util.cc index 0c9d4ddb59..2169aad090 100644 --- a/base/util.cc +++ b/base/util.cc @@ -86,4 +86,32 @@ int64 WallTimer::GetInMs() const { return local_sum_usec / kMilliSecInMicroSec; #endif } + +// GetUsedMemory + +#if defined(__APPLE__) && defined(__GNUC__) +#include +#include + +int64 GetMemoryUsage () { + task_t task = MACH_PORT_NULL; + struct task_basic_info t_info; + mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; + + if (KERN_SUCCESS != task_info(mach_task_self(), + TASK_BASIC_INFO, + (task_info_t)&t_info, + &t_info_count)) { + return -1; + } + int64 resident_memory = t_info.resident_size; + // int64 virtual_memory = t_info.virtual_size; + return resident_memory; +} +#else +int64 GetMemoryUsage() { + return 0; +} +#endif + } // namespace operations_research diff --git a/base/util.h b/base/util.h index 1ac5cb2b12..5e85bf1087 100644 --- a/base/util.h +++ b/base/util.h @@ -170,6 +170,8 @@ class WallTimer { DISALLOW_COPY_AND_ASSIGN(WallTimer); }; +int64 GetMemoryUsage(); + } // namespace operations_research diff --git a/constraint_solver/constraint_solver.cc b/constraint_solver/constraint_solver.cc index b15e031486..91d81a43e0 100644 --- a/constraint_solver/constraint_solver.cc +++ b/constraint_solver/constraint_solver.cc @@ -1258,13 +1258,9 @@ string Solver::DebugString() const { } int64 Solver::MemoryUsage() { - // FIXME: Implement the real thing. - return 0ll; - + return GetMemoryUsage(); } - - int64 Solver::wall_time() const { return timer_->GetInMs(); } diff --git a/constraint_solver/search.cc b/constraint_solver/search.cc index 5fc205abd9..9ab9b79243 100644 --- a/constraint_solver/search.cc +++ b/constraint_solver/search.cc @@ -205,8 +205,16 @@ void SearchLog::OutputLine(const string& line) { } string SearchLog::MemoryUsage() { - return StringPrintf("memory used = %" GG_LL_FORMAT "d", Solver::MemoryUsage()); - + int64 memory_usage = Solver::MemoryUsage(); + if (memory_usage > 0x200000) { + return StringPrintf("memory used = %.2lf MB", + memory_usage * 1.0 / 1024 / 1024); + } else if (memory_usage > 0x1000) { + return StringPrintf("memory used = %" GG_LL_FORMAT "d KB", + memory_usage / 1024); + } else { + return StringPrintf("memory used = %" GG_LL_FORMAT "d", memory_usage); + } } SearchMonitor* Solver::MakeSearchLog(int period) {