1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2 * Permission is hereby granted, free of charge, to any person obtaining a copy
3 * of this software and associated documentation files (the "Software"), to
4 * deal in the Software without restriction, including without limitation the
5 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 * sell copies of the Software, and to permit persons to whom the Software is
7 * furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in
10 * all copies or substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 #ifndef SUNOS_NO_IFADDRS
36 #include <sys/loadavg.h>
45 #define PORT_FIRED 0x69
46 #define PORT_UNUSED 0x0
47 #define PORT_LOADED 0x99
48 #define PORT_DELETED -1
50 #if (!defined(_LP64)) && (_FILE_OFFSET_BITS - 0 == 64)
51 #define PROCFS_FILE_OFFSET_BITS_HACK 1
52 #undef _FILE_OFFSET_BITS
54 #define PROCFS_FILE_OFFSET_BITS_HACK 0
59 #if (PROCFS_FILE_OFFSET_BITS_HACK - 0 == 1)
60 #define _FILE_OFFSET_BITS 64
64 int uv__platform_loop_init(uv_loop_t* loop, int default_loop) {
66 loop->backend_fd = port_create();
68 if (loop->backend_fd == -1)
71 uv__cloexec(loop->backend_fd, 1);
77 void uv__platform_loop_delete(uv_loop_t* loop) {
78 if (loop->fs_fd != -1) {
83 if (loop->backend_fd != -1) {
84 close(loop->backend_fd);
85 loop->backend_fd = -1;
90 void uv__io_poll(uv_loop_t* loop, int timeout) {
91 struct port_event events[1024];
92 struct port_event* pe;
105 if (loop->nfds == 0) {
106 assert(ngx_queue_empty(&loop->watcher_queue));
110 while (!ngx_queue_empty(&loop->watcher_queue)) {
111 q = ngx_queue_head(&loop->watcher_queue);
115 w = ngx_queue_data(q, uv__io_t, watcher_queue);
116 assert(w->pevents != 0);
118 if (port_associate(loop->backend_fd, PORT_SOURCE_FD, w->fd, w->pevents, 0))
121 w->events = w->pevents;
124 assert(timeout >= -1);
126 count = 48; /* Benchmarks suggest this gives the best throughput. */
130 spec.tv_sec = timeout / 1000;
131 spec.tv_nsec = (timeout % 1000) * 1000000;
134 /* Work around a kernel bug where nfds is not updated. */
135 events[0].portev_source = 0;
139 if (port_getn(loop->backend_fd,
143 timeout == -1 ? NULL : &spec)) {
144 /* Work around another kernel bug: port_getn() may return events even
147 if (errno == EINTR || errno == ETIME)
153 /* Update loop->time unconditionally. It's tempting to skip the update when
154 * timeout == 0 (i.e. non-blocking poll) but there is no guarantee that the
155 * operating system didn't reschedule our process while in the syscall.
157 SAVE_ERRNO(uv__update_time(loop));
159 if (events[0].portev_source == 0) {
170 assert(timeout != -1);
176 for (i = 0; i < nfds; i++) {
178 fd = pe->portev_object;
181 assert((unsigned) fd < loop->nwatchers);
183 w = loop->watchers[fd];
185 /* File descriptor that we've stopped watching, ignore. */
189 w->cb(loop, w, pe->portev_events);
192 /* Events Ports operates in oneshot mode, rearm timer on next run. */
193 if (w->pevents != 0 && ngx_queue_empty(&w->watcher_queue))
194 ngx_queue_insert_tail(&loop->watcher_queue, &w->watcher_queue);
198 if (nfds == ARRAY_SIZE(events) && --count != 0) {
199 /* Poll for more events but don't block this time. */
206 if (saved_errno == ETIME) {
207 assert(timeout != -1);
220 diff = loop->time - base;
221 if (diff >= (uint64_t) timeout)
229 uint64_t uv__hrtime(void) {
235 * We could use a static buffer for the path manipulations that we need outside
236 * of the function, but this function could be called by multiple consumers and
237 * we don't want to potentially create a race condition in the use of snprintf.
239 int uv_exepath(char* buffer, size_t* size) {
249 (void) snprintf(buf, sizeof(buf), "/proc/%lu/path/a.out", (unsigned long) getpid());
250 res = readlink(buf, buffer, *size - 1);
261 uint64_t uv_get_free_memory(void) {
262 return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES);
266 uint64_t uv_get_total_memory(void) {
267 return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES);
271 void uv_loadavg(double avg[3]) {
272 (void) getloadavg(avg, 3);
276 #if defined(PORT_SOURCE_FILE)
278 static void uv__fs_event_rearm(uv_fs_event_t *handle) {
279 if (handle->fd == -1)
282 if (port_associate(handle->loop->fs_fd,
284 (uintptr_t) &handle->fo,
285 FILE_ATTRIB | FILE_MODIFIED,
287 uv__set_sys_error(handle->loop, errno);
289 handle->fd = PORT_LOADED;
293 static void uv__fs_event_read(uv_loop_t* loop,
295 unsigned int revents) {
296 uv_fs_event_t *handle = NULL;
309 * Note that our use of port_getn() here (and not port_get()) is deliberate:
310 * there is a bug in event ports (Sun bug 6456558) whereby a zeroed timeout
311 * causes port_get() to return success instead of ETIME when there aren't
312 * actually any events (!); by using port_getn() in lieu of port_get(),
313 * we can at least workaround the bug by checking for zero returned events
314 * and treating it as we would ETIME.
317 memset(&timeout, 0, sizeof timeout);
318 r = port_getn(loop->fs_fd, &pe, 1, &n, &timeout);
320 while (r == -1 && errno == EINTR);
322 if ((r == -1 && errno == ETIME) || n == 0)
325 handle = (uv_fs_event_t *)pe.portev_user;
326 assert((r == 0) && "unexpected port_get() error");
329 if (pe.portev_events & (FILE_ATTRIB | FILE_MODIFIED))
331 if (pe.portev_events & ~(FILE_ATTRIB | FILE_MODIFIED))
334 handle->fd = PORT_FIRED;
335 handle->cb(handle, NULL, events, 0);
337 while (handle->fd != PORT_DELETED);
339 if (handle != NULL && handle->fd != PORT_DELETED)
340 uv__fs_event_rearm(handle);
344 int uv_fs_event_init(uv_loop_t* loop,
345 uv_fs_event_t* handle,
346 const char* filename,
352 if (loop->fs_fd == -1) {
353 if ((portfd = port_create()) == -1) {
354 uv__set_sys_error(loop, errno);
357 loop->fs_fd = portfd;
361 uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_EVENT);
362 uv__handle_start(handle); /* FIXME shouldn't start automatically */
363 handle->filename = strdup(filename);
364 handle->fd = PORT_UNUSED;
367 memset(&handle->fo, 0, sizeof handle->fo);
368 handle->fo.fo_name = handle->filename;
369 uv__fs_event_rearm(handle);
372 uv__io_init(&loop->fs_event_watcher, uv__fs_event_read, portfd);
373 uv__io_start(loop, &loop->fs_event_watcher, UV__POLLIN);
380 void uv__fs_event_close(uv_fs_event_t* handle) {
381 if (handle->fd == PORT_FIRED || handle->fd == PORT_LOADED) {
382 port_dissociate(handle->loop->fs_fd, PORT_SOURCE_FILE, (uintptr_t)&handle->fo);
384 handle->fd = PORT_DELETED;
385 free(handle->filename);
386 handle->filename = NULL;
387 handle->fo.fo_name = NULL;
388 uv__handle_stop(handle);
391 #else /* !defined(PORT_SOURCE_FILE) */
393 int uv_fs_event_init(uv_loop_t* loop,
394 uv_fs_event_t* handle,
395 const char* filename,
398 uv__set_sys_error(loop, ENOSYS);
403 void uv__fs_event_close(uv_fs_event_t* handle) {
407 #endif /* defined(PORT_SOURCE_FILE) */
410 char** uv_setup_args(int argc, char** argv) {
415 uv_err_t uv_set_process_title(const char* title) {
420 uv_err_t uv_get_process_title(char* buffer, size_t size) {
428 uv_err_t uv_resident_set_memory(size_t* rss) {
433 fd = open("/proc/self/psinfo", O_RDONLY);
435 return uv__new_sys_error(errno);
439 if (read(fd, &psinfo, sizeof(psinfo)) == sizeof(psinfo))
440 *rss = (size_t)psinfo.pr_rssize * 1024;
442 err = uv__new_sys_error(EINVAL);
450 uv_err_t uv_uptime(double* uptime) {
455 long hz = sysconf(_SC_CLK_TCK);
457 if ((kc = kstat_open()) == NULL)
458 return uv__new_sys_error(errno);
460 ksp = kstat_lookup(kc, (char *)"unix", 0, (char *)"system_misc");
462 if (kstat_read(kc, ksp, NULL) == -1) {
465 knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"clk_intr");
466 *uptime = knp->value.ul / hz;
475 uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
480 uv_cpu_info_t* cpu_info;
482 if ((kc = kstat_open()) == NULL) {
483 return uv__new_sys_error(errno);
486 /* Get count of cpus */
488 while ((ksp = kstat_lookup(kc, (char *)"cpu_info", lookup_instance, NULL))) {
492 *cpu_infos = (uv_cpu_info_t*)
493 malloc(lookup_instance * sizeof(uv_cpu_info_t));
495 return uv__new_artificial_error(UV_ENOMEM);
498 *count = lookup_instance;
500 cpu_info = *cpu_infos;
502 while ((ksp = kstat_lookup(kc, (char *)"cpu_info", lookup_instance, NULL))) {
503 if (kstat_read(kc, ksp, NULL) == -1) {
505 cpu_info->model = NULL;
507 knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"clock_MHz");
508 assert(knp->data_type == KSTAT_DATA_INT32 ||
509 knp->data_type == KSTAT_DATA_INT64);
510 cpu_info->speed = (knp->data_type == KSTAT_DATA_INT32) ? knp->value.i32
513 knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"brand");
514 assert(knp->data_type == KSTAT_DATA_STRING);
515 cpu_info->model = strdup(KSTAT_NAMED_STR_PTR(knp));
522 cpu_info = *cpu_infos;
524 while ((ksp = kstat_lookup(kc, (char *)"cpu", lookup_instance, (char *)"sys"))){
526 if (kstat_read(kc, ksp, NULL) == -1) {
527 cpu_info->cpu_times.user = 0;
528 cpu_info->cpu_times.nice = 0;
529 cpu_info->cpu_times.sys = 0;
530 cpu_info->cpu_times.idle = 0;
531 cpu_info->cpu_times.irq = 0;
533 knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"cpu_ticks_user");
534 assert(knp->data_type == KSTAT_DATA_UINT64);
535 cpu_info->cpu_times.user = knp->value.ui64;
537 knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"cpu_ticks_kernel");
538 assert(knp->data_type == KSTAT_DATA_UINT64);
539 cpu_info->cpu_times.sys = knp->value.ui64;
541 knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"cpu_ticks_idle");
542 assert(knp->data_type == KSTAT_DATA_UINT64);
543 cpu_info->cpu_times.idle = knp->value.ui64;
545 knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"intr");
546 assert(knp->data_type == KSTAT_DATA_UINT64);
547 cpu_info->cpu_times.irq = knp->value.ui64;
548 cpu_info->cpu_times.nice = 0;
561 void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count) {
564 for (i = 0; i < count; i++) {
565 free(cpu_infos[i].model);
572 uv_err_t uv_interface_addresses(uv_interface_address_t** addresses,
574 #ifdef SUNOS_NO_IFADDRS
575 return uv__new_artificial_error(UV_ENOSYS);
577 struct ifaddrs *addrs, *ent;
578 char ip[INET6_ADDRSTRLEN];
579 uv_interface_address_t* address;
581 if (getifaddrs(&addrs) != 0) {
582 return uv__new_sys_error(errno);
587 /* Count the number of interfaces */
588 for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
589 if (!(ent->ifa_flags & IFF_UP && ent->ifa_flags & IFF_RUNNING) ||
590 (ent->ifa_addr == NULL) ||
591 (ent->ifa_addr->sa_family == PF_PACKET)) {
598 *addresses = (uv_interface_address_t*)
599 malloc(*count * sizeof(uv_interface_address_t));
601 return uv__new_artificial_error(UV_ENOMEM);
604 address = *addresses;
606 for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
607 memset(&ip, 0, sizeof(ip));
609 if (!(ent->ifa_flags & IFF_UP && ent->ifa_flags & IFF_RUNNING)) {
613 if (ent->ifa_addr == NULL) {
617 address->name = strdup(ent->ifa_name);
619 if (ent->ifa_addr->sa_family == AF_INET6) {
620 address->address.address6 = *((struct sockaddr_in6 *)ent->ifa_addr);
622 address->address.address4 = *((struct sockaddr_in *)ent->ifa_addr);
625 address->is_internal = ent->ifa_flags & IFF_PRIVATE || ent->ifa_flags &
626 IFF_LOOPBACK ? 1 : 0;
634 #endif /* SUNOS_NO_IFADDRS */
638 void uv_free_interface_addresses(uv_interface_address_t* addresses,
642 for (i = 0; i < count; i++) {
643 free(addresses[i].name);