Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / base / process / process_metrics_posix.cc
blob42b3f2d665544cfed91d535b7ebee371bd5996a6
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>
8 #include <sys/time.h>
10 #include "base/logging.h"
12 namespace base {
14 int64 TimeValToMicroseconds(const struct timeval& tv) {
15 int64 ret = tv.tv_sec; // Avoid (int * int) integer overflow.
16 ret *= Time::kMicrosecondsPerSecond;
17 ret += tv.tv_usec;
18 return ret;
21 ProcessMetrics::~ProcessMetrics() { }
23 #if defined(OS_LINUX)
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;
35 #endif
37 size_t GetMaxFds() {
38 rlim_t max_fds;
39 struct rlimit nofile;
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");
44 } else {
45 max_fds = nofile.rlim_cur;
48 if (max_fds > INT_MAX)
49 max_fds = INT_MAX;
51 return static_cast<size_t>(max_fds);
55 void SetFdLimit(unsigned int max_descriptors) {
56 struct rlimit limits;
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";
66 } else {
67 PLOG(INFO) << "Failed to get file descriptor limit";
71 size_t GetPageSize() {
72 return getpagesize();
75 } // namespace base