1e66f31c5Sopenharmony_ci/* Copyright libuv project 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 <FindDirectory.h> /* find_path() */
26e66f31c5Sopenharmony_ci#include <OS.h>
27e66f31c5Sopenharmony_ci
28e66f31c5Sopenharmony_ci
29e66f31c5Sopenharmony_civoid uv_loadavg(double avg[3]) {
30e66f31c5Sopenharmony_ci  avg[0] = 0;
31e66f31c5Sopenharmony_ci  avg[1] = 0;
32e66f31c5Sopenharmony_ci  avg[2] = 0;
33e66f31c5Sopenharmony_ci}
34e66f31c5Sopenharmony_ci
35e66f31c5Sopenharmony_ci
36e66f31c5Sopenharmony_ciint uv_exepath(char* buffer, size_t* size) {
37e66f31c5Sopenharmony_ci  char abspath[B_PATH_NAME_LENGTH];
38e66f31c5Sopenharmony_ci  status_t status;
39e66f31c5Sopenharmony_ci  ssize_t abspath_len;
40e66f31c5Sopenharmony_ci
41e66f31c5Sopenharmony_ci  if (buffer == NULL || size == NULL || *size == 0)
42e66f31c5Sopenharmony_ci    return UV_EINVAL;
43e66f31c5Sopenharmony_ci
44e66f31c5Sopenharmony_ci  status = find_path(B_APP_IMAGE_SYMBOL, B_FIND_PATH_IMAGE_PATH, NULL, abspath,
45e66f31c5Sopenharmony_ci                     sizeof(abspath));
46e66f31c5Sopenharmony_ci  if (status != B_OK)
47e66f31c5Sopenharmony_ci    return UV__ERR(status);
48e66f31c5Sopenharmony_ci
49e66f31c5Sopenharmony_ci  abspath_len = uv__strscpy(buffer, abspath, *size);
50e66f31c5Sopenharmony_ci  *size -= 1;
51e66f31c5Sopenharmony_ci  if (abspath_len >= 0 && *size > (size_t)abspath_len)
52e66f31c5Sopenharmony_ci    *size = (size_t)abspath_len;
53e66f31c5Sopenharmony_ci
54e66f31c5Sopenharmony_ci  return 0;
55e66f31c5Sopenharmony_ci}
56e66f31c5Sopenharmony_ci
57e66f31c5Sopenharmony_ci
58e66f31c5Sopenharmony_ciuint64_t uv_get_free_memory(void) {
59e66f31c5Sopenharmony_ci  status_t status;
60e66f31c5Sopenharmony_ci  system_info sinfo;
61e66f31c5Sopenharmony_ci
62e66f31c5Sopenharmony_ci  status = get_system_info(&sinfo);
63e66f31c5Sopenharmony_ci  if (status != B_OK)
64e66f31c5Sopenharmony_ci    return 0;
65e66f31c5Sopenharmony_ci
66e66f31c5Sopenharmony_ci  return (sinfo.max_pages - sinfo.used_pages) * B_PAGE_SIZE;
67e66f31c5Sopenharmony_ci}
68e66f31c5Sopenharmony_ci
69e66f31c5Sopenharmony_ci
70e66f31c5Sopenharmony_ciuint64_t uv_get_total_memory(void) {
71e66f31c5Sopenharmony_ci  status_t status;
72e66f31c5Sopenharmony_ci  system_info sinfo;
73e66f31c5Sopenharmony_ci
74e66f31c5Sopenharmony_ci  status = get_system_info(&sinfo);
75e66f31c5Sopenharmony_ci  if (status != B_OK)
76e66f31c5Sopenharmony_ci    return 0;
77e66f31c5Sopenharmony_ci
78e66f31c5Sopenharmony_ci  return sinfo.max_pages * B_PAGE_SIZE;
79e66f31c5Sopenharmony_ci}
80e66f31c5Sopenharmony_ci
81e66f31c5Sopenharmony_ci
82e66f31c5Sopenharmony_ciuint64_t uv_get_constrained_memory(void) {
83e66f31c5Sopenharmony_ci  return 0;  /* Memory constraints are unknown. */
84e66f31c5Sopenharmony_ci}
85e66f31c5Sopenharmony_ci
86e66f31c5Sopenharmony_ci
87e66f31c5Sopenharmony_ciuint64_t uv_get_available_memory(void) {
88e66f31c5Sopenharmony_ci  return uv_get_free_memory();
89e66f31c5Sopenharmony_ci}
90e66f31c5Sopenharmony_ci
91e66f31c5Sopenharmony_ci
92e66f31c5Sopenharmony_ciint uv_resident_set_memory(size_t* rss) {
93e66f31c5Sopenharmony_ci  area_info area;
94e66f31c5Sopenharmony_ci  ssize_t cookie;
95e66f31c5Sopenharmony_ci  status_t status;
96e66f31c5Sopenharmony_ci  thread_info thread;
97e66f31c5Sopenharmony_ci
98e66f31c5Sopenharmony_ci  status = get_thread_info(find_thread(NULL), &thread);
99e66f31c5Sopenharmony_ci  if (status != B_OK)
100e66f31c5Sopenharmony_ci    return UV__ERR(status);
101e66f31c5Sopenharmony_ci
102e66f31c5Sopenharmony_ci  cookie = 0;
103e66f31c5Sopenharmony_ci  *rss = 0;
104e66f31c5Sopenharmony_ci  while (get_next_area_info(thread.team, &cookie, &area) == B_OK)
105e66f31c5Sopenharmony_ci    *rss += area.ram_size;
106e66f31c5Sopenharmony_ci
107e66f31c5Sopenharmony_ci  return 0;
108e66f31c5Sopenharmony_ci}
109e66f31c5Sopenharmony_ci
110e66f31c5Sopenharmony_ci
111e66f31c5Sopenharmony_ciint uv_uptime(double* uptime) {
112e66f31c5Sopenharmony_ci  /* system_time() returns time since booting in microseconds */
113e66f31c5Sopenharmony_ci  *uptime = (double)system_time() / 1000000;
114e66f31c5Sopenharmony_ci  return 0;
115e66f31c5Sopenharmony_ci}
116e66f31c5Sopenharmony_ci
117e66f31c5Sopenharmony_ci
118e66f31c5Sopenharmony_ciint uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
119e66f31c5Sopenharmony_ci  cpu_topology_node_info* topology_infos;
120e66f31c5Sopenharmony_ci  int i;
121e66f31c5Sopenharmony_ci  status_t status;
122e66f31c5Sopenharmony_ci  system_info system;
123e66f31c5Sopenharmony_ci  uint32_t topology_count;
124e66f31c5Sopenharmony_ci  uint64_t cpuspeed;
125e66f31c5Sopenharmony_ci  uv_cpu_info_t* cpu_info;
126e66f31c5Sopenharmony_ci
127e66f31c5Sopenharmony_ci  if (cpu_infos == NULL || count == NULL)
128e66f31c5Sopenharmony_ci    return UV_EINVAL;
129e66f31c5Sopenharmony_ci
130e66f31c5Sopenharmony_ci  status = get_cpu_topology_info(NULL, &topology_count);
131e66f31c5Sopenharmony_ci  if (status != B_OK)
132e66f31c5Sopenharmony_ci    return UV__ERR(status);
133e66f31c5Sopenharmony_ci
134e66f31c5Sopenharmony_ci  topology_infos = uv__malloc(topology_count * sizeof(*topology_infos));
135e66f31c5Sopenharmony_ci  if (topology_infos == NULL)
136e66f31c5Sopenharmony_ci    return UV_ENOMEM;
137e66f31c5Sopenharmony_ci
138e66f31c5Sopenharmony_ci  status = get_cpu_topology_info(topology_infos, &topology_count);
139e66f31c5Sopenharmony_ci  if (status != B_OK) {
140e66f31c5Sopenharmony_ci    uv__free(topology_infos);
141e66f31c5Sopenharmony_ci    return UV__ERR(status);
142e66f31c5Sopenharmony_ci  }
143e66f31c5Sopenharmony_ci
144e66f31c5Sopenharmony_ci  cpuspeed = 0;
145e66f31c5Sopenharmony_ci  for (i = 0; i < (int)topology_count; i++) {
146e66f31c5Sopenharmony_ci    if (topology_infos[i].type == B_TOPOLOGY_CORE) {
147e66f31c5Sopenharmony_ci      cpuspeed = topology_infos[i].data.core.default_frequency;
148e66f31c5Sopenharmony_ci      break;
149e66f31c5Sopenharmony_ci    }
150e66f31c5Sopenharmony_ci  }
151e66f31c5Sopenharmony_ci
152e66f31c5Sopenharmony_ci  uv__free(topology_infos);
153e66f31c5Sopenharmony_ci
154e66f31c5Sopenharmony_ci  status = get_system_info(&system);
155e66f31c5Sopenharmony_ci  if (status != B_OK)
156e66f31c5Sopenharmony_ci    return UV__ERR(status);
157e66f31c5Sopenharmony_ci
158e66f31c5Sopenharmony_ci  *cpu_infos = uv__calloc(system.cpu_count, sizeof(**cpu_infos));
159e66f31c5Sopenharmony_ci  if (*cpu_infos == NULL)
160e66f31c5Sopenharmony_ci    return UV_ENOMEM;
161e66f31c5Sopenharmony_ci
162e66f31c5Sopenharmony_ci  /* CPU time and model are not exposed by Haiku. */
163e66f31c5Sopenharmony_ci  cpu_info = *cpu_infos;
164e66f31c5Sopenharmony_ci  for (i = 0; i < (int)system.cpu_count; i++) {
165e66f31c5Sopenharmony_ci    cpu_info->model = uv__strdup("unknown");
166e66f31c5Sopenharmony_ci    cpu_info->speed = (int)(cpuspeed / 1000000);
167e66f31c5Sopenharmony_ci    cpu_info++;
168e66f31c5Sopenharmony_ci  }
169e66f31c5Sopenharmony_ci  *count = system.cpu_count;
170e66f31c5Sopenharmony_ci
171e66f31c5Sopenharmony_ci  return 0;
172e66f31c5Sopenharmony_ci}
173