Simple Cache: a few tests for rare corner cases with CRC check missing.
[chromium-blink-merge.git] / base / sys_info_linux.cc
blob58a0aefee6212e0647476370f10e5fd0d04d30af
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"
7 #include <limits>
9 #include "base/file_util.h"
10 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h"
13 namespace {
15 int64 AmountOfMemory(int pages_name) {
16 long pages = sysconf(pages_name);
17 long page_size = sysconf(_SC_PAGESIZE);
18 if (pages == -1 || page_size == -1) {
19 NOTREACHED();
20 return 0;
22 return static_cast<int64>(pages) * page_size;
25 } // namespace
27 namespace base {
29 // static
30 int64 SysInfo::AmountOfPhysicalMemory() {
31 return AmountOfMemory(_SC_PHYS_PAGES);
34 // static
35 int64 SysInfo::AmountOfAvailablePhysicalMemory() {
36 return AmountOfMemory(_SC_AVPHYS_PAGES);
39 // static
40 size_t SysInfo::MaxSharedMemorySize() {
41 static int64 limit;
42 static bool limit_valid = false;
43 if (!limit_valid) {
44 std::string contents;
45 file_util::ReadFileToString(FilePath("/proc/sys/kernel/shmmax"), &contents);
46 DCHECK(!contents.empty());
47 if (!contents.empty() && contents[contents.length() - 1] == '\n') {
48 contents.erase(contents.length() - 1);
50 if (base::StringToInt64(contents, &limit)) {
51 DCHECK(limit >= 0);
52 DCHECK(static_cast<uint64>(limit) <= std::numeric_limits<size_t>::max());
53 limit_valid = true;
54 } else {
55 NOTREACHED();
56 return 0;
59 return static_cast<size_t>(limit);
62 // static
63 std::string SysInfo::CPUModelName() {
64 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
65 const char kCpuModelPrefix[] = "Hardware";
66 #else
67 const char kCpuModelPrefix[] = "model name";
68 #endif
69 std::string contents;
70 file_util::ReadFileToString(FilePath("/proc/cpuinfo"), &contents);
71 DCHECK(!contents.empty());
72 if (!contents.empty()) {
73 std::istringstream iss(contents);
74 std::string line;
75 while (std::getline(iss, line)) {
76 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) {
77 size_t pos = line.find(": ");
78 return line.substr(pos + 2);
82 return std::string();
85 } // namespace base