xref: /libuv/src/unix/haiku.c (revision 988d225c)
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 
uv_loadavg(double avg[3])29 void uv_loadavg(double avg[3]) {
30   avg[0] = 0;
31   avg[1] = 0;
32   avg[2] = 0;
33 }
34 
35 
uv_exepath(char * buffer,size_t * size)36 int 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 
uv_get_free_memory(void)58 uint64_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 
uv_get_total_memory(void)70 uint64_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 
uv_get_constrained_memory(void)82 uint64_t uv_get_constrained_memory(void) {
83   return 0;  /* Memory constraints are unknown. */
84 }
85 
86 
uv_get_available_memory(void)87 uint64_t uv_get_available_memory(void) {
88   return uv_get_free_memory();
89 }
90 
91 
uv_resident_set_memory(size_t * rss)92 int uv_resident_set_memory(size_t* rss) {
93   area_info area;
94   ssize_t cookie;
95   status_t status;
96   thread_info thread;
97 
98   status = get_thread_info(find_thread(NULL), &thread);
99   if (status != B_OK)
100     return UV__ERR(status);
101 
102   cookie = 0;
103   *rss = 0;
104   while (get_next_area_info(thread.team, &cookie, &area) == B_OK)
105     *rss += area.ram_size;
106 
107   return 0;
108 }
109 
110 
uv_uptime(double * uptime)111 int uv_uptime(double* uptime) {
112   /* system_time() returns time since booting in microseconds */
113   *uptime = (double)system_time() / 1000000;
114   return 0;
115 }
116 
117 
uv_cpu_info(uv_cpu_info_t ** cpu_infos,int * count)118 int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
119   cpu_topology_node_info* topology_infos;
120   int i;
121   status_t status;
122   system_info system;
123   uint32_t topology_count;
124   uint64_t cpuspeed;
125   uv_cpu_info_t* cpu_info;
126 
127   if (cpu_infos == NULL || count == NULL)
128     return UV_EINVAL;
129 
130   status = get_cpu_topology_info(NULL, &topology_count);
131   if (status != B_OK)
132     return UV__ERR(status);
133 
134   topology_infos = uv__malloc(topology_count * sizeof(*topology_infos));
135   if (topology_infos == NULL)
136     return UV_ENOMEM;
137 
138   status = get_cpu_topology_info(topology_infos, &topology_count);
139   if (status != B_OK) {
140     uv__free(topology_infos);
141     return UV__ERR(status);
142   }
143 
144   cpuspeed = 0;
145   for (i = 0; i < (int)topology_count; i++) {
146     if (topology_infos[i].type == B_TOPOLOGY_CORE) {
147       cpuspeed = topology_infos[i].data.core.default_frequency;
148       break;
149     }
150   }
151 
152   uv__free(topology_infos);
153 
154   status = get_system_info(&system);
155   if (status != B_OK)
156     return UV__ERR(status);
157 
158   *cpu_infos = uv__calloc(system.cpu_count, sizeof(**cpu_infos));
159   if (*cpu_infos == NULL)
160     return UV_ENOMEM;
161 
162   /* CPU time and model are not exposed by Haiku. */
163   cpu_info = *cpu_infos;
164   for (i = 0; i < (int)system.cpu_count; i++) {
165     cpu_info->model = uv__strdup("unknown");
166     cpu_info->speed = (int)(cpuspeed / 1000000);
167     cpu_info++;
168   }
169   *count = system.cpu_count;
170 
171   return 0;
172 }
173