1 // Copyright (c) 2011 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/sys_info.h"
9 #include "base/file_util.h"
10 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h"
15 int64
SysInfo::AmountOfPhysicalMemory() {
16 long pages
= sysconf(_SC_PHYS_PAGES
);
17 long page_size
= sysconf(_SC_PAGE_SIZE
);
18 if (pages
== -1 || page_size
== -1) {
23 return static_cast<int64
>(pages
) * page_size
;
27 int64
SysInfo::AmountOfAvailablePhysicalMemory() {
28 long available_pages
= sysconf(_SC_AVPHYS_PAGES
);
29 long page_size
= sysconf(_SC_PAGE_SIZE
);
30 if (available_pages
== -1 || page_size
== -1) {
34 return static_cast<int64
>(available_pages
) * page_size
;
38 size_t SysInfo::MaxSharedMemorySize() {
40 static bool limit_valid
= false;
43 file_util::ReadFileToString(FilePath("/proc/sys/kernel/shmmax"), &contents
);
44 DCHECK(!contents
.empty());
45 if (!contents
.empty() && contents
[contents
.length() - 1] == '\n') {
46 contents
.erase(contents
.length() - 1);
48 if (base::StringToInt64(contents
, &limit
)) {
50 DCHECK(static_cast<uint64
>(limit
) <= std::numeric_limits
<size_t>::max());
57 return static_cast<size_t>(limit
);
61 std::string
SysInfo::CPUModelName() {
62 const char kModelNamePrefix
[] = "model name";
64 file_util::ReadFileToString(FilePath("/proc/cpuinfo"), &contents
);
65 DCHECK(!contents
.empty());
66 if (!contents
.empty()) {
67 std::istringstream
iss(contents
);
69 while (std::getline(iss
, line
)){
70 if (line
.compare(0, strlen(kModelNamePrefix
), kModelNamePrefix
) == 0) {
71 size_t pos
= line
.find(": ");
72 return line
.substr(pos
+ 2);