ipc/chromium/src/base/sys_info_posix.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "base/sys_info.h"
michael@0 6 #include "base/basictypes.h"
michael@0 7
michael@0 8 #include <errno.h>
michael@0 9 #include <string.h>
michael@0 10 #ifndef ANDROID
michael@0 11 #include <sys/statvfs.h>
michael@0 12 #endif
michael@0 13 #include <sys/utsname.h>
michael@0 14 #include <unistd.h>
michael@0 15
michael@0 16 #if defined(OS_MACOSX)
michael@0 17 #include <mach/mach_host.h>
michael@0 18 #include <mach/mach_init.h>
michael@0 19 #endif
michael@0 20
michael@0 21 #if defined(OS_NETBSD)
michael@0 22 #include <sys/param.h>
michael@0 23 #include <sys/sysctl.h>
michael@0 24 #endif
michael@0 25
michael@0 26 #include "base/logging.h"
michael@0 27 #include "base/string_util.h"
michael@0 28
michael@0 29 namespace base {
michael@0 30
michael@0 31 int SysInfo::NumberOfProcessors() {
michael@0 32 // It seems that sysconf returns the number of "logical" processors on both
michael@0 33 // mac and linux. So we get the number of "online logical" processors.
michael@0 34 #ifdef _SC_NPROCESSORS_ONLN
michael@0 35 static long res = sysconf(_SC_NPROCESSORS_ONLN);
michael@0 36 #else
michael@0 37 static long res = 1;
michael@0 38 #endif
michael@0 39 if (res == -1) {
michael@0 40 NOTREACHED();
michael@0 41 return 1;
michael@0 42 }
michael@0 43
michael@0 44 return static_cast<int>(res);
michael@0 45 }
michael@0 46
michael@0 47 // static
michael@0 48 int64_t SysInfo::AmountOfPhysicalMemory() {
michael@0 49 // _SC_PHYS_PAGES is not part of POSIX and not available on OS X
michael@0 50 #if defined(OS_MACOSX)
michael@0 51 struct host_basic_info hostinfo;
michael@0 52 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
michael@0 53 int result = host_info(mach_host_self(),
michael@0 54 HOST_BASIC_INFO,
michael@0 55 reinterpret_cast<host_info_t>(&hostinfo),
michael@0 56 &count);
michael@0 57 DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
michael@0 58 if (result != KERN_SUCCESS) {
michael@0 59 NOTREACHED();
michael@0 60 return 0;
michael@0 61 }
michael@0 62
michael@0 63 return static_cast<int64_t>(hostinfo.max_mem);
michael@0 64 #elif defined(OS_NETBSD)
michael@0 65 int mib[2];
michael@0 66 int rc;
michael@0 67 int64_t memSize;
michael@0 68 size_t len = sizeof(memSize);
michael@0 69
michael@0 70 mib[0] = CTL_HW;
michael@0 71 mib[1] = HW_PHYSMEM64;
michael@0 72 rc = sysctl( mib, 2, &memSize, &len, NULL, 0 );
michael@0 73 if (-1 != rc) {
michael@0 74 return memSize;
michael@0 75 }
michael@0 76 return 0;
michael@0 77
michael@0 78 #else
michael@0 79 long pages = sysconf(_SC_PHYS_PAGES);
michael@0 80 long page_size = sysconf(_SC_PAGE_SIZE);
michael@0 81 if (pages == -1 || page_size == -1) {
michael@0 82 NOTREACHED();
michael@0 83 return 0;
michael@0 84 }
michael@0 85
michael@0 86 return static_cast<int64_t>(pages) * page_size;
michael@0 87 #endif
michael@0 88 }
michael@0 89
michael@0 90 // static
michael@0 91 int64_t SysInfo::AmountOfFreeDiskSpace(const std::wstring& path) {
michael@0 92 #ifndef ANDROID
michael@0 93 struct statvfs stats;
michael@0 94 if (statvfs(WideToUTF8(path).c_str(), &stats) != 0) {
michael@0 95 return -1;
michael@0 96 }
michael@0 97 return static_cast<int64_t>(stats.f_bavail) * stats.f_frsize;
michael@0 98 #else
michael@0 99 return -1;
michael@0 100 #endif
michael@0 101 }
michael@0 102
michael@0 103 // static
michael@0 104 bool SysInfo::HasEnvVar(const wchar_t* var) {
michael@0 105 std::string var_utf8 = WideToUTF8(std::wstring(var));
michael@0 106 return getenv(var_utf8.c_str()) != NULL;
michael@0 107 }
michael@0 108
michael@0 109 // static
michael@0 110 std::wstring SysInfo::GetEnvVar(const wchar_t* var) {
michael@0 111 std::string var_utf8 = WideToUTF8(std::wstring(var));
michael@0 112 char* value = getenv(var_utf8.c_str());
michael@0 113 if (!value) {
michael@0 114 return L"";
michael@0 115 } else {
michael@0 116 return UTF8ToWide(value);
michael@0 117 }
michael@0 118 }
michael@0 119
michael@0 120 // static
michael@0 121 std::string SysInfo::OperatingSystemName() {
michael@0 122 utsname info;
michael@0 123 if (uname(&info) < 0) {
michael@0 124 NOTREACHED();
michael@0 125 return "";
michael@0 126 }
michael@0 127 return std::string(info.sysname);
michael@0 128 }
michael@0 129
michael@0 130 // static
michael@0 131 std::string SysInfo::OperatingSystemVersion() {
michael@0 132 utsname info;
michael@0 133 if (uname(&info) < 0) {
michael@0 134 NOTREACHED();
michael@0 135 return "";
michael@0 136 }
michael@0 137 return std::string(info.release);
michael@0 138 }
michael@0 139
michael@0 140 // static
michael@0 141 std::string SysInfo::CPUArchitecture() {
michael@0 142 utsname info;
michael@0 143 if (uname(&info) < 0) {
michael@0 144 NOTREACHED();
michael@0 145 return "";
michael@0 146 }
michael@0 147 return std::string(info.machine);
michael@0 148 }
michael@0 149
michael@0 150 // static
michael@0 151 void SysInfo::GetPrimaryDisplayDimensions(int* width, int* height) {
michael@0 152 NOTIMPLEMENTED();
michael@0 153 }
michael@0 154
michael@0 155 // static
michael@0 156 int SysInfo::DisplayCount() {
michael@0 157 NOTIMPLEMENTED();
michael@0 158 return 1;
michael@0 159 }
michael@0 160
michael@0 161 // static
michael@0 162 size_t SysInfo::VMAllocationGranularity() {
michael@0 163 return getpagesize();
michael@0 164 }
michael@0 165
michael@0 166 } // namespace base

mercurial