blob: 28c8bd6a872e62de4a8e969fcc22922faadc3513 [file] [log] [blame]
[email protected]2a758d612008-09-17 10:09:391// Copyright (c) 2008 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"
[email protected]d632798e2008-09-17 13:10:456#include "base/basictypes.h"
[email protected]2a758d612008-09-17 10:09:397
8#include <errno.h>
9#include <string.h>
[email protected]0e91dd22008-09-18 12:34:2410#include <sys/statvfs.h>
[email protected]2a758d612008-09-17 10:09:3911#include <unistd.h>
12
[email protected]d632798e2008-09-17 13:10:4513#if defined(OS_MACOSX)
14#include <mach/mach_host.h>
15#include <mach/mach_init.h>
16#endif
17
[email protected]2a758d612008-09-17 10:09:3918#include "base/logging.h"
[email protected]0e91dd22008-09-18 12:34:2419#include "base/string_util.h"
[email protected]2a758d612008-09-17 10:09:3920
21namespace base {
22
23int SysInfo::NumberOfProcessors() {
24 // It seems that sysconf returns the number of "logical" processors on both
25 // mac and linux. So we get the number of "online logical" processors.
26 long res = sysconf(_SC_NPROCESSORS_ONLN);
27 if (res == -1) {
28 NOTREACHED();
29 return 1;
30 }
31
32 return static_cast<int>(res);
33}
34
[email protected]d632798e2008-09-17 13:10:4535// static
36int64 SysInfo::AmountOfPhysicalMemory() {
37 // _SC_PHYS_PAGES is not part of POSIX and not available on OS X
38#if defined(OS_MACOSX)
39 struct host_basic_info hostinfo;
40 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
41 int result = host_info(mach_host_self(),
42 HOST_BASIC_INFO,
43 reinterpret_cast<host_info_t>(&hostinfo),
44 &count);
45 DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
46 if (result != KERN_SUCCESS) {
47 NOTREACHED();
48 return 0;
49 }
50
51 return static_cast<int64>(hostinfo.max_mem);
52#else
53 long pages = sysconf(_SC_PHYS_PAGES);
54 long page_size = sysconf(_SC_PAGE_SIZE);
55 if (pages == -1 || page_size == -1) {
56 NOTREACHED();
57 return 0;
58 }
59
60 return static_cast<int64>(pages) * page_size;
61#endif
62}
63
[email protected]0e91dd22008-09-18 12:34:2464// static
65int64 SysInfo::AmountOfFreeDiskSpace(const std::wstring& path) {
66 struct statvfs stats;
67 if (statvfs(WideToUTF8(path).c_str(), &stats) != 0) {
[email protected]02ee34a2008-09-20 01:16:2368 return -1;
[email protected]0e91dd22008-09-18 12:34:2469 }
70 return static_cast<int64>(stats.f_bavail) * stats.f_frsize;
71}
72
[email protected]2a758d612008-09-17 10:09:3973} // namespace base