Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2012 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 <ApplicationServices/ApplicationServices.h>
      8 #include <CoreServices/CoreServices.h>
      9 #include <mach/mach_host.h>
     10 #include <mach/mach_init.h>
     11 #include <stddef.h>
     12 #include <stdint.h>
     13 #include <sys/sysctl.h>
     14 #include <sys/types.h>
     15 
     16 #include "base/logging.h"
     17 #include "base/mac/scoped_mach_port.h"
     18 #include "base/macros.h"
     19 #include "base/strings/stringprintf.h"
     20 
     21 namespace base {
     22 
     23 // static
     24 std::string SysInfo::OperatingSystemName() {
     25   return "Mac OS X";
     26 }
     27 
     28 // static
     29 std::string SysInfo::OperatingSystemVersion() {
     30   int32_t major, minor, bugfix;
     31   OperatingSystemVersionNumbers(&major, &minor, &bugfix);
     32   return base::StringPrintf("%d.%d.%d", major, minor, bugfix);
     33 }
     34 
     35 // static
     36 void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
     37                                             int32_t* minor_version,
     38                                             int32_t* bugfix_version) {
     39   Gestalt(gestaltSystemVersionMajor,
     40       reinterpret_cast<SInt32*>(major_version));
     41   Gestalt(gestaltSystemVersionMinor,
     42       reinterpret_cast<SInt32*>(minor_version));
     43   Gestalt(gestaltSystemVersionBugFix,
     44       reinterpret_cast<SInt32*>(bugfix_version));
     45 }
     46 
     47 // static
     48 int64_t SysInfo::AmountOfPhysicalMemory() {
     49   struct host_basic_info hostinfo;
     50   mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
     51   base::mac::ScopedMachSendRight host(mach_host_self());
     52   int result = host_info(host.get(),
     53                          HOST_BASIC_INFO,
     54                          reinterpret_cast<host_info_t>(&hostinfo),
     55                          &count);
     56   if (result != KERN_SUCCESS) {
     57     NOTREACHED();
     58     return 0;
     59   }
     60   DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
     61   return static_cast<int64_t>(hostinfo.max_mem);
     62 }
     63 
     64 // static
     65 int64_t SysInfo::AmountOfAvailablePhysicalMemory() {
     66   base::mac::ScopedMachSendRight host(mach_host_self());
     67   vm_statistics_data_t vm_info;
     68   mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
     69 
     70   if (host_statistics(host.get(),
     71                       HOST_VM_INFO,
     72                       reinterpret_cast<host_info_t>(&vm_info),
     73                       &count) != KERN_SUCCESS) {
     74     NOTREACHED();
     75     return 0;
     76   }
     77 
     78   return static_cast<int64_t>(vm_info.free_count - vm_info.speculative_count) *
     79          PAGE_SIZE;
     80 }
     81 
     82 // static
     83 std::string SysInfo::CPUModelName() {
     84   char name[256];
     85   size_t len = arraysize(name);
     86   if (sysctlbyname("machdep.cpu.brand_string", &name, &len, NULL, 0) == 0)
     87     return name;
     88   return std::string();
     89 }
     90 
     91 std::string SysInfo::HardwareModelName() {
     92   char model[256];
     93   size_t len = sizeof(model);
     94   if (sysctlbyname("hw.model", model, &len, NULL, 0) == 0)
     95     return std::string(model, 0, len);
     96   return std::string();
     97 }
     98 
     99 }  // namespace base
    100