ipc/chromium/src/base/sys_info_posix.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/sys_info_posix.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,166 @@
     1.4 +// Copyright (c) 2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#include "base/sys_info.h"
     1.9 +#include "base/basictypes.h"
    1.10 +
    1.11 +#include <errno.h>
    1.12 +#include <string.h>
    1.13 +#ifndef ANDROID
    1.14 +#include <sys/statvfs.h>
    1.15 +#endif
    1.16 +#include <sys/utsname.h>
    1.17 +#include <unistd.h>
    1.18 +
    1.19 +#if defined(OS_MACOSX)
    1.20 +#include <mach/mach_host.h>
    1.21 +#include <mach/mach_init.h>
    1.22 +#endif
    1.23 +
    1.24 +#if defined(OS_NETBSD)
    1.25 +#include <sys/param.h>
    1.26 +#include <sys/sysctl.h>
    1.27 +#endif
    1.28 +
    1.29 +#include "base/logging.h"
    1.30 +#include "base/string_util.h"
    1.31 +
    1.32 +namespace base {
    1.33 +
    1.34 +int SysInfo::NumberOfProcessors() {
    1.35 +  // It seems that sysconf returns the number of "logical" processors on both
    1.36 +  // mac and linux.  So we get the number of "online logical" processors.
    1.37 +#ifdef _SC_NPROCESSORS_ONLN
    1.38 +  static long res = sysconf(_SC_NPROCESSORS_ONLN);
    1.39 +#else
    1.40 +  static long res = 1;
    1.41 +#endif
    1.42 +  if (res == -1) {
    1.43 +    NOTREACHED();
    1.44 +    return 1;
    1.45 +  }
    1.46 +
    1.47 +  return static_cast<int>(res);
    1.48 +}
    1.49 +
    1.50 +// static
    1.51 +int64_t SysInfo::AmountOfPhysicalMemory() {
    1.52 +  // _SC_PHYS_PAGES is not part of POSIX and not available on OS X
    1.53 +#if defined(OS_MACOSX)
    1.54 +  struct host_basic_info hostinfo;
    1.55 +  mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
    1.56 +  int result = host_info(mach_host_self(),
    1.57 +                         HOST_BASIC_INFO,
    1.58 +                         reinterpret_cast<host_info_t>(&hostinfo),
    1.59 +                         &count);
    1.60 +  DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
    1.61 +  if (result != KERN_SUCCESS) {
    1.62 +    NOTREACHED();
    1.63 +    return 0;
    1.64 +  }
    1.65 +
    1.66 +  return static_cast<int64_t>(hostinfo.max_mem);
    1.67 +#elif defined(OS_NETBSD)
    1.68 +  int mib[2];
    1.69 +  int rc;
    1.70 +  int64_t memSize;
    1.71 +  size_t len = sizeof(memSize);
    1.72 +
    1.73 +  mib[0] = CTL_HW;
    1.74 +  mib[1] = HW_PHYSMEM64;
    1.75 +  rc = sysctl( mib, 2, &memSize, &len, NULL, 0 );
    1.76 +  if (-1 != rc)  {
    1.77 +    return memSize;
    1.78 +  }
    1.79 +  return 0;
    1.80 +
    1.81 +#else
    1.82 +  long pages = sysconf(_SC_PHYS_PAGES);
    1.83 +  long page_size = sysconf(_SC_PAGE_SIZE);
    1.84 +  if (pages == -1 || page_size == -1) {
    1.85 +    NOTREACHED();
    1.86 +    return 0;
    1.87 +  }
    1.88 +
    1.89 +  return static_cast<int64_t>(pages) * page_size;
    1.90 +#endif
    1.91 +}
    1.92 +
    1.93 +// static
    1.94 +int64_t SysInfo::AmountOfFreeDiskSpace(const std::wstring& path) {
    1.95 +#ifndef ANDROID
    1.96 +  struct statvfs stats;
    1.97 +  if (statvfs(WideToUTF8(path).c_str(), &stats) != 0) {
    1.98 +    return -1;
    1.99 +  }
   1.100 +  return static_cast<int64_t>(stats.f_bavail) * stats.f_frsize;
   1.101 +#else
   1.102 +  return -1;
   1.103 +#endif
   1.104 +}
   1.105 +
   1.106 +// static
   1.107 +bool SysInfo::HasEnvVar(const wchar_t* var) {
   1.108 +  std::string var_utf8 = WideToUTF8(std::wstring(var));
   1.109 +  return getenv(var_utf8.c_str()) != NULL;
   1.110 +}
   1.111 +
   1.112 +// static
   1.113 +std::wstring SysInfo::GetEnvVar(const wchar_t* var) {
   1.114 +  std::string var_utf8 = WideToUTF8(std::wstring(var));
   1.115 +  char* value = getenv(var_utf8.c_str());
   1.116 +  if (!value) {
   1.117 +    return L"";
   1.118 +  } else {
   1.119 +    return UTF8ToWide(value);
   1.120 +  }
   1.121 +}
   1.122 +
   1.123 +// static
   1.124 +std::string SysInfo::OperatingSystemName() {
   1.125 +  utsname info;
   1.126 +  if (uname(&info) < 0) {
   1.127 +    NOTREACHED();
   1.128 +    return "";
   1.129 +  }
   1.130 +  return std::string(info.sysname);
   1.131 +}
   1.132 +
   1.133 +// static
   1.134 +std::string SysInfo::OperatingSystemVersion() {
   1.135 +  utsname info;
   1.136 +  if (uname(&info) < 0) {
   1.137 +    NOTREACHED();
   1.138 +    return "";
   1.139 +  }
   1.140 +  return std::string(info.release);
   1.141 +}
   1.142 +
   1.143 +// static
   1.144 +std::string SysInfo::CPUArchitecture() {
   1.145 +  utsname info;
   1.146 +  if (uname(&info) < 0) {
   1.147 +    NOTREACHED();
   1.148 +    return "";
   1.149 +  }
   1.150 +  return std::string(info.machine);
   1.151 +}
   1.152 +
   1.153 +// static
   1.154 +void SysInfo::GetPrimaryDisplayDimensions(int* width, int* height) {
   1.155 +  NOTIMPLEMENTED();
   1.156 +}
   1.157 +
   1.158 +// static
   1.159 +int SysInfo::DisplayCount() {
   1.160 +  NOTIMPLEMENTED();
   1.161 +  return 1;
   1.162 +}
   1.163 +
   1.164 +// static
   1.165 +size_t SysInfo::VMAllocationGranularity() {
   1.166 +  return getpagesize();
   1.167 +}
   1.168 +
   1.169 +}  // namespace base

mercurial