darwin.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 <stdint.h>
26 #include <errno.h>
27 
28 #include <mach/mach.h>
29 #include <mach/mach_time.h>
30 #include <mach-o/dyld.h> /* _NSGetExecutablePath */
31 #include <sys/resource.h>
32 #include <sys/sysctl.h>
33 #include <unistd.h> /* sysconf */
34 
35 
37  loop->cf_state = NULL;
38 
39  if (uv__kqueue_init(loop))
40  return UV__ERR(errno);
41 
42  return 0;
43 }
44 
45 
48 }
49 
50 
52  static mach_timebase_info_data_t info;
53 
54  if ((ACCESS_ONCE(uint32_t, info.numer) == 0 ||
55  ACCESS_ONCE(uint32_t, info.denom) == 0) &&
56  mach_timebase_info(&info) != KERN_SUCCESS)
57  abort();
58 
59  return mach_absolute_time() * info.numer / info.denom;
60 }
61 
62 
63 int uv_exepath(char* buffer, size_t* size) {
64  /* realpath(exepath) may be > PATH_MAX so double it to be on the safe side. */
65  char abspath[PATH_MAX * 2 + 1];
66  char exepath[PATH_MAX + 1];
68  size_t abspath_size;
69 
70  if (buffer == NULL || size == NULL || *size == 0)
71  return UV_EINVAL;
72 
73  exepath_size = sizeof(exepath);
74  if (_NSGetExecutablePath(exepath, &exepath_size))
75  return UV_EIO;
76 
77  if (realpath(exepath, abspath) != abspath)
78  return UV__ERR(errno);
79 
80  abspath_size = strlen(abspath);
81  if (abspath_size == 0)
82  return UV_EIO;
83 
84  *size -= 1;
85  if (*size > abspath_size)
86  *size = abspath_size;
87 
88  memcpy(buffer, abspath, *size);
89  buffer[*size] = '\0';
90 
91  return 0;
92 }
93 
94 
96  vm_statistics_data_t info;
97  mach_msg_type_number_t count = sizeof(info) / sizeof(integer_t);
98 
99  if (host_statistics(mach_host_self(), HOST_VM_INFO,
100  (host_info_t)&info, &count) != KERN_SUCCESS) {
101  return UV_EINVAL; /* FIXME(bnoordhuis) Translate error. */
102  }
103 
104  return (uint64_t) info.free_count * sysconf(_SC_PAGESIZE);
105 }
106 
107 
109  uint64_t info;
110  int which[] = {CTL_HW, HW_MEMSIZE};
111  size_t size = sizeof(info);
112 
113  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
114  return UV__ERR(errno);
115 
116  return (uint64_t) info;
117 }
118 
119 
121  return 0; /* Memory constraints are unknown. */
122 }
123 
124 
125 void uv_loadavg(double avg[3]) {
126  struct loadavg info;
127  size_t size = sizeof(info);
128  int which[] = {CTL_VM, VM_LOADAVG};
129 
130  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) < 0) return;
131 
132  avg[0] = (double) info.ldavg[0] / info.fscale;
133  avg[1] = (double) info.ldavg[1] / info.fscale;
134  avg[2] = (double) info.ldavg[2] / info.fscale;
135 }
136 
137 
138 int uv_resident_set_memory(size_t* rss) {
139  mach_msg_type_number_t count;
140  task_basic_info_data_t info;
141  kern_return_t err;
142 
143  count = TASK_BASIC_INFO_COUNT;
144  err = task_info(mach_task_self(),
145  TASK_BASIC_INFO,
146  (task_info_t) &info,
147  &count);
148  (void) &err;
149  /* task_info(TASK_BASIC_INFO) cannot really fail. Anything other than
150  * KERN_SUCCESS implies a libuv bug.
151  */
152  assert(err == KERN_SUCCESS);
153  *rss = info.resident_size;
154 
155  return 0;
156 }
157 
158 
159 int uv_uptime(double* uptime) {
160  time_t now;
161  struct timeval info;
162  size_t size = sizeof(info);
163  static int which[] = {CTL_KERN, KERN_BOOTTIME};
164 
165  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
166  return UV__ERR(errno);
167 
168  now = time(NULL);
169  *uptime = now - info.tv_sec;
170 
171  return 0;
172 }
173 
174 int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
175  unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
176  multiplier = ((uint64_t)1000L / ticks);
177  char model[512];
178  uint64_t cpuspeed;
179  size_t size;
180  unsigned int i;
181  natural_t numcpus;
182  mach_msg_type_number_t msg_type;
183  processor_cpu_load_info_data_t *info;
184  uv_cpu_info_t* cpu_info;
185 
186  size = sizeof(model);
187  if (sysctlbyname("machdep.cpu.brand_string", &model, &size, NULL, 0) &&
188  sysctlbyname("hw.model", &model, &size, NULL, 0)) {
189  return UV__ERR(errno);
190  }
191 
192  size = sizeof(cpuspeed);
193  if (sysctlbyname("hw.cpufrequency", &cpuspeed, &size, NULL, 0))
194  return UV__ERR(errno);
195 
196  if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numcpus,
197  (processor_info_array_t*)&info,
198  &msg_type) != KERN_SUCCESS) {
199  return UV_EINVAL; /* FIXME(bnoordhuis) Translate error. */
200  }
201 
202  *cpu_infos = uv__malloc(numcpus * sizeof(**cpu_infos));
203  if (!(*cpu_infos)) {
204  vm_deallocate(mach_task_self(), (vm_address_t)info, msg_type);
205  return UV_ENOMEM;
206  }
207 
208  *count = numcpus;
209 
210  for (i = 0; i < numcpus; i++) {
211  cpu_info = &(*cpu_infos)[i];
212 
213  cpu_info->cpu_times.user = (uint64_t)(info[i].cpu_ticks[0]) * multiplier;
214  cpu_info->cpu_times.nice = (uint64_t)(info[i].cpu_ticks[3]) * multiplier;
215  cpu_info->cpu_times.sys = (uint64_t)(info[i].cpu_ticks[1]) * multiplier;
216  cpu_info->cpu_times.idle = (uint64_t)(info[i].cpu_ticks[2]) * multiplier;
217  cpu_info->cpu_times.irq = 0;
218 
219  cpu_info->model = uv__strdup(model);
220  cpu_info->speed = cpuspeed/1000000;
221  }
222  vm_deallocate(mach_task_self(), (vm_address_t)info, msg_type);
223 
224  return 0;
225 }
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
integer_t
int32_t integer_t
Definition: machine.h:68
uv_cpu_times_s::irq
uint64_t irq
Definition: uv.h:1076
uv__platform_loop_init
int uv__platform_loop_init(uv_loop_t *loop)
Definition: darwin.c:36
uv_get_total_memory
uint64_t uv_get_total_memory(void)
Definition: darwin.c:108
uv_exepath
int uv_exepath(char *buffer, size_t *size)
Definition: darwin.c:63
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_get_free_memory
uint64_t uv_get_free_memory(void)
Definition: darwin.c:95
uv__kqueue_init
int uv__kqueue_init(uv_loop_t *loop)
Definition: kqueue.c:51
uv__malloc
void * uv__malloc(size_t size)
Definition: uv-common.c:75
error_ref_leak.err
err
Definition: error_ref_leak.py:35
uv_cpu_info_s::cpu_times
struct uv_cpu_times_s cpu_times
Definition: uv.h:1082
uv_clocktype_t
uv_clocktype_t
Definition: third_party/libuv/src/unix/internal.h:150
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
exepath
static char exepath[1024]
Definition: benchmark-spawn.c:34
uv_loadavg
void uv_loadavg(double avg[3])
Definition: darwin.c:125
uv_cpu_times_s::nice
uint64_t nice
Definition: uv.h:1073
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
ACCESS_ONCE
#define ACCESS_ONCE(type, var)
Definition: third_party/libuv/src/unix/internal.h:81
xds_interop_client.int
int
Definition: xds_interop_client.py:113
uv_resident_set_memory
int uv_resident_set_memory(size_t *rss)
Definition: darwin.c:138
uv__strdup
char * uv__strdup(const char *s)
Definition: uv-common.c:55
uv_get_constrained_memory
uint64_t uv_get_constrained_memory(void)
Definition: darwin.c:120
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
python_utils.jobset.which
def which(filename)
Definition: jobset.py:157
UV__ERR
#define UV__ERR(x)
Definition: errno.h:29
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
uv__fsevents_loop_delete
void uv__fsevents_loop_delete(uv_loop_t *loop)
Definition: fsevents.c:39
uv_cpu_times_s::user
uint64_t user
Definition: uv.h:1072
stdint.h
uv_cpu_info_s::model
char * model
Definition: uv.h:1080
uv.h
exepath_size
static size_t exepath_size
Definition: benchmark-spawn.c:35
uv__platform_loop_delete
void uv__platform_loop_delete(uv_loop_t *loop)
Definition: darwin.c:46
internal.h
timeval::tv_sec
long tv_sec
Definition: setup_once.h:121
uv_uptime
int uv_uptime(double *uptime)
Definition: darwin.c:159
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
uv_cpu_info
int uv_cpu_info(uv_cpu_info_t **cpu_infos, int *count)
Definition: darwin.c:174
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
uv__hrtime
uint64_t uv__hrtime(uv_clocktype_t type)
Definition: darwin.c:51
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
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


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:02