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#define _GNU_SOURCE 1 23e66f31c5Sopenharmony_ci 24e66f31c5Sopenharmony_ci#include "uv.h" 25e66f31c5Sopenharmony_ci#include "internal.h" 26e66f31c5Sopenharmony_ci 27e66f31c5Sopenharmony_ci#include <hurd.h> 28e66f31c5Sopenharmony_ci#include <hurd/process.h> 29e66f31c5Sopenharmony_ci#include <mach/task_info.h> 30e66f31c5Sopenharmony_ci#include <mach/vm_statistics.h> 31e66f31c5Sopenharmony_ci#include <mach/vm_param.h> 32e66f31c5Sopenharmony_ci 33e66f31c5Sopenharmony_ci#include <inttypes.h> 34e66f31c5Sopenharmony_ci#include <stddef.h> 35e66f31c5Sopenharmony_ci#include <unistd.h> 36e66f31c5Sopenharmony_ci#include <string.h> 37e66f31c5Sopenharmony_ci#include <limits.h> 38e66f31c5Sopenharmony_ci 39e66f31c5Sopenharmony_ciint uv_exepath(char* buffer, size_t* size) { 40e66f31c5Sopenharmony_ci kern_return_t err; 41e66f31c5Sopenharmony_ci /* XXX in current Hurd, strings are char arrays of 1024 elements */ 42e66f31c5Sopenharmony_ci string_t exepath; 43e66f31c5Sopenharmony_ci ssize_t copied; 44e66f31c5Sopenharmony_ci 45e66f31c5Sopenharmony_ci if (buffer == NULL || size == NULL || *size == 0) 46e66f31c5Sopenharmony_ci return UV_EINVAL; 47e66f31c5Sopenharmony_ci 48e66f31c5Sopenharmony_ci if (*size - 1 > 0) { 49e66f31c5Sopenharmony_ci /* XXX limited length of buffer in current Hurd, this API will probably 50e66f31c5Sopenharmony_ci * evolve in the future */ 51e66f31c5Sopenharmony_ci err = proc_get_exe(getproc(), getpid(), exepath); 52e66f31c5Sopenharmony_ci 53e66f31c5Sopenharmony_ci if (err) 54e66f31c5Sopenharmony_ci return UV__ERR(err); 55e66f31c5Sopenharmony_ci } 56e66f31c5Sopenharmony_ci 57e66f31c5Sopenharmony_ci copied = uv__strscpy(buffer, exepath, *size); 58e66f31c5Sopenharmony_ci 59e66f31c5Sopenharmony_ci /* do not return error on UV_E2BIG failure */ 60e66f31c5Sopenharmony_ci *size = copied < 0 ? strlen(buffer) : (size_t) copied; 61e66f31c5Sopenharmony_ci 62e66f31c5Sopenharmony_ci return 0; 63e66f31c5Sopenharmony_ci} 64e66f31c5Sopenharmony_ci 65e66f31c5Sopenharmony_ciint uv_resident_set_memory(size_t* rss) { 66e66f31c5Sopenharmony_ci kern_return_t err; 67e66f31c5Sopenharmony_ci struct task_basic_info bi; 68e66f31c5Sopenharmony_ci mach_msg_type_number_t count; 69e66f31c5Sopenharmony_ci 70e66f31c5Sopenharmony_ci count = TASK_BASIC_INFO_COUNT; 71e66f31c5Sopenharmony_ci err = task_info(mach_task_self(), TASK_BASIC_INFO, 72e66f31c5Sopenharmony_ci (task_info_t) &bi, &count); 73e66f31c5Sopenharmony_ci 74e66f31c5Sopenharmony_ci if (err) 75e66f31c5Sopenharmony_ci return UV__ERR(err); 76e66f31c5Sopenharmony_ci 77e66f31c5Sopenharmony_ci *rss = bi.resident_size; 78e66f31c5Sopenharmony_ci 79e66f31c5Sopenharmony_ci return 0; 80e66f31c5Sopenharmony_ci} 81e66f31c5Sopenharmony_ci 82e66f31c5Sopenharmony_ciuint64_t uv_get_free_memory(void) { 83e66f31c5Sopenharmony_ci kern_return_t err; 84e66f31c5Sopenharmony_ci struct vm_statistics vmstats; 85e66f31c5Sopenharmony_ci 86e66f31c5Sopenharmony_ci err = vm_statistics(mach_task_self(), &vmstats); 87e66f31c5Sopenharmony_ci 88e66f31c5Sopenharmony_ci if (err) 89e66f31c5Sopenharmony_ci return 0; 90e66f31c5Sopenharmony_ci 91e66f31c5Sopenharmony_ci return vmstats.free_count * vm_page_size; 92e66f31c5Sopenharmony_ci} 93e66f31c5Sopenharmony_ci 94e66f31c5Sopenharmony_ci 95e66f31c5Sopenharmony_ciuint64_t uv_get_total_memory(void) { 96e66f31c5Sopenharmony_ci kern_return_t err; 97e66f31c5Sopenharmony_ci host_basic_info_data_t hbi; 98e66f31c5Sopenharmony_ci mach_msg_type_number_t cnt; 99e66f31c5Sopenharmony_ci 100e66f31c5Sopenharmony_ci cnt = HOST_BASIC_INFO_COUNT; 101e66f31c5Sopenharmony_ci err = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t) &hbi, &cnt); 102e66f31c5Sopenharmony_ci 103e66f31c5Sopenharmony_ci if (err) 104e66f31c5Sopenharmony_ci return 0; 105e66f31c5Sopenharmony_ci 106e66f31c5Sopenharmony_ci return hbi.memory_size; 107e66f31c5Sopenharmony_ci} 108e66f31c5Sopenharmony_ci 109e66f31c5Sopenharmony_ci 110e66f31c5Sopenharmony_ciint uv_uptime(double* uptime) { 111e66f31c5Sopenharmony_ci char buf[128]; 112e66f31c5Sopenharmony_ci 113e66f31c5Sopenharmony_ci /* Try /proc/uptime first */ 114e66f31c5Sopenharmony_ci if (0 == uv__slurp("/proc/uptime", buf, sizeof(buf))) 115e66f31c5Sopenharmony_ci if (1 == sscanf(buf, "%lf", uptime)) 116e66f31c5Sopenharmony_ci return 0; 117e66f31c5Sopenharmony_ci 118e66f31c5Sopenharmony_ci /* Reimplement here code from procfs to calculate uptime if not mounted? */ 119e66f31c5Sopenharmony_ci 120e66f31c5Sopenharmony_ci return UV__ERR(EIO); 121e66f31c5Sopenharmony_ci} 122e66f31c5Sopenharmony_ci 123e66f31c5Sopenharmony_civoid uv_loadavg(double avg[3]) { 124e66f31c5Sopenharmony_ci char buf[128]; /* Large enough to hold all of /proc/loadavg. */ 125e66f31c5Sopenharmony_ci 126e66f31c5Sopenharmony_ci if (0 == uv__slurp("/proc/loadavg", buf, sizeof(buf))) 127e66f31c5Sopenharmony_ci if (3 == sscanf(buf, "%lf %lf %lf", &avg[0], &avg[1], &avg[2])) 128e66f31c5Sopenharmony_ci return; 129e66f31c5Sopenharmony_ci 130e66f31c5Sopenharmony_ci /* Reimplement here code from procfs to calculate loadavg if not mounted? */ 131e66f31c5Sopenharmony_ci} 132e66f31c5Sopenharmony_ci 133e66f31c5Sopenharmony_ci 134e66f31c5Sopenharmony_ciint uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) { 135e66f31c5Sopenharmony_ci kern_return_t err; 136e66f31c5Sopenharmony_ci host_basic_info_data_t hbi; 137e66f31c5Sopenharmony_ci mach_msg_type_number_t cnt; 138e66f31c5Sopenharmony_ci 139e66f31c5Sopenharmony_ci /* Get count of cpus */ 140e66f31c5Sopenharmony_ci cnt = HOST_BASIC_INFO_COUNT; 141e66f31c5Sopenharmony_ci err = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t) &hbi, &cnt); 142e66f31c5Sopenharmony_ci 143e66f31c5Sopenharmony_ci if (err) { 144e66f31c5Sopenharmony_ci err = UV__ERR(err); 145e66f31c5Sopenharmony_ci goto abort; 146e66f31c5Sopenharmony_ci } 147e66f31c5Sopenharmony_ci 148e66f31c5Sopenharmony_ci /* XXX not implemented on the Hurd */ 149e66f31c5Sopenharmony_ci *cpu_infos = uv__calloc(hbi.avail_cpus, sizeof(**cpu_infos)); 150e66f31c5Sopenharmony_ci if (*cpu_infos == NULL) { 151e66f31c5Sopenharmony_ci err = UV_ENOMEM; 152e66f31c5Sopenharmony_ci goto abort; 153e66f31c5Sopenharmony_ci } 154e66f31c5Sopenharmony_ci 155e66f31c5Sopenharmony_ci *count = hbi.avail_cpus; 156e66f31c5Sopenharmony_ci 157e66f31c5Sopenharmony_ci return 0; 158e66f31c5Sopenharmony_ci 159e66f31c5Sopenharmony_ci abort: 160e66f31c5Sopenharmony_ci *cpu_infos = NULL; 161e66f31c5Sopenharmony_ci *count = 0; 162e66f31c5Sopenharmony_ci return err; 163e66f31c5Sopenharmony_ci} 164e66f31c5Sopenharmony_ci 165e66f31c5Sopenharmony_ciuint64_t uv_get_constrained_memory(void) { 166e66f31c5Sopenharmony_ci return 0; /* Memory constraints are unknown. */ 167e66f31c5Sopenharmony_ci} 168e66f31c5Sopenharmony_ci 169e66f31c5Sopenharmony_ci 170e66f31c5Sopenharmony_ciuint64_t uv_get_available_memory(void) { 171e66f31c5Sopenharmony_ci return uv_get_free_memory(); 172e66f31c5Sopenharmony_ci} 173