Home | History | Annotate | Download | only in base
      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.
      4 
      5 #include "base/sys_info.h"
      6 
      7 #include <errno.h>
      8 #include <string.h>
      9 #include <sys/param.h>
     10 #include <sys/utsname.h>
     11 #include <unistd.h>
     12 
     13 #include "base/basictypes.h"
     14 #include "base/file_util.h"
     15 #include "base/logging.h"
     16 #include "base/strings/utf_string_conversions.h"
     17 #include "base/threading/thread_restrictions.h"
     18 
     19 #if defined(OS_ANDROID)
     20 #include <sys/vfs.h>
     21 #define statvfs statfs  // Android uses a statvfs-like statfs struct and call.
     22 #else
     23 #include <sys/statvfs.h>
     24 #endif
     25 
     26 namespace base {
     27 
     28 #if !defined(OS_OPENBSD)
     29 int SysInfo::NumberOfProcessors() {
     30   // It seems that sysconf returns the number of "logical" processors on both
     31   // Mac and Linux.  So we get the number of "online logical" processors.
     32   long res = sysconf(_SC_NPROCESSORS_ONLN);
     33   if (res == -1) {
     34     NOTREACHED();
     35     return 1;
     36   }
     37 
     38   return static_cast<int>(res);
     39 }
     40 #endif
     41 
     42 // static
     43 int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) {
     44   base::ThreadRestrictions::AssertIOAllowed();
     45 
     46   struct statvfs stats;
     47   if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0)
     48     return -1;
     49   return static_cast<int64>(stats.f_bavail) * stats.f_frsize;
     50 }
     51 
     52 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
     53 // static
     54 std::string SysInfo::OperatingSystemName() {
     55   struct utsname info;
     56   if (uname(&info) < 0) {
     57     NOTREACHED();
     58     return std::string();
     59   }
     60   return std::string(info.sysname);
     61 }
     62 #endif
     63 
     64 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
     65 // static
     66 std::string SysInfo::OperatingSystemVersion() {
     67   struct utsname info;
     68   if (uname(&info) < 0) {
     69     NOTREACHED();
     70     return std::string();
     71   }
     72   return std::string(info.release);
     73 }
     74 #endif
     75 
     76 // static
     77 std::string SysInfo::OperatingSystemArchitecture() {
     78   struct utsname info;
     79   if (uname(&info) < 0) {
     80     NOTREACHED();
     81     return std::string();
     82   }
     83   std::string arch(info.machine);
     84   if (arch == "i386" || arch == "i486" || arch == "i586" || arch == "i686") {
     85     arch = "x86";
     86   } else if (arch == "amd64") {
     87     arch = "x86_64";
     88   }
     89   return arch;
     90 }
     91 
     92 // static
     93 size_t SysInfo::VMAllocationGranularity() {
     94   return getpagesize();
     95 }
     96 
     97 }  // namespace base
     98