-
Notifications
You must be signed in to change notification settings - Fork 3
/
libtop.h
453 lines (371 loc) · 10.8 KB
/
libtop.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* Copyright (c) 2002-2004, 2019 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* The contents of this file constitute Original Code as defined in and
* are subject to the Apple Public Source License Version 1.1 (the
* "License"). You may not use this file except in compliance with the
* License. Please obtain a copy of the License at
* http://www.apple.com/publicsource and read it before using this file.
*
* This Original Code and all software distributed under the License are
* distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include <mach/boolean.h>
#include <mach/host_info.h>
#include <mach/task_info.h>
#include <mach/vm_types.h>
#include <stdarg.h>
#include <sys/sysctl.h>
#include <sys/time.h>
__BEGIN_DECLS
/*
* Flags for determining whether to collect memory region information on a
* per-process basis, used byt libtop_preg().
*/
typedef enum {
/*
* Collect memory region information iff libtop_sample()'s a_reg
* parameter is TRUE.
*/
LIBTOP_PREG_default = 0,
/* Do not collect memory region information. */
LIBTOP_PREG_off,
/* Always collect memory region information. */
LIBTOP_PREG_on
} libtop_preg_t;
typedef struct libtop_i64 {
uint64_t accumulator;
int last_value;
} libtop_i64_t;
typedef struct libtop_i64_values {
libtop_i64_t i64;
uint64_t now;
uint64_t began;
uint64_t previous;
} libtop_i64_values_t;
/*
* Type used for specifying a printing function that is called when an error
* occurs. libtop does not print a '\n' at the end of the string, so it is
* up to the printing function to add it if desired.
*/
typedef boolean_t libtop_print_t(void *a_user_data, const char *a_format, ...);
/*
* General sample information.
*
* Fields prefix meanings:
*
* b_ : Value for first sample.
* p_ : Value for previous sample (same as b_ if p_seq is 0).
*/
typedef struct {
/*
* Sample sequence number, incremented for every sample. The first
* sample has a sequence number of 1.
*/
uint32_t seq;
/* Number of processes. */
uint32_t nprocs;
/* CPU loads. */
host_cpu_load_info_data_t cpu;
host_cpu_load_info_data_t b_cpu;
host_cpu_load_info_data_t p_cpu;
/* Load averages for 1, 5, and 15 minutes. */
float loadavg[3];
/* Start time, previous sample time, and current sample time. */
struct timeval time;
struct timeval b_time;
struct timeval p_time;
/* Total number of threads. */
uint32_t threads;
/* VM page size. */
vm_size_t pagesize;
/* Physical memory size. */
uint64_t memsize;
/* VM statistics. */
vm_statistics64_data_t vm_stat;
vm_statistics64_data_t b_vm_stat;
vm_statistics64_data_t p_vm_stat;
boolean_t purgeable_is_valid;
/* Swap usage */
struct xsw_usage xsu;
boolean_t xsu_is_valid;
/* Total number of memory regions. */
uint32_t reg;
/* Total shared, private, virtual sizes. */
uint64_t rshrd;
uint64_t rprvt;
uint64_t vsize;
/* Total private resident memory used by frameworks. */
uint64_t fw_private;
/* Total virtual memory used by frameworks. */
uint64_t fw_vsize;
/* Number of frameworks. */
uint32_t fw_count;
/* Code size of frameworks. */
uint64_t fw_code;
/* Data size of frameworks. */
uint64_t fw_data;
/* Linkedit size of frameworks. */
uint64_t fw_linkedit;
#define LIBTOP_STATE_MAX 7
#define LIBTOP_NSTATES (LIBTOP_STATE_MAX + 1)
#define LIBTOP_STATE_MAXLEN 8
int state_breakdown[LIBTOP_NSTATES];
/* Network statistics. */
uint64_t net_ipackets;
uint64_t b_net_ipackets;
uint64_t p_net_ipackets;
uint64_t net_opackets;
uint64_t b_net_opackets;
uint64_t p_net_opackets;
uint64_t net_ibytes;
uint64_t b_net_ibytes;
uint64_t p_net_ibytes;
uint64_t net_obytes;
uint64_t b_net_obytes;
uint64_t p_net_obytes;
/* Disk statistics. */
uint64_t disk_rops;
uint64_t b_disk_rops;
uint64_t p_disk_rops;
uint64_t disk_wops;
uint64_t b_disk_wops;
uint64_t p_disk_wops;
uint64_t disk_rbytes;
uint64_t b_disk_rbytes;
uint64_t p_disk_rbytes;
uint64_t disk_wbytes;
uint64_t b_disk_wbytes;
uint64_t p_disk_wbytes;
uint64_t pages_stolen;
/* Extended statistics. */
uint64_t timens;
uint64_t b_timens;
uint64_t p_timens;
} libtop_tsamp_t;
/*
* Process sample information.
*
* Fields prefix meanings:
*
* b_ : Value for first sample.
* p_ : Value for previous sample (invalid if p_seq is 0).
*/
typedef struct libtop_psamp_s libtop_psamp_t;
struct libtop_psamp_s {
uid_t uid;
pid_t pid;
pid_t ppid;
gid_t pgrp;
/* Memory statistics. */
uint64_t rsize;
uint64_t vsize;
uint64_t rprvt;
uint64_t vprvt;
uint64_t rshrd;
uint64_t fw_private;
uint64_t empty;
uint32_t reg;
uint32_t p_reg;
uint64_t p_rsize;
uint64_t p_vprvt;
uint64_t p_vsize;
uint64_t p_rprvt;
uint64_t p_rshrd;
uint64_t p_empty;
int32_t jetsam_priority;
int32_t b_jetsam_priority;
int32_t p_jetsam_priority;
/* Physical/purgeable memory statistics. */
uint64_t purgeable;
uint64_t pfootprint;
uint64_t p_purgeable;
uint64_t p_pfootprint;
/* Compressed memory statistics. */
uint64_t compressed;
uint64_t p_compressed;
/* Number of threads. */
uint32_t th;
uint32_t p_th;
uint32_t running_th;
uint32_t p_running_th;
/* Number of ports. */
uint32_t prt;
uint32_t p_prt;
/* CPU state/usage statistics. */
int state; /* Process state. */
/* Total time consumed by process. */
struct timeval total_time;
struct timeval b_total_time;
struct timeval p_total_time;
/* Performance counters. */
uint64_t instructions;
uint64_t b_instructions;
uint64_t p_instructions;
uint64_t cycles;
uint64_t b_cycles;
uint64_t p_cycles;
/* Total on behalf cpu time. */
uint64_t cpu_billed_to_me;
uint64_t cpu_billed_to_others;
uint64_t b_cpu_billed_to_me;
uint64_t b_cpu_billed_to_others;
uint64_t p_cpu_billed_to_me;
uint64_t p_cpu_billed_to_others;
/* assert counts */
uint64_t assertcnt;
uint64_t p_assertcnt;
uint64_t b_assertcnt;
/* boost transition counts */
uint64_t boosts;
uint64_t b_boosts;
uint64_t p_boosts;
/* last seq we were boost donating */
boolean_t boost_donating;
uint32_t boost_last_donating_seq;
/* Event counters. */
task_events_info_data_t events;
task_events_info_data_t b_events;
task_events_info_data_t p_events;
libtop_i64_values_t faults;
libtop_i64_values_t pageins;
libtop_i64_values_t cow_faults;
libtop_i64_values_t messages_sent;
libtop_i64_values_t messages_recv;
libtop_i64_values_t syscalls_mach;
libtop_i64_values_t syscalls_bsd;
libtop_i64_values_t csw;
uint64_t palloc;
uint64_t pfree;
uint64_t salloc;
uint64_t sfree;
uint64_t p_palloc;
uint64_t p_pfree;
uint64_t p_salloc;
uint64_t p_sfree;
/* malloc()ed '\0'-terminated string. */
char *command;
/* Sequence number, used to detect defunct processes. */
uint32_t seq;
/*
* Previous sequence number, used to detect processes that have only
* existed for the current sample (p_seq == 0).
*/
uint32_t p_seq;
/* time process was started */
struct timeval started;
/* process cpu type */
cpu_type_t cputype;
uint32_t wq_nthreads;
uint32_t wq_run_threads;
uint32_t wq_blocked_threads;
uint32_t p_wq_nthreads;
uint32_t p_wq_run_threads;
uint32_t p_wq_blocked_threads;
/* Power info. */
task_power_info_data_t power;
task_power_info_data_t b_power;
task_power_info_data_t p_power;
/* Extended statistics. */
uint64_t total_timens;
uint64_t b_total_timens;
uint64_t p_total_timens;
};
/*
* Initialize libtop. If a non-NULL printing function pointer is passed in,
* libtop will call the printing function when errors occur.
*
* Returns zero for success, non-zero for error.
*/
int libtop_init(libtop_print_t *a_print, void *a_user_data);
/* Shut down libtop. */
void libtop_fini(void);
/*
* Take a sample.
*
* If a_reg is FALSE, do not calculate reg, vprvt, rprvt, or rshrd.
*
* If a_fw is FALSE, do not calculate fw_count, fw_code, fw_data, or
* fw_linkedit.
*
* Returns zero for success, non-zero for error.
*/
int libtop_sample(boolean_t a_reg, boolean_t a_fw);
/*
* Return a pointer to a structure containing the generic information collected
* during the most recent sample. The return value from this function can be
* used for the duration of program execution (i.e. the return value does not
* change between samples).
*/
const libtop_tsamp_t *libtop_tsamp(void);
/*
* Type for psamp comparison function.
*
* Arguments : (void *) : Opaque data pointer.
* (libtop_psamp_t *) : psamp.
*
* Return values : -1 : Second argument less than third argument.
* 0 : Second argument equal to third argument.
* 1 : Second argument greater than third argument.
*/
typedef int libtop_sort_t(void *, const libtop_psamp_t *, const libtop_psamp_t *);
/*
* Sort processes using a_sort(). Pass a_data as the opaque data pointer to
* a_sort().
*/
void libtop_psort(libtop_sort_t *a_sort, void *a_data);
/*
* Iteratively return a pointer to each process which was in the most recent
* sample. If libtop_psort() was called after the most recent libtop_sample()
* call, the processes are iterated over in sorted order. Otherwise, they are
* iterated over in increasing pid order.
*
* A NULL return value indicates that there are no more processes to iterate
* over.
*/
const libtop_psamp_t *libtop_piterate(void);
/*
* Set whether to collect memory region information for the process with pid
* a_pid.
*
* Returns zero for success, non-zero for error.
*/
int libtop_preg(pid_t a_pid, libtop_preg_t a_preg);
/*
* Set the interval between framework updates.
*
* Returns zero for success, non-zero for error.
*/
int libtop_set_interval(uint32_t ival);
#define LIBTOP_MAX_INTERVAL 100
/*
* Return a pointer to a username string (truncated to the first 8 characters),
* given a uid. If the uid cannot be matched to a username, NULL is returned.
*/
const char *libtop_username(uid_t a_uid);
/*
* Return a pointer to a string representation of a process state (names of
* states that are contained in libtop_tsamp_t's state_breakdown array).
*/
const char *libtop_state_str(uint32_t a_state);
/*
* These i64 functions are special functions that operate on an accumulator
* and work with overflowing 32-bit integers. So if the value overflows in the kernel
* counter, because it is a 32-bit value, they will in most cases capture the
* changes overtime to the value. The assumption is that all updates are increments
* of 0 or more (based on the deltas) so this doesn't work with values that
* potentially go negative.
*/
libtop_i64_t libtop_i64_init(uint64_t acc, int last_value);
void libtop_i64_update(libtop_i64_t *i, int value);
uint64_t libtop_i64_value(libtop_i64_t *i);
__END_DECLS