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