11cb0ef41Sopenharmony_ci// Copyright 2014 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/base/sys-info.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#if V8_OS_POSIX
81cb0ef41Sopenharmony_ci#include <sys/stat.h>
91cb0ef41Sopenharmony_ci#include <sys/time.h>
101cb0ef41Sopenharmony_ci#include <sys/types.h>
111cb0ef41Sopenharmony_ci#include <unistd.h>
121cb0ef41Sopenharmony_ci#if !V8_OS_FUCHSIA
131cb0ef41Sopenharmony_ci#include <sys/resource.h>
141cb0ef41Sopenharmony_ci#endif
151cb0ef41Sopenharmony_ci#endif
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci#if V8_OS_BSD
181cb0ef41Sopenharmony_ci#include <sys/sysctl.h>
191cb0ef41Sopenharmony_ci#endif
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci#include <limits>
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci#include "src/base/logging.h"
241cb0ef41Sopenharmony_ci#include "src/base/macros.h"
251cb0ef41Sopenharmony_ci#if V8_OS_WIN
261cb0ef41Sopenharmony_ci#include <windows.h>
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci#include "src/base/win32-headers.h"
291cb0ef41Sopenharmony_ci#endif
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci#if V8_OS_STARBOARD
321cb0ef41Sopenharmony_ci#include "starboard/system.h"
331cb0ef41Sopenharmony_ci#endif
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_cinamespace v8 {
361cb0ef41Sopenharmony_cinamespace base {
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci// static
391cb0ef41Sopenharmony_ciint SysInfo::NumberOfProcessors() {
401cb0ef41Sopenharmony_ci#if V8_OS_OPENBSD
411cb0ef41Sopenharmony_ci  int mib[2] = {CTL_HW, HW_NCPU};
421cb0ef41Sopenharmony_ci  int ncpu = 0;
431cb0ef41Sopenharmony_ci  size_t len = sizeof(ncpu);
441cb0ef41Sopenharmony_ci  if (sysctl(mib, arraysize(mib), &ncpu, &len, nullptr, 0) != 0) {
451cb0ef41Sopenharmony_ci    return 1;
461cb0ef41Sopenharmony_ci  }
471cb0ef41Sopenharmony_ci  return ncpu;
481cb0ef41Sopenharmony_ci#elif V8_OS_POSIX
491cb0ef41Sopenharmony_ci  long result = sysconf(_SC_NPROCESSORS_ONLN);  // NOLINT(runtime/int)
501cb0ef41Sopenharmony_ci  if (result == -1) {
511cb0ef41Sopenharmony_ci    return 1;
521cb0ef41Sopenharmony_ci  }
531cb0ef41Sopenharmony_ci  return static_cast<int>(result);
541cb0ef41Sopenharmony_ci#elif V8_OS_WIN
551cb0ef41Sopenharmony_ci  SYSTEM_INFO system_info = {};
561cb0ef41Sopenharmony_ci  ::GetNativeSystemInfo(&system_info);
571cb0ef41Sopenharmony_ci  return static_cast<int>(system_info.dwNumberOfProcessors);
581cb0ef41Sopenharmony_ci#elif V8_OS_STARBOARD
591cb0ef41Sopenharmony_ci  return SbSystemGetNumberOfProcessors();
601cb0ef41Sopenharmony_ci#endif
611cb0ef41Sopenharmony_ci}
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci// static
651cb0ef41Sopenharmony_ciint64_t SysInfo::AmountOfPhysicalMemory() {
661cb0ef41Sopenharmony_ci#if V8_OS_DARWIN
671cb0ef41Sopenharmony_ci  int mib[2] = {CTL_HW, HW_MEMSIZE};
681cb0ef41Sopenharmony_ci  int64_t memsize = 0;
691cb0ef41Sopenharmony_ci  size_t len = sizeof(memsize);
701cb0ef41Sopenharmony_ci  if (sysctl(mib, arraysize(mib), &memsize, &len, nullptr, 0) != 0) {
711cb0ef41Sopenharmony_ci    return 0;
721cb0ef41Sopenharmony_ci  }
731cb0ef41Sopenharmony_ci  return memsize;
741cb0ef41Sopenharmony_ci#elif V8_OS_FREEBSD
751cb0ef41Sopenharmony_ci  int pages, page_size;
761cb0ef41Sopenharmony_ci  size_t size = sizeof(pages);
771cb0ef41Sopenharmony_ci  sysctlbyname("vm.stats.vm.v_page_count", &pages, &size, nullptr, 0);
781cb0ef41Sopenharmony_ci  sysctlbyname("vm.stats.vm.v_page_size", &page_size, &size, nullptr, 0);
791cb0ef41Sopenharmony_ci  if (pages == -1 || page_size == -1) {
801cb0ef41Sopenharmony_ci    return 0;
811cb0ef41Sopenharmony_ci  }
821cb0ef41Sopenharmony_ci  return static_cast<int64_t>(pages) * page_size;
831cb0ef41Sopenharmony_ci#elif V8_OS_CYGWIN || V8_OS_WIN
841cb0ef41Sopenharmony_ci  MEMORYSTATUSEX memory_info;
851cb0ef41Sopenharmony_ci  memory_info.dwLength = sizeof(memory_info);
861cb0ef41Sopenharmony_ci  if (!GlobalMemoryStatusEx(&memory_info)) {
871cb0ef41Sopenharmony_ci    return 0;
881cb0ef41Sopenharmony_ci  }
891cb0ef41Sopenharmony_ci  int64_t result = static_cast<int64_t>(memory_info.ullTotalPhys);
901cb0ef41Sopenharmony_ci  if (result < 0) result = std::numeric_limits<int64_t>::max();
911cb0ef41Sopenharmony_ci  return result;
921cb0ef41Sopenharmony_ci#elif V8_OS_QNX
931cb0ef41Sopenharmony_ci  struct stat stat_buf;
941cb0ef41Sopenharmony_ci  if (stat("/proc", &stat_buf) != 0) {
951cb0ef41Sopenharmony_ci    return 0;
961cb0ef41Sopenharmony_ci  }
971cb0ef41Sopenharmony_ci  return static_cast<int64_t>(stat_buf.st_size);
981cb0ef41Sopenharmony_ci#elif V8_OS_AIX
991cb0ef41Sopenharmony_ci  int64_t result = sysconf(_SC_AIX_REALMEM);
1001cb0ef41Sopenharmony_ci  return static_cast<int64_t>(result) * 1024L;
1011cb0ef41Sopenharmony_ci#elif V8_OS_POSIX
1021cb0ef41Sopenharmony_ci  long pages = sysconf(_SC_PHYS_PAGES);    // NOLINT(runtime/int)
1031cb0ef41Sopenharmony_ci  long page_size = sysconf(_SC_PAGESIZE);  // NOLINT(runtime/int)
1041cb0ef41Sopenharmony_ci  if (pages == -1 || page_size == -1) {
1051cb0ef41Sopenharmony_ci    return 0;
1061cb0ef41Sopenharmony_ci  }
1071cb0ef41Sopenharmony_ci  return static_cast<int64_t>(pages) * page_size;
1081cb0ef41Sopenharmony_ci#elif V8_OS_STARBOARD
1091cb0ef41Sopenharmony_ci  return SbSystemGetTotalCPUMemory();
1101cb0ef41Sopenharmony_ci#endif
1111cb0ef41Sopenharmony_ci}
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci// static
1151cb0ef41Sopenharmony_ciint64_t SysInfo::AmountOfVirtualMemory() {
1161cb0ef41Sopenharmony_ci#if V8_OS_WIN || V8_OS_FUCHSIA
1171cb0ef41Sopenharmony_ci  return 0;
1181cb0ef41Sopenharmony_ci#elif V8_OS_POSIX
1191cb0ef41Sopenharmony_ci  struct rlimit rlim;
1201cb0ef41Sopenharmony_ci  int result = getrlimit(RLIMIT_DATA, &rlim);
1211cb0ef41Sopenharmony_ci  if (result != 0) {
1221cb0ef41Sopenharmony_ci    return 0;
1231cb0ef41Sopenharmony_ci  }
1241cb0ef41Sopenharmony_ci  return (rlim.rlim_cur == RLIM_INFINITY) ? 0 : rlim.rlim_cur;
1251cb0ef41Sopenharmony_ci#elif V8_OS_STARBOARD
1261cb0ef41Sopenharmony_ci  return 0;
1271cb0ef41Sopenharmony_ci#endif
1281cb0ef41Sopenharmony_ci}
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci}  // namespace base
1311cb0ef41Sopenharmony_ci}  // namespace v8
132