2011-06-17 03:27:52 +00:00
|
|
|
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "uv.h"
|
2012-02-22 15:52:38 +00:00
|
|
|
#include "../internal.h"
|
2011-06-17 03:27:52 +00:00
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2011-12-13 09:29:27 +00:00
|
|
|
#include <stdio.h>
|
2011-09-20 04:45:51 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
2011-12-13 09:29:27 +00:00
|
|
|
#include <net/if.h>
|
|
|
|
|
#include <sys/param.h>
|
2011-10-01 17:48:42 +00:00
|
|
|
#include <sys/sysinfo.h>
|
2011-07-13 21:46:15 +00:00
|
|
|
#include <unistd.h>
|
2011-12-16 14:50:19 +00:00
|
|
|
#include <fcntl.h>
|
2011-06-17 03:27:52 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
|
2012-02-17 22:13:10 +00:00
|
|
|
#define HAVE_IFADDRS_H 1
|
|
|
|
|
#ifdef __UCLIBC__
|
|
|
|
|
# if __UCLIBC_MAJOR__ < 0 || __UCLIBC_MINOR__ < 9 || __UCLIBC_SUBLEVEL__ < 32
|
|
|
|
|
# undef HAVE_IFADDRS_H
|
|
|
|
|
# endif
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_IFADDRS_H
|
|
|
|
|
# include <ifaddrs.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-07-01 13:51:24 +00:00
|
|
|
#undef NANOSEC
|
2012-06-07 13:40:14 +00:00
|
|
|
#define NANOSEC ((uint64_t) 1e9)
|
2011-07-01 13:51:24 +00:00
|
|
|
|
2012-03-19 14:40:50 +00:00
|
|
|
/* This is rather annoying: CLOCK_BOOTTIME lives in <linux/time.h> but we can't
|
|
|
|
|
* include that file because it conflicts with <time.h>. We'll just have to
|
|
|
|
|
* define it ourselves.
|
|
|
|
|
*/
|
|
|
|
|
#ifndef CLOCK_BOOTTIME
|
|
|
|
|
# define CLOCK_BOOTTIME 7
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-12-13 09:29:27 +00:00
|
|
|
static char buf[MAXPATHLEN + 1];
|
|
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
|
char *str;
|
|
|
|
|
size_t len;
|
|
|
|
|
} process_title;
|
|
|
|
|
|
2012-08-14 15:00:25 +00:00
|
|
|
static void read_models(unsigned int numcpus, uv_cpu_info_t* ci);
|
|
|
|
|
static void read_speeds(unsigned int numcpus, uv_cpu_info_t* ci);
|
|
|
|
|
static void read_times(unsigned int numcpus, uv_cpu_info_t* ci);
|
|
|
|
|
static unsigned long read_cpufreq(unsigned int cpunum);
|
|
|
|
|
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2012-08-17 12:48:51 +00:00
|
|
|
int uv__platform_loop_init(uv_loop_t* loop, int default_loop) {
|
|
|
|
|
loop->inotify_watchers = NULL;
|
|
|
|
|
loop->inotify_fd = -1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void uv__platform_loop_delete(uv_loop_t* loop) {
|
|
|
|
|
if (loop->inotify_fd == -1) return;
|
|
|
|
|
uv__io_stop(loop, &loop->inotify_read_watcher);
|
|
|
|
|
close(loop->inotify_fd);
|
|
|
|
|
loop->inotify_fd = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-06-28 12:26:18 +00:00
|
|
|
uint64_t uv_hrtime() {
|
2011-06-17 03:27:52 +00:00
|
|
|
struct timespec ts;
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
2012-08-17 18:24:24 +00:00
|
|
|
return (((uint64_t) ts.tv_sec) * NANOSEC + ts.tv_nsec);
|
2011-06-17 03:27:52 +00:00
|
|
|
}
|
2011-06-17 11:13:18 +00:00
|
|
|
|
2011-12-16 19:14:16 +00:00
|
|
|
|
2011-10-01 17:48:42 +00:00
|
|
|
void uv_loadavg(double avg[3]) {
|
|
|
|
|
struct sysinfo info;
|
|
|
|
|
|
|
|
|
|
if (sysinfo(&info) < 0) return;
|
|
|
|
|
|
|
|
|
|
avg[0] = (double) info.loads[0] / 65536.0;
|
|
|
|
|
avg[1] = (double) info.loads[1] / 65536.0;
|
|
|
|
|
avg[2] = (double) info.loads[2] / 65536.0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-17 11:13:18 +00:00
|
|
|
|
2011-06-28 12:26:18 +00:00
|
|
|
int uv_exepath(char* buffer, size_t* size) {
|
2012-02-21 12:26:42 +00:00
|
|
|
ssize_t n;
|
|
|
|
|
|
2011-06-17 11:13:18 +00:00
|
|
|
if (!buffer || !size) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-21 12:26:42 +00:00
|
|
|
n = readlink("/proc/self/exe", buffer, *size - 1);
|
|
|
|
|
if (n <= 0) return -1;
|
|
|
|
|
buffer[n] = '\0';
|
|
|
|
|
*size = n;
|
|
|
|
|
|
2011-06-17 11:13:18 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2011-09-20 04:45:51 +00:00
|
|
|
|
2011-12-16 19:14:16 +00:00
|
|
|
|
2011-10-21 17:00:41 +00:00
|
|
|
uint64_t uv_get_free_memory(void) {
|
|
|
|
|
return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES);
|
2011-10-01 08:45:42 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-16 19:14:16 +00:00
|
|
|
|
2011-10-21 17:00:41 +00:00
|
|
|
uint64_t uv_get_total_memory(void) {
|
|
|
|
|
return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES);
|
2011-10-01 08:45:42 +00:00
|
|
|
}
|
2011-09-20 04:45:51 +00:00
|
|
|
|
2011-12-13 09:29:27 +00:00
|
|
|
|
|
|
|
|
char** uv_setup_args(int argc, char** argv) {
|
|
|
|
|
char **new_argv;
|
|
|
|
|
char **new_env;
|
|
|
|
|
size_t size;
|
|
|
|
|
int envc;
|
|
|
|
|
char *s;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (envc = 0; environ[envc]; envc++);
|
|
|
|
|
|
|
|
|
|
s = envc ? environ[envc - 1] : argv[argc - 1];
|
|
|
|
|
|
|
|
|
|
process_title.str = argv[0];
|
|
|
|
|
process_title.len = s + strlen(s) + 1 - argv[0];
|
|
|
|
|
|
|
|
|
|
size = process_title.len;
|
|
|
|
|
size += (argc + 1) * sizeof(char **);
|
|
|
|
|
size += (envc + 1) * sizeof(char **);
|
|
|
|
|
|
|
|
|
|
if ((s = (char *) malloc(size)) == NULL) {
|
|
|
|
|
process_title.str = NULL;
|
|
|
|
|
process_title.len = 0;
|
|
|
|
|
return argv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new_argv = (char **) s;
|
|
|
|
|
new_env = new_argv + argc + 1;
|
|
|
|
|
s = (char *) (new_env + envc + 1);
|
|
|
|
|
memcpy(s, process_title.str, process_title.len);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
|
new_argv[i] = s + (argv[i] - argv[0]);
|
|
|
|
|
new_argv[argc] = NULL;
|
|
|
|
|
|
|
|
|
|
s += environ[0] - argv[0];
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < envc; i++)
|
|
|
|
|
new_env[i] = s + (environ[i] - environ[0]);
|
|
|
|
|
new_env[envc] = NULL;
|
|
|
|
|
|
|
|
|
|
environ = new_env;
|
|
|
|
|
return new_argv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uv_err_t uv_set_process_title(const char* title) {
|
|
|
|
|
/* No need to terminate, last char is always '\0'. */
|
|
|
|
|
if (process_title.len)
|
|
|
|
|
strncpy(process_title.str, title, process_title.len - 1);
|
|
|
|
|
|
|
|
|
|
return uv_ok_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uv_err_t uv_get_process_title(char* buffer, size_t size) {
|
|
|
|
|
if (process_title.str) {
|
|
|
|
|
strncpy(buffer, process_title.str, size);
|
|
|
|
|
} else {
|
|
|
|
|
if (size > 0) {
|
|
|
|
|
buffer[0] = '\0';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return uv_ok_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uv_err_t uv_resident_set_memory(size_t* rss) {
|
|
|
|
|
FILE* f;
|
|
|
|
|
int itmp;
|
|
|
|
|
char ctmp;
|
2011-12-16 19:39:35 +00:00
|
|
|
unsigned int utmp;
|
2011-12-13 09:29:27 +00:00
|
|
|
size_t page_size = getpagesize();
|
|
|
|
|
char *cbuf;
|
|
|
|
|
int foundExeEnd;
|
|
|
|
|
|
|
|
|
|
f = fopen("/proc/self/stat", "r");
|
|
|
|
|
if (!f) return uv__new_sys_error(errno);
|
|
|
|
|
|
|
|
|
|
/* PID */
|
|
|
|
|
if (fscanf(f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
|
|
|
|
|
/* Exec file */
|
|
|
|
|
cbuf = buf;
|
|
|
|
|
foundExeEnd = 0;
|
|
|
|
|
if (fscanf (f, "%c", cbuf++) == 0) goto error;
|
|
|
|
|
while (1) {
|
|
|
|
|
if (fscanf(f, "%c", cbuf) == 0) goto error;
|
|
|
|
|
if (*cbuf == ')') {
|
|
|
|
|
foundExeEnd = 1;
|
|
|
|
|
} else if (foundExeEnd && *cbuf == ' ') {
|
|
|
|
|
*cbuf = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cbuf++;
|
|
|
|
|
}
|
|
|
|
|
/* State */
|
|
|
|
|
if (fscanf (f, "%c ", &ctmp) == 0) goto error; /* coverity[secure_coding] */
|
|
|
|
|
/* Parent process */
|
|
|
|
|
if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
|
|
|
|
|
/* Process group */
|
|
|
|
|
if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
|
|
|
|
|
/* Session id */
|
|
|
|
|
if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
|
|
|
|
|
/* TTY */
|
|
|
|
|
if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
|
|
|
|
|
/* TTY owner process group */
|
|
|
|
|
if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
|
|
|
|
|
/* Flags */
|
2011-12-16 19:39:35 +00:00
|
|
|
if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
|
2011-12-13 09:29:27 +00:00
|
|
|
/* Minor faults (no memory page) */
|
2011-12-16 19:39:35 +00:00
|
|
|
if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
|
2011-12-13 09:29:27 +00:00
|
|
|
/* Minor faults, children */
|
2011-12-16 19:39:35 +00:00
|
|
|
if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
|
2011-12-13 09:29:27 +00:00
|
|
|
/* Major faults (memory page faults) */
|
2011-12-16 19:39:35 +00:00
|
|
|
if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
|
2011-12-13 09:29:27 +00:00
|
|
|
/* Major faults, children */
|
2011-12-16 19:39:35 +00:00
|
|
|
if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
|
2011-12-13 09:29:27 +00:00
|
|
|
/* utime */
|
|
|
|
|
if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
|
|
|
|
|
/* stime */
|
|
|
|
|
if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
|
|
|
|
|
/* utime, children */
|
|
|
|
|
if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
|
|
|
|
|
/* stime, children */
|
|
|
|
|
if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
|
|
|
|
|
/* jiffies remaining in current time slice */
|
|
|
|
|
if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
|
|
|
|
|
/* 'nice' value */
|
|
|
|
|
if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
|
|
|
|
|
/* jiffies until next timeout */
|
2011-12-16 19:39:35 +00:00
|
|
|
if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
|
2011-12-13 09:29:27 +00:00
|
|
|
/* jiffies until next SIGALRM */
|
2011-12-16 19:39:35 +00:00
|
|
|
if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
|
2011-12-13 09:29:27 +00:00
|
|
|
/* start time (jiffies since system boot) */
|
|
|
|
|
if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
|
|
|
|
|
|
|
|
|
|
/* Virtual memory size */
|
2011-12-16 19:39:35 +00:00
|
|
|
if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
|
2011-12-13 09:29:27 +00:00
|
|
|
|
|
|
|
|
/* Resident set size */
|
2011-12-16 19:39:35 +00:00
|
|
|
if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
|
|
|
|
|
*rss = (size_t) utmp * page_size;
|
2011-12-13 09:29:27 +00:00
|
|
|
|
|
|
|
|
/* rlim */
|
2011-12-16 19:39:35 +00:00
|
|
|
if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
|
2011-12-13 09:29:27 +00:00
|
|
|
/* Start of text */
|
2011-12-16 19:39:35 +00:00
|
|
|
if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
|
2011-12-13 09:29:27 +00:00
|
|
|
/* End of text */
|
2011-12-16 19:39:35 +00:00
|
|
|
if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
|
2011-12-13 09:29:27 +00:00
|
|
|
/* Start of stack */
|
2011-12-16 19:39:35 +00:00
|
|
|
if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
|
2011-12-13 09:29:27 +00:00
|
|
|
|
|
|
|
|
fclose (f);
|
|
|
|
|
return uv_ok_;
|
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
fclose (f);
|
|
|
|
|
return uv__new_sys_error(errno);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uv_err_t uv_uptime(double* uptime) {
|
2012-03-19 14:40:50 +00:00
|
|
|
static volatile int no_clock_boottime;
|
2011-12-13 09:29:27 +00:00
|
|
|
struct timespec now;
|
2012-03-19 14:40:50 +00:00
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
/* Try CLOCK_BOOTTIME first, fall back to CLOCK_MONOTONIC if not available
|
|
|
|
|
* (pre-2.6.39 kernels). CLOCK_MONOTONIC doesn't increase when the system
|
|
|
|
|
* is suspended.
|
|
|
|
|
*/
|
|
|
|
|
if (no_clock_boottime) {
|
|
|
|
|
retry: r = clock_gettime(CLOCK_MONOTONIC, &now);
|
2011-12-13 09:29:27 +00:00
|
|
|
}
|
2012-03-19 14:40:50 +00:00
|
|
|
else if ((r = clock_gettime(CLOCK_BOOTTIME, &now)) && errno == EINVAL) {
|
|
|
|
|
no_clock_boottime = 1;
|
|
|
|
|
goto retry;
|
2011-12-13 09:29:27 +00:00
|
|
|
}
|
2012-03-19 14:40:50 +00:00
|
|
|
|
|
|
|
|
if (r)
|
|
|
|
|
return uv__new_sys_error(errno);
|
|
|
|
|
|
|
|
|
|
*uptime = now.tv_sec;
|
|
|
|
|
*uptime += (double)now.tv_nsec / 1000000000.0;
|
2011-12-13 09:29:27 +00:00
|
|
|
return uv_ok_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
|
2012-08-14 15:00:25 +00:00
|
|
|
unsigned int numcpus;
|
|
|
|
|
uv_cpu_info_t* ci;
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2012-08-14 15:00:25 +00:00
|
|
|
*cpu_infos = NULL;
|
|
|
|
|
*count = 0;
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2012-08-14 15:00:25 +00:00
|
|
|
numcpus = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
|
assert(numcpus != (unsigned int) -1);
|
|
|
|
|
assert(numcpus != 0);
|
|
|
|
|
|
|
|
|
|
ci = calloc(numcpus, sizeof(*ci));
|
|
|
|
|
if (ci == NULL)
|
|
|
|
|
return uv__new_sys_error(ENOMEM);
|
|
|
|
|
|
|
|
|
|
read_speeds(numcpus, ci);
|
|
|
|
|
read_models(numcpus, ci);
|
|
|
|
|
read_times(numcpus, ci);
|
|
|
|
|
|
|
|
|
|
*cpu_infos = ci;
|
2011-12-13 09:29:27 +00:00
|
|
|
*count = numcpus;
|
|
|
|
|
|
2012-08-14 15:00:25 +00:00
|
|
|
return uv_ok_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void read_speeds(unsigned int numcpus, uv_cpu_info_t* ci) {
|
|
|
|
|
unsigned int num;
|
|
|
|
|
|
|
|
|
|
for (num = 0; num < numcpus; num++)
|
|
|
|
|
ci[num].speed = read_cpufreq(num) / 1000;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void read_models(unsigned int numcpus, uv_cpu_info_t* ci) {
|
|
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
|
static const char marker[] = "model name\t: ";
|
|
|
|
|
#elif defined(__arm__)
|
|
|
|
|
static const char marker[] = "Processor\t: ";
|
|
|
|
|
#elif defined(__mips__)
|
|
|
|
|
static const char marker[] = "cpu model\t\t: ";
|
|
|
|
|
#else
|
|
|
|
|
# warning uv_cpu_info() is not supported on this architecture.
|
|
|
|
|
static const char marker[] = "(dummy)";
|
|
|
|
|
#endif
|
|
|
|
|
unsigned int num;
|
|
|
|
|
char buf[1024];
|
|
|
|
|
char* model;
|
|
|
|
|
FILE* fp;
|
|
|
|
|
|
|
|
|
|
fp = fopen("/proc/cpuinfo", "r");
|
|
|
|
|
if (fp == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
num = 0;
|
|
|
|
|
|
|
|
|
|
while (fgets(buf, sizeof(buf), fp)) {
|
|
|
|
|
if (num >= numcpus)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (strncmp(buf, marker, sizeof(marker) - 1))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
model = buf + sizeof(marker) - 1;
|
|
|
|
|
model = strndup(model, strlen(model) - 1); /* strip newline */
|
|
|
|
|
ci[num++].model = model;
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void read_times(unsigned int numcpus, uv_cpu_info_t* ci) {
|
|
|
|
|
unsigned long clock_ticks;
|
|
|
|
|
struct uv_cpu_times_s ts;
|
|
|
|
|
unsigned long user;
|
|
|
|
|
unsigned long nice;
|
|
|
|
|
unsigned long sys;
|
|
|
|
|
unsigned long idle;
|
|
|
|
|
unsigned long dummy;
|
|
|
|
|
unsigned long irq;
|
|
|
|
|
unsigned int num;
|
|
|
|
|
unsigned int len;
|
|
|
|
|
char buf[1024];
|
|
|
|
|
FILE* fp;
|
|
|
|
|
|
|
|
|
|
clock_ticks = sysconf(_SC_CLK_TCK);
|
|
|
|
|
assert(clock_ticks != (unsigned long) -1);
|
|
|
|
|
assert(clock_ticks != 0);
|
|
|
|
|
|
|
|
|
|
fp = fopen("/proc/stat", "r");
|
|
|
|
|
if (fp == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!fgets(buf, sizeof(buf), fp))
|
|
|
|
|
abort();
|
|
|
|
|
|
|
|
|
|
num = 0;
|
|
|
|
|
|
|
|
|
|
while (fgets(buf, sizeof(buf), fp)) {
|
|
|
|
|
if (num >= numcpus)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (strncmp(buf, "cpu", 3))
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* skip "cpu<num> " marker */
|
|
|
|
|
{
|
|
|
|
|
unsigned int n = num;
|
|
|
|
|
for (len = sizeof("cpu0"); n /= 10; len++);
|
|
|
|
|
assert(sscanf(buf, "cpu%u ", &n) == 1 && n == num);
|
2011-12-13 09:29:27 +00:00
|
|
|
}
|
2012-08-14 15:00:25 +00:00
|
|
|
|
|
|
|
|
/* Line contains user, nice, system, idle, iowait, irq, softirq, steal,
|
|
|
|
|
* guest, guest_nice but we're only interested in the first four + irq.
|
|
|
|
|
*
|
|
|
|
|
* Don't use %*s to skip fields or %ll to read straight into the uint64_t
|
|
|
|
|
* fields, they're not allowed in C89 mode.
|
|
|
|
|
*/
|
|
|
|
|
if (6 != sscanf(buf + len,
|
|
|
|
|
"%lu %lu %lu %lu %lu %lu",
|
|
|
|
|
&user,
|
|
|
|
|
&nice,
|
|
|
|
|
&sys,
|
|
|
|
|
&idle,
|
|
|
|
|
&dummy,
|
|
|
|
|
&irq))
|
|
|
|
|
abort();
|
|
|
|
|
|
|
|
|
|
ts.user = clock_ticks * user;
|
|
|
|
|
ts.nice = clock_ticks * nice;
|
|
|
|
|
ts.sys = clock_ticks * sys;
|
|
|
|
|
ts.idle = clock_ticks * idle;
|
|
|
|
|
ts.irq = clock_ticks * irq;
|
|
|
|
|
ci[num++].cpu_times = ts;
|
2011-12-13 09:29:27 +00:00
|
|
|
}
|
2012-08-14 15:00:25 +00:00
|
|
|
fclose(fp);
|
|
|
|
|
}
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2012-08-14 15:00:25 +00:00
|
|
|
|
|
|
|
|
static unsigned long read_cpufreq(unsigned int cpunum) {
|
|
|
|
|
unsigned long val;
|
|
|
|
|
char buf[1024];
|
|
|
|
|
FILE* fp;
|
|
|
|
|
|
|
|
|
|
snprintf(buf,
|
|
|
|
|
sizeof(buf),
|
|
|
|
|
"/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq",
|
|
|
|
|
cpunum);
|
|
|
|
|
|
|
|
|
|
fp = fopen(buf, "r");
|
|
|
|
|
if (fp == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
val = 0;
|
|
|
|
|
fscanf(fp, "%lu", &val);
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
|
|
return val;
|
2011-12-13 09:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count) {
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
|
free(cpu_infos[i].model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(cpu_infos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uv_err_t uv_interface_addresses(uv_interface_address_t** addresses,
|
|
|
|
|
int* count) {
|
2012-02-17 22:13:10 +00:00
|
|
|
#ifndef HAVE_IFADDRS_H
|
|
|
|
|
return uv__new_artificial_error(UV_ENOSYS);
|
|
|
|
|
#else
|
2011-12-13 09:29:27 +00:00
|
|
|
struct ifaddrs *addrs, *ent;
|
|
|
|
|
char ip[INET6_ADDRSTRLEN];
|
|
|
|
|
uv_interface_address_t* address;
|
|
|
|
|
|
|
|
|
|
if (getifaddrs(&addrs) != 0) {
|
|
|
|
|
return uv__new_sys_error(errno);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*count = 0;
|
|
|
|
|
|
|
|
|
|
/* Count the number of interfaces */
|
|
|
|
|
for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
|
|
|
|
|
if (!(ent->ifa_flags & IFF_UP && ent->ifa_flags & IFF_RUNNING) ||
|
|
|
|
|
(ent->ifa_addr == NULL) ||
|
|
|
|
|
(ent->ifa_addr->sa_family == PF_PACKET)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(*count)++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*addresses = (uv_interface_address_t*)
|
|
|
|
|
malloc(*count * sizeof(uv_interface_address_t));
|
|
|
|
|
if (!(*addresses)) {
|
|
|
|
|
return uv__new_artificial_error(UV_ENOMEM);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
address = *addresses;
|
|
|
|
|
|
|
|
|
|
for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
|
|
|
|
|
bzero(&ip, sizeof (ip));
|
|
|
|
|
if (!(ent->ifa_flags & IFF_UP && ent->ifa_flags & IFF_RUNNING)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ent->ifa_addr == NULL) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* On Linux getifaddrs returns information related to the raw underlying
|
|
|
|
|
* devices. We're not interested in this information.
|
|
|
|
|
*/
|
|
|
|
|
if (ent->ifa_addr->sa_family == PF_PACKET) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
address->name = strdup(ent->ifa_name);
|
|
|
|
|
|
|
|
|
|
if (ent->ifa_addr->sa_family == AF_INET6) {
|
|
|
|
|
address->address.address6 = *((struct sockaddr_in6 *)ent->ifa_addr);
|
|
|
|
|
} else {
|
|
|
|
|
address->address.address4 = *((struct sockaddr_in *)ent->ifa_addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
address->is_internal = ent->ifa_flags & IFF_LOOPBACK ? 1 : 0;
|
|
|
|
|
|
|
|
|
|
address++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
freeifaddrs(addrs);
|
|
|
|
|
|
|
|
|
|
return uv_ok_;
|
2012-02-17 22:13:10 +00:00
|
|
|
#endif
|
2011-12-13 09:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void uv_free_interface_addresses(uv_interface_address_t* addresses,
|
|
|
|
|
int count) {
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
|
free(addresses[i].name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(addresses);
|
|
|
|
|
}
|