1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "base/process/process_metrics.h" 6 7 #include <mach/task.h> 8 9 namespace base { 10 11 namespace { 12 GetTaskInfo(task_basic_info_64 * task_info_data)13bool GetTaskInfo(task_basic_info_64* task_info_data) { 14 mach_msg_type_number_t count = TASK_BASIC_INFO_64_COUNT; 15 kern_return_t kr = task_info(mach_task_self(), 16 TASK_BASIC_INFO_64, 17 reinterpret_cast<task_info_t>(task_info_data), 18 &count); 19 return kr == KERN_SUCCESS; 20 } 21 22 } // namespace 23 ProcessMetrics(ProcessHandle process)24ProcessMetrics::ProcessMetrics(ProcessHandle process) {} 25 ~ProcessMetrics()26ProcessMetrics::~ProcessMetrics() {} 27 28 // static CreateProcessMetrics(ProcessHandle process)29ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) { 30 return new ProcessMetrics(process); 31 } 32 GetPagefileUsage() const33size_t ProcessMetrics::GetPagefileUsage() const { 34 task_basic_info_64 task_info_data; 35 if (!GetTaskInfo(&task_info_data)) 36 return 0; 37 return task_info_data.virtual_size; 38 } 39 GetWorkingSetSize() const40size_t ProcessMetrics::GetWorkingSetSize() const { 41 task_basic_info_64 task_info_data; 42 if (!GetTaskInfo(&task_info_data)) 43 return 0; 44 return task_info_data.resident_size; 45 } 46 GetMaxFds()47size_t GetMaxFds() { 48 static const rlim_t kSystemDefaultMaxFds = 256; 49 rlim_t max_fds; 50 struct rlimit nofile; 51 if (getrlimit(RLIMIT_NOFILE, &nofile)) { 52 // Error case: Take a best guess. 53 max_fds = kSystemDefaultMaxFds; 54 } else { 55 max_fds = nofile.rlim_cur; 56 } 57 58 if (max_fds > INT_MAX) 59 max_fds = INT_MAX; 60 61 return static_cast<size_t>(max_fds); 62 } 63 64 } // namespace base 65