1e66f31c5Sopenharmony_ci/* Copyright libuv contributors. All rights reserved.
2e66f31c5Sopenharmony_ci  *
3e66f31c5Sopenharmony_ci  * Permission is hereby granted, free of charge, to any person obtaining a copy
4e66f31c5Sopenharmony_ci  * of this software and associated documentation files (the "Software"), to
5e66f31c5Sopenharmony_ci  * deal in the Software without restriction, including without limitation the
6e66f31c5Sopenharmony_ci  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7e66f31c5Sopenharmony_ci  * sell copies of the Software, and to permit persons to whom the Software is
8e66f31c5Sopenharmony_ci  * furnished to do so, subject to the following conditions:
9e66f31c5Sopenharmony_ci  *
10e66f31c5Sopenharmony_ci  * The above copyright notice and this permission notice shall be included in
11e66f31c5Sopenharmony_ci  * all copies or substantial portions of the Software.
12e66f31c5Sopenharmony_ci  *
13e66f31c5Sopenharmony_ci  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14e66f31c5Sopenharmony_ci  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15e66f31c5Sopenharmony_ci  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16e66f31c5Sopenharmony_ci  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17e66f31c5Sopenharmony_ci  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18e66f31c5Sopenharmony_ci  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19e66f31c5Sopenharmony_ci  * IN THE SOFTWARE.
20e66f31c5Sopenharmony_ci  */
21e66f31c5Sopenharmony_ci
22e66f31c5Sopenharmony_ci#include "uv.h"
23e66f31c5Sopenharmony_ci#include "internal.h"
24e66f31c5Sopenharmony_ci
25e66f31c5Sopenharmony_ci#include <string.h>
26e66f31c5Sopenharmony_ci#include <sys/process.h>
27e66f31c5Sopenharmony_ci#include <sys/neutrino.h>
28e66f31c5Sopenharmony_ci#include <sys/memmsg.h>
29e66f31c5Sopenharmony_ci#include <sys/syspage.h>
30e66f31c5Sopenharmony_ci#include <sys/procfs.h>
31e66f31c5Sopenharmony_ci
32e66f31c5Sopenharmony_cistatic void
33e66f31c5Sopenharmony_ciget_mem_info(uint64_t* totalmem, uint64_t* freemem) {
34e66f31c5Sopenharmony_ci  mem_info_t msg;
35e66f31c5Sopenharmony_ci
36e66f31c5Sopenharmony_ci  memset(&msg, 0, sizeof(msg));
37e66f31c5Sopenharmony_ci  msg.i.type = _MEM_INFO;
38e66f31c5Sopenharmony_ci  msg.i.fd = -1;
39e66f31c5Sopenharmony_ci
40e66f31c5Sopenharmony_ci  if (MsgSend(MEMMGR_COID, &msg.i, sizeof(msg.i), &msg.o, sizeof(msg.o))
41e66f31c5Sopenharmony_ci      != -1) {
42e66f31c5Sopenharmony_ci    *totalmem = msg.o.info.__posix_tmi_total;
43e66f31c5Sopenharmony_ci    *freemem = msg.o.info.posix_tmi_length;
44e66f31c5Sopenharmony_ci  } else {
45e66f31c5Sopenharmony_ci    *totalmem = 0;
46e66f31c5Sopenharmony_ci    *freemem = 0;
47e66f31c5Sopenharmony_ci  }
48e66f31c5Sopenharmony_ci}
49e66f31c5Sopenharmony_ci
50e66f31c5Sopenharmony_ci
51e66f31c5Sopenharmony_civoid uv_loadavg(double avg[3]) {
52e66f31c5Sopenharmony_ci  avg[0] = 0.0;
53e66f31c5Sopenharmony_ci  avg[1] = 0.0;
54e66f31c5Sopenharmony_ci  avg[2] = 0.0;
55e66f31c5Sopenharmony_ci}
56e66f31c5Sopenharmony_ci
57e66f31c5Sopenharmony_ci
58e66f31c5Sopenharmony_ciint uv_exepath(char* buffer, size_t* size) {
59e66f31c5Sopenharmony_ci  char path[PATH_MAX];
60e66f31c5Sopenharmony_ci  if (buffer == NULL || size == NULL || *size == 0)
61e66f31c5Sopenharmony_ci    return UV_EINVAL;
62e66f31c5Sopenharmony_ci
63e66f31c5Sopenharmony_ci  realpath(_cmdname(NULL), path);
64e66f31c5Sopenharmony_ci  strlcpy(buffer, path, *size);
65e66f31c5Sopenharmony_ci  *size = strlen(buffer);
66e66f31c5Sopenharmony_ci  return 0;
67e66f31c5Sopenharmony_ci}
68e66f31c5Sopenharmony_ci
69e66f31c5Sopenharmony_ci
70e66f31c5Sopenharmony_ciuint64_t uv_get_free_memory(void) {
71e66f31c5Sopenharmony_ci  uint64_t totalmem;
72e66f31c5Sopenharmony_ci  uint64_t freemem;
73e66f31c5Sopenharmony_ci  get_mem_info(&totalmem, &freemem);
74e66f31c5Sopenharmony_ci  return freemem;
75e66f31c5Sopenharmony_ci}
76e66f31c5Sopenharmony_ci
77e66f31c5Sopenharmony_ci
78e66f31c5Sopenharmony_ciuint64_t uv_get_total_memory(void) {
79e66f31c5Sopenharmony_ci  uint64_t totalmem;
80e66f31c5Sopenharmony_ci  uint64_t freemem;
81e66f31c5Sopenharmony_ci  get_mem_info(&totalmem, &freemem);
82e66f31c5Sopenharmony_ci  return totalmem;
83e66f31c5Sopenharmony_ci}
84e66f31c5Sopenharmony_ci
85e66f31c5Sopenharmony_ci
86e66f31c5Sopenharmony_ciuint64_t uv_get_constrained_memory(void) {
87e66f31c5Sopenharmony_ci  return 0;
88e66f31c5Sopenharmony_ci}
89e66f31c5Sopenharmony_ci
90e66f31c5Sopenharmony_ci
91e66f31c5Sopenharmony_ciuint64_t uv_get_available_memory(void) {
92e66f31c5Sopenharmony_ci  return uv_get_free_memory();
93e66f31c5Sopenharmony_ci}
94e66f31c5Sopenharmony_ci
95e66f31c5Sopenharmony_ci
96e66f31c5Sopenharmony_ciint uv_resident_set_memory(size_t* rss) {
97e66f31c5Sopenharmony_ci  int fd;
98e66f31c5Sopenharmony_ci  procfs_asinfo asinfo;
99e66f31c5Sopenharmony_ci
100e66f31c5Sopenharmony_ci  fd = uv__open_cloexec("/proc/self/ctl", O_RDONLY);
101e66f31c5Sopenharmony_ci  if (fd == -1)
102e66f31c5Sopenharmony_ci    return UV__ERR(errno);
103e66f31c5Sopenharmony_ci
104e66f31c5Sopenharmony_ci  if (devctl(fd, DCMD_PROC_ASINFO, &asinfo, sizeof(asinfo), 0) == -1) {
105e66f31c5Sopenharmony_ci    uv__close(fd);
106e66f31c5Sopenharmony_ci    return UV__ERR(errno);
107e66f31c5Sopenharmony_ci  }
108e66f31c5Sopenharmony_ci
109e66f31c5Sopenharmony_ci  uv__close(fd);
110e66f31c5Sopenharmony_ci  *rss = asinfo.rss;
111e66f31c5Sopenharmony_ci  return 0;
112e66f31c5Sopenharmony_ci}
113e66f31c5Sopenharmony_ci
114e66f31c5Sopenharmony_ci
115e66f31c5Sopenharmony_ciint uv_uptime(double* uptime) {
116e66f31c5Sopenharmony_ci  struct qtime_entry* qtime = _SYSPAGE_ENTRY(_syspage_ptr, qtime);
117e66f31c5Sopenharmony_ci  *uptime = (qtime->nsec / 1000000000.0);
118e66f31c5Sopenharmony_ci  return 0;
119e66f31c5Sopenharmony_ci}
120e66f31c5Sopenharmony_ci
121e66f31c5Sopenharmony_ci
122e66f31c5Sopenharmony_ciint uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
123e66f31c5Sopenharmony_ci  struct cpuinfo_entry* cpuinfo =
124e66f31c5Sopenharmony_ci    (struct cpuinfo_entry*)_SYSPAGE_ENTRY(_syspage_ptr, new_cpuinfo);
125e66f31c5Sopenharmony_ci  size_t cpuinfo_size = _SYSPAGE_ELEMENT_SIZE(_syspage_ptr, cpuinfo);
126e66f31c5Sopenharmony_ci  struct strings_entry* strings = _SYSPAGE_ENTRY(_syspage_ptr, strings);
127e66f31c5Sopenharmony_ci  int num_cpus = _syspage_ptr->num_cpu;
128e66f31c5Sopenharmony_ci  int i;
129e66f31c5Sopenharmony_ci
130e66f31c5Sopenharmony_ci  *count = num_cpus;
131e66f31c5Sopenharmony_ci  *cpu_infos = uv__malloc(num_cpus * sizeof(**cpu_infos));
132e66f31c5Sopenharmony_ci  if (*cpu_infos == NULL)
133e66f31c5Sopenharmony_ci    return UV_ENOMEM;
134e66f31c5Sopenharmony_ci
135e66f31c5Sopenharmony_ci  for (i = 0; i < num_cpus; i++) {
136e66f31c5Sopenharmony_ci    (*cpu_infos)[i].model = strdup(&strings->data[cpuinfo->name]);
137e66f31c5Sopenharmony_ci    (*cpu_infos)[i].speed = cpuinfo->speed;
138e66f31c5Sopenharmony_ci    SYSPAGE_ARRAY_ADJ_OFFSET(cpuinfo, cpuinfo, cpuinfo_size);
139e66f31c5Sopenharmony_ci  }
140e66f31c5Sopenharmony_ci
141e66f31c5Sopenharmony_ci  return 0;
142e66f31c5Sopenharmony_ci}
143