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/files/file_util.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/sys_info_internal.h"
17 int64
AmountOfMemory(int pages_name
) {
18 long pages
= sysconf(pages_name
);
19 long page_size
= sysconf(_SC_PAGESIZE
);
20 if (pages
== -1 || page_size
== -1) {
24 return static_cast<int64
>(pages
) * page_size
;
27 int64
AmountOfPhysicalMemory() {
28 return AmountOfMemory(_SC_PHYS_PAGES
);
31 size_t MaxSharedMemorySize() {
33 base::ReadFileToString(base::FilePath("/proc/sys/kernel/shmmax"), &contents
);
34 DCHECK(!contents
.empty());
35 if (!contents
.empty() && contents
[contents
.length() - 1] == '\n') {
36 contents
.erase(contents
.length() - 1);
40 if (!base::StringToUint64(contents
, &limit
)) {
43 if (limit
> std::numeric_limits
<size_t>::max()) {
47 return static_cast<size_t>(limit
);
51 base::internal::LazySysInfoValue
<int64
, AmountOfPhysicalMemory
> >::Leaky
52 g_lazy_physical_memory
= LAZY_INSTANCE_INITIALIZER
;
54 base::internal::LazySysInfoValue
<size_t, MaxSharedMemorySize
> >::Leaky
55 g_lazy_max_shared_memory
= LAZY_INSTANCE_INITIALIZER
;
62 int64
SysInfo::AmountOfAvailablePhysicalMemory() {
63 return AmountOfMemory(_SC_AVPHYS_PAGES
);
67 int64
SysInfo::AmountOfPhysicalMemory() {
68 return g_lazy_physical_memory
.Get().value();
71 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
72 bool SysInfo::HasSeekPenalty(const FilePath
& path
, bool* has_seek_penalty
) {
73 // TODO(dbeam): implement.
79 size_t SysInfo::MaxSharedMemorySize() {
80 return g_lazy_max_shared_memory
.Get().value();
84 std::string
SysInfo::CPUModelName() {
85 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
86 const char kCpuModelPrefix
[] = "Hardware";
88 const char kCpuModelPrefix
[] = "model name";
91 ReadFileToString(FilePath("/proc/cpuinfo"), &contents
);
92 DCHECK(!contents
.empty());
93 if (!contents
.empty()) {
94 std::istringstream
iss(contents
);
96 while (std::getline(iss
, line
)) {
97 if (line
.compare(0, strlen(kCpuModelPrefix
), kCpuModelPrefix
) == 0) {
98 size_t pos
= line
.find(": ");
99 return line
.substr(pos
+ 2);
103 return std::string();