michael@0: // Copyright (c) 2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/sys_info.h" michael@0: #include "base/basictypes.h" michael@0: michael@0: #include michael@0: #include michael@0: #ifndef ANDROID michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: michael@0: #if defined(OS_MACOSX) michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #if defined(OS_NETBSD) michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #include "base/logging.h" michael@0: #include "base/string_util.h" michael@0: michael@0: namespace base { michael@0: michael@0: int SysInfo::NumberOfProcessors() { michael@0: // It seems that sysconf returns the number of "logical" processors on both michael@0: // mac and linux. So we get the number of "online logical" processors. michael@0: #ifdef _SC_NPROCESSORS_ONLN michael@0: static long res = sysconf(_SC_NPROCESSORS_ONLN); michael@0: #else michael@0: static long res = 1; michael@0: #endif michael@0: if (res == -1) { michael@0: NOTREACHED(); michael@0: return 1; michael@0: } michael@0: michael@0: return static_cast(res); michael@0: } michael@0: michael@0: // static michael@0: int64_t SysInfo::AmountOfPhysicalMemory() { michael@0: // _SC_PHYS_PAGES is not part of POSIX and not available on OS X michael@0: #if defined(OS_MACOSX) michael@0: struct host_basic_info hostinfo; michael@0: mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; michael@0: int result = host_info(mach_host_self(), michael@0: HOST_BASIC_INFO, michael@0: reinterpret_cast(&hostinfo), michael@0: &count); michael@0: DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); michael@0: if (result != KERN_SUCCESS) { michael@0: NOTREACHED(); michael@0: return 0; michael@0: } michael@0: michael@0: return static_cast(hostinfo.max_mem); michael@0: #elif defined(OS_NETBSD) michael@0: int mib[2]; michael@0: int rc; michael@0: int64_t memSize; michael@0: size_t len = sizeof(memSize); michael@0: michael@0: mib[0] = CTL_HW; michael@0: mib[1] = HW_PHYSMEM64; michael@0: rc = sysctl( mib, 2, &memSize, &len, NULL, 0 ); michael@0: if (-1 != rc) { michael@0: return memSize; michael@0: } michael@0: return 0; michael@0: michael@0: #else michael@0: long pages = sysconf(_SC_PHYS_PAGES); michael@0: long page_size = sysconf(_SC_PAGE_SIZE); michael@0: if (pages == -1 || page_size == -1) { michael@0: NOTREACHED(); michael@0: return 0; michael@0: } michael@0: michael@0: return static_cast(pages) * page_size; michael@0: #endif michael@0: } michael@0: michael@0: // static michael@0: int64_t SysInfo::AmountOfFreeDiskSpace(const std::wstring& path) { michael@0: #ifndef ANDROID michael@0: struct statvfs stats; michael@0: if (statvfs(WideToUTF8(path).c_str(), &stats) != 0) { michael@0: return -1; michael@0: } michael@0: return static_cast(stats.f_bavail) * stats.f_frsize; michael@0: #else michael@0: return -1; michael@0: #endif michael@0: } michael@0: michael@0: // static michael@0: bool SysInfo::HasEnvVar(const wchar_t* var) { michael@0: std::string var_utf8 = WideToUTF8(std::wstring(var)); michael@0: return getenv(var_utf8.c_str()) != NULL; michael@0: } michael@0: michael@0: // static michael@0: std::wstring SysInfo::GetEnvVar(const wchar_t* var) { michael@0: std::string var_utf8 = WideToUTF8(std::wstring(var)); michael@0: char* value = getenv(var_utf8.c_str()); michael@0: if (!value) { michael@0: return L""; michael@0: } else { michael@0: return UTF8ToWide(value); michael@0: } michael@0: } michael@0: michael@0: // static michael@0: std::string SysInfo::OperatingSystemName() { michael@0: utsname info; michael@0: if (uname(&info) < 0) { michael@0: NOTREACHED(); michael@0: return ""; michael@0: } michael@0: return std::string(info.sysname); michael@0: } michael@0: michael@0: // static michael@0: std::string SysInfo::OperatingSystemVersion() { michael@0: utsname info; michael@0: if (uname(&info) < 0) { michael@0: NOTREACHED(); michael@0: return ""; michael@0: } michael@0: return std::string(info.release); michael@0: } michael@0: michael@0: // static michael@0: std::string SysInfo::CPUArchitecture() { michael@0: utsname info; michael@0: if (uname(&info) < 0) { michael@0: NOTREACHED(); michael@0: return ""; michael@0: } michael@0: return std::string(info.machine); michael@0: } michael@0: michael@0: // static michael@0: void SysInfo::GetPrimaryDisplayDimensions(int* width, int* height) { michael@0: NOTIMPLEMENTED(); michael@0: } michael@0: michael@0: // static michael@0: int SysInfo::DisplayCount() { michael@0: NOTIMPLEMENTED(); michael@0: return 1; michael@0: } michael@0: michael@0: // static michael@0: size_t SysInfo::VMAllocationGranularity() { michael@0: return getpagesize(); michael@0: } michael@0: michael@0: } // namespace base