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