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.
5 #include "base/process/process_metrics.h"
7 #include <sys/resource.h>
10 #include "base/logging.h"
14 int64
TimeValToMicroseconds(const struct timeval
& tv
) {
15 int64 ret
= tv
.tv_sec
; // Avoid (int * int) integer overflow.
16 ret
*= Time::kMicrosecondsPerSecond
;
21 ProcessMetrics::~ProcessMetrics() { }
24 static const rlim_t kSystemDefaultMaxFds
= 8192;
25 #elif defined(OS_MACOSX)
26 static const rlim_t kSystemDefaultMaxFds
= 256;
27 #elif defined(OS_SOLARIS)
28 static const rlim_t kSystemDefaultMaxFds
= 8192;
29 #elif defined(OS_FREEBSD)
30 static const rlim_t kSystemDefaultMaxFds
= 8192;
31 #elif defined(OS_OPENBSD)
32 static const rlim_t kSystemDefaultMaxFds
= 256;
33 #elif defined(OS_ANDROID)
34 static const rlim_t kSystemDefaultMaxFds
= 1024;
40 if (getrlimit(RLIMIT_NOFILE
, &nofile
)) {
41 // getrlimit failed. Take a best guess.
42 max_fds
= kSystemDefaultMaxFds
;
43 RAW_LOG(ERROR
, "getrlimit(RLIMIT_NOFILE) failed");
45 max_fds
= nofile
.rlim_cur
;
48 if (max_fds
> INT_MAX
)
51 return static_cast<size_t>(max_fds
);
55 void SetFdLimit(unsigned int max_descriptors
) {
57 if (getrlimit(RLIMIT_NOFILE
, &limits
) == 0) {
58 unsigned int new_limit
= max_descriptors
;
59 if (limits
.rlim_max
> 0 && limits
.rlim_max
< max_descriptors
) {
60 new_limit
= limits
.rlim_max
;
62 limits
.rlim_cur
= new_limit
;
63 if (setrlimit(RLIMIT_NOFILE
, &limits
) != 0) {
64 PLOG(INFO
) << "Failed to set file descriptor limit";
67 PLOG(INFO
) << "Failed to get file descriptor limit";
71 size_t GetPageSize() {