netbsd.c
Go to the documentation of this file.
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:
8  *
9  * The above copyright notice and this permission notice shall be included in
10  * all copies or substantial portions of the Software.
11  *
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
18  * IN THE SOFTWARE.
19  */
20 
21 #include "uv.h"
22 #include "internal.h"
23 
24 #include <assert.h>
25 #include <string.h>
26 #include <errno.h>
27 
28 #include <kvm.h>
29 #include <paths.h>
30 #include <unistd.h>
31 #include <time.h>
32 #include <stdlib.h>
33 #include <fcntl.h>
34 
35 #include <sys/resource.h>
36 #include <sys/types.h>
37 #include <sys/sysctl.h>
38 #include <uvm/uvm_extern.h>
39 
40 #include <unistd.h>
41 #include <time.h>
42 
43 
45  return uv__kqueue_init(loop);
46 }
47 
48 
50 }
51 
52 
53 void uv_loadavg(double avg[3]) {
54  struct loadavg info;
55  size_t size = sizeof(info);
56  int which[] = {CTL_VM, VM_LOADAVG};
57 
58  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) == -1) return;
59 
60  avg[0] = (double) info.ldavg[0] / info.fscale;
61  avg[1] = (double) info.ldavg[1] / info.fscale;
62  avg[2] = (double) info.ldavg[2] / info.fscale;
63 }
64 
65 
66 int uv_exepath(char* buffer, size_t* size) {
67  /* Intermediate buffer, retrieving partial path name does not work
68  * As of NetBSD-8(beta), vnode->path translator does not handle files
69  * with longer names than 31 characters.
70  */
71  char int_buf[PATH_MAX];
72  size_t int_size;
73  int mib[4];
74 
75  if (buffer == NULL || size == NULL || *size == 0)
76  return UV_EINVAL;
77 
78  mib[0] = CTL_KERN;
79  mib[1] = KERN_PROC_ARGS;
80  mib[2] = -1;
81  mib[3] = KERN_PROC_PATHNAME;
82  int_size = ARRAY_SIZE(int_buf);
83 
84  if (sysctl(mib, 4, int_buf, &int_size, NULL, 0))
85  return UV__ERR(errno);
86 
87  /* Copy string from the intermediate buffer to outer one with appropriate
88  * length.
89  */
90  /* TODO(bnoordhuis) Check uv__strscpy() return value. */
91  uv__strscpy(buffer, int_buf, *size);
92 
93  /* Set new size. */
94  *size = strlen(buffer);
95 
96  return 0;
97 }
98 
99 
101  struct uvmexp info;
102  size_t size = sizeof(info);
103  int which[] = {CTL_VM, VM_UVMEXP};
104 
105  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
106  return UV__ERR(errno);
107 
108  return (uint64_t) info.free * sysconf(_SC_PAGESIZE);
109 }
110 
111 
113 #if defined(HW_PHYSMEM64)
114  uint64_t info;
115  int which[] = {CTL_HW, HW_PHYSMEM64};
116 #else
117  unsigned int info;
118  int which[] = {CTL_HW, HW_PHYSMEM};
119 #endif
120  size_t size = sizeof(info);
121 
122  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
123  return UV__ERR(errno);
124 
125  return (uint64_t) info;
126 }
127 
128 
130  return 0; /* Memory constraints are unknown. */
131 }
132 
133 
134 int uv_resident_set_memory(size_t* rss) {
135  kvm_t *kd = NULL;
136  struct kinfo_proc2 *kinfo = NULL;
137  pid_t pid;
138  int nprocs;
139  int max_size = sizeof(struct kinfo_proc2);
140  int page_size;
141 
142  page_size = getpagesize();
143  pid = getpid();
144 
145  kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open");
146 
147  if (kd == NULL) goto error;
148 
149  kinfo = kvm_getproc2(kd, KERN_PROC_PID, pid, max_size, &nprocs);
150  if (kinfo == NULL) goto error;
151 
152  *rss = kinfo->p_vm_rssize * page_size;
153 
154  kvm_close(kd);
155 
156  return 0;
157 
158 error:
159  if (kd) kvm_close(kd);
160  return UV_EPERM;
161 }
162 
163 
164 int uv_uptime(double* uptime) {
165  time_t now;
166  struct timeval info;
167  size_t size = sizeof(info);
168  static int which[] = {CTL_KERN, KERN_BOOTTIME};
169 
170  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
171  return UV__ERR(errno);
172 
173  now = time(NULL);
174 
175  *uptime = (double)(now - info.tv_sec);
176  return 0;
177 }
178 
179 
180 int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
181  unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK);
182  unsigned int multiplier = ((uint64_t)1000L / ticks);
183  unsigned int cur = 0;
184  uv_cpu_info_t* cpu_info;
185  u_int64_t* cp_times;
186  char model[512];
187  u_int64_t cpuspeed;
188  int numcpus;
189  size_t size;
190  int i;
191 
192  size = sizeof(model);
193  if (sysctlbyname("machdep.cpu_brand", &model, &size, NULL, 0) &&
194  sysctlbyname("hw.model", &model, &size, NULL, 0)) {
195  return UV__ERR(errno);
196  }
197 
198  size = sizeof(numcpus);
199  if (sysctlbyname("hw.ncpu", &numcpus, &size, NULL, 0))
200  return UV__ERR(errno);
201  *count = numcpus;
202 
203  /* Only i386 and amd64 have machdep.tsc_freq */
204  size = sizeof(cpuspeed);
205  if (sysctlbyname("machdep.tsc_freq", &cpuspeed, &size, NULL, 0))
206  cpuspeed = 0;
207 
208  size = numcpus * CPUSTATES * sizeof(*cp_times);
209  cp_times = uv__malloc(size);
210  if (cp_times == NULL)
211  return UV_ENOMEM;
212 
213  if (sysctlbyname("kern.cp_time", cp_times, &size, NULL, 0))
214  return UV__ERR(errno);
215 
216  *cpu_infos = uv__malloc(numcpus * sizeof(**cpu_infos));
217  if (!(*cpu_infos)) {
218  uv__free(cp_times);
219  uv__free(*cpu_infos);
220  return UV_ENOMEM;
221  }
222 
223  for (i = 0; i < numcpus; i++) {
224  cpu_info = &(*cpu_infos)[i];
225  cpu_info->cpu_times.user = (uint64_t)(cp_times[CP_USER+cur]) * multiplier;
226  cpu_info->cpu_times.nice = (uint64_t)(cp_times[CP_NICE+cur]) * multiplier;
227  cpu_info->cpu_times.sys = (uint64_t)(cp_times[CP_SYS+cur]) * multiplier;
228  cpu_info->cpu_times.idle = (uint64_t)(cp_times[CP_IDLE+cur]) * multiplier;
229  cpu_info->cpu_times.irq = (uint64_t)(cp_times[CP_INTR+cur]) * multiplier;
230  cpu_info->model = uv__strdup(model);
231  cpu_info->speed = (int)(cpuspeed/(uint64_t) 1e6);
232  cur += CPUSTATES;
233  }
234  uv__free(cp_times);
235  return 0;
236 }
237 
238 int uv__random_sysctl(void* buf, size_t len) {
239  static int name[] = {CTL_KERN, KERN_ARND};
240  size_t count, req;
241  unsigned char* p;
242 
243  p = buf;
244  while (len) {
245  req = len < 32 ? len : 32;
246  count = req;
247 
248  if (sysctl(name, ARRAY_SIZE(name), p, &count, NULL, 0) == -1)
249  return UV__ERR(errno);
250 
251  if (count != req)
252  return UV_EIO; /* Can't happen. */
253 
254  p += count;
255  len -= count;
256  }
257 
258  return 0;
259 }
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
uv_cpu_times_s::irq
uint64_t irq
Definition: uv.h:1076
now
static double now(void)
Definition: test/core/fling/client.cc:130
ARRAY_SIZE
#define ARRAY_SIZE(array)
Definition: bloaty.cc:101
uv_cpu_info_s
Definition: uv.h:1079
uv__kqueue_init
int uv__kqueue_init(uv_loop_t *loop)
Definition: kqueue.c:51
uv__random_sysctl
int uv__random_sysctl(void *buf, size_t len)
Definition: netbsd.c:238
uv__platform_loop_delete
void uv__platform_loop_delete(uv_loop_t *loop)
Definition: netbsd.c:49
uv__platform_loop_init
int uv__platform_loop_init(uv_loop_t *loop)
Definition: netbsd.c:44
uv_get_constrained_memory
uint64_t uv_get_constrained_memory(void)
Definition: netbsd.c:129
uv__malloc
void * uv__malloc(size_t size)
Definition: uv-common.c:75
string.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
error
grpc_error_handle error
Definition: retry_filter.cc:499
uv_uptime
int uv_uptime(double *uptime)
Definition: netbsd.c:164
setup.name
name
Definition: setup.py:542
xds_manager.p
p
Definition: xds_manager.py:60
uv_cpu_info_s::cpu_times
struct uv_cpu_times_s cpu_times
Definition: uv.h:1082
uv_cpu_info
int uv_cpu_info(uv_cpu_info_t **cpu_infos, int *count)
Definition: netbsd.c:180
uv_cpu_times_s::nice
uint64_t nice
Definition: uv.h:1073
CP_SYS
#define CP_SYS
Definition: freebsd.c:45
xds_interop_client.int
int
Definition: xds_interop_client.py:113
CPUSTATES
#define CPUSTATES
Definition: freebsd.c:40
CP_IDLE
#define CP_IDLE
Definition: freebsd.c:46
CP_NICE
#define CP_NICE
Definition: freebsd.c:44
req
static uv_connect_t req
Definition: test-connection-fail.c:30
uv_exepath
int uv_exepath(char *buffer, size_t *size)
Definition: netbsd.c:66
uv__strdup
char * uv__strdup(const char *s)
Definition: uv-common.c:55
uv_get_total_memory
uint64_t uv_get_total_memory(void)
Definition: netbsd.c:112
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
python_utils.jobset.which
def which(filename)
Definition: jobset.py:157
uv__free
void uv__free(void *ptr)
Definition: uv-common.c:81
UV__ERR
#define UV__ERR(x)
Definition: errno.h:29
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
uv_cpu_times_s::user
uint64_t user
Definition: uv.h:1072
CP_USER
#define CP_USER
Definition: freebsd.c:43
uv_cpu_info_s::model
char * model
Definition: uv.h:1080
uv.h
memory_diff.cur
def cur
Definition: memory_diff.py:83
internal.h
timeval::tv_sec
long tv_sec
Definition: setup_once.h:121
uv__strscpy
ssize_t uv__strscpy(char *d, const char *s, size_t n)
Definition: strscpy.c:4
uv_get_free_memory
uint64_t uv_get_free_memory(void)
Definition: netbsd.c:100
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
timeval
Definition: setup_once.h:113
L
lua_State * L
Definition: upb/upb/bindings/lua/main.c:35
uv_loop_s
Definition: uv.h:1767
CP_INTR
#define CP_INTR
Definition: freebsd.c:47
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
uv_cpu_info_s::speed
int speed
Definition: uv.h:1081
uv_cpu_times_s::sys
uint64_t sys
Definition: uv.h:1074
errno.h
uv_resident_set_memory
int uv_resident_set_memory(size_t *rss)
Definition: netbsd.c:134
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
ticks
static unsigned long ticks
Definition: benchmark-loop-count.c:30
uv_cpu_times_s::idle
uint64_t idle
Definition: uv.h:1075
uv_loadavg
void uv_loadavg(double avg[3])
Definition: netbsd.c:53


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:42