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 static const int kMicrosecondsPerSecond
= 1000000;
16 int64 ret
= tv
.tv_sec
; // Avoid (int * int) integer overflow.
17 ret
*= kMicrosecondsPerSecond
;
22 ProcessMetrics::~ProcessMetrics() { }
25 static const rlim_t kSystemDefaultMaxFds
= 8192;
26 #elif defined(OS_MACOSX)
27 static const rlim_t kSystemDefaultMaxFds
= 256;
28 #elif defined(OS_SOLARIS)
29 static const rlim_t kSystemDefaultMaxFds
= 8192;
30 #elif defined(OS_FREEBSD)
31 static const rlim_t kSystemDefaultMaxFds
= 8192;
32 #elif defined(OS_OPENBSD)
33 static const rlim_t kSystemDefaultMaxFds
= 256;
34 #elif defined(OS_ANDROID)
35 static const rlim_t kSystemDefaultMaxFds
= 1024;
41 if (getrlimit(RLIMIT_NOFILE
, &nofile
)) {
42 // getrlimit failed. Take a best guess.
43 max_fds
= kSystemDefaultMaxFds
;
44 RAW_LOG(ERROR
, "getrlimit(RLIMIT_NOFILE) failed");
46 max_fds
= nofile
.rlim_cur
;
49 if (max_fds
> INT_MAX
)
52 return static_cast<size_t>(max_fds
);
56 void SetFdLimit(unsigned int max_descriptors
) {
58 if (getrlimit(RLIMIT_NOFILE
, &limits
) == 0) {
59 unsigned int new_limit
= max_descriptors
;
60 if (limits
.rlim_max
> 0 && limits
.rlim_max
< max_descriptors
) {
61 new_limit
= limits
.rlim_max
;
63 limits
.rlim_cur
= new_limit
;
64 if (setrlimit(RLIMIT_NOFILE
, &limits
) != 0) {
65 PLOG(INFO
) << "Failed to set file descriptor limit";
68 PLOG(INFO
) << "Failed to get file descriptor limit";