11cb0ef41Sopenharmony_ci/* Copyright libuv 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#include "uv.h"
231cb0ef41Sopenharmony_ci#include "internal.h"
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci#include <string.h>
261cb0ef41Sopenharmony_ci#include <sys/process.h>
271cb0ef41Sopenharmony_ci#include <sys/neutrino.h>
281cb0ef41Sopenharmony_ci#include <sys/memmsg.h>
291cb0ef41Sopenharmony_ci#include <sys/syspage.h>
301cb0ef41Sopenharmony_ci#include <sys/procfs.h>
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cistatic void
331cb0ef41Sopenharmony_ciget_mem_info(uint64_t* totalmem, uint64_t* freemem) {
341cb0ef41Sopenharmony_ci  mem_info_t msg;
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  memset(&msg, 0, sizeof(msg));
371cb0ef41Sopenharmony_ci  msg.i.type = _MEM_INFO;
381cb0ef41Sopenharmony_ci  msg.i.fd = -1;
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  if (MsgSend(MEMMGR_COID, &msg.i, sizeof(msg.i), &msg.o, sizeof(msg.o))
411cb0ef41Sopenharmony_ci      != -1) {
421cb0ef41Sopenharmony_ci    *totalmem = msg.o.info.__posix_tmi_total;
431cb0ef41Sopenharmony_ci    *freemem = msg.o.info.posix_tmi_length;
441cb0ef41Sopenharmony_ci  } else {
451cb0ef41Sopenharmony_ci    *totalmem = 0;
461cb0ef41Sopenharmony_ci    *freemem = 0;
471cb0ef41Sopenharmony_ci  }
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_civoid uv_loadavg(double avg[3]) {
521cb0ef41Sopenharmony_ci  avg[0] = 0.0;
531cb0ef41Sopenharmony_ci  avg[1] = 0.0;
541cb0ef41Sopenharmony_ci  avg[2] = 0.0;
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ciint uv_exepath(char* buffer, size_t* size) {
591cb0ef41Sopenharmony_ci  char path[PATH_MAX];
601cb0ef41Sopenharmony_ci  if (buffer == NULL || size == NULL || *size == 0)
611cb0ef41Sopenharmony_ci    return UV_EINVAL;
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  realpath(_cmdname(NULL), path);
641cb0ef41Sopenharmony_ci  strlcpy(buffer, path, *size);
651cb0ef41Sopenharmony_ci  *size = strlen(buffer);
661cb0ef41Sopenharmony_ci  return 0;
671cb0ef41Sopenharmony_ci}
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ciuint64_t uv_get_free_memory(void) {
711cb0ef41Sopenharmony_ci  uint64_t totalmem;
721cb0ef41Sopenharmony_ci  uint64_t freemem;
731cb0ef41Sopenharmony_ci  get_mem_info(&totalmem, &freemem);
741cb0ef41Sopenharmony_ci  return freemem;
751cb0ef41Sopenharmony_ci}
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ciuint64_t uv_get_total_memory(void) {
791cb0ef41Sopenharmony_ci  uint64_t totalmem;
801cb0ef41Sopenharmony_ci  uint64_t freemem;
811cb0ef41Sopenharmony_ci  get_mem_info(&totalmem, &freemem);
821cb0ef41Sopenharmony_ci  return totalmem;
831cb0ef41Sopenharmony_ci}
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ciuint64_t uv_get_constrained_memory(void) {
871cb0ef41Sopenharmony_ci  return 0;
881cb0ef41Sopenharmony_ci}
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ciint uv_resident_set_memory(size_t* rss) {
921cb0ef41Sopenharmony_ci  int fd;
931cb0ef41Sopenharmony_ci  procfs_asinfo asinfo;
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci  fd = uv__open_cloexec("/proc/self/ctl", O_RDONLY);
961cb0ef41Sopenharmony_ci  if (fd == -1)
971cb0ef41Sopenharmony_ci    return UV__ERR(errno);
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  if (devctl(fd, DCMD_PROC_ASINFO, &asinfo, sizeof(asinfo), 0) == -1) {
1001cb0ef41Sopenharmony_ci    uv__close(fd);
1011cb0ef41Sopenharmony_ci    return UV__ERR(errno);
1021cb0ef41Sopenharmony_ci  }
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci  uv__close(fd);
1051cb0ef41Sopenharmony_ci  *rss = asinfo.rss;
1061cb0ef41Sopenharmony_ci  return 0;
1071cb0ef41Sopenharmony_ci}
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ciint uv_uptime(double* uptime) {
1111cb0ef41Sopenharmony_ci  struct qtime_entry* qtime = _SYSPAGE_ENTRY(_syspage_ptr, qtime);
1121cb0ef41Sopenharmony_ci  *uptime = (qtime->nsec / 1000000000.0);
1131cb0ef41Sopenharmony_ci  return 0;
1141cb0ef41Sopenharmony_ci}
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ciint uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
1181cb0ef41Sopenharmony_ci  struct cpuinfo_entry* cpuinfo =
1191cb0ef41Sopenharmony_ci    (struct cpuinfo_entry*)_SYSPAGE_ENTRY(_syspage_ptr, new_cpuinfo);
1201cb0ef41Sopenharmony_ci  size_t cpuinfo_size = _SYSPAGE_ELEMENT_SIZE(_syspage_ptr, cpuinfo);
1211cb0ef41Sopenharmony_ci  struct strings_entry* strings = _SYSPAGE_ENTRY(_syspage_ptr, strings);
1221cb0ef41Sopenharmony_ci  int num_cpus = _syspage_ptr->num_cpu;
1231cb0ef41Sopenharmony_ci  int i;
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  *count = num_cpus;
1261cb0ef41Sopenharmony_ci  *cpu_infos = uv__malloc(num_cpus * sizeof(**cpu_infos));
1271cb0ef41Sopenharmony_ci  if (*cpu_infos == NULL)
1281cb0ef41Sopenharmony_ci    return UV_ENOMEM;
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci  for (i = 0; i < num_cpus; i++) {
1311cb0ef41Sopenharmony_ci    (*cpu_infos)[i].model = strdup(&strings->data[cpuinfo->name]);
1321cb0ef41Sopenharmony_ci    (*cpu_infos)[i].speed = cpuinfo->speed;
1331cb0ef41Sopenharmony_ci    SYSPAGE_ARRAY_ADJ_OFFSET(cpuinfo, cpuinfo, cpuinfo_size);
1341cb0ef41Sopenharmony_ci  }
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci  return 0;
1371cb0ef41Sopenharmony_ci}
138