Update strings for turning off Smart Lock.
[chromium-blink-merge.git] / base / process / process_metrics_posix.cc
blobea79d7348ff47f58fa16f946577386052d692429
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 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;
22 ProcessMetrics::~ProcessMetrics() { }
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
38 size_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;
49 if (max_fds > INT_MAX)
50 max_fds = INT_MAX;
52 return static_cast<size_t>(max_fds);
56 void 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;
63 limits.rlim_cur = new_limit;
64 if (setrlimit(RLIMIT_NOFILE, &limits) != 0) {
65 PLOG(INFO) << "Failed to set file descriptor limit";
67 } else {
68 PLOG(INFO) << "Failed to get file descriptor limit";
72 } // namespace base