process_metrics_ios.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include "base/process/process_metrics.h"
  5. #include <limits.h>
  6. #include <mach/task.h>
  7. #include <malloc/malloc.h>
  8. #include <stddef.h>
  9. #include "base/check_op.h"
  10. #include "base/mac/scoped_mach_port.h"
  11. #include "base/memory/ptr_util.h"
  12. #include "base/notreached.h"
  13. #include "base/numerics/safe_conversions.h"
  14. namespace base {
  15. ProcessMetrics::ProcessMetrics(ProcessHandle process) {}
  16. ProcessMetrics::~ProcessMetrics() {}
  17. // static
  18. std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics(
  19. ProcessHandle process) {
  20. return WrapUnique(new ProcessMetrics(process));
  21. }
  22. TimeDelta ProcessMetrics::GetCumulativeCPUUsage() {
  23. NOTIMPLEMENTED();
  24. return TimeDelta();
  25. }
  26. size_t GetMaxFds() {
  27. static const rlim_t kSystemDefaultMaxFds = 256;
  28. rlim_t max_fds;
  29. struct rlimit nofile;
  30. if (getrlimit(RLIMIT_NOFILE, &nofile)) {
  31. // Error case: Take a best guess.
  32. max_fds = kSystemDefaultMaxFds;
  33. } else {
  34. max_fds = nofile.rlim_cur;
  35. }
  36. if (max_fds > INT_MAX)
  37. max_fds = INT_MAX;
  38. return static_cast<size_t>(max_fds);
  39. }
  40. void IncreaseFdLimitTo(unsigned int max_descriptors) {
  41. // Unimplemented.
  42. }
  43. // Bytes committed by the system.
  44. size_t GetSystemCommitCharge() {
  45. NOTIMPLEMENTED();
  46. return 0;
  47. }
  48. bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
  49. struct host_basic_info hostinfo;
  50. mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
  51. base::mac::ScopedMachSendRight host(mach_host_self());
  52. int result = host_info(host.get(), HOST_BASIC_INFO,
  53. reinterpret_cast<host_info_t>(&hostinfo), &count);
  54. if (result != KERN_SUCCESS)
  55. return false;
  56. DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
  57. meminfo->total = static_cast<int>(hostinfo.max_mem / 1024);
  58. vm_statistics64_data_t vm_info;
  59. count = HOST_VM_INFO64_COUNT;
  60. if (host_statistics64(host.get(), HOST_VM_INFO64,
  61. reinterpret_cast<host_info64_t>(&vm_info),
  62. &count) != KERN_SUCCESS) {
  63. return false;
  64. }
  65. DCHECK_EQ(HOST_VM_INFO64_COUNT, count);
  66. // Check that PAGE_SIZE is divisible by 1024 (2^10).
  67. CHECK_EQ(PAGE_SIZE, (PAGE_SIZE >> 10) << 10);
  68. meminfo->free = saturated_cast<int>(
  69. PAGE_SIZE / 1024 * (vm_info.free_count - vm_info.speculative_count));
  70. meminfo->speculative =
  71. saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.speculative_count);
  72. meminfo->file_backed =
  73. saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.external_page_count);
  74. meminfo->purgeable =
  75. saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.purgeable_count);
  76. return true;
  77. }
  78. size_t ProcessMetrics::GetMallocUsage() {
  79. malloc_statistics_t stats;
  80. malloc_zone_statistics(nullptr, &stats);
  81. return stats.size_in_use;
  82. }
  83. } // namespace base