11cb0ef41Sopenharmony_ci/* Copyright libuv project contributors. All rights reserved.
21cb0ef41Sopenharmony_ci *
31cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
41cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to
51cb0ef41Sopenharmony_ci * deal in the Software without restriction, including without limitation the
61cb0ef41Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
71cb0ef41Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
81cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions:
91cb0ef41Sopenharmony_ci *
101cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
111cb0ef41Sopenharmony_ci * all copies or substantial portions of the Software.
121cb0ef41Sopenharmony_ci *
131cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
141cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
151cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
161cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
171cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
181cb0ef41Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
191cb0ef41Sopenharmony_ci * IN THE SOFTWARE.
201cb0ef41Sopenharmony_ci */
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci#define _GNU_SOURCE 1
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci#include "uv.h"
251cb0ef41Sopenharmony_ci#include "internal.h"
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci#include <hurd.h>
281cb0ef41Sopenharmony_ci#include <hurd/process.h>
291cb0ef41Sopenharmony_ci#include <mach/task_info.h>
301cb0ef41Sopenharmony_ci#include <mach/vm_statistics.h>
311cb0ef41Sopenharmony_ci#include <mach/vm_param.h>
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci#include <inttypes.h>
341cb0ef41Sopenharmony_ci#include <stddef.h>
351cb0ef41Sopenharmony_ci#include <unistd.h>
361cb0ef41Sopenharmony_ci#include <string.h>
371cb0ef41Sopenharmony_ci#include <limits.h>
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciint uv_exepath(char* buffer, size_t* size) {
401cb0ef41Sopenharmony_ci  kern_return_t err;
411cb0ef41Sopenharmony_ci  /* XXX in current Hurd, strings are char arrays of 1024 elements */
421cb0ef41Sopenharmony_ci  string_t exepath;
431cb0ef41Sopenharmony_ci  ssize_t copied;
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  if (buffer == NULL || size == NULL || *size == 0)
461cb0ef41Sopenharmony_ci    return UV_EINVAL;
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  if (*size - 1 > 0) {
491cb0ef41Sopenharmony_ci    /* XXX limited length of buffer in current Hurd, this API will probably
501cb0ef41Sopenharmony_ci     * evolve in the future */
511cb0ef41Sopenharmony_ci    err = proc_get_exe(getproc(), getpid(), exepath);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci    if (err)
541cb0ef41Sopenharmony_ci      return UV__ERR(err);
551cb0ef41Sopenharmony_ci  }
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  copied = uv__strscpy(buffer, exepath, *size);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  /* do not return error on UV_E2BIG failure */
601cb0ef41Sopenharmony_ci  *size = copied < 0 ? strlen(buffer) : (size_t) copied;
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  return 0;
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ciint uv_resident_set_memory(size_t* rss) {
661cb0ef41Sopenharmony_ci  kern_return_t err;
671cb0ef41Sopenharmony_ci  struct task_basic_info bi;
681cb0ef41Sopenharmony_ci  mach_msg_type_number_t count;
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  count = TASK_BASIC_INFO_COUNT;
711cb0ef41Sopenharmony_ci  err = task_info(mach_task_self(), TASK_BASIC_INFO,
721cb0ef41Sopenharmony_ci		  (task_info_t) &bi, &count);
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  if (err)
751cb0ef41Sopenharmony_ci    return UV__ERR(err);
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  *rss = bi.resident_size;
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  return 0;
801cb0ef41Sopenharmony_ci}
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ciuint64_t uv_get_free_memory(void) {
831cb0ef41Sopenharmony_ci  kern_return_t err;
841cb0ef41Sopenharmony_ci  struct vm_statistics vmstats;
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  err = vm_statistics(mach_task_self(), &vmstats);
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  if (err)
891cb0ef41Sopenharmony_ci    return 0;
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci  return vmstats.free_count * vm_page_size;
921cb0ef41Sopenharmony_ci}
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ciuint64_t uv_get_total_memory(void) {
961cb0ef41Sopenharmony_ci  kern_return_t err;
971cb0ef41Sopenharmony_ci  host_basic_info_data_t hbi;
981cb0ef41Sopenharmony_ci  mach_msg_type_number_t cnt;
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci  cnt = HOST_BASIC_INFO_COUNT;
1011cb0ef41Sopenharmony_ci  err = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t) &hbi, &cnt);
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci  if (err)
1041cb0ef41Sopenharmony_ci    return 0;
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci  return hbi.memory_size;
1071cb0ef41Sopenharmony_ci}
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ciint uv_uptime(double* uptime) {
1111cb0ef41Sopenharmony_ci  char buf[128];
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci  /* Try /proc/uptime first */
1141cb0ef41Sopenharmony_ci  if (0 == uv__slurp("/proc/uptime", buf, sizeof(buf)))
1151cb0ef41Sopenharmony_ci    if (1 == sscanf(buf, "%lf", uptime))
1161cb0ef41Sopenharmony_ci      return 0;
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci  /* Reimplement here code from procfs to calculate uptime if not mounted? */
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci  return UV__ERR(EIO);
1211cb0ef41Sopenharmony_ci}
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_civoid uv_loadavg(double avg[3]) {
1241cb0ef41Sopenharmony_ci  char buf[128];  /* Large enough to hold all of /proc/loadavg. */
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci  if (0 == uv__slurp("/proc/loadavg", buf, sizeof(buf)))
1271cb0ef41Sopenharmony_ci    if (3 == sscanf(buf, "%lf %lf %lf", &avg[0], &avg[1], &avg[2]))
1281cb0ef41Sopenharmony_ci      return;
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci  /* Reimplement here code from procfs to calculate loadavg if not mounted? */
1311cb0ef41Sopenharmony_ci}
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ciint uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
1351cb0ef41Sopenharmony_ci  kern_return_t err;
1361cb0ef41Sopenharmony_ci  host_basic_info_data_t hbi;
1371cb0ef41Sopenharmony_ci  mach_msg_type_number_t cnt;
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci  /* Get count of cpus  */
1401cb0ef41Sopenharmony_ci  cnt = HOST_BASIC_INFO_COUNT;
1411cb0ef41Sopenharmony_ci  err = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t) &hbi, &cnt);
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci  if (err) {
1441cb0ef41Sopenharmony_ci    err = UV__ERR(err);
1451cb0ef41Sopenharmony_ci    goto abort;
1461cb0ef41Sopenharmony_ci  }
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci  /* XXX not implemented on the Hurd */
1491cb0ef41Sopenharmony_ci  *cpu_infos = uv__calloc(hbi.avail_cpus, sizeof(**cpu_infos));
1501cb0ef41Sopenharmony_ci  if (*cpu_infos == NULL) {
1511cb0ef41Sopenharmony_ci    err = UV_ENOMEM;
1521cb0ef41Sopenharmony_ci    goto abort;
1531cb0ef41Sopenharmony_ci  }
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci  *count = hbi.avail_cpus;
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci  return 0;
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci abort:
1601cb0ef41Sopenharmony_ci  *cpu_infos = NULL;
1611cb0ef41Sopenharmony_ci  *count = 0;
1621cb0ef41Sopenharmony_ci  return err;
1631cb0ef41Sopenharmony_ci}
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ciuint64_t uv_get_constrained_memory(void) {
1661cb0ef41Sopenharmony_ci  return 0;  /* Memory constraints are unknown. */
1671cb0ef41Sopenharmony_ci}
168