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 5 #include "base/process/process_metrics.h" 6 7 #include <sys/resource.h> 8 #include <sys/time.h> 9 10 #include "base/logging.h" 11 12 namespace base { 13 TimeValToMicroseconds(const struct timeval & tv)14int64 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; 18 ret += tv.tv_usec; 19 return ret; 20 } 21 ~ProcessMetrics()22ProcessMetrics::~ProcessMetrics() { } 23 24 #if defined(OS_LINUX) 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; 36 #endif 37 GetMaxFds()38size_t GetMaxFds() { 39 rlim_t max_fds; 40 struct rlimit nofile; 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"); 45 } else { 46 max_fds = nofile.rlim_cur; 47 } 48 49 if (max_fds > INT_MAX) 50 max_fds = INT_MAX; 51 52 return static_cast<size_t>(max_fds); 53 } 54 55 SetFdLimit(unsigned int max_descriptors)56void SetFdLimit(unsigned int max_descriptors) { 57 struct rlimit limits; 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; 62 } 63 limits.rlim_cur = new_limit; 64 if (setrlimit(RLIMIT_NOFILE, &limits) != 0) { 65 PLOG(INFO) << "Failed to set file descriptor limit"; 66 } 67 } else { 68 PLOG(INFO) << "Failed to get file descriptor limit"; 69 } 70 } 71 72 } // namespace base 73