xref: /aosp_15_r20/external/libchrome/base/process/process_metrics_posix.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "base/process/process_metrics.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include <limits.h>
8*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
9*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
10*635a8641SAndroid Build Coastguard Worker #include <sys/time.h>
11*635a8641SAndroid Build Coastguard Worker #include <unistd.h>
12*635a8641SAndroid Build Coastguard Worker 
13*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
14*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
15*635a8641SAndroid Build Coastguard Worker 
16*635a8641SAndroid Build Coastguard Worker #if !defined(OS_FUCHSIA)
17*635a8641SAndroid Build Coastguard Worker #include <sys/resource.h>
18*635a8641SAndroid Build Coastguard Worker #endif
19*635a8641SAndroid Build Coastguard Worker 
20*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX)
21*635a8641SAndroid Build Coastguard Worker #include <malloc/malloc.h>
22*635a8641SAndroid Build Coastguard Worker #else
23*635a8641SAndroid Build Coastguard Worker #include <malloc.h>
24*635a8641SAndroid Build Coastguard Worker #endif
25*635a8641SAndroid Build Coastguard Worker 
26*635a8641SAndroid Build Coastguard Worker namespace base {
27*635a8641SAndroid Build Coastguard Worker 
TimeValToMicroseconds(const struct timeval & tv)28*635a8641SAndroid Build Coastguard Worker int64_t TimeValToMicroseconds(const struct timeval& tv) {
29*635a8641SAndroid Build Coastguard Worker   int64_t ret = tv.tv_sec;  // Avoid (int * int) integer overflow.
30*635a8641SAndroid Build Coastguard Worker   ret *= Time::kMicrosecondsPerSecond;
31*635a8641SAndroid Build Coastguard Worker   ret += tv.tv_usec;
32*635a8641SAndroid Build Coastguard Worker   return ret;
33*635a8641SAndroid Build Coastguard Worker }
34*635a8641SAndroid Build Coastguard Worker 
35*635a8641SAndroid Build Coastguard Worker ProcessMetrics::~ProcessMetrics() = default;
36*635a8641SAndroid Build Coastguard Worker 
37*635a8641SAndroid Build Coastguard Worker #if !defined(OS_FUCHSIA)
38*635a8641SAndroid Build Coastguard Worker 
39*635a8641SAndroid Build Coastguard Worker #if defined(OS_LINUX)
40*635a8641SAndroid Build Coastguard Worker static const rlim_t kSystemDefaultMaxFds = 8192;
41*635a8641SAndroid Build Coastguard Worker #elif defined(OS_MACOSX)
42*635a8641SAndroid Build Coastguard Worker static const rlim_t kSystemDefaultMaxFds = 256;
43*635a8641SAndroid Build Coastguard Worker #elif defined(OS_SOLARIS)
44*635a8641SAndroid Build Coastguard Worker static const rlim_t kSystemDefaultMaxFds = 8192;
45*635a8641SAndroid Build Coastguard Worker #elif defined(OS_FREEBSD)
46*635a8641SAndroid Build Coastguard Worker static const rlim_t kSystemDefaultMaxFds = 8192;
47*635a8641SAndroid Build Coastguard Worker #elif defined(OS_NETBSD)
48*635a8641SAndroid Build Coastguard Worker static const rlim_t kSystemDefaultMaxFds = 1024;
49*635a8641SAndroid Build Coastguard Worker #elif defined(OS_OPENBSD)
50*635a8641SAndroid Build Coastguard Worker static const rlim_t kSystemDefaultMaxFds = 256;
51*635a8641SAndroid Build Coastguard Worker #elif defined(OS_ANDROID)
52*635a8641SAndroid Build Coastguard Worker static const rlim_t kSystemDefaultMaxFds = 1024;
53*635a8641SAndroid Build Coastguard Worker #elif defined(OS_AIX)
54*635a8641SAndroid Build Coastguard Worker static const rlim_t kSystemDefaultMaxFds = 8192;
55*635a8641SAndroid Build Coastguard Worker #endif
56*635a8641SAndroid Build Coastguard Worker 
GetMaxFds()57*635a8641SAndroid Build Coastguard Worker size_t GetMaxFds() {
58*635a8641SAndroid Build Coastguard Worker   rlim_t max_fds;
59*635a8641SAndroid Build Coastguard Worker   struct rlimit nofile;
60*635a8641SAndroid Build Coastguard Worker   if (getrlimit(RLIMIT_NOFILE, &nofile)) {
61*635a8641SAndroid Build Coastguard Worker     // getrlimit failed. Take a best guess.
62*635a8641SAndroid Build Coastguard Worker     max_fds = kSystemDefaultMaxFds;
63*635a8641SAndroid Build Coastguard Worker     RAW_LOG(ERROR, "getrlimit(RLIMIT_NOFILE) failed");
64*635a8641SAndroid Build Coastguard Worker   } else {
65*635a8641SAndroid Build Coastguard Worker     max_fds = nofile.rlim_cur;
66*635a8641SAndroid Build Coastguard Worker   }
67*635a8641SAndroid Build Coastguard Worker 
68*635a8641SAndroid Build Coastguard Worker   if (max_fds > INT_MAX)
69*635a8641SAndroid Build Coastguard Worker     max_fds = INT_MAX;
70*635a8641SAndroid Build Coastguard Worker 
71*635a8641SAndroid Build Coastguard Worker   return static_cast<size_t>(max_fds);
72*635a8641SAndroid Build Coastguard Worker }
73*635a8641SAndroid Build Coastguard Worker 
IncreaseFdLimitTo(unsigned int max_descriptors)74*635a8641SAndroid Build Coastguard Worker void IncreaseFdLimitTo(unsigned int max_descriptors) {
75*635a8641SAndroid Build Coastguard Worker   struct rlimit limits;
76*635a8641SAndroid Build Coastguard Worker   if (getrlimit(RLIMIT_NOFILE, &limits) == 0) {
77*635a8641SAndroid Build Coastguard Worker     unsigned int new_limit = max_descriptors;
78*635a8641SAndroid Build Coastguard Worker     if (max_descriptors <= limits.rlim_cur)
79*635a8641SAndroid Build Coastguard Worker       return;
80*635a8641SAndroid Build Coastguard Worker     if (limits.rlim_max > 0 && limits.rlim_max < max_descriptors) {
81*635a8641SAndroid Build Coastguard Worker       new_limit = limits.rlim_max;
82*635a8641SAndroid Build Coastguard Worker     }
83*635a8641SAndroid Build Coastguard Worker     limits.rlim_cur = new_limit;
84*635a8641SAndroid Build Coastguard Worker     if (setrlimit(RLIMIT_NOFILE, &limits) != 0) {
85*635a8641SAndroid Build Coastguard Worker       PLOG(INFO) << "Failed to set file descriptor limit";
86*635a8641SAndroid Build Coastguard Worker     }
87*635a8641SAndroid Build Coastguard Worker   } else {
88*635a8641SAndroid Build Coastguard Worker     PLOG(INFO) << "Failed to get file descriptor limit";
89*635a8641SAndroid Build Coastguard Worker   }
90*635a8641SAndroid Build Coastguard Worker }
91*635a8641SAndroid Build Coastguard Worker 
92*635a8641SAndroid Build Coastguard Worker #endif  // !defined(OS_FUCHSIA)
93*635a8641SAndroid Build Coastguard Worker 
GetPageSize()94*635a8641SAndroid Build Coastguard Worker size_t GetPageSize() {
95*635a8641SAndroid Build Coastguard Worker   return getpagesize();
96*635a8641SAndroid Build Coastguard Worker }
97*635a8641SAndroid Build Coastguard Worker 
GetMallocUsage()98*635a8641SAndroid Build Coastguard Worker size_t ProcessMetrics::GetMallocUsage() {
99*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX) || defined(OS_IOS)
100*635a8641SAndroid Build Coastguard Worker   malloc_statistics_t stats = {0};
101*635a8641SAndroid Build Coastguard Worker   malloc_zone_statistics(nullptr, &stats);
102*635a8641SAndroid Build Coastguard Worker   return stats.size_in_use;
103*635a8641SAndroid Build Coastguard Worker #elif defined(OS_LINUX) || defined(OS_ANDROID)
104*635a8641SAndroid Build Coastguard Worker   struct mallinfo minfo = mallinfo();
105*635a8641SAndroid Build Coastguard Worker #if defined(USE_TCMALLOC)
106*635a8641SAndroid Build Coastguard Worker   return minfo.uordblks;
107*635a8641SAndroid Build Coastguard Worker #else
108*635a8641SAndroid Build Coastguard Worker   return minfo.hblkhd + minfo.arena;
109*635a8641SAndroid Build Coastguard Worker #endif
110*635a8641SAndroid Build Coastguard Worker #elif defined(OS_FUCHSIA)
111*635a8641SAndroid Build Coastguard Worker   // TODO(fuchsia): Not currently exposed. https://crbug.com/735087.
112*635a8641SAndroid Build Coastguard Worker   return 0;
113*635a8641SAndroid Build Coastguard Worker #endif
114*635a8641SAndroid Build Coastguard Worker }
115*635a8641SAndroid Build Coastguard Worker 
116*635a8641SAndroid Build Coastguard Worker }  // namespace base
117