1 // 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" 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 24 namespace base { 25 26 #if !defined(OS_OPENBSD) 27 int 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 41 int64 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 50 bool SysInfo::HasEnvVar(const wchar_t* var) { 51 std::string var_utf8 = WideToUTF8(std::wstring(var)); 52 return getenv(var_utf8.c_str()) != NULL; 53 } 54 55 // static 56 std::wstring SysInfo::GetEnvVar(const wchar_t* var) { 57 std::string var_utf8 = WideToUTF8(std::wstring(var)); 58 char* value = getenv(var_utf8.c_str()); 59 if (!value) { 60 return std::wstring(); 61 } else { 62 return UTF8ToWide(value); 63 } 64 } 65 66 // static 67 std::string SysInfo::OperatingSystemName() { 68 utsname info; 69 if (uname(&info) < 0) { 70 NOTREACHED(); 71 return ""; 72 } 73 return std::string(info.sysname); 74 } 75 76 // static 77 std::string SysInfo::OperatingSystemVersion() { 78 utsname info; 79 if (uname(&info) < 0) { 80 NOTREACHED(); 81 return ""; 82 } 83 return std::string(info.release); 84 } 85 86 // static 87 std::string SysInfo::CPUArchitecture() { 88 utsname info; 89 if (uname(&info) < 0) { 90 NOTREACHED(); 91 return ""; 92 } 93 return std::string(info.machine); 94 } 95 96 #if !defined(OS_MACOSX) 97 // static 98 void SysInfo::GetPrimaryDisplayDimensions(int* width, int* height) { 99 // Note that Bad Things Happen if this isn't called from the UI thread, 100 // but also that there's no way to check that from here. :( 101 GdkScreen* screen = gdk_screen_get_default(); 102 if (width) 103 *width = gdk_screen_get_width(screen); 104 if (height) 105 *height = gdk_screen_get_height(screen); 106 } 107 108 // static 109 int SysInfo::DisplayCount() { 110 // Note that Bad Things Happen if this isn't called from the UI thread, 111 // but also that there's no way to check that from here. :( 112 113 // This query is kinda bogus for Linux -- do we want number of X screens? 114 // The number of monitors Xinerama has? We'll just use whatever GDK uses. 115 GdkScreen* screen = gdk_screen_get_default(); 116 return gdk_screen_get_n_monitors(screen); 117 } 118 #endif 119 120 // static 121 size_t SysInfo::VMAllocationGranularity() { 122 return getpagesize(); 123 } 124 125 } // namespace base 126