|
Packit Service |
7c31a4 |
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
|
Packit Service |
7c31a4 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
Packit Service |
7c31a4 |
* of this software and associated documentation files (the "Software"), to
|
|
Packit Service |
7c31a4 |
* deal in the Software without restriction, including without limitation the
|
|
Packit Service |
7c31a4 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
Packit Service |
7c31a4 |
* sell copies of the Software, and to permit persons to whom the Software is
|
|
Packit Service |
7c31a4 |
* furnished to do so, subject to the following conditions:
|
|
Packit Service |
7c31a4 |
*
|
|
Packit Service |
7c31a4 |
* The above copyright notice and this permission notice shall be included in
|
|
Packit Service |
7c31a4 |
* all copies or substantial portions of the Software.
|
|
Packit Service |
7c31a4 |
*
|
|
Packit Service |
7c31a4 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
Packit Service |
7c31a4 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
Packit Service |
7c31a4 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
Packit Service |
7c31a4 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
Packit Service |
7c31a4 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
Packit Service |
7c31a4 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
Packit Service |
7c31a4 |
* IN THE SOFTWARE.
|
|
Packit Service |
7c31a4 |
*/
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
#include "uv.h"
|
|
Packit Service |
7c31a4 |
#include "internal.h"
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
#include <assert.h>
|
|
Packit Service |
7c31a4 |
#include <stdint.h>
|
|
Packit Service |
7c31a4 |
#include <errno.h>
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
e2ebee |
#include <dlfcn.h>
|
|
Packit Service |
7c31a4 |
#include <mach/mach.h>
|
|
Packit Service |
7c31a4 |
#include <mach/mach_time.h>
|
|
Packit Service |
7c31a4 |
#include <mach-o/dyld.h> /* _NSGetExecutablePath */
|
|
Packit Service |
7c31a4 |
#include <sys/resource.h>
|
|
Packit Service |
7c31a4 |
#include <sys/sysctl.h>
|
|
Packit Service |
7c31a4 |
#include <unistd.h> /* sysconf */
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
e2ebee |
#if !TARGET_OS_IPHONE
|
|
Packit Service |
e2ebee |
#include "darwin-stub.h"
|
|
Packit Service |
e2ebee |
#endif
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
static uv_once_t once = UV_ONCE_INIT;
|
|
Packit Service |
e2ebee |
static uint64_t (*time_func)(void);
|
|
Packit Service |
e2ebee |
static mach_timebase_info_data_t timebase;
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
typedef unsigned char UInt8;
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
int uv__platform_loop_init(uv_loop_t* loop) {
|
|
Packit Service |
7c31a4 |
loop->cf_state = NULL;
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
if (uv__kqueue_init(loop))
|
|
Packit Service |
7c31a4 |
return UV__ERR(errno);
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
return 0;
|
|
Packit Service |
7c31a4 |
}
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
void uv__platform_loop_delete(uv_loop_t* loop) {
|
|
Packit Service |
7c31a4 |
uv__fsevents_loop_delete(loop);
|
|
Packit Service |
7c31a4 |
}
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
e2ebee |
static void uv__hrtime_init_once(void) {
|
|
Packit Service |
e2ebee |
if (KERN_SUCCESS != mach_timebase_info(&timebase))
|
|
Packit Service |
7c31a4 |
abort();
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
e2ebee |
time_func = (uint64_t (*)(void)) dlsym(RTLD_DEFAULT, "mach_continuous_time");
|
|
Packit Service |
e2ebee |
if (time_func == NULL)
|
|
Packit Service |
e2ebee |
time_func = mach_absolute_time;
|
|
Packit Service |
e2ebee |
}
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
uint64_t uv__hrtime(uv_clocktype_t type) {
|
|
Packit Service |
e2ebee |
uv_once(&once, uv__hrtime_init_once);
|
|
Packit Service |
e2ebee |
return time_func() * timebase.numer / timebase.denom;
|
|
Packit Service |
7c31a4 |
}
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
int uv_exepath(char* buffer, size_t* size) {
|
|
Packit Service |
7c31a4 |
/* realpath(exepath) may be > PATH_MAX so double it to be on the safe side. */
|
|
Packit Service |
7c31a4 |
char abspath[PATH_MAX * 2 + 1];
|
|
Packit Service |
7c31a4 |
char exepath[PATH_MAX + 1];
|
|
Packit Service |
7c31a4 |
uint32_t exepath_size;
|
|
Packit Service |
7c31a4 |
size_t abspath_size;
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
if (buffer == NULL || size == NULL || *size == 0)
|
|
Packit Service |
7c31a4 |
return UV_EINVAL;
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
exepath_size = sizeof(exepath);
|
|
Packit Service |
7c31a4 |
if (_NSGetExecutablePath(exepath, &exepath_size))
|
|
Packit Service |
7c31a4 |
return UV_EIO;
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
if (realpath(exepath, abspath) != abspath)
|
|
Packit Service |
7c31a4 |
return UV__ERR(errno);
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
abspath_size = strlen(abspath);
|
|
Packit Service |
7c31a4 |
if (abspath_size == 0)
|
|
Packit Service |
7c31a4 |
return UV_EIO;
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
*size -= 1;
|
|
Packit Service |
7c31a4 |
if (*size > abspath_size)
|
|
Packit Service |
7c31a4 |
*size = abspath_size;
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
memcpy(buffer, abspath, *size);
|
|
Packit Service |
7c31a4 |
buffer[*size] = '\0';
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
return 0;
|
|
Packit Service |
7c31a4 |
}
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
uint64_t uv_get_free_memory(void) {
|
|
Packit Service |
7c31a4 |
vm_statistics_data_t info;
|
|
Packit Service |
7c31a4 |
mach_msg_type_number_t count = sizeof(info) / sizeof(integer_t);
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
if (host_statistics(mach_host_self(), HOST_VM_INFO,
|
|
Packit Service |
7c31a4 |
(host_info_t)&info, &count) != KERN_SUCCESS) {
|
|
Packit Service |
7c31a4 |
return UV_EINVAL; /* FIXME(bnoordhuis) Translate error. */
|
|
Packit Service |
7c31a4 |
}
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
return (uint64_t) info.free_count * sysconf(_SC_PAGESIZE);
|
|
Packit Service |
7c31a4 |
}
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
uint64_t uv_get_total_memory(void) {
|
|
Packit Service |
7c31a4 |
uint64_t info;
|
|
Packit Service |
7c31a4 |
int which[] = {CTL_HW, HW_MEMSIZE};
|
|
Packit Service |
7c31a4 |
size_t size = sizeof(info);
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
|
|
Packit Service |
7c31a4 |
return UV__ERR(errno);
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
return (uint64_t) info;
|
|
Packit Service |
7c31a4 |
}
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
uint64_t uv_get_constrained_memory(void) {
|
|
Packit Service |
7c31a4 |
return 0; /* Memory constraints are unknown. */
|
|
Packit Service |
7c31a4 |
}
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
void uv_loadavg(double avg[3]) {
|
|
Packit Service |
7c31a4 |
struct loadavg info;
|
|
Packit Service |
7c31a4 |
size_t size = sizeof(info);
|
|
Packit Service |
7c31a4 |
int which[] = {CTL_VM, VM_LOADAVG};
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) < 0) return;
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
avg[0] = (double) info.ldavg[0] / info.fscale;
|
|
Packit Service |
7c31a4 |
avg[1] = (double) info.ldavg[1] / info.fscale;
|
|
Packit Service |
7c31a4 |
avg[2] = (double) info.ldavg[2] / info.fscale;
|
|
Packit Service |
7c31a4 |
}
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
int uv_resident_set_memory(size_t* rss) {
|
|
Packit Service |
7c31a4 |
mach_msg_type_number_t count;
|
|
Packit Service |
7c31a4 |
task_basic_info_data_t info;
|
|
Packit Service |
7c31a4 |
kern_return_t err;
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
count = TASK_BASIC_INFO_COUNT;
|
|
Packit Service |
7c31a4 |
err = task_info(mach_task_self(),
|
|
Packit Service |
7c31a4 |
TASK_BASIC_INFO,
|
|
Packit Service |
7c31a4 |
(task_info_t) &info,
|
|
Packit Service |
7c31a4 |
&count);
|
|
Packit Service |
7c31a4 |
(void) &err;
|
|
Packit Service |
7c31a4 |
/* task_info(TASK_BASIC_INFO) cannot really fail. Anything other than
|
|
Packit Service |
7c31a4 |
* KERN_SUCCESS implies a libuv bug.
|
|
Packit Service |
7c31a4 |
*/
|
|
Packit Service |
7c31a4 |
assert(err == KERN_SUCCESS);
|
|
Packit Service |
7c31a4 |
*rss = info.resident_size;
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
return 0;
|
|
Packit Service |
7c31a4 |
}
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
int uv_uptime(double* uptime) {
|
|
Packit Service |
7c31a4 |
time_t now;
|
|
Packit Service |
7c31a4 |
struct timeval info;
|
|
Packit Service |
7c31a4 |
size_t size = sizeof(info);
|
|
Packit Service |
7c31a4 |
static int which[] = {CTL_KERN, KERN_BOOTTIME};
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
|
|
Packit Service |
7c31a4 |
return UV__ERR(errno);
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
now = time(NULL);
|
|
Packit Service |
7c31a4 |
*uptime = now - info.tv_sec;
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
return 0;
|
|
Packit Service |
7c31a4 |
}
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
e2ebee |
static int uv__get_cpu_speed(uint64_t* speed) {
|
|
Packit Service |
e2ebee |
/* IOKit */
|
|
Packit Service |
e2ebee |
void (*pIOObjectRelease)(io_object_t);
|
|
Packit Service |
e2ebee |
kern_return_t (*pIOMasterPort)(mach_port_t, mach_port_t*);
|
|
Packit Service |
e2ebee |
CFMutableDictionaryRef (*pIOServiceMatching)(const char*);
|
|
Packit Service |
e2ebee |
kern_return_t (*pIOServiceGetMatchingServices)(mach_port_t,
|
|
Packit Service |
e2ebee |
CFMutableDictionaryRef,
|
|
Packit Service |
e2ebee |
io_iterator_t*);
|
|
Packit Service |
e2ebee |
io_service_t (*pIOIteratorNext)(io_iterator_t);
|
|
Packit Service |
e2ebee |
CFTypeRef (*pIORegistryEntryCreateCFProperty)(io_registry_entry_t,
|
|
Packit Service |
e2ebee |
CFStringRef,
|
|
Packit Service |
e2ebee |
CFAllocatorRef,
|
|
Packit Service |
e2ebee |
IOOptionBits);
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
/* CoreFoundation */
|
|
Packit Service |
e2ebee |
CFStringRef (*pCFStringCreateWithCString)(CFAllocatorRef,
|
|
Packit Service |
e2ebee |
const char*,
|
|
Packit Service |
e2ebee |
CFStringEncoding);
|
|
Packit Service |
e2ebee |
CFStringEncoding (*pCFStringGetSystemEncoding)(void);
|
|
Packit Service |
e2ebee |
UInt8 *(*pCFDataGetBytePtr)(CFDataRef);
|
|
Packit Service |
e2ebee |
CFIndex (*pCFDataGetLength)(CFDataRef);
|
|
Packit Service |
e2ebee |
void (*pCFDataGetBytes)(CFDataRef, CFRange, UInt8*);
|
|
Packit Service |
e2ebee |
void (*pCFRelease)(CFTypeRef);
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
void* core_foundation_handle;
|
|
Packit Service |
e2ebee |
void* iokit_handle;
|
|
Packit Service |
e2ebee |
int err;
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
kern_return_t kr;
|
|
Packit Service |
e2ebee |
mach_port_t mach_port;
|
|
Packit Service |
e2ebee |
io_iterator_t it;
|
|
Packit Service |
e2ebee |
io_object_t service;
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
mach_port = 0;
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
err = UV_ENOENT;
|
|
Packit Service |
e2ebee |
core_foundation_handle = dlopen("/System/Library/Frameworks/"
|
|
Packit Service |
e2ebee |
"CoreFoundation.framework/"
|
|
Packit Service |
e2ebee |
"Versions/A/CoreFoundation",
|
|
Packit Service |
e2ebee |
RTLD_LAZY | RTLD_LOCAL);
|
|
Packit Service |
e2ebee |
iokit_handle = dlopen("/System/Library/Frameworks/IOKit.framework/"
|
|
Packit Service |
e2ebee |
"Versions/A/IOKit",
|
|
Packit Service |
e2ebee |
RTLD_LAZY | RTLD_LOCAL);
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
if (core_foundation_handle == NULL || iokit_handle == NULL)
|
|
Packit Service |
e2ebee |
goto out;
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
#define V(handle, symbol) \
|
|
Packit Service |
e2ebee |
do { \
|
|
Packit Service |
e2ebee |
*(void **)(&p ## symbol) = dlsym((handle), #symbol); \
|
|
Packit Service |
e2ebee |
if (p ## symbol == NULL) \
|
|
Packit Service |
e2ebee |
goto out; \
|
|
Packit Service |
e2ebee |
} \
|
|
Packit Service |
e2ebee |
while (0)
|
|
Packit Service |
e2ebee |
V(iokit_handle, IOMasterPort);
|
|
Packit Service |
e2ebee |
V(iokit_handle, IOServiceMatching);
|
|
Packit Service |
e2ebee |
V(iokit_handle, IOServiceGetMatchingServices);
|
|
Packit Service |
e2ebee |
V(iokit_handle, IOIteratorNext);
|
|
Packit Service |
e2ebee |
V(iokit_handle, IOObjectRelease);
|
|
Packit Service |
e2ebee |
V(iokit_handle, IORegistryEntryCreateCFProperty);
|
|
Packit Service |
e2ebee |
V(core_foundation_handle, CFStringCreateWithCString);
|
|
Packit Service |
e2ebee |
V(core_foundation_handle, CFStringGetSystemEncoding);
|
|
Packit Service |
e2ebee |
V(core_foundation_handle, CFDataGetBytePtr);
|
|
Packit Service |
e2ebee |
V(core_foundation_handle, CFDataGetLength);
|
|
Packit Service |
e2ebee |
V(core_foundation_handle, CFDataGetBytes);
|
|
Packit Service |
e2ebee |
V(core_foundation_handle, CFRelease);
|
|
Packit Service |
e2ebee |
#undef V
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
#define S(s) pCFStringCreateWithCString(NULL, (s), kCFStringEncodingUTF8)
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
kr = pIOMasterPort(MACH_PORT_NULL, &mach_port);
|
|
Packit Service |
e2ebee |
assert(kr == KERN_SUCCESS);
|
|
Packit Service |
e2ebee |
CFMutableDictionaryRef classes_to_match
|
|
Packit Service |
e2ebee |
= pIOServiceMatching("IOPlatformDevice");
|
|
Packit Service |
e2ebee |
kr = pIOServiceGetMatchingServices(mach_port, classes_to_match, &it);
|
|
Packit Service |
e2ebee |
assert(kr == KERN_SUCCESS);
|
|
Packit Service |
e2ebee |
service = pIOIteratorNext(it);
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
CFStringRef device_type_str = S("device_type");
|
|
Packit Service |
e2ebee |
CFStringRef clock_frequency_str = S("clock-frequency");
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
while (service != 0) {
|
|
Packit Service |
e2ebee |
CFDataRef data;
|
|
Packit Service |
e2ebee |
data = pIORegistryEntryCreateCFProperty(service,
|
|
Packit Service |
e2ebee |
device_type_str,
|
|
Packit Service |
e2ebee |
NULL,
|
|
Packit Service |
e2ebee |
0);
|
|
Packit Service |
e2ebee |
if (data) {
|
|
Packit Service |
e2ebee |
const UInt8* raw = pCFDataGetBytePtr(data);
|
|
Packit Service |
e2ebee |
if (strncmp((char*)raw, "cpu", 3) == 0 ||
|
|
Packit Service |
e2ebee |
strncmp((char*)raw, "processor", 9) == 0) {
|
|
Packit Service |
e2ebee |
CFDataRef freq_ref;
|
|
Packit Service |
e2ebee |
freq_ref = pIORegistryEntryCreateCFProperty(service,
|
|
Packit Service |
e2ebee |
clock_frequency_str,
|
|
Packit Service |
e2ebee |
NULL,
|
|
Packit Service |
e2ebee |
0);
|
|
Packit Service |
e2ebee |
if (freq_ref) {
|
|
Packit Service |
e2ebee |
uint32_t freq;
|
|
Packit Service |
e2ebee |
CFIndex len = pCFDataGetLength(freq_ref);
|
|
Packit Service |
e2ebee |
CFRange range;
|
|
Packit Service |
e2ebee |
range.location = 0;
|
|
Packit Service |
e2ebee |
range.length = len;
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
pCFDataGetBytes(freq_ref, range, (UInt8*)&freq);
|
|
Packit Service |
e2ebee |
*speed = freq;
|
|
Packit Service |
e2ebee |
pCFRelease(freq_ref);
|
|
Packit Service |
e2ebee |
pCFRelease(data);
|
|
Packit Service |
e2ebee |
break;
|
|
Packit Service |
e2ebee |
}
|
|
Packit Service |
e2ebee |
}
|
|
Packit Service |
e2ebee |
pCFRelease(data);
|
|
Packit Service |
e2ebee |
}
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
service = pIOIteratorNext(it);
|
|
Packit Service |
e2ebee |
}
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
pIOObjectRelease(it);
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
err = 0;
|
|
Packit Service |
e2ebee |
out:
|
|
Packit Service |
e2ebee |
if (core_foundation_handle != NULL)
|
|
Packit Service |
e2ebee |
dlclose(core_foundation_handle);
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
if (iokit_handle != NULL)
|
|
Packit Service |
e2ebee |
dlclose(iokit_handle);
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
mach_port_deallocate(mach_task_self(), mach_port);
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
e2ebee |
return err;
|
|
Packit Service |
e2ebee |
}
|
|
Packit Service |
e2ebee |
|
|
Packit Service |
7c31a4 |
int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
|
|
Packit Service |
7c31a4 |
unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
|
|
Packit Service |
7c31a4 |
multiplier = ((uint64_t)1000L / ticks);
|
|
Packit Service |
7c31a4 |
char model[512];
|
|
Packit Service |
7c31a4 |
size_t size;
|
|
Packit Service |
7c31a4 |
unsigned int i;
|
|
Packit Service |
7c31a4 |
natural_t numcpus;
|
|
Packit Service |
7c31a4 |
mach_msg_type_number_t msg_type;
|
|
Packit Service |
7c31a4 |
processor_cpu_load_info_data_t *info;
|
|
Packit Service |
7c31a4 |
uv_cpu_info_t* cpu_info;
|
|
Packit Service |
e2ebee |
uint64_t cpuspeed;
|
|
Packit Service |
e2ebee |
int err;
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
size = sizeof(model);
|
|
Packit Service |
7c31a4 |
if (sysctlbyname("machdep.cpu.brand_string", &model, &size, NULL, 0) &&
|
|
Packit Service |
7c31a4 |
sysctlbyname("hw.model", &model, &size, NULL, 0)) {
|
|
Packit Service |
7c31a4 |
return UV__ERR(errno);
|
|
Packit Service |
7c31a4 |
}
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
e2ebee |
err = uv__get_cpu_speed(&cpuspeed);
|
|
Packit Service |
e2ebee |
if (err < 0)
|
|
Packit Service |
e2ebee |
return err;
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numcpus,
|
|
Packit Service |
7c31a4 |
(processor_info_array_t*)&info,
|
|
Packit Service |
7c31a4 |
&msg_type) != KERN_SUCCESS) {
|
|
Packit Service |
7c31a4 |
return UV_EINVAL; /* FIXME(bnoordhuis) Translate error. */
|
|
Packit Service |
7c31a4 |
}
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
*cpu_infos = uv__malloc(numcpus * sizeof(**cpu_infos));
|
|
Packit Service |
7c31a4 |
if (!(*cpu_infos)) {
|
|
Packit Service |
7c31a4 |
vm_deallocate(mach_task_self(), (vm_address_t)info, msg_type);
|
|
Packit Service |
7c31a4 |
return UV_ENOMEM;
|
|
Packit Service |
7c31a4 |
}
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
*count = numcpus;
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
for (i = 0; i < numcpus; i++) {
|
|
Packit Service |
7c31a4 |
cpu_info = &(*cpu_infos)[i];
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
cpu_info->cpu_times.user = (uint64_t)(info[i].cpu_ticks[0]) * multiplier;
|
|
Packit Service |
7c31a4 |
cpu_info->cpu_times.nice = (uint64_t)(info[i].cpu_ticks[3]) * multiplier;
|
|
Packit Service |
7c31a4 |
cpu_info->cpu_times.sys = (uint64_t)(info[i].cpu_ticks[1]) * multiplier;
|
|
Packit Service |
7c31a4 |
cpu_info->cpu_times.idle = (uint64_t)(info[i].cpu_ticks[2]) * multiplier;
|
|
Packit Service |
7c31a4 |
cpu_info->cpu_times.irq = 0;
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
cpu_info->model = uv__strdup(model);
|
|
Packit Service |
7c31a4 |
cpu_info->speed = cpuspeed/1000000;
|
|
Packit Service |
7c31a4 |
}
|
|
Packit Service |
7c31a4 |
vm_deallocate(mach_task_self(), (vm_address_t)info, msg_type);
|
|
Packit Service |
7c31a4 |
|
|
Packit Service |
7c31a4 |
return 0;
|
|
Packit Service |
7c31a4 |
}
|