process_metrics_posix.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 <stddef.h>
  7. #include <stdint.h>
  8. #include <sys/time.h>
  9. #include <unistd.h>
  10. #include "base/allocator/buildflags.h"
  11. #include "base/logging.h"
  12. #include "build/build_config.h"
  13. #if !BUILDFLAG(IS_FUCHSIA)
  14. #include <sys/resource.h>
  15. #endif
  16. #if BUILDFLAG(IS_APPLE)
  17. #include <malloc/malloc.h>
  18. #else
  19. #include <malloc.h>
  20. #endif
  21. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
  22. #include <features.h>
  23. #include "base/numerics/safe_conversions.h"
  24. #endif
  25. namespace base {
  26. int64_t TimeValToMicroseconds(const struct timeval& tv) {
  27. int64_t ret = tv.tv_sec; // Avoid (int * int) integer overflow.
  28. ret *= Time::kMicrosecondsPerSecond;
  29. ret += tv.tv_usec;
  30. return ret;
  31. }
  32. ProcessMetrics::~ProcessMetrics() = default;
  33. #if !BUILDFLAG(IS_FUCHSIA)
  34. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  35. static const rlim_t kSystemDefaultMaxFds = 8192;
  36. #elif BUILDFLAG(IS_APPLE)
  37. static const rlim_t kSystemDefaultMaxFds = 256;
  38. #elif BUILDFLAG(IS_SOLARIS)
  39. static const rlim_t kSystemDefaultMaxFds = 8192;
  40. #elif BUILDFLAG(IS_FREEBSD)
  41. static const rlim_t kSystemDefaultMaxFds = 8192;
  42. #elif BUILDFLAG(IS_NETBSD)
  43. static const rlim_t kSystemDefaultMaxFds = 1024;
  44. #elif BUILDFLAG(IS_OPENBSD)
  45. static const rlim_t kSystemDefaultMaxFds = 256;
  46. #elif BUILDFLAG(IS_ANDROID)
  47. static const rlim_t kSystemDefaultMaxFds = 1024;
  48. #elif BUILDFLAG(IS_AIX)
  49. static const rlim_t kSystemDefaultMaxFds = 8192;
  50. #endif
  51. size_t GetMaxFds() {
  52. rlim_t max_fds;
  53. struct rlimit nofile;
  54. if (getrlimit(RLIMIT_NOFILE, &nofile)) {
  55. // getrlimit failed. Take a best guess.
  56. max_fds = kSystemDefaultMaxFds;
  57. RAW_LOG(ERROR, "getrlimit(RLIMIT_NOFILE) failed");
  58. } else {
  59. max_fds = nofile.rlim_cur;
  60. }
  61. if (max_fds > INT_MAX)
  62. max_fds = INT_MAX;
  63. return static_cast<size_t>(max_fds);
  64. }
  65. size_t GetHandleLimit() {
  66. #if BUILDFLAG(IS_APPLE)
  67. // Taken from a small test that allocated ports in a loop.
  68. return static_cast<size_t>(1 << 18);
  69. #else
  70. return GetMaxFds();
  71. #endif
  72. }
  73. void IncreaseFdLimitTo(unsigned int max_descriptors) {
  74. struct rlimit limits;
  75. if (getrlimit(RLIMIT_NOFILE, &limits) == 0) {
  76. rlim_t new_limit = max_descriptors;
  77. if (max_descriptors <= limits.rlim_cur)
  78. return;
  79. if (limits.rlim_max > 0 && limits.rlim_max < max_descriptors) {
  80. new_limit = limits.rlim_max;
  81. }
  82. limits.rlim_cur = new_limit;
  83. if (setrlimit(RLIMIT_NOFILE, &limits) != 0) {
  84. PLOG(INFO) << "Failed to set file descriptor limit";
  85. }
  86. } else {
  87. PLOG(INFO) << "Failed to get file descriptor limit";
  88. }
  89. }
  90. #endif // !BUILDFLAG(IS_FUCHSIA)
  91. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
  92. namespace {
  93. size_t GetMallocUsageMallinfo() {
  94. #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
  95. #if __GLIBC_PREREQ(2, 33)
  96. #define MALLINFO2_FOUND_IN_LIBC
  97. struct mallinfo2 minfo = mallinfo2();
  98. #endif
  99. #endif // defined(__GLIBC__) && defined(__GLIBC_PREREQ)
  100. #if !defined(MALLINFO2_FOUND_IN_LIBC)
  101. struct mallinfo minfo = mallinfo();
  102. #endif
  103. #undef MALLINFO2_FOUND_IN_LIBC
  104. return checked_cast<size_t>(minfo.hblkhd + minfo.arena);
  105. }
  106. } // namespace
  107. #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) ||
  108. // BUILDFLAG(IS_ANDROID)
  109. size_t ProcessMetrics::GetMallocUsage() {
  110. #if BUILDFLAG(IS_APPLE)
  111. malloc_statistics_t stats = {0};
  112. malloc_zone_statistics(nullptr, &stats);
  113. return stats.size_in_use;
  114. #elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
  115. return GetMallocUsageMallinfo();
  116. #elif BUILDFLAG(IS_FUCHSIA)
  117. // TODO(fuchsia): Not currently exposed. https://crbug.com/735087.
  118. return 0;
  119. #endif
  120. }
  121. } // namespace base