sys_info_posix.cc revision 00d26a728db2814620f390b418a7d6325ce5aca6
1// Copyright (c) 2010 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#ifndef ANDROID
11#include <sys/statvfs.h>
12#endif
13#include <sys/sysctl.h>
14#include <sys/utsname.h>
15#include <unistd.h>
16
17#if !defined(OS_MACOSX) && !defined(ANDROID)
18#include <gdk/gdk.h>
19#endif
20
21#include "base/basictypes.h"
22#include "base/file_util.h"
23#include "base/logging.h"
24#include "base/utf_string_conversions.h"
25
26namespace base {
27
28#if !defined(OS_OPENBSD)
29int 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
43int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) {
44#ifdef ANDROID
45  return -1;
46#else
47  struct statvfs stats;
48  if (statvfs(path.value().c_str(), &stats) != 0) {
49    return -1;
50  }
51  return static_cast<int64>(stats.f_bavail) * stats.f_frsize;
52#endif
53}
54
55// static
56std::string SysInfo::OperatingSystemName() {
57  utsname info;
58  if (uname(&info) < 0) {
59    NOTREACHED();
60    return "";
61  }
62  return std::string(info.sysname);
63}
64
65// static
66std::string SysInfo::OperatingSystemVersion() {
67  utsname info;
68  if (uname(&info) < 0) {
69    NOTREACHED();
70    return "";
71  }
72  return std::string(info.release);
73}
74
75// static
76std::string SysInfo::CPUArchitecture() {
77  utsname info;
78  if (uname(&info) < 0) {
79    NOTREACHED();
80    return "";
81  }
82  return std::string(info.machine);
83}
84
85#if !defined(OS_MACOSX)
86// static
87void SysInfo::GetPrimaryDisplayDimensions(int* width, int* height) {
88#ifdef ANDROID
89  return;
90#else
91  // Note that Bad Things Happen if this isn't called from the UI thread,
92  // but also that there's no way to check that from here.  :(
93  GdkScreen* screen = gdk_screen_get_default();
94  if (width)
95    *width = gdk_screen_get_width(screen);
96  if (height)
97    *height = gdk_screen_get_height(screen);
98#endif
99}
100
101// static
102int SysInfo::DisplayCount() {
103#ifdef ANDROID
104  return 1;
105#else
106  // Note that Bad Things Happen if this isn't called from the UI thread,
107  // but also that there's no way to check that from here.  :(
108
109  // This query is kinda bogus for Linux -- do we want number of X screens?
110  // The number of monitors Xinerama has?  We'll just use whatever GDK uses.
111  GdkScreen* screen = gdk_screen_get_default();
112  return gdk_screen_get_n_monitors(screen);
113#endif
114}
115#endif
116
117// static
118size_t SysInfo::VMAllocationGranularity() {
119  return getpagesize();
120}
121
122}  // namespace base
123