2 * Copyright (c) 1999-2016 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 cc -I/System/Library/Frameworks/System.framework/Versions/B/PrivateHeaders -DPRIVATE -D__APPLE_PRIVATE -arch x86_64 -arch i386 -O -o latency latency.c -lncurses -lutil
28 #include <mach/mach.h>
44 #include <sys/types.h>
45 #include <sys/param.h>
47 #include <sys/sysctl.h>
48 #include <sys/ioctl.h>
50 #ifndef KERNEL_PRIVATE
51 #define KERNEL_PRIVATE
52 #include <sys/kdebug.h>
55 #include <sys/kdebug.h>
56 #endif /*KERNEL_PRIVATE*/
58 #include <mach/mach_error.h>
59 #include <mach/mach_types.h>
60 #include <mach/message.h>
61 #include <mach/mach_syscalls.h>
62 #include <mach/clock_types.h>
63 #include <mach/mach_time.h>
65 #include <libkern/OSTypes.h>
68 int s_usec_10_bins
[10];
69 int s_usec_100_bins
[10];
70 int s_msec_1_bins
[10];
71 int s_msec_10_bins
[5];
74 int s_min_latency
= 0;
75 long long s_total_latency
= 0;
76 int s_total_samples
= 0;
78 int s_exceeded_threshold
= 0;
81 #define N_HIGH_RES_BINS 500
82 int use_high_res_bins
= false;
85 int i_usec_10_bins
[10];
86 int i_usec_100_bins
[10];
87 int i_msec_1_bins
[10];
88 int i_msec_10_bins
[5];
94 int i_exceeded_threshold
;
95 uint64_t i_total_latency
;
98 struct i_latencies
*i_lat
;
99 boolean_t i_latency_per_cpu
= FALSE
;
101 int i_high_res_bins
[N_HIGH_RES_BINS
];
105 int watch_priority_min
= 97;
106 int watch_priority_max
= 97;
113 char *kernelpath
= NULL
;
116 void *k_sym_addr
; /* kernel symbol address from nm */
117 size_t k_sym_len
; /* length of kernel symbol string */
118 char *k_sym_name
; /* kernel symbol string from nm */
121 kern_sym_t
*kern_sym_tbl
; /* pointer to the nm table */
122 int kern_sym_count
; /* number of entries in nm table */
126 #define MAX_ENTRIES 4096
130 } codes_tab
[MAX_ENTRIES
];
132 char *code_file
= NULL
;
133 int num_of_codes
= 0;
137 sig_atomic_t gotSIGWINCH
= 0;
138 int trace_enabled
= 0;
139 int need_new_map
= 1;
140 int set_remove_flag
= 1; /* By default, remove trace buffer */
145 uint64_t first_now
= 0;
146 uint64_t last_now
= 0;
150 #define SAMPLE_TIME_USECS 50000
151 #define SAMPLE_SIZE 300000
152 #define MAX_LOG_COUNT 30 /* limits the number of entries dumped in log_decrementer */
154 kbufinfo_t bufinfo
= {0, 0, 0};
158 uint64_t sample_TOD_secs
;
159 uint32_t sample_TOD_usecs
;
163 int sample_generation
= 0;
164 int num_i_latency_cpus
= 1;
169 kd_buf
**last_decrementer_kd
; /* last DECR_TRAP per cpu */
174 typedef struct event
*event_t
;
181 uint64_t ev_timestamp
;
185 typedef struct lookup
*lookup_t
;
193 long lk_pathname
[NUMPARMS
+ 1];
197 typedef struct threadmap
*threadmap_t
;
203 uintptr_t tm_pthread
;
204 char tm_command
[MAXCOMLEN
+ 1];
205 char tm_orig_command
[MAXCOMLEN
+ 1];
209 typedef struct threadrun
*threadrun_t
;
216 uint64_t tr_timestamp
;
221 typedef struct thread_entry
*thread_entry_t
;
223 struct thread_entry
{
224 thread_entry_t te_next
;
229 #define HASH_SIZE 1024
230 #define HASH_MASK 1023
232 event_t event_hash
[HASH_SIZE
];
233 lookup_t lookup_hash
[HASH_SIZE
];
234 threadmap_t threadmap_hash
[HASH_SIZE
];
235 threadrun_t threadrun_hash
[HASH_SIZE
];
237 event_t event_freelist
;
238 lookup_t lookup_freelist
;
239 threadrun_t threadrun_freelist
;
240 threadmap_t threadmap_freelist
;
241 threadmap_t threadmap_temp
;
243 thread_entry_t thread_entry_freelist
;
244 thread_entry_t thread_delete_list
;
245 thread_entry_t thread_reset_list
;
246 thread_entry_t thread_event_list
;
247 thread_entry_t thread_lookup_list
;
248 thread_entry_t thread_run_list
;
259 #define RAW_VERSION0 0x55aa0000
260 #define RAW_VERSION1 0x55aa0101
265 #define KERNEL_MODE 1
268 #define INTERRUPT 0x01050000
269 #define DECR_TRAP 0x01090000
270 #define DECR_SET 0x01090004
271 #define MACH_vmfault 0x01300008
272 #define MACH_sched 0x01400000
273 #define MACH_stkhandoff 0x01400008
274 #define MACH_makerunnable 0x01400018
275 #define MACH_idle 0x01400024
276 #define IES_action 0x050b0018
277 #define IES_filter 0x050b001c
278 #define TES_action 0x050c0010
279 #define CQ_action 0x050d0018
280 #define CPUPM_CPUSTER_RUNCOUNT 0x05310144
282 #define BSC_exit 0x040C0004
283 #define BSC_thread_terminate 0x040c05a4
285 #define DBG_FUNC_MASK ~(DBG_FUNC_START | DBG_FUNC_END)
287 #define CPU_NUMBER(kp) kdbg_get_cpu(kp)
289 #define EMPTYSTRING ""
291 const char *fault_name
[] = {
303 const char *sched_reasons
[] = {
323 #define ARRAYSIZE(x) ((int)(sizeof(x) / sizeof(*x)))
324 #define MAX_REASON ARRAYSIZE(sched_reasons)
326 static double handle_decrementer(kd_buf
*, int);
327 static kd_buf
*log_decrementer(kd_buf
*kd_beg
, kd_buf
*kd_end
, kd_buf
*end_of_sample
, double i_latency
);
328 static void read_command_map(void);
329 static void enter_syscall(FILE *fp
, kd_buf
*kd
, uintptr_t thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, int print_info
);
330 static void exit_syscall(FILE *fp
, kd_buf
*kd
, uintptr_t thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, int print_info
);
331 static void print_entry(FILE *fp
, kd_buf
*kd
, uintptr_t thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, kd_buf
*kd_note
);
332 static void log_info(uint64_t now
, uint64_t idelta
, uint64_t start_bias
, kd_buf
*kd
, kd_buf
*kd_note
);
333 static char *find_code(int);
334 static void pc_to_string(char *pcstring
, uintptr_t pc
, int max_len
, int mode
);
335 static void getdivisor(void);
336 static int sample_sc(void);
337 static void init_code_file(void);
338 static void do_kernel_nm(void);
339 static void open_logfile(const char*);
340 static int binary_search(kern_sym_t
*list
, int low
, int high
, uintptr_t addr
);
342 static void create_map_entry(uintptr_t, char *);
343 static void check_for_thread_update(uintptr_t thread
, int debugid_base
, kd_buf
*kbufp
, char **command
);
344 static void log_scheduler(kd_buf
*kd_start
, kd_buf
*kd_stop
, kd_buf
*end_of_sample
, int s_priority
, double s_latency
, uintptr_t thread
);
345 static int check_for_scheduler_latency(int type
, uintptr_t *thread
, uint64_t now
, kd_buf
*kd
, kd_buf
**kd_start
, int *priority
, double *latency
);
346 static void open_rawfile(const char *path
);
348 static void screen_update(FILE *);
350 static void set_enable(int);
351 static void set_remove(void);
361 * This flag is turned off when calling
362 * quit() due to a set_remove() failure.
364 if (set_remove_flag
) {
380 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDENABLE
, val
};
383 if (sysctl(mib
, ARRAYSIZE(mib
), NULL
, &needed
, NULL
, 0) < 0) {
384 quit("trace facility failure, KERN_KDENABLE\n");
389 set_numbufs(int nbufs
)
391 int mib1
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDSETBUF
, nbufs
};
392 int mib2
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDSETUP
};
395 if (sysctl(mib1
, ARRAYSIZE(mib1
), NULL
, &needed
, NULL
, 0) < 0) {
396 quit("trace facility failure, KERN_KDSETBUF\n");
398 if (sysctl(mib2
, ARRAYSIZE(mib2
), NULL
, &needed
, NULL
, 0) < 0) {
399 quit("trace facility failure, KERN_KDSETUP\n");
404 set_pidexclude(int pid
, int on_off
)
406 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDPIDEX
};
407 size_t needed
= sizeof(kd_regtype
);
410 .type
= KDBG_TYPENONE
,
415 sysctl(mib
, ARRAYSIZE(mib
), &kr
, &needed
, NULL
, 0);
419 get_bufinfo(kbufinfo_t
*val
)
421 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDGETBUF
};
422 size_t needed
= sizeof (*val
);
424 if (sysctl(mib
, ARRAYSIZE(mib
), val
, &needed
, 0, 0) < 0) {
425 quit("trace facility failure, KERN_KDGETBUF\n");
432 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDREMOVE
};
437 if (sysctl(mib
, ARRAYSIZE(mib
), NULL
, &needed
, NULL
, 0) < 0) {
439 if (errno
== EBUSY
) {
440 quit("the trace facility is currently in use...\n fs_usage, sc_usage, and latency use this feature.\n\n");
442 quit("trace facility failure, KERN_KDREMOVE\n");
449 write_high_res_latencies(void)
454 if (use_high_res_bins
) {
455 if ((f
= fopen("latencies.csv","w"))) {
456 for (i
= 0; i
< N_HIGH_RES_BINS
; i
++) {
457 fprintf(f
, "%d,%d\n", i
, i_high_res_bins
[i
]);
465 sigintr(int signo
__attribute__((unused
)))
467 write_high_res_latencies();
470 set_pidexclude(getpid(), 0);
471 screen_update(log_fp
);
478 /* exit under normal conditions -- signal handler */
480 leave(int signo
__attribute__((unused
)))
482 write_high_res_latencies();
485 set_pidexclude(getpid(), 0);
493 sigwinch(int signo
__attribute__((unused
)))
499 print_total(FILE *fp
, char *s
, int total
)
504 struct i_latencies
*il
;
507 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
509 itotal
+= il
->i_total
;
511 clen
= sprintf(tbuf
, "%s %10d %9d", s
, total
, itotal
);
513 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
516 if (i_latency_per_cpu
== TRUE
) {
517 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_total
);
522 sprintf(&tbuf
[clen
], "\n");
524 fprintf(fp
, "%s", tbuf
);
533 screen_update(FILE *fp
)
542 long min_lat
, max_lat
;
544 unsigned int average_s_latency
;
545 unsigned int average_i_latency
;
546 struct i_latencies
*il
;
553 fprintf(fp
,"\n\n===================================================================================================\n");
556 * Display the current time.
557 * "ctime" always returns a string that looks like this:
559 * Sun Sep 16 01:03:52 1973
560 * 012345678901234567890123
563 * We want indices 11 thru 18 (length 8).
566 curr_time
= (unsigned long)sample_TOD_secs
;
567 elapsed_secs
= ((last_now
- first_now
) / divisor
) / 1000000;
569 elapsed_secs
= curr_time
- start_time
;
572 elapsed_hours
= elapsed_secs
/ 3600;
573 elapsed_secs
-= elapsed_hours
* 3600;
574 elapsed_mins
= elapsed_secs
/ 60;
575 elapsed_secs
-= elapsed_mins
* 60;
577 sprintf(tbuf
, "%-19.19s %2ld:%02ld:%02ld\n", &(ctime(&curr_time
)[0]),
578 (long)elapsed_hours
, (long)elapsed_mins
, (long)elapsed_secs
);
580 fprintf(fp
, "%s", tbuf
);
585 sprintf(tbuf
, " SCHEDULER INTERRUPTS\n");
587 fprintf(fp
, "%s", tbuf
);
592 if (i_latency_per_cpu
== TRUE
) {
593 clen
= sprintf(tbuf
, " Total");
595 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
597 clen
+= sprintf(&tbuf
[clen
], " CPU %d", cpu
);
599 clen
+= sprintf(&tbuf
[clen
], " CPU %d", cpu
);
603 fprintf(fp
, "%s", tbuf
);
608 clen
= sprintf(tbuf
, "\n-------------------------------------------------------");
610 for (cpu
= 1; cpu
< num_i_latency_cpus
; cpu
++) {
611 clen
+= sprintf(&tbuf
[clen
], "----------");
614 fprintf(fp
, "%s", tbuf
);
619 sprintf(tbuf
, "---------------------------------------------");
621 fprintf(fp
, "%s", tbuf
);
626 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
628 itotal
+= il
->i_total_samples
;
630 clen
= sprintf(tbuf
, "\ntotal_samples %10d %9d", s_total_samples
, itotal
);
632 if (i_latency_per_cpu
== TRUE
) {
633 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
636 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_total_samples
);
639 sprintf(&tbuf
[clen
], "\n");
641 fprintf(fp
, "%s", tbuf
);
647 for (stotal
= 0, i
= 0; i
< 10; i
++) {
648 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
651 itotal
+= il
->i_usec_10_bins
[i
];
652 il
->i_total
+= il
->i_usec_10_bins
[i
];
654 clen
= sprintf(tbuf
, "\ndelays < %3d usecs %10d %9d", (i
+ 1) * 10, s_usec_10_bins
[i
], itotal
);
656 stotal
+= s_usec_10_bins
[i
];
658 if (i_latency_per_cpu
== TRUE
) {
659 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
662 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_usec_10_bins
[i
]);
666 fprintf(fp
, "%s", tbuf
);
671 print_total(fp
, "\ntotal < 100 usecs", stotal
);
673 for (stotal
= 0, i
= 1; i
< 10; i
++) {
674 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
677 itotal
+= il
->i_usec_100_bins
[i
];
678 il
->i_total
+= il
->i_usec_100_bins
[i
];
681 clen
= sprintf(tbuf
, "\ndelays < %3d usecs %10d %9d", (i
+ 1) * 100, s_usec_100_bins
[i
], itotal
);
683 clen
= sprintf(tbuf
, "\ndelays < 1 msec %10d %9d", s_usec_100_bins
[i
], itotal
);
686 stotal
+= s_usec_100_bins
[i
];
688 if (i_latency_per_cpu
== TRUE
) {
689 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
692 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_usec_100_bins
[i
]);
696 fprintf(fp
, "%s", tbuf
);
701 print_total(fp
, "\ntotal < 1 msec ", stotal
);
704 for (stotal
= 0, i
= 1; i
< 10; i
++) {
705 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
708 itotal
+= il
->i_msec_1_bins
[i
];
709 il
->i_total
+= il
->i_msec_1_bins
[i
];
711 clen
= sprintf(tbuf
, "\ndelays < %3d msecs %10d %9d", (i
+ 1), s_msec_1_bins
[i
], itotal
);
713 stotal
+= s_msec_1_bins
[i
];
715 if (i_latency_per_cpu
== TRUE
) {
716 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
719 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_msec_1_bins
[i
]);
723 fprintf(fp
, "%s", tbuf
);
728 print_total(fp
, "\ntotal < 10 msecs", stotal
);
730 for (stotal
= 0, i
= 1; i
< 5; i
++) {
731 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
734 itotal
+= il
->i_msec_10_bins
[i
];
735 il
->i_total
+= il
->i_msec_10_bins
[i
];
737 clen
= sprintf(tbuf
, "\ndelays < %3d msecs %10d %9d", (i
+ 1)*10, s_msec_10_bins
[i
], itotal
);
739 stotal
+= s_msec_10_bins
[i
];
741 if (i_latency_per_cpu
== TRUE
) {
742 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
745 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_msec_10_bins
[i
]);
749 fprintf(fp
, "%s", tbuf
);
754 print_total(fp
, "\ntotal < 50 msecs", stotal
);
757 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
759 itotal
+= il
->i_too_slow
;
761 clen
= sprintf(tbuf
, "\ndelays > 50 msecs %10d %9d", s_too_slow
, itotal
);
763 if (i_latency_per_cpu
== TRUE
) {
764 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
767 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_too_slow
);
771 fprintf(fp
, "%s", tbuf
);
776 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
779 if (cpu
== 0 || (il
->i_min_latency
< min_lat
)) {
780 min_lat
= il
->i_min_latency
;
783 clen
= sprintf(tbuf
, "\n\nminimum latency(usecs) %7d %9ld", s_min_latency
, min_lat
);
785 if (i_latency_per_cpu
== TRUE
) {
786 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
789 clen
+= sprintf(&tbuf
[clen
], " %9ld", il
->i_min_latency
);
793 fprintf(fp
, "%s", tbuf
);
799 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
802 if (cpu
== 0 || (il
->i_max_latency
> max_lat
)) {
803 max_lat
= il
->i_max_latency
;
806 clen
= sprintf(tbuf
, "\nmaximum latency(usecs) %7d %9ld", s_max_latency
, max_lat
);
808 if (i_latency_per_cpu
== TRUE
) {
809 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
812 clen
+= sprintf(&tbuf
[clen
], " %9ld", il
->i_max_latency
);
816 fprintf(fp
, "%s", tbuf
);
821 if (s_total_samples
) {
822 average_s_latency
= (unsigned int)(s_total_latency
/s_total_samples
);
824 average_s_latency
= 0;
827 for (itotal
= 0, tot_lat
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
830 itotal
+= il
->i_total_samples
;
831 tot_lat
+= il
->i_total_latency
;
834 average_i_latency
= (unsigned)(tot_lat
/itotal
);
836 average_i_latency
= 0;
839 clen
= sprintf(tbuf
, "\naverage latency(usecs) %7d %9d", average_s_latency
, average_i_latency
);
841 if (i_latency_per_cpu
== TRUE
) {
842 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
845 if (il
->i_total_samples
) {
846 average_i_latency
= (unsigned int)(il
->i_total_latency
/il
->i_total_samples
);
848 average_i_latency
= 0;
851 clen
+= sprintf(&tbuf
[clen
], " %9d", average_i_latency
);
855 fprintf(fp
, "%s", tbuf
);
860 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
863 itotal
+= il
->i_exceeded_threshold
;
865 clen
= sprintf(tbuf
, "\nexceeded threshold %7d %9d", s_exceeded_threshold
, itotal
);
867 if (i_latency_per_cpu
== TRUE
) {
868 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
871 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_exceeded_threshold
);
874 sprintf(&tbuf
[clen
], "\n");
877 fprintf(fp
, "%s", tbuf
);
892 fprintf(stderr
, "Usage: latency [-p <priority>] [-h] [-m] [-st <threshold>] [-it <threshold>]\n");
893 fprintf(stderr
, " [-c <codefile>] [-l <logfile>] [-R <rawfile>] [-n <kernel>]\n\n");
895 fprintf(stderr
, " -p specify scheduling priority to watch... default is realtime. Can also be a range, e.g. \"31-47\".\n");
896 fprintf(stderr
, " -h Display high resolution interrupt latencies and write them to latencies.csv (truncate existing file) upon exit.\n");
897 fprintf(stderr
, " -st set scheduler latency threshold in microseconds... if latency exceeds this, then log trace\n");
898 fprintf(stderr
, " -m specify per-CPU interrupt latency reporting\n");
899 fprintf(stderr
, " -it set interrupt latency threshold in microseconds... if latency exceeds this, then log trace\n");
900 fprintf(stderr
, " -c specify name of codes file... default is /usr/share/misc/trace.codes\n");
901 fprintf(stderr
, " -l specify name of file to log trace entries to when the specified threshold is exceeded\n");
902 fprintf(stderr
, " -R specify name of raw trace file to process\n");
903 fprintf(stderr
, " -n specify kernel... default is /System/Library/Kernels/kernel.development\n");
905 fprintf(stderr
, "\nlatency must be run as root\n\n");
911 main(int argc
, char *argv
[])
915 if (0 != reexec_to_match_kernel()) {
916 fprintf(stderr
, "Could not re-execute: %d\n", errno
);
921 if (strcmp(argv
[1], "-R") == 0) {
926 open_rawfile(argv
[1]);
933 } else if (strcmp(argv
[1], "-p") == 0) {
938 if (2 == sscanf(argv
[1], "%d-%d", &watch_priority_min
, &watch_priority_max
)) {
939 if (watch_priority_min
> watch_priority_max
) {
941 } else if (watch_priority_min
< 0) {
945 if (1 == sscanf(argv
[1], "%d", &watch_priority_min
)) {
946 watch_priority_max
= watch_priority_min
;
954 } else if (strcmp(argv
[1], "-st") == 0) {
959 s_thresh_hold
= atoi(argv
[1]);
963 } else if (strcmp(argv
[1], "-it") == 0) {
968 i_thresh_hold
= atoi(argv
[1]);
972 } else if (strcmp(argv
[1], "-c") == 0) {
981 } else if (strcmp(argv
[1], "-l") == 0) {
986 open_logfile(argv
[1]);
990 } else if (strcmp(argv
[1], "-n") == 0) {
995 kernelpath
= argv
[1];
999 } else if (strcmp(argv
[1], "-h") == 0) {
1000 use_high_res_bins
= TRUE
;
1002 } else if (strcmp(argv
[1], "-m") == 0) {
1003 i_latency_per_cpu
= TRUE
;
1013 if (geteuid() != 0) {
1014 printf("'latency' must be run as root...\n");
1018 if (kernelpath
== NULL
) {
1019 kernelpath
= "/System/Library/Kernels/kernel.development";
1022 if (code_file
== NULL
) {
1023 code_file
= "/usr/share/misc/trace.codes";
1033 if (initscr() == NULL
) {
1034 printf("Unrecognized TERM type, try vt100\n");
1040 signal(SIGWINCH
, sigwinch
);
1041 signal(SIGINT
, sigintr
);
1042 signal(SIGQUIT
, leave
);
1043 signal(SIGTERM
, leave
);
1044 signal(SIGHUP
, leave
);
1047 * grab the number of cpus and scale the buffer size
1049 int mib
[] = { CTL_HW
, HW_NCPU
};
1050 size_t len
= sizeof(num_cpus
);
1052 sysctl(mib
, ARRAYSIZE(mib
), &num_cpus
, &len
, NULL
, 0);
1055 set_numbufs(SAMPLE_SIZE
* num_cpus
);
1057 get_bufinfo(&bufinfo
);
1061 set_pidexclude(getpid(), 1);
1064 num_entries
= bufinfo
.nkdbufs
;
1066 num_entries
= 50000;
1070 for (cpu_mask
= 0, i
= 0; i
< num_cpus
; i
++)
1071 cpu_mask
|= ((uint64_t)1 << i
);
1073 if ((my_buffer
= malloc(num_entries
* sizeof(kd_buf
))) == NULL
) {
1074 quit("can't allocate memory for tracing info\n");
1077 if ((last_decrementer_kd
= (kd_buf
**)malloc(num_cpus
* sizeof(kd_buf
*))) == NULL
) {
1078 quit("can't allocate memory for decrementer tracing info\n");
1081 if (i_latency_per_cpu
== FALSE
) {
1082 num_i_latency_cpus
= 1;
1084 num_i_latency_cpus
= num_cpus
;
1087 if ((i_lat
= (struct i_latencies
*)malloc(num_i_latency_cpus
* sizeof(struct i_latencies
))) == NULL
) {
1088 quit("can't allocate memory for interrupt latency info\n");
1091 bzero((char *)i_lat
, num_i_latency_cpus
* sizeof(struct i_latencies
));
1094 while (sample_sc()) {
1099 screen_update(log_fp
);
1102 screen_update(stdout
);
1107 double nanosecs_to_sleep
;
1109 nanosecs_to_sleep
= (double)(SAMPLE_TIME_USECS
* 1000);
1110 fdelay
= nanosecs_to_sleep
* (divisor
/1000);
1111 adelay
= (uint64_t)fdelay
;
1115 start_time
= time(NULL
);
1116 refresh_time
= start_time
;
1119 curr_time
= time(NULL
);
1121 if (curr_time
>= refresh_time
) {
1122 screen_update(NULL
);
1123 refresh_time
= curr_time
+ 1;
1125 mach_wait_until(mach_absolute_time() + adelay
);
1131 * No need to check for initscr error return.
1132 * We won't get here if it fails on the first call.
1145 read_command_map(void)
1147 kd_threadmap
*mapptr
= 0;
1148 int total_threads
= 0;
1152 RAW_header header
= {0};
1155 if (read(RAW_fd
, &header
, sizeof(RAW_header
)) != sizeof(RAW_header
)) {
1156 perror("read failed");
1159 if (header
.version_no
!= RAW_VERSION1
) {
1160 header
.version_no
= RAW_VERSION0
;
1161 header
.TOD_secs
= time(NULL
);
1162 header
.TOD_usecs
= 0;
1164 lseek(RAW_fd
, (off_t
)0, SEEK_SET
);
1166 if (read(RAW_fd
, &header
.thread_count
, sizeof(int)) != sizeof(int)) {
1167 perror("read failed");
1171 total_threads
= header
.thread_count
;
1173 sample_TOD_secs
= header
.TOD_secs
;
1174 sample_TOD_usecs
= header
.TOD_usecs
;
1176 if (total_threads
== 0 && header
.version_no
!= RAW_VERSION0
) {
1177 offset
= lseek(RAW_fd
, (off_t
)0, SEEK_CUR
);
1178 offset
= (offset
+ (4095)) & ~4095;
1180 lseek(RAW_fd
, offset
, SEEK_SET
);
1183 total_threads
= bufinfo
.nkdthreads
;
1186 size
= total_threads
* sizeof(kd_threadmap
);
1188 if (size
== 0 || ((mapptr
= (kd_threadmap
*) malloc(size
)) == 0)) {
1191 bzero (mapptr
, size
);
1194 * Now read the threadmap
1197 if (read(RAW_fd
, mapptr
, size
) != size
) {
1198 printf("Can't read the thread map -- this is not fatal\n");
1200 if (header
.version_no
!= RAW_VERSION0
) {
1201 offset
= lseek(RAW_fd
, (off_t
)0, SEEK_CUR
);
1202 offset
= (offset
+ (4095)) & ~4095;
1204 lseek(RAW_fd
, offset
, SEEK_SET
);
1207 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDTHRMAP
};
1208 if (sysctl(mib
, ARRAYSIZE(mib
), mapptr
, &size
, NULL
, 0) < 0) {
1210 * This is not fatal -- just means I cant map command strings
1212 printf("Can't read the thread map -- this is not fatal\n");
1217 for (i
= 0; i
< total_threads
; i
++) {
1218 create_map_entry(mapptr
[i
].thread
, &mapptr
[i
].command
[0]);
1224 create_map_entry(uintptr_t thread
, char *command
)
1228 if ((tme
= threadmap_freelist
)) {
1229 threadmap_freelist
= tme
->tm_next
;
1231 tme
= (threadmap_t
)malloc(sizeof(struct threadmap
));
1234 tme
->tm_thread
= thread
;
1236 (void)strncpy (tme
->tm_command
, command
, MAXCOMLEN
);
1237 tme
->tm_command
[MAXCOMLEN
] = '\0';
1238 tme
->tm_orig_command
[0] = '\0';
1240 int hashid
= thread
& HASH_MASK
;
1242 tme
->tm_next
= threadmap_hash
[hashid
];
1243 threadmap_hash
[hashid
] = tme
;
1247 delete_thread_entry(uintptr_t thread
)
1251 int hashid
= thread
& HASH_MASK
;
1253 if ((tme
= threadmap_hash
[hashid
])) {
1254 if (tme
->tm_thread
== thread
) {
1255 threadmap_hash
[hashid
] = tme
->tm_next
;
1257 threadmap_t tme_prev
= tme
;
1259 for (tme
= tme
->tm_next
; tme
; tme
= tme
->tm_next
) {
1260 if (tme
->tm_thread
== thread
) {
1261 tme_prev
->tm_next
= tme
->tm_next
;
1268 tme
->tm_next
= threadmap_freelist
;
1269 threadmap_freelist
= tme
;
1275 find_and_insert_tmp_map_entry(uintptr_t pthread
, char *command
)
1279 if ((tme
= threadmap_temp
)) {
1280 if (tme
->tm_pthread
== pthread
) {
1281 threadmap_temp
= tme
->tm_next
;
1283 threadmap_t tme_prev
= tme
;
1285 for (tme
= tme
->tm_next
; tme
; tme
= tme
->tm_next
) {
1286 if (tme
->tm_pthread
== pthread
) {
1287 tme_prev
->tm_next
= tme
->tm_next
;
1294 (void)strncpy (tme
->tm_command
, command
, MAXCOMLEN
);
1295 tme
->tm_command
[MAXCOMLEN
] = '\0';
1296 tme
->tm_orig_command
[0] = '\0';
1298 int hashid
= tme
->tm_thread
& HASH_MASK
;
1299 tme
->tm_next
= threadmap_hash
[hashid
];
1300 threadmap_hash
[hashid
] = tme
;
1306 create_tmp_map_entry(uintptr_t thread
, uintptr_t pthread
)
1310 if ((tme
= threadmap_freelist
)) {
1311 threadmap_freelist
= tme
->tm_next
;
1313 tme
= malloc(sizeof(struct threadmap
));
1316 tme
->tm_thread
= thread
;
1317 tme
->tm_pthread
= pthread
;
1318 tme
->tm_command
[0] = '\0';
1319 tme
->tm_orig_command
[0] = '\0';
1321 tme
->tm_next
= threadmap_temp
;
1322 threadmap_temp
= tme
;
1326 find_thread_entry(uintptr_t thread
)
1330 int hashid
= thread
& HASH_MASK
;
1332 for (tme
= threadmap_hash
[hashid
]; tme
; tme
= tme
->tm_next
) {
1333 if (tme
->tm_thread
== thread
) {
1341 find_thread_name(uintptr_t thread
, char **command
)
1345 if ((tme
= find_thread_entry(thread
))) {
1346 *command
= tme
->tm_command
;
1348 *command
= EMPTYSTRING
;
1353 add_thread_entry_to_list(thread_entry_t
*list
, uintptr_t thread
)
1357 if ((te
= thread_entry_freelist
)) {
1358 thread_entry_freelist
= te
->te_next
;
1360 te
= (thread_entry_t
)malloc(sizeof(struct thread_entry
));
1363 te
->te_thread
= thread
;
1364 te
->te_next
= *list
;
1369 exec_thread_entry(uintptr_t thread
, char *command
)
1373 if ((tme
= find_thread_entry(thread
))) {
1374 if (tme
->tm_orig_command
[0] == '\0') {
1375 (void)strncpy (tme
->tm_orig_command
, tme
->tm_command
, MAXCOMLEN
);
1376 tme
->tm_orig_command
[MAXCOMLEN
] = '\0';
1378 (void)strncpy (tme
->tm_command
, command
, MAXCOMLEN
);
1379 tme
->tm_command
[MAXCOMLEN
] = '\0';
1381 add_thread_entry_to_list(&thread_reset_list
, thread
);
1383 create_map_entry(thread
, command
);
1388 record_thread_entry_for_gc(uintptr_t thread
)
1390 add_thread_entry_to_list(&thread_delete_list
, thread
);
1394 gc_thread_entries(void)
1397 thread_entry_t te_next
;
1400 for (te
= thread_delete_list
; te
; te
= te_next
) {
1401 delete_thread_entry(te
->te_thread
);
1403 te_next
= te
->te_next
;
1404 te
->te_next
= thread_entry_freelist
;
1405 thread_entry_freelist
= te
;
1409 thread_delete_list
= 0;
1413 gc_reset_entries(void)
1416 thread_entry_t te_next
;
1419 for (te
= thread_reset_list
; te
; te
= te_next
) {
1420 te_next
= te
->te_next
;
1421 te
->te_next
= thread_entry_freelist
;
1422 thread_entry_freelist
= te
;
1426 thread_reset_list
= 0;
1430 reset_thread_names(void)
1433 thread_entry_t te_next
;
1436 for (te
= thread_reset_list
; te
; te
= te_next
) {
1439 if ((tme
= find_thread_entry(te
->te_thread
))) {
1440 if (tme
->tm_orig_command
[0]) {
1441 (void)strncpy (tme
->tm_command
, tme
->tm_orig_command
, MAXCOMLEN
);
1442 tme
->tm_command
[MAXCOMLEN
] = '\0';
1443 tme
->tm_orig_command
[0] = '\0';
1446 te_next
= te
->te_next
;
1447 te
->te_next
= thread_entry_freelist
;
1448 thread_entry_freelist
= te
;
1452 thread_reset_list
= 0;
1456 delete_all_thread_entries(void)
1458 threadmap_t tme
= 0;
1459 threadmap_t tme_next
= 0;
1462 for (i
= 0; i
< HASH_SIZE
; i
++) {
1463 for (tme
= threadmap_hash
[i
]; tme
; tme
= tme_next
) {
1464 tme_next
= tme
->tm_next
;
1465 tme
->tm_next
= threadmap_freelist
;
1466 threadmap_freelist
= tme
;
1468 threadmap_hash
[i
] = 0;
1473 insert_run_event(uintptr_t thread
, int priority
, kd_buf
*kd
, uint64_t now
)
1477 int hashid
= thread
& HASH_MASK
;
1479 for (trp
= threadrun_hash
[hashid
]; trp
; trp
= trp
->tr_next
) {
1480 if (trp
->tr_thread
== thread
) {
1485 if ((trp
= threadrun_freelist
)) {
1486 threadrun_freelist
= trp
->tr_next
;
1488 trp
= (threadrun_t
)malloc(sizeof(struct threadrun
));
1491 trp
->tr_thread
= thread
;
1493 trp
->tr_next
= threadrun_hash
[hashid
];
1494 threadrun_hash
[hashid
] = trp
;
1496 add_thread_entry_to_list(&thread_run_list
, thread
);
1499 trp
->tr_timestamp
= now
;
1500 trp
->tr_priority
= priority
;
1504 find_run_event(uintptr_t thread
)
1507 int hashid
= thread
& HASH_MASK
;
1509 for (trp
= threadrun_hash
[hashid
]; trp
; trp
= trp
->tr_next
) {
1510 if (trp
->tr_thread
== thread
) {
1518 delete_run_event(uintptr_t thread
)
1520 threadrun_t trp
= 0;
1521 threadrun_t trp_prev
;
1523 int hashid
= thread
& HASH_MASK
;
1525 if ((trp
= threadrun_hash
[hashid
])) {
1526 if (trp
->tr_thread
== thread
) {
1527 threadrun_hash
[hashid
] = trp
->tr_next
;
1531 for (trp
= trp
->tr_next
; trp
; trp
= trp
->tr_next
) {
1532 if (trp
->tr_thread
== thread
) {
1533 trp_prev
->tr_next
= trp
->tr_next
;
1540 trp
->tr_next
= threadrun_freelist
;
1541 threadrun_freelist
= trp
;
1550 thread_entry_t te_next
;
1552 threadrun_t trp_next
;
1555 for (te
= thread_run_list
; te
; te
= te_next
) {
1556 int hashid
= te
->te_thread
& HASH_MASK
;
1558 for (trp
= threadrun_hash
[hashid
]; trp
; trp
= trp_next
) {
1559 trp_next
= trp
->tr_next
;
1560 trp
->tr_next
= threadrun_freelist
;
1561 threadrun_freelist
= trp
;
1564 threadrun_hash
[hashid
] = 0;
1566 te_next
= te
->te_next
;
1567 te
->te_next
= thread_entry_freelist
;
1568 thread_entry_freelist
= te
;
1570 thread_run_list
= 0;
1576 insert_start_event(uintptr_t thread
, int type
, uint64_t now
)
1580 int hashid
= thread
& HASH_MASK
;
1582 for (evp
= event_hash
[hashid
]; evp
; evp
= evp
->ev_next
) {
1583 if (evp
->ev_thread
== thread
&& evp
->ev_type
== type
) {
1588 if ((evp
= event_freelist
)) {
1589 event_freelist
= evp
->ev_next
;
1591 evp
= (event_t
)malloc(sizeof(struct event
));
1594 evp
->ev_thread
= thread
;
1595 evp
->ev_type
= type
;
1597 evp
->ev_next
= event_hash
[hashid
];
1598 event_hash
[hashid
] = evp
;
1600 add_thread_entry_to_list(&thread_event_list
, thread
);
1602 evp
->ev_timestamp
= now
;
1607 consume_start_event(uintptr_t thread
, int type
, uint64_t now
)
1611 uint64_t elapsed
= 0;
1613 int hashid
= thread
& HASH_MASK
;
1615 if ((evp
= event_hash
[hashid
])) {
1616 if (evp
->ev_thread
== thread
&& evp
->ev_type
== type
) {
1617 event_hash
[hashid
] = evp
->ev_next
;
1621 for (evp
= evp
->ev_next
; evp
; evp
= evp
->ev_next
) {
1622 if (evp
->ev_thread
== thread
&& evp
->ev_type
== type
) {
1623 evp_prev
->ev_next
= evp
->ev_next
;
1630 elapsed
= now
- evp
->ev_timestamp
;
1632 if (now
< evp
->ev_timestamp
) {
1633 printf("consume: now = %qd, timestamp = %qd\n", now
, evp
->ev_timestamp
);
1636 evp
->ev_next
= event_freelist
;
1637 event_freelist
= evp
;
1644 gc_start_events(void)
1647 thread_entry_t te_next
;
1653 for (te
= thread_event_list
; te
; te
= te_next
) {
1655 hashid
= te
->te_thread
& HASH_MASK
;
1657 for (evp
= event_hash
[hashid
]; evp
; evp
= evp_next
) {
1658 evp_next
= evp
->ev_next
;
1659 evp
->ev_next
= event_freelist
;
1660 event_freelist
= evp
;
1663 event_hash
[hashid
] = 0;
1665 te_next
= te
->te_next
;
1666 te
->te_next
= thread_entry_freelist
;
1667 thread_entry_freelist
= te
;
1669 thread_event_list
= 0;
1673 thread_in_user_mode(uintptr_t thread
, char *command
)
1677 if (strcmp(command
, "kernel_task") == 0) {
1681 int hashid
= thread
& HASH_MASK
;
1683 for (evp
= event_hash
[hashid
]; evp
; evp
= evp
->ev_next
) {
1684 if (evp
->ev_thread
== thread
) {
1692 handle_lookup_event(uintptr_t thread
, int debugid
, kd_buf
*kdp
)
1695 boolean_t first_record
= FALSE
;
1697 int hashid
= thread
& HASH_MASK
;
1699 if (debugid
& DBG_FUNC_START
) {
1700 first_record
= TRUE
;
1703 for (lkp
= lookup_hash
[hashid
]; lkp
; lkp
= lkp
->lk_next
) {
1704 if (lkp
->lk_thread
== thread
) {
1709 if (first_record
== FALSE
) {
1713 if ((lkp
= lookup_freelist
)) {
1714 lookup_freelist
= lkp
->lk_next
;
1716 lkp
= (lookup_t
)malloc(sizeof(struct lookup
));
1719 lkp
->lk_thread
= thread
;
1721 lkp
->lk_next
= lookup_hash
[hashid
];
1722 lookup_hash
[hashid
] = lkp
;
1724 add_thread_entry_to_list(&thread_lookup_list
, thread
);
1727 if (first_record
== TRUE
) {
1728 lkp
->lk_pathptr
= lkp
->lk_pathname
;
1729 lkp
->lk_dvp
= kdp
->arg1
;
1731 if (lkp
->lk_pathptr
> &lkp
->lk_pathname
[NUMPARMS
-4]) {
1734 *lkp
->lk_pathptr
++ = kdp
->arg1
;
1736 *lkp
->lk_pathptr
++ = kdp
->arg2
;
1737 *lkp
->lk_pathptr
++ = kdp
->arg3
;
1738 *lkp
->lk_pathptr
++ = kdp
->arg4
;
1739 *lkp
->lk_pathptr
= 0;
1741 if (debugid
& DBG_FUNC_END
) {
1749 delete_lookup_event(uintptr_t thread
, lookup_t lkp_to_delete
)
1755 hashid
= thread
& HASH_MASK
;
1757 if ((lkp
= lookup_hash
[hashid
])) {
1758 if (lkp
== lkp_to_delete
) {
1759 lookup_hash
[hashid
] = lkp
->lk_next
;
1763 for (lkp
= lkp
->lk_next
; lkp
; lkp
= lkp
->lk_next
) {
1764 if (lkp
== lkp_to_delete
) {
1765 lkp_prev
->lk_next
= lkp
->lk_next
;
1772 lkp
->lk_next
= lookup_freelist
;
1773 lookup_freelist
= lkp
;
1779 gc_lookup_events(void)
1782 thread_entry_t te_next
;
1788 for (te
= thread_lookup_list
; te
; te
= te_next
) {
1789 hashid
= te
->te_thread
& HASH_MASK
;
1791 for (lkp
= lookup_hash
[hashid
]; lkp
; lkp
= lkp_next
) {
1792 lkp_next
= lkp
->lk_next
;
1793 lkp
->lk_next
= lookup_freelist
;
1794 lookup_freelist
= lkp
;
1797 lookup_hash
[hashid
] = 0;
1799 te_next
= te
->te_next
;
1800 te
->te_next
= thread_entry_freelist
;
1801 thread_entry_freelist
= te
;
1803 thread_lookup_list
= 0;
1809 kd_buf
*kd
, *end_of_sample
;
1816 * Get kernel buffer information
1818 get_bufinfo(&bufinfo
);
1821 delete_all_thread_entries();
1828 bytes_read
= read(RAW_fd
, my_buffer
, num_entries
* sizeof(kd_buf
));
1830 if (bytes_read
== -1) {
1831 perror("read failed");
1834 count
= bytes_read
/ sizeof(kd_buf
);
1836 if (count
!= num_entries
) {
1841 kd
= (kd_buf
*)my_buffer
;
1842 first_now
= kd
->timestamp
& KDBG_TIMESTAMP_MASK
;
1847 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDREADTR
};
1848 size_t needed
= bufinfo
.nkdbufs
* sizeof(kd_buf
);
1850 if (sysctl(mib
, ARRAYSIZE(mib
), my_buffer
, &needed
, NULL
, 0) < 0) {
1851 quit("trace facility failure, KERN_KDREADTR\n");
1855 sample_generation
++;
1857 if (bufinfo
.flags
& KDBG_WRAPPED
) {
1861 fprintf(log_fp
, "\n\n%-19.19s sample = %d <<<<<<< trace buffer wrapped >>>>>>>\n\n",
1862 &(ctime(&curr_time
)[0]), sample_generation
);
1868 end_of_sample
= &((kd_buf
*)my_buffer
)[count
];
1871 * Always reinitialize the DECR_TRAP array
1873 for (i
= 0; i
< num_cpus
; i
++) {
1874 last_decrementer_kd
[i
] = (kd_buf
*)my_buffer
;
1877 for (kd
= (kd_buf
*)my_buffer
; kd
< end_of_sample
; kd
++) {
1879 uintptr_t thread
= kd
->arg5
;
1880 int type
= kd
->debugid
& DBG_FUNC_MASK
;
1882 (void)check_for_thread_update(thread
, type
, kd
, NULL
);
1884 uint64_t now
= kd
->timestamp
& KDBG_TIMESTAMP_MASK
;
1887 if (type
== DECR_TRAP
) {
1888 int cpunum
= CPU_NUMBER(kd
);
1889 double i_latency
= handle_decrementer(kd
, cpunum
);
1892 if (i_thresh_hold
&& (int)i_latency
> i_thresh_hold
) {
1893 kd_start
= last_decrementer_kd
[cpunum
];
1895 log_decrementer(kd_start
, kd
, end_of_sample
, i_latency
);
1897 last_decrementer_kd
[cpunum
] = kd
;
1902 if (check_for_scheduler_latency(type
, &thread
, now
, kd
, &kd_start
, &s_priority
, &s_latency
)) {
1903 log_scheduler(kd_start
, kd
, end_of_sample
, s_priority
, s_latency
, thread
);
1911 gc_thread_entries();
1919 enter_syscall(FILE *fp
, kd_buf
*kd
, uintptr_t thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, int print_info
)
1926 int cpunum
= CPU_NUMBER(kd
);
1928 if (print_info
&& fp
) {
1929 timestamp
= (double)(now
- start_bias
) / divisor
;
1930 delta
= (double)idelta
/ divisor
;
1932 if ((p
= find_code(type
))) {
1933 if (type
== INTERRUPT
) {
1942 pc_to_string(&pcstring
[0], kd
->arg2
, 58, mode
);
1944 fprintf(fp
, "%9.1f %8.1f\t\tINTERRUPT[%2lx] @ %-58.58s %8lx %2d %s\n",
1945 timestamp
, delta
, kd
->arg1
, &pcstring
[0], thread
, cpunum
, command
);
1946 } else if (type
== MACH_vmfault
) {
1947 fprintf(fp
, "%9.1f %8.1f\t\t%-28.28s %8lx %2d %s\n",
1948 timestamp
, delta
, p
, thread
, cpunum
, command
);
1950 fprintf(fp
, "%9.1f %8.1f\t\t%-28.28s %-16lx %-16lx %-16lx %-16lx %8lx %2d %s\n",
1951 timestamp
, delta
, p
, kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
,
1952 thread
, cpunum
, command
);
1955 fprintf(fp
, "%9.1f %8.1f\t\t%-8x %-16lx %-16lx %-16lx %-16lx %8lx %2d %s\n",
1956 timestamp
, delta
, type
, kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
,
1957 thread
, cpunum
, command
);
1960 if (type
!= BSC_thread_terminate
&& type
!= BSC_exit
) {
1961 insert_start_event(thread
, type
, now
);
1966 exit_syscall(FILE *fp
, kd_buf
*kd
, uintptr_t thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, int print_info
)
1972 double elapsed_timestamp
;
1974 elapsed_timestamp
= (double)consume_start_event(thread
, type
, now
) / divisor
;
1976 if (print_info
&& fp
) {
1977 int cpunum
= CPU_NUMBER(kd
);
1979 timestamp
= (double)(now
- start_bias
) / divisor
;
1980 delta
= (double)idelta
/ divisor
;
1982 fprintf(fp
, "%9.1f %8.1f(%.1f) \t", timestamp
, delta
, elapsed_timestamp
);
1984 if ((p
= find_code(type
))) {
1985 if (type
== INTERRUPT
) {
1986 fprintf(fp
, "INTERRUPT %8lx %2d %s\n", thread
, cpunum
, command
);
1987 } else if (type
== MACH_vmfault
&& kd
->arg4
<= DBG_PAGEIND_FAULT
) {
1988 user_addr
= ((uint64_t)kd
->arg1
<< 32) | (uint32_t)kd
->arg2
;
1990 fprintf(fp
, "%-28.28s %-10.10s %-16qx %8lx %2d %s\n",
1991 p
, fault_name
[kd
->arg4
], user_addr
,
1992 thread
, cpunum
, command
);
1994 fprintf(fp
, "%-28.28s %-16lx %-16lx %8lx %2d %s\n",
1995 p
, kd
->arg1
, kd
->arg2
,
1996 thread
, cpunum
, command
);
1999 fprintf(fp
, "%-8x %-16lx %-16lx %8lx %2d %s\n",
2000 type
, kd
->arg1
, kd
->arg2
,
2001 thread
, cpunum
, command
);
2007 print_entry(FILE *fp
, kd_buf
*kd
, uintptr_t thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, kd_buf
*kd_note
)
2015 int cpunum
= CPU_NUMBER(kd
);
2017 double timestamp
= (double)(now
- start_bias
) / divisor
;
2018 double delta
= (double)idelta
/ divisor
;
2020 if ((p
= find_code(type
))) {
2021 if (kd
== kd_note
) {
2022 fprintf(fp
, "%9.1f %8.1f\t**\t", timestamp
, delta
);
2024 fprintf(fp
, "%9.1f %8.1f\t\t", timestamp
, delta
);
2026 fprintf(fp
, "%-28.28s %-16lx %-16lx %-16lx %-16lx %8lx %2d %s\n",
2027 p
, kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
, thread
, cpunum
, command
);
2029 fprintf(fp
, "%9.1f %8.1f\t\t%-8x %-16lx %-16lx %-16lx %-16lx %8lx %2d %s\n",
2030 timestamp
, delta
, type
, kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
,
2031 thread
, cpunum
, command
);
2036 check_for_thread_update(uintptr_t thread
, int debugid_base
, kd_buf
*kbufp
, char **command
)
2038 if (debugid_base
== TRACE_DATA_NEWTHREAD
) {
2040 * Save the create thread data
2042 create_tmp_map_entry(kbufp
->arg1
, thread
);
2043 } else if (debugid_base
== TRACE_STRING_NEWTHREAD
) {
2045 * process new map entry
2047 find_and_insert_tmp_map_entry(thread
, (char *)&kbufp
->arg1
);
2048 } else if (debugid_base
== TRACE_STRING_EXEC
) {
2049 exec_thread_entry(thread
, (char *)&kbufp
->arg1
);
2051 if (debugid_base
== BSC_exit
|| debugid_base
== BSC_thread_terminate
) {
2052 record_thread_entry_for_gc(thread
);
2055 find_thread_name(thread
, command
);
2061 log_info(uint64_t now
, uint64_t idelta
, uint64_t start_bias
, kd_buf
*kd
, kd_buf
*kd_note
)
2069 char command_buf
[32];
2070 char sched_info
[64];
2072 const char *sched_reason
;
2078 uintptr_t thread
= kd
->arg5
;
2079 int cpunum
= CPU_NUMBER(kd
);
2080 int debugid
= kd
->debugid
;
2081 int type
= kd
->debugid
& DBG_FUNC_MASK
;
2083 (void)check_for_thread_update(thread
, type
, kd
, &command
);
2085 if ((type
>> 24) == DBG_TRACE
) {
2086 if (((type
>> 16) & 0xff) != DBG_TRACE_INFO
) {
2090 timestamp
= (double)(now
- start_bias
) / divisor
;
2091 delta
= (double)idelta
/ divisor
;
2096 pc_to_string(&pcstring
[0], kd
->arg1
, 84, KERNEL_MODE
);
2098 fprintf(log_fp
, "%9.1f %8.1f\t\tCQ_action @ %-84.84s %8lx %2d %s\n",
2099 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2103 pc_to_string(&pcstring
[0], kd
->arg1
, 83, KERNEL_MODE
);
2105 fprintf(log_fp
, "%9.1f %8.1f\t\tTES_action @ %-83.83s %8lx %2d %s\n",
2106 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2110 pc_to_string(&pcstring
[0], kd
->arg1
, 83, KERNEL_MODE
);
2112 fprintf(log_fp
, "%9.1f %8.1f\t\tIES_action @ %-83.83s %8lx %2d %s\n",
2113 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2117 pc_to_string(&pcstring
[0], kd
->arg1
, 83, KERNEL_MODE
);
2119 fprintf(log_fp
, "%9.1f %8.1f\t\tIES_filter @ %-83.83s %8lx %2d %s\n",
2120 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2124 if ((int)kd
->arg1
>= 0) {
2127 i_latency
= (((double)(-1 - kd
->arg1
)) / divisor
);
2130 if (i_thresh_hold
&& (int)i_latency
> i_thresh_hold
) {
2142 pc_to_string(&pcstring
[0], kd
->arg2
, 84, mode
);
2144 fprintf(log_fp
, "%9.1f %8.1f[%.1f]%s\tDECR_TRAP @ %-84.84s %8lx %2d %s\n",
2145 timestamp
, delta
, i_latency
, p
, &pcstring
[0], thread
, cpunum
, command
);
2149 fprintf(log_fp
, "%9.1f %8.1f[%.1f] \t%-28.28s %8lx %2d %s\n",
2150 timestamp
, delta
, (double)kd
->arg1
/divisor
, "DECR_SET", thread
, cpunum
, command
);
2154 case MACH_stkhandoff
:
2156 find_thread_name(kd
->arg2
, &command1
);
2158 if (command1
== EMPTYSTRING
) {
2159 command1
= command_buf
;
2160 sprintf(command1
, "%-8lx", kd
->arg2
);
2162 if (thread_in_user_mode(kd
->arg2
, command1
)) {
2170 if (reason
> MAX_REASON
) {
2173 sched_reason
= sched_reasons
[reason
];
2176 if (sched_reason
[0] == '?') {
2177 sprintf(joe
, "%lx", reason
);
2180 sprintf(sched_info
, "%16.16s @ pri %3lu --> %16.16s @ pri %3lu%s", command
, kd
->arg3
, command1
, kd
->arg4
, p
);
2182 fprintf(log_fp
, "%9.1f %8.1f\t\t%-10.10s[%s] %s %8lx %2d\n",
2183 timestamp
, delta
, "MACH_SCHED", sched_reason
, sched_info
, thread
, cpunum
);
2187 if ((lkp
= handle_lookup_event(thread
, debugid
, kd
))) {
2189 * print the tail end of the pathname
2191 p
= (char *)lkp
->lk_pathname
;
2192 size_t clen
= strlen(p
);
2200 fprintf(log_fp
, "%9.1f %8.1f\t\t%-14.14s %-59s %-16lx %8lx %2d %s\n",
2201 timestamp
, delta
, "VFS_LOOKUP",
2202 &p
[clen
], lkp
->lk_dvp
, thread
, cpunum
, command
);
2204 delete_lookup_event(thread
, lkp
);
2209 if (debugid
& DBG_FUNC_START
) {
2210 enter_syscall(log_fp
, kd
, thread
, type
, command
, now
, idelta
, start_bias
, 1);
2211 } else if (debugid
& DBG_FUNC_END
) {
2212 exit_syscall(log_fp
, kd
, thread
, type
, command
, now
, idelta
, start_bias
, 1);
2214 print_entry(log_fp
, kd
, thread
, type
, command
, now
, idelta
, start_bias
, kd_note
);
2221 log_range(kd_buf
*kd_buffer
, kd_buf
*kd_start
, kd_buf
*kd_stop
, kd_buf
*kd_note
, char *buf1
)
2223 uint64_t last_timestamp
= 0;
2225 uint64_t start_bias
= 0;
2231 clen
= strlen(buf1
);
2232 memset(buf2
, '-', clen
);
2234 fprintf(log_fp
, "\n\n%s\n", buf2
);
2235 fprintf(log_fp
, "%s\n\n", buf1
);
2237 fprintf(log_fp
, "RelTime(Us) Delta debugid arg1 arg2 arg3 arg4 thread cpu command\n\n");
2239 reset_thread_names();
2241 last_timestamp
= kd_start
->timestamp
& KDBG_TIMESTAMP_MASK
;
2242 start_bias
= last_timestamp
;
2244 for (kd
= kd_buffer
; kd
<= kd_stop
; kd
++) {
2245 now
= kd
->timestamp
& KDBG_TIMESTAMP_MASK
;
2247 if (kd
>= kd_start
) {
2248 delta
= now
- last_timestamp
;
2250 log_info(now
, delta
, start_bias
, kd
, kd_note
);
2252 last_timestamp
= now
;
2254 int debugid
= kd
->debugid
;
2255 uintptr_t thread
= kd
->arg5
;
2256 int type
= kd
->debugid
& DBG_FUNC_MASK
;
2258 if ((type
>> 24) == DBG_TRACE
) {
2259 if (((type
>> 16) & 0xff) != DBG_TRACE_INFO
) {
2263 if (type
== BSC_thread_terminate
|| type
== BSC_exit
) {
2267 if (debugid
& DBG_FUNC_START
) {
2268 insert_start_event(thread
, type
, now
);
2269 } else if (debugid
& DBG_FUNC_END
) {
2270 (void)consume_start_event(thread
, type
, now
);
2279 log_decrementer(kd_buf
*kd_beg
, kd_buf
*kd_end
, kd_buf
*end_of_sample
, double i_latency
)
2281 kd_buf
*kd_start
, *kd_stop
;
2282 int kd_count
; /* Limit the boundary of kd_start */
2284 double sample_timestamp
;
2287 uintptr_t thread
= kd_beg
->arg5
;
2288 int cpunum
= CPU_NUMBER(kd_end
);
2290 for (kd_count
= 0, kd_start
= kd_beg
- 1; (kd_start
>= (kd_buf
*)my_buffer
); kd_start
--, kd_count
++) {
2291 if (kd_count
== MAX_LOG_COUNT
) {
2295 if (CPU_NUMBER(kd_start
) != cpunum
) {
2299 if ((kd_start
->debugid
& DBG_FUNC_MASK
) == DECR_TRAP
) {
2303 if (kd_start
->arg5
!= thread
) {
2307 if (kd_start
< (kd_buf
*)my_buffer
) {
2308 kd_start
= (kd_buf
*)my_buffer
;
2311 thread
= kd_end
->arg5
;
2313 for (kd_stop
= kd_end
+ 1; kd_stop
< end_of_sample
; kd_stop
++) {
2314 if (CPU_NUMBER(kd_stop
) != cpunum
) {
2318 if ((kd_stop
->debugid
& DBG_FUNC_MASK
) == INTERRUPT
) {
2322 if (kd_stop
->arg5
!= thread
) {
2326 if (kd_stop
>= end_of_sample
) {
2327 kd_stop
= end_of_sample
- 1;
2334 now
= kd_start
->timestamp
& KDBG_TIMESTAMP_MASK
;
2335 sample_timestamp
= (double)(now
- first_now
) / divisor
;
2337 TOD_usecs
= (uint64_t)sample_timestamp
;
2338 TOD_secs
= (unsigned long)sample_TOD_secs
+ (unsigned long)((sample_TOD_usecs
+ TOD_usecs
) / 1000000);
2340 sprintf(buf1
, "%-19.19s interrupt latency = %.1fus [timestamp %.1f]", ctime(&TOD_secs
), i_latency
, sample_timestamp
);
2342 sprintf(buf1
, "%-19.19s interrupt latency = %.1fus [sample %d]", &(ctime(&curr_time
)[0]), i_latency
, sample_generation
);
2345 log_range((kd_buf
*)my_buffer
, kd_start
, kd_stop
, 0, buf1
);
2352 log_scheduler(kd_buf
*kd_beg
, kd_buf
*kd_end
, kd_buf
*end_of_sample
, int s_priority
, double s_latency
, uintptr_t thread
)
2354 kd_buf
*kd_start
, *kd_stop
;
2359 double sample_timestamp
;
2362 for (count
= 0, kd_start
= kd_beg
; (kd_start
>= (kd_buf
*)my_buffer
); kd_start
--) {
2363 cpunum
= CPU_NUMBER(kd_start
);
2365 cmask
|= ((uint64_t)1 << cpunum
);
2367 if (cmask
== cpu_mask
) {
2372 if (kd_start
< (kd_buf
*)my_buffer
) {
2373 kd_start
= (kd_buf
*)my_buffer
;
2376 for (kd_stop
= kd_end
+ 1; kd_stop
< end_of_sample
; kd_stop
++) {
2377 if (kd_stop
->arg5
== thread
) {
2381 if (kd_stop
>= end_of_sample
) {
2382 kd_stop
= end_of_sample
- 1;
2389 now
= kd_start
->timestamp
& KDBG_TIMESTAMP_MASK
;
2390 sample_timestamp
= (double)(now
- first_now
) / divisor
;
2392 TOD_usecs
= (uint64_t)sample_timestamp
;
2393 TOD_secs
= (unsigned long)sample_TOD_secs
+ (unsigned long)((sample_TOD_usecs
+ TOD_usecs
) / 1000000);
2395 sprintf(buf1
, "%-19.19s priority = %d, scheduling latency = %.1fus [timestamp %.1f]", ctime(&TOD_secs
), s_priority
, s_latency
, sample_timestamp
);
2397 sprintf(buf1
, "%-19.19s priority = %d, scheduling latency = %.1fus [sample %d]", &(ctime(&curr_time
)[0]), s_priority
, s_latency
, sample_generation
);
2400 log_range((kd_buf
*)my_buffer
, kd_start
, kd_stop
, kd_beg
, buf1
);
2404 check_for_scheduler_latency(int type
, uintptr_t *thread
, uint64_t now
, kd_buf
*kd
, kd_buf
**kd_start
, int *priority
, double *latency
)
2406 int found_latency
= 0;
2408 if (type
== MACH_makerunnable
) {
2409 if (watch_priority_min
<= kd
->arg2
&& kd
->arg2
<= watch_priority_max
) {
2410 insert_run_event(kd
->arg1
, (int)kd
->arg2
, kd
, now
);
2412 } else if (type
== MACH_sched
|| type
== MACH_stkhandoff
) {
2413 threadrun_t trp
= find_run_event(kd
->arg2
);
2415 if (type
== MACH_sched
|| type
== MACH_stkhandoff
) {
2419 if ((trp
= find_run_event(*thread
))) {
2420 double d_s_latency
= (((double)(now
- trp
->tr_timestamp
)) / divisor
);
2421 int s_latency
= (int)d_s_latency
;
2424 if (s_latency
< 100) {
2425 s_usec_10_bins
[s_latency
/10]++;
2427 if (s_latency
< 1000) {
2428 s_usec_100_bins
[s_latency
/100]++;
2429 } else if (s_latency
< 10000) {
2430 s_msec_1_bins
[s_latency
/1000]++;
2431 } else if (s_latency
< 50000) {
2432 s_msec_10_bins
[s_latency
/10000]++;
2437 if (s_latency
> s_max_latency
) {
2438 s_max_latency
= s_latency
;
2440 if (s_latency
< s_min_latency
|| s_total_samples
== 0) {
2441 s_min_latency
= s_latency
;
2443 s_total_latency
+= s_latency
;
2446 if (s_thresh_hold
&& s_latency
> s_thresh_hold
) {
2447 s_exceeded_threshold
++;
2450 *kd_start
= trp
->tr_entry
;
2451 *priority
= trp
->tr_priority
;
2452 *latency
= d_s_latency
;
2457 delete_run_event(*thread
);
2460 return found_latency
;
2464 handle_decrementer(kd_buf
*kd
, int cpunum
)
2466 struct i_latencies
*il
;
2470 if (i_latency_per_cpu
== FALSE
) {
2474 il
= &i_lat
[cpunum
];
2476 if ((long)(kd
->arg1
) >= 0) {
2479 latency
= (((double)(-1 - kd
->arg1
)) / divisor
);
2481 elapsed_usecs
= (long)latency
;
2483 if (elapsed_usecs
< 100) {
2484 il
->i_usec_10_bins
[elapsed_usecs
/10]++;
2487 if (elapsed_usecs
< 1000) {
2488 il
->i_usec_100_bins
[elapsed_usecs
/100]++;
2489 } else if (elapsed_usecs
< 10000) {
2490 il
->i_msec_1_bins
[elapsed_usecs
/1000]++;
2491 } else if (elapsed_usecs
< 50000) {
2492 il
->i_msec_10_bins
[elapsed_usecs
/10000]++;
2497 if (use_high_res_bins
&& elapsed_usecs
< N_HIGH_RES_BINS
) {
2498 i_high_res_bins
[elapsed_usecs
]++;
2500 if (i_thresh_hold
&& elapsed_usecs
> i_thresh_hold
) {
2501 il
->i_exceeded_threshold
++;
2503 if (elapsed_usecs
> il
->i_max_latency
) {
2504 il
->i_max_latency
= elapsed_usecs
;
2506 if (elapsed_usecs
< il
->i_min_latency
|| il
->i_total_samples
== 0) {
2507 il
->i_min_latency
= elapsed_usecs
;
2509 il
->i_total_latency
+= elapsed_usecs
;
2510 il
->i_total_samples
++;
2519 for (i
= 0; i
< num_of_codes
; i
++) {
2520 if (codes_tab
[i
].type
== type
) {
2521 return codes_tab
[i
].name
;
2528 init_code_file(void)
2533 if ((fp
= fopen(code_file
, "r")) == NULL
) {
2535 fprintf(log_fp
, "open of %s failed\n", code_file
);
2539 for (i
= 0; i
< MAX_ENTRIES
; i
++) {
2542 int n
= fscanf(fp
, "%x%127s\n", &code
, name
);
2544 if (n
== 1 && i
== 0) {
2546 * old code file format, just skip
2554 strncpy(codes_tab
[i
].name
, name
, 32);
2555 codes_tab
[i
].type
= code
;
2568 char tmp_nm_file
[128];
2572 bzero(tmp_nm_file
, 128);
2573 bzero(tmpstr
, 1024);
2576 * Build the temporary nm file path
2578 strcpy(tmp_nm_file
,"/tmp/knm.out.XXXXXX");
2580 if (!mktemp(tmp_nm_file
)) {
2581 fprintf(stderr
, "Error in mktemp call\n");
2586 * Build the nm command and create a tmp file with the output
2588 sprintf (tmpstr
, "/usr/bin/nm -n %s -s __TEXT __text > %s",
2589 kernelpath
, tmp_nm_file
);
2593 * Parse the output from the nm command
2595 if ((fp
= fopen(tmp_nm_file
, "r")) == NULL
) {
2596 /* Hmmm, let's not treat this as fatal */
2597 fprintf(stderr
, "Failed to open nm symbol file [%s]\n", tmp_nm_file
);
2601 * Count the number of symbols in the nm symbol table
2605 while ((inchr
= getc(fp
)) != -1) {
2606 if (inchr
== '\n') {
2613 * Malloc the space for symbol table
2615 if (kern_sym_count
> 0) {
2616 kern_sym_tbl
= malloc(kern_sym_count
* sizeof(kern_sym_t
));
2618 if (!kern_sym_tbl
) {
2620 * Hmmm, lets not treat this as fatal
2622 fprintf(stderr
, "Can't allocate memory for kernel symbol table\n");
2624 bzero(kern_sym_tbl
, kern_sym_count
* sizeof(kern_sym_t
));
2628 * Hmmm, lets not treat this as fatal
2630 fprintf(stderr
, "No kernel symbol table \n");
2632 for (i
= 0; i
< kern_sym_count
; i
++) {
2633 bzero(tmpstr
, 1024);
2635 if (fscanf(fp
, "%p %c %s", &kern_sym_tbl
[i
].k_sym_addr
, &inchr
, tmpstr
) != 3) {
2638 len
= strlen(tmpstr
);
2639 kern_sym_tbl
[i
].k_sym_name
= malloc(len
+ 1);
2641 if (kern_sym_tbl
[i
].k_sym_name
== NULL
) {
2642 fprintf(stderr
, "Can't allocate memory for symbol name [%s]\n", tmpstr
);
2643 kern_sym_tbl
[i
].k_sym_name
= NULL
;
2646 strcpy(kern_sym_tbl
[i
].k_sym_name
, tmpstr
);
2649 kern_sym_tbl
[i
].k_sym_len
= len
;
2652 if (i
!= kern_sym_count
) {
2654 * Hmmm, didn't build up entire table from nm
2655 * scrap the entire thing
2658 kern_sym_tbl
= NULL
;
2664 * Remove the temporary nm file
2666 unlink(tmp_nm_file
);
2669 * Dump the kernel symbol table
2671 for (i
= 0; i
< kern_sym_count
; i
++) {
2672 if (kern_sym_tbl
[i
].k_sym_name
) {
2673 printf ("[%d] %-16p %s\n", i
,
2674 kern_sym_tbl
[i
].k_sym_addr
, kern_sym_tbl
[i
].k_sym_name
);
2676 printf ("[%d] %-16p %s\n", i
,
2677 kern_sym_tbl
[i
].k_sym_addr
, "No symbol name");
2684 pc_to_string(char *pcstring
, uintptr_t pc
, int max_len
, int mode
)
2689 if (mode
== USER_MODE
) {
2690 sprintf(pcstring
, "%-16lx [usermode addr]", pc
);
2693 ret
= binary_search(kern_sym_tbl
, 0, kern_sym_count
-1, pc
);
2695 if (ret
== -1 || kern_sym_tbl
[ret
].k_sym_name
== NULL
) {
2696 sprintf(pcstring
, "%-16lx", pc
);
2699 if ((len
= kern_sym_tbl
[ret
].k_sym_len
) > (max_len
- 8)) {
2703 memcpy(pcstring
, kern_sym_tbl
[ret
].k_sym_name
, len
);
2705 sprintf(&pcstring
[len
], "+0x%-5lx", pc
- (uintptr_t)kern_sym_tbl
[ret
].k_sym_addr
);
2710 * Return -1 if not found, else return index
2713 binary_search(kern_sym_t
*list
, int low
, int high
, uintptr_t addr
)
2717 if (kern_sym_count
== 0) {
2722 return -1; /* failed */
2725 if (low
+ 1 == high
) {
2726 if ((uintptr_t)list
[low
].k_sym_addr
<= addr
&& addr
< (uintptr_t)list
[high
].k_sym_addr
) {
2728 * We have a range match
2732 if ((uintptr_t)list
[high
].k_sym_addr
<= addr
) {
2740 mid
= (low
+ high
) / 2;
2742 if (addr
< (uintptr_t)list
[mid
].k_sym_addr
) {
2743 return binary_search(list
, low
, mid
, addr
);
2746 return binary_search(list
, mid
, high
, addr
);
2750 open_logfile(const char *path
)
2752 log_fp
= fopen(path
, "a");
2756 * failed to open path
2758 fprintf(stderr
, "latency: failed to open logfile [%s]\n", path
);
2764 open_rawfile(const char *path
)
2766 RAW_fd
= open(path
, O_RDONLY
);
2770 * failed to open path
2772 fprintf(stderr
, "latency: failed to open RAWfile [%s]\n", path
);
2780 mach_timebase_info_data_t info
;
2782 (void)mach_timebase_info(&info
);
2784 divisor
= ((double)info
.denom
/ (double)info
.numer
) * 1000;