xref: /third_party/node/deps/uv/src/unix/haiku.c (revision 1cb0ef41)
1/* Copyright libuv project contributors. All rights reserved.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21
22#include "uv.h"
23#include "internal.h"
24
25#include <FindDirectory.h> /* find_path() */
26#include <OS.h>
27
28
29void uv_loadavg(double avg[3]) {
30  avg[0] = 0;
31  avg[1] = 0;
32  avg[2] = 0;
33}
34
35
36int uv_exepath(char* buffer, size_t* size) {
37  char abspath[B_PATH_NAME_LENGTH];
38  status_t status;
39  ssize_t abspath_len;
40
41  if (buffer == NULL || size == NULL || *size == 0)
42    return UV_EINVAL;
43
44  status = find_path(B_APP_IMAGE_SYMBOL, B_FIND_PATH_IMAGE_PATH, NULL, abspath,
45                     sizeof(abspath));
46  if (status != B_OK)
47    return UV__ERR(status);
48
49  abspath_len = uv__strscpy(buffer, abspath, *size);
50  *size -= 1;
51  if (abspath_len >= 0 && *size > (size_t)abspath_len)
52    *size = (size_t)abspath_len;
53
54  return 0;
55}
56
57
58uint64_t uv_get_free_memory(void) {
59  status_t status;
60  system_info sinfo;
61
62  status = get_system_info(&sinfo);
63  if (status != B_OK)
64    return 0;
65
66  return (sinfo.max_pages - sinfo.used_pages) * B_PAGE_SIZE;
67}
68
69
70uint64_t uv_get_total_memory(void) {
71  status_t status;
72  system_info sinfo;
73
74  status = get_system_info(&sinfo);
75  if (status != B_OK)
76    return 0;
77
78  return sinfo.max_pages * B_PAGE_SIZE;
79}
80
81
82uint64_t uv_get_constrained_memory(void) {
83  return 0;  /* Memory constraints are unknown. */
84}
85
86
87int uv_resident_set_memory(size_t* rss) {
88  area_info area;
89  ssize_t cookie;
90  status_t status;
91  thread_info thread;
92
93  status = get_thread_info(find_thread(NULL), &thread);
94  if (status != B_OK)
95    return UV__ERR(status);
96
97  cookie = 0;
98  *rss = 0;
99  while (get_next_area_info(thread.team, &cookie, &area) == B_OK)
100    *rss += area.ram_size;
101
102  return 0;
103}
104
105
106int uv_uptime(double* uptime) {
107  /* system_time() returns time since booting in microseconds */
108  *uptime = (double)system_time() / 1000000;
109  return 0;
110}
111
112
113int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
114  cpu_topology_node_info* topology_infos;
115  int i;
116  status_t status;
117  system_info system;
118  uint32_t topology_count;
119  uint64_t cpuspeed;
120  uv_cpu_info_t* cpu_info;
121
122  if (cpu_infos == NULL || count == NULL)
123    return UV_EINVAL;
124
125  status = get_cpu_topology_info(NULL, &topology_count);
126  if (status != B_OK)
127    return UV__ERR(status);
128
129  topology_infos = uv__malloc(topology_count * sizeof(*topology_infos));
130  if (topology_infos == NULL)
131    return UV_ENOMEM;
132
133  status = get_cpu_topology_info(topology_infos, &topology_count);
134  if (status != B_OK) {
135    uv__free(topology_infos);
136    return UV__ERR(status);
137  }
138
139  cpuspeed = 0;
140  for (i = 0; i < (int)topology_count; i++) {
141    if (topology_infos[i].type == B_TOPOLOGY_CORE) {
142      cpuspeed = topology_infos[i].data.core.default_frequency;
143      break;
144    }
145  }
146
147  uv__free(topology_infos);
148
149  status = get_system_info(&system);
150  if (status != B_OK)
151    return UV__ERR(status);
152
153  *cpu_infos = uv__calloc(system.cpu_count, sizeof(**cpu_infos));
154  if (*cpu_infos == NULL)
155    return UV_ENOMEM;
156
157  /* CPU time and model are not exposed by Haiku. */
158  cpu_info = *cpu_infos;
159  for (i = 0; i < (int)system.cpu_count; i++) {
160    cpu_info->model = uv__strdup("unknown");
161    cpu_info->speed = (int)(cpuspeed / 1000000);
162    cpu_info++;
163  }
164  *count = system.cpu_count;
165
166  return 0;
167}
168