11cb0ef41Sopenharmony_ci/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
21cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
31cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to
41cb0ef41Sopenharmony_ci * deal in the Software without restriction, including without limitation the
51cb0ef41Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
61cb0ef41Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
71cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions:
81cb0ef41Sopenharmony_ci *
91cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
101cb0ef41Sopenharmony_ci * all copies or substantial portions of the Software.
111cb0ef41Sopenharmony_ci *
121cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
131cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
141cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
151cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
161cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
171cb0ef41Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
181cb0ef41Sopenharmony_ci * IN THE SOFTWARE.
191cb0ef41Sopenharmony_ci */
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci#include "uv.h"
221cb0ef41Sopenharmony_ci#include "internal.h"
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci#include <sys/types.h>
251cb0ef41Sopenharmony_ci#include <sys/param.h>
261cb0ef41Sopenharmony_ci#include <sys/resource.h>
271cb0ef41Sopenharmony_ci#include <sys/sched.h>
281cb0ef41Sopenharmony_ci#include <sys/time.h>
291cb0ef41Sopenharmony_ci#include <sys/sysctl.h>
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci#include <errno.h>
321cb0ef41Sopenharmony_ci#include <fcntl.h>
331cb0ef41Sopenharmony_ci#include <paths.h>
341cb0ef41Sopenharmony_ci#include <stdlib.h>
351cb0ef41Sopenharmony_ci#include <string.h>
361cb0ef41Sopenharmony_ci#include <unistd.h>
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciint uv__platform_loop_init(uv_loop_t* loop) {
401cb0ef41Sopenharmony_ci  return uv__kqueue_init(loop);
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_civoid uv__platform_loop_delete(uv_loop_t* loop) {
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_civoid uv_loadavg(double avg[3]) {
491cb0ef41Sopenharmony_ci  struct loadavg info;
501cb0ef41Sopenharmony_ci  size_t size = sizeof(info);
511cb0ef41Sopenharmony_ci  int which[] = {CTL_VM, VM_LOADAVG};
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) < 0) return;
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  avg[0] = (double) info.ldavg[0] / info.fscale;
561cb0ef41Sopenharmony_ci  avg[1] = (double) info.ldavg[1] / info.fscale;
571cb0ef41Sopenharmony_ci  avg[2] = (double) info.ldavg[2] / info.fscale;
581cb0ef41Sopenharmony_ci}
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ciint uv_exepath(char* buffer, size_t* size) {
621cb0ef41Sopenharmony_ci  int mib[4];
631cb0ef41Sopenharmony_ci  char **argsbuf = NULL;
641cb0ef41Sopenharmony_ci  size_t argsbuf_size = 100U;
651cb0ef41Sopenharmony_ci  size_t exepath_size;
661cb0ef41Sopenharmony_ci  pid_t mypid;
671cb0ef41Sopenharmony_ci  int err;
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  if (buffer == NULL || size == NULL || *size == 0)
701cb0ef41Sopenharmony_ci    return UV_EINVAL;
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  mypid = getpid();
731cb0ef41Sopenharmony_ci  for (;;) {
741cb0ef41Sopenharmony_ci    err = UV_ENOMEM;
751cb0ef41Sopenharmony_ci    argsbuf = uv__reallocf(argsbuf, argsbuf_size);
761cb0ef41Sopenharmony_ci    if (argsbuf == NULL)
771cb0ef41Sopenharmony_ci      goto out;
781cb0ef41Sopenharmony_ci    mib[0] = CTL_KERN;
791cb0ef41Sopenharmony_ci    mib[1] = KERN_PROC_ARGS;
801cb0ef41Sopenharmony_ci    mib[2] = mypid;
811cb0ef41Sopenharmony_ci    mib[3] = KERN_PROC_ARGV;
821cb0ef41Sopenharmony_ci    if (sysctl(mib, ARRAY_SIZE(mib), argsbuf, &argsbuf_size, NULL, 0) == 0) {
831cb0ef41Sopenharmony_ci      break;
841cb0ef41Sopenharmony_ci    }
851cb0ef41Sopenharmony_ci    if (errno != ENOMEM) {
861cb0ef41Sopenharmony_ci      err = UV__ERR(errno);
871cb0ef41Sopenharmony_ci      goto out;
881cb0ef41Sopenharmony_ci    }
891cb0ef41Sopenharmony_ci    argsbuf_size *= 2U;
901cb0ef41Sopenharmony_ci  }
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  if (argsbuf[0] == NULL) {
931cb0ef41Sopenharmony_ci    err = UV_EINVAL;  /* FIXME(bnoordhuis) More appropriate error. */
941cb0ef41Sopenharmony_ci    goto out;
951cb0ef41Sopenharmony_ci  }
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  *size -= 1;
981cb0ef41Sopenharmony_ci  exepath_size = strlen(argsbuf[0]);
991cb0ef41Sopenharmony_ci  if (*size > exepath_size)
1001cb0ef41Sopenharmony_ci    *size = exepath_size;
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  memcpy(buffer, argsbuf[0], *size);
1031cb0ef41Sopenharmony_ci  buffer[*size] = '\0';
1041cb0ef41Sopenharmony_ci  err = 0;
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ciout:
1071cb0ef41Sopenharmony_ci  uv__free(argsbuf);
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci  return err;
1101cb0ef41Sopenharmony_ci}
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ciuint64_t uv_get_free_memory(void) {
1141cb0ef41Sopenharmony_ci  struct uvmexp info;
1151cb0ef41Sopenharmony_ci  size_t size = sizeof(info);
1161cb0ef41Sopenharmony_ci  int which[] = {CTL_VM, VM_UVMEXP};
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
1191cb0ef41Sopenharmony_ci    return UV__ERR(errno);
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci  return (uint64_t) info.free * sysconf(_SC_PAGESIZE);
1221cb0ef41Sopenharmony_ci}
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ciuint64_t uv_get_total_memory(void) {
1261cb0ef41Sopenharmony_ci  uint64_t info;
1271cb0ef41Sopenharmony_ci  int which[] = {CTL_HW, HW_PHYSMEM64};
1281cb0ef41Sopenharmony_ci  size_t size = sizeof(info);
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
1311cb0ef41Sopenharmony_ci    return UV__ERR(errno);
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci  return (uint64_t) info;
1341cb0ef41Sopenharmony_ci}
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ciuint64_t uv_get_constrained_memory(void) {
1381cb0ef41Sopenharmony_ci  return 0;  /* Memory constraints are unknown. */
1391cb0ef41Sopenharmony_ci}
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ciint uv_resident_set_memory(size_t* rss) {
1431cb0ef41Sopenharmony_ci  struct kinfo_proc kinfo;
1441cb0ef41Sopenharmony_ci  size_t page_size = getpagesize();
1451cb0ef41Sopenharmony_ci  size_t size = sizeof(struct kinfo_proc);
1461cb0ef41Sopenharmony_ci  int mib[6];
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci  mib[0] = CTL_KERN;
1491cb0ef41Sopenharmony_ci  mib[1] = KERN_PROC;
1501cb0ef41Sopenharmony_ci  mib[2] = KERN_PROC_PID;
1511cb0ef41Sopenharmony_ci  mib[3] = getpid();
1521cb0ef41Sopenharmony_ci  mib[4] = sizeof(struct kinfo_proc);
1531cb0ef41Sopenharmony_ci  mib[5] = 1;
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci  if (sysctl(mib, ARRAY_SIZE(mib), &kinfo, &size, NULL, 0) < 0)
1561cb0ef41Sopenharmony_ci    return UV__ERR(errno);
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci  *rss = kinfo.p_vm_rssize * page_size;
1591cb0ef41Sopenharmony_ci  return 0;
1601cb0ef41Sopenharmony_ci}
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ciint uv_uptime(double* uptime) {
1641cb0ef41Sopenharmony_ci  time_t now;
1651cb0ef41Sopenharmony_ci  struct timeval info;
1661cb0ef41Sopenharmony_ci  size_t size = sizeof(info);
1671cb0ef41Sopenharmony_ci  static int which[] = {CTL_KERN, KERN_BOOTTIME};
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ci  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
1701cb0ef41Sopenharmony_ci    return UV__ERR(errno);
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci  now = time(NULL);
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci  *uptime = (double)(now - info.tv_sec);
1751cb0ef41Sopenharmony_ci  return 0;
1761cb0ef41Sopenharmony_ci}
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ciint uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
1801cb0ef41Sopenharmony_ci  unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
1811cb0ef41Sopenharmony_ci               multiplier = ((uint64_t)1000L / ticks), cpuspeed;
1821cb0ef41Sopenharmony_ci  uint64_t info[CPUSTATES];
1831cb0ef41Sopenharmony_ci  char model[512];
1841cb0ef41Sopenharmony_ci  int numcpus = 1;
1851cb0ef41Sopenharmony_ci  int which[] = {CTL_HW,HW_MODEL};
1861cb0ef41Sopenharmony_ci  int percpu[] = {CTL_KERN,KERN_CPTIME2,0};
1871cb0ef41Sopenharmony_ci  size_t size;
1881cb0ef41Sopenharmony_ci  int i, j;
1891cb0ef41Sopenharmony_ci  uv_cpu_info_t* cpu_info;
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_ci  size = sizeof(model);
1921cb0ef41Sopenharmony_ci  if (sysctl(which, ARRAY_SIZE(which), &model, &size, NULL, 0))
1931cb0ef41Sopenharmony_ci    return UV__ERR(errno);
1941cb0ef41Sopenharmony_ci
1951cb0ef41Sopenharmony_ci  which[1] = HW_NCPUONLINE;
1961cb0ef41Sopenharmony_ci  size = sizeof(numcpus);
1971cb0ef41Sopenharmony_ci  if (sysctl(which, ARRAY_SIZE(which), &numcpus, &size, NULL, 0))
1981cb0ef41Sopenharmony_ci    return UV__ERR(errno);
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci  *cpu_infos = uv__malloc(numcpus * sizeof(**cpu_infos));
2011cb0ef41Sopenharmony_ci  if (!(*cpu_infos))
2021cb0ef41Sopenharmony_ci    return UV_ENOMEM;
2031cb0ef41Sopenharmony_ci
2041cb0ef41Sopenharmony_ci  i = 0;
2051cb0ef41Sopenharmony_ci  *count = numcpus;
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci  which[1] = HW_CPUSPEED;
2081cb0ef41Sopenharmony_ci  size = sizeof(cpuspeed);
2091cb0ef41Sopenharmony_ci  if (sysctl(which, ARRAY_SIZE(which), &cpuspeed, &size, NULL, 0))
2101cb0ef41Sopenharmony_ci    goto error;
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ci  size = sizeof(info);
2131cb0ef41Sopenharmony_ci  for (i = 0; i < numcpus; i++) {
2141cb0ef41Sopenharmony_ci    percpu[2] = i;
2151cb0ef41Sopenharmony_ci    if (sysctl(percpu, ARRAY_SIZE(percpu), &info, &size, NULL, 0))
2161cb0ef41Sopenharmony_ci      goto error;
2171cb0ef41Sopenharmony_ci
2181cb0ef41Sopenharmony_ci    cpu_info = &(*cpu_infos)[i];
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ci    cpu_info->cpu_times.user = (uint64_t)(info[CP_USER]) * multiplier;
2211cb0ef41Sopenharmony_ci    cpu_info->cpu_times.nice = (uint64_t)(info[CP_NICE]) * multiplier;
2221cb0ef41Sopenharmony_ci    cpu_info->cpu_times.sys = (uint64_t)(info[CP_SYS]) * multiplier;
2231cb0ef41Sopenharmony_ci    cpu_info->cpu_times.idle = (uint64_t)(info[CP_IDLE]) * multiplier;
2241cb0ef41Sopenharmony_ci    cpu_info->cpu_times.irq = (uint64_t)(info[CP_INTR]) * multiplier;
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_ci    cpu_info->model = uv__strdup(model);
2271cb0ef41Sopenharmony_ci    cpu_info->speed = cpuspeed;
2281cb0ef41Sopenharmony_ci  }
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_ci  return 0;
2311cb0ef41Sopenharmony_ci
2321cb0ef41Sopenharmony_cierror:
2331cb0ef41Sopenharmony_ci  *count = 0;
2341cb0ef41Sopenharmony_ci  for (j = 0; j < i; j++)
2351cb0ef41Sopenharmony_ci    uv__free((*cpu_infos)[j].model);
2361cb0ef41Sopenharmony_ci
2371cb0ef41Sopenharmony_ci  uv__free(*cpu_infos);
2381cb0ef41Sopenharmony_ci  *cpu_infos = NULL;
2391cb0ef41Sopenharmony_ci  return UV__ERR(errno);
2401cb0ef41Sopenharmony_ci}
241