1*5f2f4271Schristos /* Copyright libuv project contributors. All rights reserved.
2*5f2f4271Schristos *
3*5f2f4271Schristos * Permission is hereby granted, free of charge, to any person obtaining a copy
4*5f2f4271Schristos * of this software and associated documentation files (the "Software"), to
5*5f2f4271Schristos * deal in the Software without restriction, including without limitation the
6*5f2f4271Schristos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7*5f2f4271Schristos * sell copies of the Software, and to permit persons to whom the Software is
8*5f2f4271Schristos * furnished to do so, subject to the following conditions:
9*5f2f4271Schristos *
10*5f2f4271Schristos * The above copyright notice and this permission notice shall be included in
11*5f2f4271Schristos * all copies or substantial portions of the Software.
12*5f2f4271Schristos *
13*5f2f4271Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14*5f2f4271Schristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15*5f2f4271Schristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16*5f2f4271Schristos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17*5f2f4271Schristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18*5f2f4271Schristos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19*5f2f4271Schristos * IN THE SOFTWARE.
20*5f2f4271Schristos */
21*5f2f4271Schristos
22*5f2f4271Schristos #define _GNU_SOURCE 1
23*5f2f4271Schristos
24*5f2f4271Schristos #include "uv.h"
25*5f2f4271Schristos #include "internal.h"
26*5f2f4271Schristos
27*5f2f4271Schristos #include <hurd.h>
28*5f2f4271Schristos #include <hurd/process.h>
29*5f2f4271Schristos #include <mach/task_info.h>
30*5f2f4271Schristos #include <mach/vm_statistics.h>
31*5f2f4271Schristos #include <mach/vm_param.h>
32*5f2f4271Schristos
33*5f2f4271Schristos #include <inttypes.h>
34*5f2f4271Schristos #include <stddef.h>
35*5f2f4271Schristos #include <unistd.h>
36*5f2f4271Schristos #include <string.h>
37*5f2f4271Schristos #include <limits.h>
38*5f2f4271Schristos
uv_exepath(char * buffer,size_t * size)39*5f2f4271Schristos int uv_exepath(char* buffer, size_t* size) {
40*5f2f4271Schristos kern_return_t err;
41*5f2f4271Schristos /* XXX in current Hurd, strings are char arrays of 1024 elements */
42*5f2f4271Schristos string_t exepath;
43*5f2f4271Schristos ssize_t copied;
44*5f2f4271Schristos
45*5f2f4271Schristos if (buffer == NULL || size == NULL || *size == 0)
46*5f2f4271Schristos return UV_EINVAL;
47*5f2f4271Schristos
48*5f2f4271Schristos if (*size - 1 > 0) {
49*5f2f4271Schristos /* XXX limited length of buffer in current Hurd, this API will probably
50*5f2f4271Schristos * evolve in the future */
51*5f2f4271Schristos err = proc_get_exe(getproc(), getpid(), exepath);
52*5f2f4271Schristos
53*5f2f4271Schristos if (err)
54*5f2f4271Schristos return UV__ERR(err);
55*5f2f4271Schristos }
56*5f2f4271Schristos
57*5f2f4271Schristos copied = uv__strscpy(buffer, exepath, *size);
58*5f2f4271Schristos
59*5f2f4271Schristos /* do not return error on UV_E2BIG failure */
60*5f2f4271Schristos *size = copied < 0 ? strlen(buffer) : (size_t) copied;
61*5f2f4271Schristos
62*5f2f4271Schristos return 0;
63*5f2f4271Schristos }
64*5f2f4271Schristos
uv_resident_set_memory(size_t * rss)65*5f2f4271Schristos int uv_resident_set_memory(size_t* rss) {
66*5f2f4271Schristos kern_return_t err;
67*5f2f4271Schristos struct task_basic_info bi;
68*5f2f4271Schristos mach_msg_type_number_t count;
69*5f2f4271Schristos
70*5f2f4271Schristos count = TASK_BASIC_INFO_COUNT;
71*5f2f4271Schristos err = task_info(mach_task_self(), TASK_BASIC_INFO,
72*5f2f4271Schristos (task_info_t) &bi, &count);
73*5f2f4271Schristos
74*5f2f4271Schristos if (err)
75*5f2f4271Schristos return UV__ERR(err);
76*5f2f4271Schristos
77*5f2f4271Schristos *rss = bi.resident_size;
78*5f2f4271Schristos
79*5f2f4271Schristos return 0;
80*5f2f4271Schristos }
81*5f2f4271Schristos
uv_get_free_memory(void)82*5f2f4271Schristos uint64_t uv_get_free_memory(void) {
83*5f2f4271Schristos kern_return_t err;
84*5f2f4271Schristos struct vm_statistics vmstats;
85*5f2f4271Schristos
86*5f2f4271Schristos err = vm_statistics(mach_task_self(), &vmstats);
87*5f2f4271Schristos
88*5f2f4271Schristos if (err)
89*5f2f4271Schristos return 0;
90*5f2f4271Schristos
91*5f2f4271Schristos return vmstats.free_count * vm_page_size;
92*5f2f4271Schristos }
93*5f2f4271Schristos
94*5f2f4271Schristos
uv_get_total_memory(void)95*5f2f4271Schristos uint64_t uv_get_total_memory(void) {
96*5f2f4271Schristos kern_return_t err;
97*5f2f4271Schristos host_basic_info_data_t hbi;
98*5f2f4271Schristos mach_msg_type_number_t cnt;
99*5f2f4271Schristos
100*5f2f4271Schristos cnt = HOST_BASIC_INFO_COUNT;
101*5f2f4271Schristos err = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t) &hbi, &cnt);
102*5f2f4271Schristos
103*5f2f4271Schristos if (err)
104*5f2f4271Schristos return 0;
105*5f2f4271Schristos
106*5f2f4271Schristos return hbi.memory_size;
107*5f2f4271Schristos }
108*5f2f4271Schristos
109*5f2f4271Schristos
uv_uptime(double * uptime)110*5f2f4271Schristos int uv_uptime(double* uptime) {
111*5f2f4271Schristos char buf[128];
112*5f2f4271Schristos
113*5f2f4271Schristos /* Try /proc/uptime first */
114*5f2f4271Schristos if (0 == uv__slurp("/proc/uptime", buf, sizeof(buf)))
115*5f2f4271Schristos if (1 == sscanf(buf, "%lf", uptime))
116*5f2f4271Schristos return 0;
117*5f2f4271Schristos
118*5f2f4271Schristos /* Reimplement here code from procfs to calculate uptime if not mounted? */
119*5f2f4271Schristos
120*5f2f4271Schristos return UV__ERR(EIO);
121*5f2f4271Schristos }
122*5f2f4271Schristos
uv_loadavg(double avg[3])123*5f2f4271Schristos void uv_loadavg(double avg[3]) {
124*5f2f4271Schristos char buf[128]; /* Large enough to hold all of /proc/loadavg. */
125*5f2f4271Schristos
126*5f2f4271Schristos if (0 == uv__slurp("/proc/loadavg", buf, sizeof(buf)))
127*5f2f4271Schristos if (3 == sscanf(buf, "%lf %lf %lf", &avg[0], &avg[1], &avg[2]))
128*5f2f4271Schristos return;
129*5f2f4271Schristos
130*5f2f4271Schristos /* Reimplement here code from procfs to calculate loadavg if not mounted? */
131*5f2f4271Schristos }
132*5f2f4271Schristos
133*5f2f4271Schristos
uv_cpu_info(uv_cpu_info_t ** cpu_infos,int * count)134*5f2f4271Schristos int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
135*5f2f4271Schristos kern_return_t err;
136*5f2f4271Schristos host_basic_info_data_t hbi;
137*5f2f4271Schristos mach_msg_type_number_t cnt;
138*5f2f4271Schristos
139*5f2f4271Schristos /* Get count of cpus */
140*5f2f4271Schristos cnt = HOST_BASIC_INFO_COUNT;
141*5f2f4271Schristos err = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t) &hbi, &cnt);
142*5f2f4271Schristos
143*5f2f4271Schristos if (err) {
144*5f2f4271Schristos err = UV__ERR(err);
145*5f2f4271Schristos goto abort;
146*5f2f4271Schristos }
147*5f2f4271Schristos
148*5f2f4271Schristos /* XXX not implemented on the Hurd */
149*5f2f4271Schristos *cpu_infos = uv__calloc(hbi.avail_cpus, sizeof(**cpu_infos));
150*5f2f4271Schristos if (*cpu_infos == NULL) {
151*5f2f4271Schristos err = UV_ENOMEM;
152*5f2f4271Schristos goto abort;
153*5f2f4271Schristos }
154*5f2f4271Schristos
155*5f2f4271Schristos *count = hbi.avail_cpus;
156*5f2f4271Schristos
157*5f2f4271Schristos return 0;
158*5f2f4271Schristos
159*5f2f4271Schristos abort:
160*5f2f4271Schristos *cpu_infos = NULL;
161*5f2f4271Schristos *count = 0;
162*5f2f4271Schristos return err;
163*5f2f4271Schristos }
164*5f2f4271Schristos
uv_get_constrained_memory(void)165*5f2f4271Schristos uint64_t uv_get_constrained_memory(void) {
166*5f2f4271Schristos return 0; /* Memory constraints are unknown. */
167*5f2f4271Schristos }
168