15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/sys_info.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <ApplicationServices/ApplicationServices.h> 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <CoreServices/CoreServices.h> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <mach/mach_host.h> 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <mach/mach_init.h> 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/sysctl.h> 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/types.h> 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/mac/scoped_mach_port.h" 16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base { 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string SysInfo::OperatingSystemName() { 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return "Mac OS X"; 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string SysInfo::OperatingSystemVersion() { 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int32 major, minor, bugfix; 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) OperatingSystemVersionNumbers(&major, &minor, &bugfix); 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return base::StringPrintf("%d.%d.%d", major, minor, bugfix); 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SysInfo::OperatingSystemVersionNumbers(int32* major_version, 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int32* minor_version, 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int32* bugfix_version) { 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Gestalt(gestaltSystemVersionMajor, 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) reinterpret_cast<SInt32*>(major_version)); 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Gestalt(gestaltSystemVersionMinor, 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) reinterpret_cast<SInt32*>(minor_version)); 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Gestalt(gestaltSystemVersionBugFix, 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) reinterpret_cast<SInt32*>(bugfix_version)); 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int64 SysInfo::AmountOfPhysicalMemory() { 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) struct host_basic_info hostinfo; 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::mac::ScopedMachPort host(mach_host_self()); 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int result = host_info(host, 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HOST_BASIC_INFO, 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) reinterpret_cast<host_info_t>(&hostinfo), 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) &count); 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (result != KERN_SUCCESS) { 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NOTREACHED(); 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 0; 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return static_cast<int64>(hostinfo.max_mem); 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int64 SysInfo::AmountOfAvailablePhysicalMemory() { 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::mac::ScopedMachPort host(mach_host_self()); 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) vm_statistics_data_t vm_info; 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) mach_msg_type_number_t count = HOST_VM_INFO_COUNT; 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (host_statistics(host.get(), 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HOST_VM_INFO, 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) reinterpret_cast<host_info_t>(&vm_info), 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) &count) != KERN_SUCCESS) { 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NOTREACHED(); 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 0; 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return static_cast<int64>( 762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) vm_info.free_count - vm_info.speculative_count) * PAGE_SIZE; 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string SysInfo::CPUModelName() { 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) char name[256]; 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) size_t len = arraysize(name); 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (sysctlbyname("machdep.cpu.brand_string", &name, &len, NULL, 0) == 0) 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return name; 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return std::string(); 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace base 89