2 * Copyright (c) 1999-2010 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 u_int 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
;
230 #define HASH_SIZE 1024
231 #define HASH_MASK 1023
233 event_t event_hash
[HASH_SIZE
];
234 lookup_t lookup_hash
[HASH_SIZE
];
235 threadmap_t threadmap_hash
[HASH_SIZE
];
236 threadrun_t threadrun_hash
[HASH_SIZE
];
238 event_t event_freelist
;
239 lookup_t lookup_freelist
;
240 threadrun_t threadrun_freelist
;
241 threadmap_t threadmap_freelist
;
242 threadmap_t threadmap_temp
;
244 thread_entry_t thread_entry_freelist
;
245 thread_entry_t thread_delete_list
;
246 thread_entry_t thread_reset_list
;
247 thread_entry_t thread_event_list
;
248 thread_entry_t thread_lookup_list
;
249 thread_entry_t thread_run_list
;
260 #define RAW_VERSION0 0x55aa0000
261 #define RAW_VERSION1 0x55aa0101
266 #define KERNEL_MODE 1
269 #define TRACE_DATA_NEWTHREAD 0x07000004
270 #define TRACE_STRING_NEWTHREAD 0x07010004
271 #define TRACE_STRING_EXEC 0x07010008
273 #define INTERRUPT 0x01050000
274 #define DECR_TRAP 0x01090000
275 #define DECR_SET 0x01090004
276 #define MACH_vmfault 0x01300008
277 #define MACH_sched 0x01400000
278 #define MACH_stkhandoff 0x01400008
279 #define MACH_makerunnable 0x01400018
280 #define MACH_idle 0x01400024
281 #define VFS_LOOKUP 0x03010090
282 #define IES_action 0x050b0018
283 #define IES_filter 0x050b001c
284 #define TES_action 0x050c0010
285 #define CQ_action 0x050d0018
286 #define CPUPM_CPUSTER_RUNCOUNT 0x05310144
288 #define BSC_exit 0x040C0004
289 #define BSC_thread_terminate 0x040c05a4
291 #define DBG_FUNC_MASK ~(DBG_FUNC_START | DBG_FUNC_END)
293 #define CPU_NUMBER(kp) kdbg_get_cpu(kp)
295 #define EMPTYSTRING ""
298 const char *fault_name
[] = {
310 const char *sched_reasons
[] = {
330 #define ARRAYSIZE(x) ((int)(sizeof(x) / sizeof(*x)))
331 #define MAX_REASON ARRAYSIZE(sched_reasons)
333 static double handle_decrementer(kd_buf
*, int);
334 static kd_buf
*log_decrementer(kd_buf
*kd_beg
, kd_buf
*kd_end
, kd_buf
*end_of_sample
, double i_latency
);
335 static void read_command_map(void);
336 static void enter_syscall(FILE *fp
, kd_buf
*kd
, int thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, int print_info
);
337 static void exit_syscall(FILE *fp
, kd_buf
*kd
, int thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, int print_info
);
338 static void print_entry(FILE *fp
, kd_buf
*kd
, int thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, kd_buf
*kd_note
);
339 static void log_info(uint64_t now
, uint64_t idelta
, uint64_t start_bias
, kd_buf
*kd
, kd_buf
*kd_note
);
340 static char *find_code(int);
341 static void pc_to_string(char *pcstring
, uintptr_t pc
, int max_len
, int mode
);
342 static void getdivisor(void);
343 static int sample_sc(void);
344 static void init_code_file(void);
345 static void do_kernel_nm(void);
346 static void open_logfile(const char*);
347 static int binary_search(kern_sym_t
*list
, int low
, int high
, uintptr_t addr
);
349 static void create_map_entry(uintptr_t, char *);
350 static void check_for_thread_update(uintptr_t thread
, int debugid_base
, kd_buf
*kbufp
, char **command
);
351 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
);
352 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
);
353 static void open_rawfile(const char *path
);
355 static void screen_update(FILE *);
357 static void set_enable(int);
358 static void set_remove(void);
368 * This flag is turned off when calling
369 * quit() due to a set_remove() failure.
371 if (set_remove_flag
) {
385 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDENABLE
, val
};
388 if (sysctl(mib
, ARRAYSIZE(mib
), NULL
, &needed
, NULL
, 0) < 0) {
389 quit("trace facility failure, KERN_KDENABLE\n");
394 set_numbufs(int nbufs
)
396 int mib1
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDSETBUF
, nbufs
};
397 int mib2
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDSETUP
};
400 if (sysctl(mib1
, ARRAYSIZE(mib1
), NULL
, &needed
, NULL
, 0) < 0) {
401 quit("trace facility failure, KERN_KDSETBUF\n");
403 if (sysctl(mib2
, ARRAYSIZE(mib2
), NULL
, &needed
, NULL
, 0) < 0) {
404 quit("trace facility failure, KERN_KDSETUP\n");
409 set_pidexclude(int pid
, int on_off
)
411 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDPIDEX
};
412 size_t needed
= sizeof(kd_regtype
);
415 .type
= KDBG_TYPENONE
,
420 sysctl(mib
, ARRAYSIZE(mib
), &kr
, &needed
, NULL
, 0);
424 get_bufinfo(kbufinfo_t
*val
)
426 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDGETBUF
};
427 size_t needed
= sizeof (*val
);
429 if (sysctl(mib
, ARRAYSIZE(mib
), val
, &needed
, 0, 0) < 0) {
430 quit("trace facility failure, KERN_KDGETBUF\n");
437 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDREMOVE
};
442 if (sysctl(mib
, ARRAYSIZE(mib
), NULL
, &needed
, NULL
, 0) < 0) {
444 if (errno
== EBUSY
) {
445 quit("the trace facility is currently in use...\n fs_usage, sc_usage, and latency use this feature.\n\n");
447 quit("trace facility failure, KERN_KDREMOVE\n");
454 write_high_res_latencies(void)
459 if (use_high_res_bins
) {
460 if ((f
= fopen("latencies.csv","w"))) {
461 for (i
= 0; i
< N_HIGH_RES_BINS
; i
++) {
462 fprintf(f
, "%d,%d\n", i
, i_high_res_bins
[i
]);
470 sigintr(int signo
__attribute__((unused
)))
472 write_high_res_latencies();
475 set_pidexclude(getpid(), 0);
476 screen_update(log_fp
);
483 /* exit under normal conditions -- signal handler */
485 leave(int signo
__attribute__((unused
)))
487 write_high_res_latencies();
490 set_pidexclude(getpid(), 0);
498 sigwinch(int signo
__attribute__((unused
)))
504 print_total(FILE *fp
, char *s
, int total
)
509 struct i_latencies
*il
;
512 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
514 itotal
+= il
->i_total
;
516 clen
= sprintf(tbuf
, "%s %10d %9d", s
, total
, itotal
);
518 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
521 if (i_latency_per_cpu
== TRUE
) {
522 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_total
);
527 sprintf(&tbuf
[clen
], "\n");
529 fprintf(fp
, "%s", tbuf
);
538 screen_update(FILE *fp
)
547 int min_lat
, max_lat
;
549 unsigned int average_s_latency
;
550 unsigned int average_i_latency
;
551 struct i_latencies
*il
;
558 fprintf(fp
,"\n\n===================================================================================================\n");
561 * Display the current time.
562 * "ctime" always returns a string that looks like this:
564 * Sun Sep 16 01:03:52 1973
565 * 012345678901234567890123
568 * We want indices 11 thru 18 (length 8).
571 curr_time
= sample_TOD_secs
;
572 elapsed_secs
= ((last_now
- first_now
) / divisor
) / 1000000;
574 elapsed_secs
= curr_time
- start_time
;
577 elapsed_hours
= elapsed_secs
/ 3600;
578 elapsed_secs
-= elapsed_hours
* 3600;
579 elapsed_mins
= elapsed_secs
/ 60;
580 elapsed_secs
-= elapsed_mins
* 60;
582 sprintf(tbuf
, "%-19.19s %2ld:%02ld:%02ld\n", &(ctime(&curr_time
)[0]),
583 (long)elapsed_hours
, (long)elapsed_mins
, (long)elapsed_secs
);
585 fprintf(fp
, "%s", tbuf
);
590 sprintf(tbuf
, " SCHEDULER INTERRUPTS\n");
592 fprintf(fp
, "%s", tbuf
);
597 if (i_latency_per_cpu
== TRUE
) {
598 clen
= sprintf(tbuf
, " Total");
600 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
602 clen
+= sprintf(&tbuf
[clen
], " CPU %d", cpu
);
604 clen
+= sprintf(&tbuf
[clen
], " CPU %d", cpu
);
608 fprintf(fp
, "%s", tbuf
);
613 clen
= sprintf(tbuf
, "\n-------------------------------------------------------");
615 for (cpu
= 1; cpu
< num_i_latency_cpus
; cpu
++) {
616 clen
+= sprintf(&tbuf
[clen
], "----------");
619 fprintf(fp
, "%s", tbuf
);
624 sprintf(tbuf
, "---------------------------------------------");
626 fprintf(fp
, "%s", tbuf
);
631 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
633 itotal
+= il
->i_total_samples
;
635 clen
= sprintf(tbuf
, "\ntotal_samples %10d %9d", s_total_samples
, itotal
);
637 if (i_latency_per_cpu
== TRUE
) {
638 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
641 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_total_samples
);
644 sprintf(&tbuf
[clen
], "\n");
646 fprintf(fp
, "%s", tbuf
);
652 for (stotal
= 0, i
= 0; i
< 10; i
++) {
653 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
656 itotal
+= il
->i_usec_10_bins
[i
];
657 il
->i_total
+= il
->i_usec_10_bins
[i
];
659 clen
= sprintf(tbuf
, "\ndelays < %3d usecs %10d %9d", (i
+ 1) * 10, s_usec_10_bins
[i
], itotal
);
661 stotal
+= s_usec_10_bins
[i
];
663 if (i_latency_per_cpu
== TRUE
) {
664 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
667 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_usec_10_bins
[i
]);
671 fprintf(fp
, "%s", tbuf
);
676 print_total(fp
, "\ntotal < 100 usecs", stotal
);
678 for (stotal
= 0, i
= 1; i
< 10; i
++) {
679 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
682 itotal
+= il
->i_usec_100_bins
[i
];
683 il
->i_total
+= il
->i_usec_100_bins
[i
];
686 clen
= sprintf(tbuf
, "\ndelays < %3d usecs %10d %9d", (i
+ 1) * 100, s_usec_100_bins
[i
], itotal
);
688 clen
= sprintf(tbuf
, "\ndelays < 1 msec %10d %9d", s_usec_100_bins
[i
], itotal
);
691 stotal
+= s_usec_100_bins
[i
];
693 if (i_latency_per_cpu
== TRUE
) {
694 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
697 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_usec_100_bins
[i
]);
701 fprintf(fp
, "%s", tbuf
);
706 print_total(fp
, "\ntotal < 1 msec ", stotal
);
709 for (stotal
= 0, i
= 1; i
< 10; i
++) {
710 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
713 itotal
+= il
->i_msec_1_bins
[i
];
714 il
->i_total
+= il
->i_msec_1_bins
[i
];
716 clen
= sprintf(tbuf
, "\ndelays < %3d msecs %10d %9d", (i
+ 1), s_msec_1_bins
[i
], itotal
);
718 stotal
+= s_msec_1_bins
[i
];
720 if (i_latency_per_cpu
== TRUE
) {
721 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
724 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_msec_1_bins
[i
]);
728 fprintf(fp
, "%s", tbuf
);
733 print_total(fp
, "\ntotal < 10 msecs", stotal
);
735 for (stotal
= 0, i
= 1; i
< 5; i
++) {
736 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
739 itotal
+= il
->i_msec_10_bins
[i
];
740 il
->i_total
+= il
->i_msec_10_bins
[i
];
742 clen
= sprintf(tbuf
, "\ndelays < %3d msecs %10d %9d", (i
+ 1)*10, s_msec_10_bins
[i
], itotal
);
744 stotal
+= s_msec_10_bins
[i
];
746 if (i_latency_per_cpu
== TRUE
) {
747 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
750 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_msec_10_bins
[i
]);
754 fprintf(fp
, "%s", tbuf
);
759 print_total(fp
, "\ntotal < 50 msecs", stotal
);
762 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
764 itotal
+= il
->i_too_slow
;
766 clen
= sprintf(tbuf
, "\ndelays > 50 msecs %10d %9d", s_too_slow
, itotal
);
768 if (i_latency_per_cpu
== TRUE
) {
769 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
772 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_too_slow
);
776 fprintf(fp
, "%s", tbuf
);
781 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
784 if (cpu
== 0 || (il
->i_min_latency
< min_lat
)) {
785 min_lat
= il
->i_min_latency
;
788 clen
= sprintf(tbuf
, "\n\nminimum latency(usecs) %7d %9d", s_min_latency
, min_lat
);
790 if (i_latency_per_cpu
== TRUE
) {
791 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
794 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_min_latency
);
798 fprintf(fp
, "%s", tbuf
);
804 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
807 if (cpu
== 0 || (il
->i_max_latency
> max_lat
)) {
808 max_lat
= il
->i_max_latency
;
811 clen
= sprintf(tbuf
, "\nmaximum latency(usecs) %7d %9d", s_max_latency
, max_lat
);
813 if (i_latency_per_cpu
== TRUE
) {
814 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
817 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_max_latency
);
821 fprintf(fp
, "%s", tbuf
);
826 if (s_total_samples
) {
827 average_s_latency
= (unsigned int)(s_total_latency
/s_total_samples
);
829 average_s_latency
= 0;
832 for (itotal
= 0, tot_lat
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
835 itotal
+= il
->i_total_samples
;
836 tot_lat
+= il
->i_total_latency
;
839 average_i_latency
= (unsigned)(tot_lat
/itotal
);
841 average_i_latency
= 0;
844 clen
= sprintf(tbuf
, "\naverage latency(usecs) %7d %9d", average_s_latency
, average_i_latency
);
846 if (i_latency_per_cpu
== TRUE
) {
847 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
850 if (il
->i_total_samples
) {
851 average_i_latency
= (unsigned int)(il
->i_total_latency
/il
->i_total_samples
);
853 average_i_latency
= 0;
856 clen
+= sprintf(&tbuf
[clen
], " %9d", average_i_latency
);
860 fprintf(fp
, "%s", tbuf
);
865 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
868 itotal
+= il
->i_exceeded_threshold
;
870 clen
= sprintf(tbuf
, "\nexceeded threshold %7d %9d", s_exceeded_threshold
, itotal
);
872 if (i_latency_per_cpu
== TRUE
) {
873 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
876 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_exceeded_threshold
);
879 sprintf(&tbuf
[clen
], "\n");
882 fprintf(fp
, "%s", tbuf
);
897 fprintf(stderr
, "Usage: latency [-p <priority>] [-h] [-m] [-st <threshold>] [-it <threshold>]\n");
898 fprintf(stderr
, " [-c <codefile>] [-l <logfile>] [-R <rawfile>] [-n <kernel>]\n\n");
900 fprintf(stderr
, " -p specify scheduling priority to watch... default is realtime. Can also be a range, e.g. \"31-47\".\n");
901 fprintf(stderr
, " -h Display high resolution interrupt latencies and write them to latencies.csv (truncate existing file) upon exit.\n");
902 fprintf(stderr
, " -st set scheduler latency threshold in microseconds... if latency exceeds this, then log trace\n");
903 fprintf(stderr
, " -m specify per-CPU interrupt latency reporting\n");
904 fprintf(stderr
, " -it set interrupt latency threshold in microseconds... if latency exceeds this, then log trace\n");
905 fprintf(stderr
, " -c specify name of codes file... default is /usr/share/misc/trace.codes\n");
906 fprintf(stderr
, " -l specify name of file to log trace entries to when the specified threshold is exceeded\n");
907 fprintf(stderr
, " -R specify name of raw trace file to process\n");
908 fprintf(stderr
, " -n specify kernel... default is /System/Library/Kernels/kernel.development\n");
910 fprintf(stderr
, "\nlatency must be run as root\n\n");
917 main(int argc
, char *argv
[])
921 if (0 != reexec_to_match_kernel()) {
922 fprintf(stderr
, "Could not re-execute: %d\n", errno
);
927 if (strcmp(argv
[1], "-R") == 0) {
932 open_rawfile(argv
[1]);
939 } else if (strcmp(argv
[1], "-p") == 0) {
944 if (2 == sscanf(argv
[1], "%d-%d", &watch_priority_min
, &watch_priority_max
)) {
945 if (watch_priority_min
> watch_priority_max
) {
947 } else if (watch_priority_min
< 0) {
951 if (1 == sscanf(argv
[1], "%d", &watch_priority_min
)) {
952 watch_priority_max
= watch_priority_min
;
960 } else if (strcmp(argv
[1], "-st") == 0) {
965 s_thresh_hold
= atoi(argv
[1]);
969 } else if (strcmp(argv
[1], "-it") == 0) {
974 i_thresh_hold
= atoi(argv
[1]);
978 } else if (strcmp(argv
[1], "-c") == 0) {
987 } else if (strcmp(argv
[1], "-l") == 0) {
992 open_logfile(argv
[1]);
996 } else if (strcmp(argv
[1], "-n") == 0) {
1001 kernelpath
= argv
[1];
1005 } else if (strcmp(argv
[1], "-h") == 0) {
1006 use_high_res_bins
= TRUE
;
1008 } else if (strcmp(argv
[1], "-m") == 0) {
1009 i_latency_per_cpu
= TRUE
;
1019 if (geteuid() != 0) {
1020 printf("'latency' must be run as root...\n");
1024 if (kernelpath
== NULL
) {
1025 kernelpath
= "/System/Library/Kernels/kernel.development";
1028 if (code_file
== NULL
) {
1029 code_file
= "/usr/share/misc/trace.codes";
1039 if (initscr() == NULL
) {
1040 printf("Unrecognized TERM type, try vt100\n");
1046 signal(SIGWINCH
, sigwinch
);
1047 signal(SIGINT
, sigintr
);
1048 signal(SIGQUIT
, leave
);
1049 signal(SIGTERM
, leave
);
1050 signal(SIGHUP
, leave
);
1053 * grab the number of cpus and scale the buffer size
1055 int mib
[] = { CTL_HW
, HW_NCPU
};
1056 size_t len
= sizeof(num_cpus
);
1058 sysctl(mib
, ARRAYSIZE(mib
), &num_cpus
, &len
, NULL
, 0);
1061 set_numbufs(SAMPLE_SIZE
* num_cpus
);
1063 get_bufinfo(&bufinfo
);
1067 set_pidexclude(getpid(), 1);
1070 num_entries
= bufinfo
.nkdbufs
;
1072 num_entries
= 50000;
1076 for (cpu_mask
= 0, i
= 0; i
< num_cpus
; i
++)
1077 cpu_mask
|= ((uint64_t)1 << i
);
1079 if ((my_buffer
= malloc(num_entries
* sizeof(kd_buf
))) == NULL
) {
1080 quit("can't allocate memory for tracing info\n");
1083 if ((last_decrementer_kd
= (kd_buf
**)malloc(num_cpus
* sizeof(kd_buf
*))) == NULL
) {
1084 quit("can't allocate memory for decrementer tracing info\n");
1087 if (i_latency_per_cpu
== FALSE
) {
1088 num_i_latency_cpus
= 1;
1090 num_i_latency_cpus
= num_cpus
;
1093 if ((i_lat
= (struct i_latencies
*)malloc(num_i_latency_cpus
* sizeof(struct i_latencies
))) == NULL
) {
1094 quit("can't allocate memory for interrupt latency info\n");
1097 bzero((char *)i_lat
, num_i_latency_cpus
* sizeof(struct i_latencies
));
1100 while (sample_sc()) {
1105 screen_update(log_fp
);
1108 screen_update(stdout
);
1113 double nanosecs_to_sleep
;
1115 nanosecs_to_sleep
= (double)(SAMPLE_TIME_USECS
* 1000);
1116 fdelay
= nanosecs_to_sleep
* (divisor
/1000);
1117 adelay
= (uint64_t)fdelay
;
1121 start_time
= time(NULL
);
1122 refresh_time
= start_time
;
1125 curr_time
= time(NULL
);
1127 if (curr_time
>= refresh_time
) {
1128 screen_update(NULL
);
1129 refresh_time
= curr_time
+ 1;
1131 mach_wait_until(mach_absolute_time() + adelay
);
1137 * No need to check for initscr error return.
1138 * We won't get here if it fails on the first call.
1153 read_command_map(void)
1155 kd_threadmap
*mapptr
= 0;
1156 int total_threads
= 0;
1160 RAW_header header
= {0};
1163 if (read(RAW_fd
, &header
, sizeof(RAW_header
)) != sizeof(RAW_header
)) {
1164 perror("read failed");
1167 if (header
.version_no
!= RAW_VERSION1
) {
1168 header
.version_no
= RAW_VERSION0
;
1169 header
.TOD_secs
= time(NULL
);
1170 header
.TOD_usecs
= 0;
1172 lseek(RAW_fd
, (off_t
)0, SEEK_SET
);
1174 if (read(RAW_fd
, &header
.thread_count
, sizeof(int)) != sizeof(int)) {
1175 perror("read failed");
1179 total_threads
= header
.thread_count
;
1181 sample_TOD_secs
= header
.TOD_secs
;
1182 sample_TOD_usecs
= header
.TOD_usecs
;
1184 if (total_threads
== 0 && header
.version_no
!= RAW_VERSION0
) {
1185 offset
= lseek(RAW_fd
, (off_t
)0, SEEK_CUR
);
1186 offset
= (offset
+ (4095)) & ~4095;
1188 lseek(RAW_fd
, offset
, SEEK_SET
);
1191 total_threads
= bufinfo
.nkdthreads
;
1194 size
= total_threads
* sizeof(kd_threadmap
);
1196 if (size
== 0 || ((mapptr
= (kd_threadmap
*) malloc(size
)) == 0)) {
1199 bzero (mapptr
, size
);
1202 * Now read the threadmap
1205 if (read(RAW_fd
, mapptr
, size
) != size
) {
1206 printf("Can't read the thread map -- this is not fatal\n");
1208 if (header
.version_no
!= RAW_VERSION0
) {
1209 offset
= lseek(RAW_fd
, (off_t
)0, SEEK_CUR
);
1210 offset
= (offset
+ (4095)) & ~4095;
1212 lseek(RAW_fd
, offset
, SEEK_SET
);
1215 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDTHRMAP
};
1216 if (sysctl(mib
, ARRAYSIZE(mib
), mapptr
, &size
, NULL
, 0) < 0) {
1218 * This is not fatal -- just means I cant map command strings
1220 printf("Can't read the thread map -- this is not fatal\n");
1225 for (i
= 0; i
< total_threads
; i
++) {
1226 create_map_entry(mapptr
[i
].thread
, &mapptr
[i
].command
[0]);
1232 create_map_entry(uintptr_t thread
, char *command
)
1236 if ((tme
= threadmap_freelist
)) {
1237 threadmap_freelist
= tme
->tm_next
;
1239 tme
= (threadmap_t
)malloc(sizeof(struct threadmap
));
1242 tme
->tm_thread
= thread
;
1244 (void)strncpy (tme
->tm_command
, command
, MAXCOMLEN
);
1245 tme
->tm_command
[MAXCOMLEN
] = '\0';
1246 tme
->tm_orig_command
[0] = '\0';
1248 int hashid
= thread
& HASH_MASK
;
1250 tme
->tm_next
= threadmap_hash
[hashid
];
1251 threadmap_hash
[hashid
] = tme
;
1255 delete_thread_entry(uintptr_t thread
)
1259 int hashid
= thread
& HASH_MASK
;
1261 if ((tme
= threadmap_hash
[hashid
])) {
1262 if (tme
->tm_thread
== thread
) {
1263 threadmap_hash
[hashid
] = tme
->tm_next
;
1265 threadmap_t tme_prev
= tme
;
1267 for (tme
= tme
->tm_next
; tme
; tme
= tme
->tm_next
) {
1268 if (tme
->tm_thread
== thread
) {
1269 tme_prev
->tm_next
= tme
->tm_next
;
1276 tme
->tm_next
= threadmap_freelist
;
1277 threadmap_freelist
= tme
;
1283 find_and_insert_tmp_map_entry(uintptr_t pthread
, char *command
)
1287 if ((tme
= threadmap_temp
)) {
1288 if (tme
->tm_pthread
== pthread
) {
1289 threadmap_temp
= tme
->tm_next
;
1291 threadmap_t tme_prev
= tme
;
1293 for (tme
= tme
->tm_next
; tme
; tme
= tme
->tm_next
) {
1294 if (tme
->tm_pthread
== pthread
) {
1295 tme_prev
->tm_next
= tme
->tm_next
;
1302 (void)strncpy (tme
->tm_command
, command
, MAXCOMLEN
);
1303 tme
->tm_command
[MAXCOMLEN
] = '\0';
1304 tme
->tm_orig_command
[0] = '\0';
1306 int hashid
= tme
->tm_thread
& HASH_MASK
;
1307 tme
->tm_next
= threadmap_hash
[hashid
];
1308 threadmap_hash
[hashid
] = tme
;
1314 create_tmp_map_entry(uintptr_t thread
, uintptr_t pthread
)
1318 if ((tme
= threadmap_freelist
)) {
1319 threadmap_freelist
= tme
->tm_next
;
1321 tme
= malloc(sizeof(struct threadmap
));
1324 tme
->tm_thread
= thread
;
1325 tme
->tm_pthread
= pthread
;
1326 tme
->tm_command
[0] = '\0';
1327 tme
->tm_orig_command
[0] = '\0';
1329 tme
->tm_next
= threadmap_temp
;
1330 threadmap_temp
= tme
;
1334 find_thread_entry(uintptr_t thread
)
1338 int hashid
= thread
& HASH_MASK
;
1340 for (tme
= threadmap_hash
[hashid
]; tme
; tme
= tme
->tm_next
) {
1341 if (tme
->tm_thread
== thread
) {
1349 find_thread_name(uintptr_t thread
, char **command
)
1353 if ((tme
= find_thread_entry(thread
))) {
1354 *command
= tme
->tm_command
;
1356 *command
= EMPTYSTRING
;
1361 add_thread_entry_to_list(thread_entry_t
*list
, uintptr_t thread
)
1365 if ((te
= thread_entry_freelist
)) {
1366 thread_entry_freelist
= te
->te_next
;
1368 te
= (thread_entry_t
)malloc(sizeof(struct thread_entry
));
1371 te
->te_thread
= thread
;
1372 te
->te_next
= *list
;
1377 exec_thread_entry(uintptr_t thread
, char *command
)
1381 if ((tme
= find_thread_entry(thread
))) {
1382 if (tme
->tm_orig_command
[0] == '\0') {
1383 (void)strncpy (tme
->tm_orig_command
, tme
->tm_command
, MAXCOMLEN
);
1384 tme
->tm_orig_command
[MAXCOMLEN
] = '\0';
1386 (void)strncpy (tme
->tm_command
, command
, MAXCOMLEN
);
1387 tme
->tm_command
[MAXCOMLEN
] = '\0';
1389 add_thread_entry_to_list(&thread_reset_list
, thread
);
1391 create_map_entry(thread
, command
);
1396 record_thread_entry_for_gc(uintptr_t thread
)
1398 add_thread_entry_to_list(&thread_delete_list
, thread
);
1402 gc_thread_entries(void)
1405 thread_entry_t te_next
;
1408 for (te
= thread_delete_list
; te
; te
= te_next
) {
1409 delete_thread_entry(te
->te_thread
);
1411 te_next
= te
->te_next
;
1412 te
->te_next
= thread_entry_freelist
;
1413 thread_entry_freelist
= te
;
1417 thread_delete_list
= 0;
1421 gc_reset_entries(void)
1424 thread_entry_t te_next
;
1427 for (te
= thread_reset_list
; te
; te
= te_next
) {
1428 te_next
= te
->te_next
;
1429 te
->te_next
= thread_entry_freelist
;
1430 thread_entry_freelist
= te
;
1434 thread_reset_list
= 0;
1438 reset_thread_names(void)
1441 thread_entry_t te_next
;
1444 for (te
= thread_reset_list
; te
; te
= te_next
) {
1447 if ((tme
= find_thread_entry(te
->te_thread
))) {
1448 if (tme
->tm_orig_command
[0]) {
1449 (void)strncpy (tme
->tm_command
, tme
->tm_orig_command
, MAXCOMLEN
);
1450 tme
->tm_command
[MAXCOMLEN
] = '\0';
1451 tme
->tm_orig_command
[0] = '\0';
1454 te_next
= te
->te_next
;
1455 te
->te_next
= thread_entry_freelist
;
1456 thread_entry_freelist
= te
;
1460 thread_reset_list
= 0;
1464 delete_all_thread_entries(void)
1466 threadmap_t tme
= 0;
1467 threadmap_t tme_next
= 0;
1470 for (i
= 0; i
< HASH_SIZE
; i
++) {
1471 for (tme
= threadmap_hash
[i
]; tme
; tme
= tme_next
) {
1472 tme_next
= tme
->tm_next
;
1473 tme
->tm_next
= threadmap_freelist
;
1474 threadmap_freelist
= tme
;
1476 threadmap_hash
[i
] = 0;
1484 insert_run_event(uintptr_t thread
, int priority
, kd_buf
*kd
, uint64_t now
)
1488 int hashid
= thread
& HASH_MASK
;
1490 for (trp
= threadrun_hash
[hashid
]; trp
; trp
= trp
->tr_next
) {
1491 if (trp
->tr_thread
== thread
) {
1496 if ((trp
= threadrun_freelist
)) {
1497 threadrun_freelist
= trp
->tr_next
;
1499 trp
= (threadrun_t
)malloc(sizeof(struct threadrun
));
1502 trp
->tr_thread
= thread
;
1504 trp
->tr_next
= threadrun_hash
[hashid
];
1505 threadrun_hash
[hashid
] = trp
;
1507 add_thread_entry_to_list(&thread_run_list
, thread
);
1510 trp
->tr_timestamp
= now
;
1511 trp
->tr_priority
= priority
;
1515 find_run_event(uintptr_t thread
)
1518 int hashid
= thread
& HASH_MASK
;
1520 for (trp
= threadrun_hash
[hashid
]; trp
; trp
= trp
->tr_next
) {
1521 if (trp
->tr_thread
== thread
) {
1529 delete_run_event(uintptr_t thread
)
1531 threadrun_t trp
= 0;
1532 threadrun_t trp_prev
;
1534 int hashid
= thread
& HASH_MASK
;
1536 if ((trp
= threadrun_hash
[hashid
])) {
1537 if (trp
->tr_thread
== thread
) {
1538 threadrun_hash
[hashid
] = trp
->tr_next
;
1542 for (trp
= trp
->tr_next
; trp
; trp
= trp
->tr_next
) {
1543 if (trp
->tr_thread
== thread
) {
1544 trp_prev
->tr_next
= trp
->tr_next
;
1551 trp
->tr_next
= threadrun_freelist
;
1552 threadrun_freelist
= trp
;
1558 gc_run_events(void) {
1560 thread_entry_t te_next
;
1562 threadrun_t trp_next
;
1565 for (te
= thread_run_list
; te
; te
= te_next
) {
1566 int hashid
= te
->te_thread
& HASH_MASK
;
1568 for (trp
= threadrun_hash
[hashid
]; trp
; trp
= trp_next
) {
1569 trp_next
= trp
->tr_next
;
1570 trp
->tr_next
= threadrun_freelist
;
1571 threadrun_freelist
= trp
;
1574 threadrun_hash
[hashid
] = 0;
1576 te_next
= te
->te_next
;
1577 te
->te_next
= thread_entry_freelist
;
1578 thread_entry_freelist
= te
;
1580 thread_run_list
= 0;
1586 insert_start_event(uintptr_t thread
, int type
, uint64_t now
)
1590 int hashid
= thread
& HASH_MASK
;
1592 for (evp
= event_hash
[hashid
]; evp
; evp
= evp
->ev_next
) {
1593 if (evp
->ev_thread
== thread
&& evp
->ev_type
== type
) {
1598 if ((evp
= event_freelist
)) {
1599 event_freelist
= evp
->ev_next
;
1601 evp
= (event_t
)malloc(sizeof(struct event
));
1604 evp
->ev_thread
= thread
;
1605 evp
->ev_type
= type
;
1607 evp
->ev_next
= event_hash
[hashid
];
1608 event_hash
[hashid
] = evp
;
1610 add_thread_entry_to_list(&thread_event_list
, thread
);
1612 evp
->ev_timestamp
= now
;
1617 consume_start_event(uintptr_t thread
, int type
, uint64_t now
)
1621 uint64_t elapsed
= 0;
1623 int hashid
= thread
& HASH_MASK
;
1625 if ((evp
= event_hash
[hashid
])) {
1626 if (evp
->ev_thread
== thread
&& evp
->ev_type
== type
) {
1627 event_hash
[hashid
] = evp
->ev_next
;
1631 for (evp
= evp
->ev_next
; evp
; evp
= evp
->ev_next
) {
1632 if (evp
->ev_thread
== thread
&& evp
->ev_type
== type
) {
1633 evp_prev
->ev_next
= evp
->ev_next
;
1640 elapsed
= now
- evp
->ev_timestamp
;
1642 if (now
< evp
->ev_timestamp
) {
1643 printf("consume: now = %qd, timestamp = %qd\n", now
, evp
->ev_timestamp
);
1646 evp
->ev_next
= event_freelist
;
1647 event_freelist
= evp
;
1654 gc_start_events(void)
1657 thread_entry_t te_next
;
1663 for (te
= thread_event_list
; te
; te
= te_next
) {
1665 hashid
= te
->te_thread
& HASH_MASK
;
1667 for (evp
= event_hash
[hashid
]; evp
; evp
= evp_next
) {
1668 evp_next
= evp
->ev_next
;
1669 evp
->ev_next
= event_freelist
;
1670 event_freelist
= evp
;
1673 event_hash
[hashid
] = 0;
1675 te_next
= te
->te_next
;
1676 te
->te_next
= thread_entry_freelist
;
1677 thread_entry_freelist
= te
;
1679 thread_event_list
= 0;
1683 thread_in_user_mode(uintptr_t thread
, char *command
)
1687 if (strcmp(command
, "kernel_task") == 0) {
1691 int hashid
= thread
& HASH_MASK
;
1693 for (evp
= event_hash
[hashid
]; evp
; evp
= evp
->ev_next
) {
1694 if (evp
->ev_thread
== thread
) {
1704 handle_lookup_event(uintptr_t thread
, int debugid
, kd_buf
*kdp
)
1707 boolean_t first_record
= FALSE
;
1709 int hashid
= thread
& HASH_MASK
;
1711 if (debugid
& DBG_FUNC_START
) {
1712 first_record
= TRUE
;
1715 for (lkp
= lookup_hash
[hashid
]; lkp
; lkp
= lkp
->lk_next
) {
1716 if (lkp
->lk_thread
== thread
) {
1721 if (first_record
== FALSE
) {
1725 if ((lkp
= lookup_freelist
)) {
1726 lookup_freelist
= lkp
->lk_next
;
1728 lkp
= (lookup_t
)malloc(sizeof(struct lookup
));
1731 lkp
->lk_thread
= thread
;
1733 lkp
->lk_next
= lookup_hash
[hashid
];
1734 lookup_hash
[hashid
] = lkp
;
1736 add_thread_entry_to_list(&thread_lookup_list
, thread
);
1739 if (first_record
== TRUE
) {
1740 lkp
->lk_pathptr
= lkp
->lk_pathname
;
1741 lkp
->lk_dvp
= kdp
->arg1
;
1743 if (lkp
->lk_pathptr
> &lkp
->lk_pathname
[NUMPARMS
-4]) {
1746 *lkp
->lk_pathptr
++ = kdp
->arg1
;
1748 *lkp
->lk_pathptr
++ = kdp
->arg2
;
1749 *lkp
->lk_pathptr
++ = kdp
->arg3
;
1750 *lkp
->lk_pathptr
++ = kdp
->arg4
;
1751 *lkp
->lk_pathptr
= 0;
1753 if (debugid
& DBG_FUNC_END
) {
1761 delete_lookup_event(uintptr_t thread
, lookup_t lkp_to_delete
)
1767 hashid
= thread
& HASH_MASK
;
1769 if ((lkp
= lookup_hash
[hashid
])) {
1770 if (lkp
== lkp_to_delete
) {
1771 lookup_hash
[hashid
] = lkp
->lk_next
;
1775 for (lkp
= lkp
->lk_next
; lkp
; lkp
= lkp
->lk_next
) {
1776 if (lkp
== lkp_to_delete
) {
1777 lkp_prev
->lk_next
= lkp
->lk_next
;
1784 lkp
->lk_next
= lookup_freelist
;
1785 lookup_freelist
= lkp
;
1791 gc_lookup_events(void) {
1793 thread_entry_t te_next
;
1799 for (te
= thread_lookup_list
; te
; te
= te_next
) {
1800 hashid
= te
->te_thread
& HASH_MASK
;
1802 for (lkp
= lookup_hash
[hashid
]; lkp
; lkp
= lkp_next
) {
1803 lkp_next
= lkp
->lk_next
;
1804 lkp
->lk_next
= lookup_freelist
;
1805 lookup_freelist
= lkp
;
1808 lookup_hash
[hashid
] = 0;
1810 te_next
= te
->te_next
;
1811 te
->te_next
= thread_entry_freelist
;
1812 thread_entry_freelist
= te
;
1814 thread_lookup_list
= 0;
1820 kd_buf
*kd
, *end_of_sample
;
1826 * Get kernel buffer information
1828 get_bufinfo(&bufinfo
);
1831 delete_all_thread_entries();
1836 uint32_t bytes_read
;
1838 bytes_read
= read(RAW_fd
, my_buffer
, num_entries
* sizeof(kd_buf
));
1840 if (bytes_read
== -1) {
1841 perror("read failed");
1844 count
= bytes_read
/ sizeof(kd_buf
);
1846 if (count
!= num_entries
) {
1851 kd
= (kd_buf
*)my_buffer
;
1852 first_now
= kd
->timestamp
& KDBG_TIMESTAMP_MASK
;
1857 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDREADTR
};
1858 size_t needed
= bufinfo
.nkdbufs
* sizeof(kd_buf
);
1860 if (sysctl(mib
, ARRAYSIZE(mib
), my_buffer
, &needed
, NULL
, 0) < 0) {
1861 quit("trace facility failure, KERN_KDREADTR\n");
1865 sample_generation
++;
1867 if (bufinfo
.flags
& KDBG_WRAPPED
) {
1871 fprintf(log_fp
, "\n\n%-19.19s sample = %d <<<<<<< trace buffer wrapped >>>>>>>\n\n",
1872 &(ctime(&curr_time
)[0]), sample_generation
);
1878 end_of_sample
= &((kd_buf
*)my_buffer
)[count
];
1881 * Always reinitialize the DECR_TRAP array
1883 for (i
= 0; i
< num_cpus
; i
++) {
1884 last_decrementer_kd
[i
] = (kd_buf
*)my_buffer
;
1887 for (kd
= (kd_buf
*)my_buffer
; kd
< end_of_sample
; kd
++) {
1889 uintptr_t thread
= kd
->arg5
;
1890 int type
= kd
->debugid
& DBG_FUNC_MASK
;
1892 (void)check_for_thread_update(thread
, type
, kd
, NULL
);
1894 uint64_t now
= kd
->timestamp
& KDBG_TIMESTAMP_MASK
;
1897 if (type
== DECR_TRAP
) {
1898 int cpunum
= CPU_NUMBER(kd
);
1899 double i_latency
= handle_decrementer(kd
, cpunum
);
1902 if (i_thresh_hold
&& (int)i_latency
> i_thresh_hold
) {
1903 kd_start
= last_decrementer_kd
[cpunum
];
1905 log_decrementer(kd_start
, kd
, end_of_sample
, i_latency
);
1907 last_decrementer_kd
[cpunum
] = kd
;
1912 if (check_for_scheduler_latency(type
, &thread
, now
, kd
, &kd_start
, &s_priority
, &s_latency
)) {
1913 log_scheduler(kd_start
, kd
, end_of_sample
, s_priority
, s_latency
, thread
);
1921 gc_thread_entries();
1931 enter_syscall(FILE *fp
, kd_buf
*kd
, int thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, int print_info
)
1938 int cpunum
= CPU_NUMBER(kd
);
1940 if (print_info
&& fp
) {
1941 timestamp
= (double)(now
- start_bias
) / divisor
;
1942 delta
= (double)idelta
/ divisor
;
1944 if ((p
= find_code(type
))) {
1945 if (type
== INTERRUPT
) {
1954 pc_to_string(&pcstring
[0], kd
->arg2
, 58, mode
);
1956 fprintf(fp
, "%9.1f %8.1f\t\tINTERRUPT[%2lx] @ %-58.58s %8x %2d %s\n",
1957 timestamp
, delta
, kd
->arg1
, &pcstring
[0], thread
, cpunum
, command
);
1958 } else if (type
== MACH_vmfault
) {
1959 fprintf(fp
, "%9.1f %8.1f\t\t%-28.28s %8x %2d %s\n",
1960 timestamp
, delta
, p
, thread
, cpunum
, command
);
1962 fprintf(fp
, "%9.1f %8.1f\t\t%-28.28s %-16lx %-16lx %-16lx %-16lx %8x %2d %s\n",
1963 timestamp
, delta
, p
, kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
,
1964 thread
, cpunum
, command
);
1967 fprintf(fp
, "%9.1f %8.1f\t\t%-8x %-16lx %-16lx %-16lx %-16lx %8x %2d %s\n",
1968 timestamp
, delta
, type
, kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
,
1969 thread
, cpunum
, command
);
1972 if (type
!= BSC_thread_terminate
&& type
!= BSC_exit
) {
1973 insert_start_event(thread
, type
, now
);
1979 exit_syscall(FILE *fp
, kd_buf
*kd
, int thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, int print_info
)
1985 double elapsed_timestamp
;
1987 elapsed_timestamp
= (double)consume_start_event(thread
, type
, now
) / divisor
;
1989 if (print_info
&& fp
) {
1990 int cpunum
= CPU_NUMBER(kd
);
1992 timestamp
= (double)(now
- start_bias
) / divisor
;
1993 delta
= (double)idelta
/ divisor
;
1995 fprintf(fp
, "%9.1f %8.1f(%.1f) \t", timestamp
, delta
, elapsed_timestamp
);
1997 if ((p
= find_code(type
))) {
1998 if (type
== INTERRUPT
) {
1999 fprintf(fp
, "INTERRUPT %8x %2d %s\n", thread
, cpunum
, command
);
2000 } else if (type
== MACH_vmfault
&& kd
->arg4
<= DBG_PAGEIND_FAULT
) {
2001 user_addr
= ((uint64_t)kd
->arg1
<< 32) | (uint32_t)kd
->arg2
;
2003 fprintf(fp
, "%-28.28s %-10.10s %-16qx %8x %2d %s\n",
2004 p
, fault_name
[kd
->arg4
], user_addr
,
2005 thread
, cpunum
, command
);
2007 fprintf(fp
, "%-28.28s %-16lx %-16lx %8x %2d %s\n",
2008 p
, kd
->arg1
, kd
->arg2
,
2009 thread
, cpunum
, command
);
2012 fprintf(fp
, "%-8x %-16lx %-16lx %8x %2d %s\n",
2013 type
, kd
->arg1
, kd
->arg2
,
2014 thread
, cpunum
, command
);
2021 print_entry(FILE *fp
, kd_buf
*kd
, int thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, kd_buf
*kd_note
)
2029 int cpunum
= CPU_NUMBER(kd
);
2031 double timestamp
= (double)(now
- start_bias
) / divisor
;
2032 double delta
= (double)idelta
/ divisor
;
2034 if ((p
= find_code(type
))) {
2035 if (kd
== kd_note
) {
2036 fprintf(fp
, "%9.1f %8.1f\t**\t", timestamp
, delta
);
2038 fprintf(fp
, "%9.1f %8.1f\t\t", timestamp
, delta
);
2040 fprintf(fp
, "%-28.28s %-16lx %-16lx %-16lx %-16lx %8x %2d %s\n",
2041 p
, kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
, thread
, cpunum
, command
);
2043 fprintf(fp
, "%9.1f %8.1f\t\t%-8x %-16lx %-16lx %-16lx %-16lx %8x %2d %s\n",
2044 timestamp
, delta
, type
, kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
,
2045 thread
, cpunum
, command
);
2051 check_for_thread_update(uintptr_t thread
, int debugid_base
, kd_buf
*kbufp
, char **command
)
2053 if (debugid_base
== TRACE_DATA_NEWTHREAD
) {
2055 * Save the create thread data
2057 create_tmp_map_entry(kbufp
->arg1
, thread
);
2058 } else if (debugid_base
== TRACE_STRING_NEWTHREAD
) {
2060 * process new map entry
2062 find_and_insert_tmp_map_entry(thread
, (char *)&kbufp
->arg1
);
2063 } else if (debugid_base
== TRACE_STRING_EXEC
) {
2064 exec_thread_entry(thread
, (char *)&kbufp
->arg1
);
2066 if (debugid_base
== BSC_exit
|| debugid_base
== BSC_thread_terminate
) {
2067 record_thread_entry_for_gc(thread
);
2070 find_thread_name(thread
, command
);
2077 log_info(uint64_t now
, uint64_t idelta
, uint64_t start_bias
, kd_buf
*kd
, kd_buf
*kd_note
)
2085 char command_buf
[32];
2086 char sched_info
[64];
2088 const char *sched_reason
;
2094 int thread
= kd
->arg5
;
2095 int cpunum
= CPU_NUMBER(kd
);
2096 int debugid
= kd
->debugid
;
2097 int type
= kd
->debugid
& DBG_FUNC_MASK
;
2099 (void)check_for_thread_update(thread
, type
, kd
, &command
);
2101 if ((type
>> 24) == DBG_TRACE
) {
2102 if (((type
>> 16) & 0xff) != DBG_TRACE_INFO
) {
2106 timestamp
= (double)(now
- start_bias
) / divisor
;
2107 delta
= (double)idelta
/ divisor
;
2112 pc_to_string(&pcstring
[0], kd
->arg1
, 84, KERNEL_MODE
);
2114 fprintf(log_fp
, "%9.1f %8.1f\t\tCQ_action @ %-84.84s %8x %2d %s\n",
2115 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2119 pc_to_string(&pcstring
[0], kd
->arg1
, 83, KERNEL_MODE
);
2121 fprintf(log_fp
, "%9.1f %8.1f\t\tTES_action @ %-83.83s %8x %2d %s\n",
2122 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2126 pc_to_string(&pcstring
[0], kd
->arg1
, 83, KERNEL_MODE
);
2128 fprintf(log_fp
, "%9.1f %8.1f\t\tIES_action @ %-83.83s %8x %2d %s\n",
2129 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2133 pc_to_string(&pcstring
[0], kd
->arg1
, 83, KERNEL_MODE
);
2135 fprintf(log_fp
, "%9.1f %8.1f\t\tIES_filter @ %-83.83s %8x %2d %s\n",
2136 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2140 if ((int)kd
->arg1
>= 0) {
2143 i_latency
= (((double)(-1 - kd
->arg1
)) / divisor
);
2146 if (i_thresh_hold
&& (int)i_latency
> i_thresh_hold
) {
2158 pc_to_string(&pcstring
[0], kd
->arg2
, 84, mode
);
2160 fprintf(log_fp
, "%9.1f %8.1f[%.1f]%s\tDECR_TRAP @ %-84.84s %8x %2d %s\n",
2161 timestamp
, delta
, i_latency
, p
, &pcstring
[0], thread
, cpunum
, command
);
2165 fprintf(log_fp
, "%9.1f %8.1f[%.1f] \t%-28.28s %8x %2d %s\n",
2166 timestamp
, delta
, (double)kd
->arg1
/divisor
, "DECR_SET", thread
, cpunum
, command
);
2170 case MACH_stkhandoff
:
2172 find_thread_name(kd
->arg2
, &command1
);
2174 if (command1
== EMPTYSTRING
) {
2175 command1
= command_buf
;
2176 sprintf(command1
, "%-8lx", kd
->arg2
);
2178 if (thread_in_user_mode(kd
->arg2
, command1
)) {
2186 if (reason
> MAX_REASON
) {
2189 sched_reason
= sched_reasons
[reason
];
2192 if (sched_reason
[0] == '?') {
2193 sprintf(joe
, "%x", reason
);
2196 sprintf(sched_info
, "%16.16s @ pri %3lu --> %16.16s @ pri %3lu%s", command
, kd
->arg3
, command1
, kd
->arg4
, p
);
2198 fprintf(log_fp
, "%9.1f %8.1f\t\t%-10.10s[%s] %s %8x %2d\n",
2199 timestamp
, delta
, "MACH_SCHED", sched_reason
, sched_info
, thread
, cpunum
);
2203 if ((lkp
= handle_lookup_event(thread
, debugid
, kd
))) {
2205 * print the tail end of the pathname
2207 p
= (char *)lkp
->lk_pathname
;
2208 int clen
= strlen(p
);
2216 fprintf(log_fp
, "%9.1f %8.1f\t\t%-14.14s %-59s %-16lx %8x %2d %s\n",
2217 timestamp
, delta
, "VFS_LOOKUP",
2218 &p
[clen
], lkp
->lk_dvp
, thread
, cpunum
, command
);
2220 delete_lookup_event(thread
, lkp
);
2225 if (debugid
& DBG_FUNC_START
) {
2226 enter_syscall(log_fp
, kd
, thread
, type
, command
, now
, idelta
, start_bias
, 1);
2227 } else if (debugid
& DBG_FUNC_END
) {
2228 exit_syscall(log_fp
, kd
, thread
, type
, command
, now
, idelta
, start_bias
, 1);
2230 print_entry(log_fp
, kd
, thread
, type
, command
, now
, idelta
, start_bias
, kd_note
);
2239 log_range(kd_buf
*kd_buffer
, kd_buf
*kd_start
, kd_buf
*kd_stop
, kd_buf
*kd_note
, char *buf1
)
2241 uint64_t last_timestamp
= 0;
2243 uint64_t start_bias
= 0;
2249 clen
= strlen(buf1
);
2250 memset(buf2
, '-', clen
);
2252 fprintf(log_fp
, "\n\n%s\n", buf2
);
2253 fprintf(log_fp
, "%s\n\n", buf1
);
2255 fprintf(log_fp
, "RelTime(Us) Delta debugid arg1 arg2 arg3 arg4 thread cpu command\n\n");
2257 reset_thread_names();
2259 last_timestamp
= kd_start
->timestamp
& KDBG_TIMESTAMP_MASK
;
2260 start_bias
= last_timestamp
;
2262 for (kd
= kd_buffer
; kd
<= kd_stop
; kd
++) {
2263 now
= kd
->timestamp
& KDBG_TIMESTAMP_MASK
;
2265 if (kd
>= kd_start
) {
2266 delta
= now
- last_timestamp
;
2268 log_info(now
, delta
, start_bias
, kd
, kd_note
);
2270 last_timestamp
= now
;
2272 int debugid
= kd
->debugid
;
2273 int thread
= kd
->arg5
;
2274 int type
= kd
->debugid
& DBG_FUNC_MASK
;
2276 if ((type
>> 24) == DBG_TRACE
) {
2277 if (((type
>> 16) & 0xff) != DBG_TRACE_INFO
) {
2281 if (type
== BSC_thread_terminate
|| type
== BSC_exit
) {
2285 if (debugid
& DBG_FUNC_START
) {
2286 insert_start_event(thread
, type
, now
);
2287 } else if (debugid
& DBG_FUNC_END
) {
2288 (void)consume_start_event(thread
, type
, now
);
2298 log_decrementer(kd_buf
*kd_beg
, kd_buf
*kd_end
, kd_buf
*end_of_sample
, double i_latency
)
2300 kd_buf
*kd_start
, *kd_stop
;
2301 int kd_count
; /* Limit the boundary of kd_start */
2303 double sample_timestamp
;
2306 int thread
= kd_beg
->arg5
;
2307 int cpunum
= CPU_NUMBER(kd_end
);
2309 for (kd_count
= 0, kd_start
= kd_beg
- 1; (kd_start
>= (kd_buf
*)my_buffer
); kd_start
--, kd_count
++) {
2310 if (kd_count
== MAX_LOG_COUNT
) {
2314 if (CPU_NUMBER(kd_start
) != cpunum
) {
2318 if ((kd_start
->debugid
& DBG_FUNC_MASK
) == DECR_TRAP
) {
2322 if (kd_start
->arg5
!= thread
) {
2326 if (kd_start
< (kd_buf
*)my_buffer
) {
2327 kd_start
= (kd_buf
*)my_buffer
;
2330 thread
= kd_end
->arg5
;
2332 for (kd_stop
= kd_end
+ 1; kd_stop
< end_of_sample
; kd_stop
++) {
2333 if (CPU_NUMBER(kd_stop
) != cpunum
) {
2337 if ((kd_stop
->debugid
& DBG_FUNC_MASK
) == INTERRUPT
) {
2341 if (kd_stop
->arg5
!= thread
) {
2345 if (kd_stop
>= end_of_sample
) {
2346 kd_stop
= end_of_sample
- 1;
2353 now
= kd_start
->timestamp
& KDBG_TIMESTAMP_MASK
;
2354 sample_timestamp
= (double)(now
- first_now
) / divisor
;
2356 TOD_usecs
= (uint64_t)sample_timestamp
;
2357 TOD_secs
= sample_TOD_secs
+ ((sample_TOD_usecs
+ TOD_usecs
) / 1000000);
2359 sprintf(buf1
, "%-19.19s interrupt latency = %.1fus [timestamp %.1f]", ctime(&TOD_secs
), i_latency
, sample_timestamp
);
2361 sprintf(buf1
, "%-19.19s interrupt latency = %.1fus [sample %d]", &(ctime(&curr_time
)[0]), i_latency
, sample_generation
);
2364 log_range((kd_buf
*)my_buffer
, kd_start
, kd_stop
, 0, buf1
);
2371 log_scheduler(kd_buf
*kd_beg
, kd_buf
*kd_end
, kd_buf
*end_of_sample
, int s_priority
, double s_latency
, uintptr_t thread
)
2373 kd_buf
*kd_start
, *kd_stop
;
2378 double sample_timestamp
;
2381 for (count
= 0, kd_start
= kd_beg
; (kd_start
>= (kd_buf
*)my_buffer
); kd_start
--) {
2382 cpunum
= CPU_NUMBER(kd_start
);
2384 cmask
|= ((uint64_t)1 << cpunum
);
2386 if (cmask
== cpu_mask
) {
2391 if (kd_start
< (kd_buf
*)my_buffer
) {
2392 kd_start
= (kd_buf
*)my_buffer
;
2395 for (kd_stop
= kd_end
+ 1; kd_stop
< end_of_sample
; kd_stop
++) {
2396 if (kd_stop
->arg5
== thread
) {
2400 if (kd_stop
>= end_of_sample
) {
2401 kd_stop
= end_of_sample
- 1;
2408 now
= kd_start
->timestamp
& KDBG_TIMESTAMP_MASK
;
2409 sample_timestamp
= (double)(now
- first_now
) / divisor
;
2411 TOD_usecs
= (uint64_t)sample_timestamp
;
2412 TOD_secs
= sample_TOD_secs
+ ((sample_TOD_usecs
+ TOD_usecs
) / 1000000);
2414 sprintf(buf1
, "%-19.19s priority = %d, scheduling latency = %.1fus [timestamp %.1f]", ctime(&TOD_secs
), s_priority
, s_latency
, sample_timestamp
);
2416 sprintf(buf1
, "%-19.19s priority = %d, scheduling latency = %.1fus [sample %d]", &(ctime(&curr_time
)[0]), s_priority
, s_latency
, sample_generation
);
2419 log_range((kd_buf
*)my_buffer
, kd_start
, kd_stop
, kd_beg
, buf1
);
2425 check_for_scheduler_latency(int type
, uintptr_t *thread
, uint64_t now
, kd_buf
*kd
, kd_buf
**kd_start
, int *priority
, double *latency
)
2427 int found_latency
= 0;
2429 if (type
== MACH_makerunnable
) {
2430 if (watch_priority_min
<= kd
->arg2
&& kd
->arg2
<= watch_priority_max
) {
2431 insert_run_event(kd
->arg1
, (int)kd
->arg2
, kd
, now
);
2433 } else if (type
== MACH_sched
|| type
== MACH_stkhandoff
) {
2434 threadrun_t trp
= find_run_event(kd
->arg2
);
2436 if (type
== MACH_sched
|| type
== MACH_stkhandoff
) {
2440 if ((trp
= find_run_event(*thread
))) {
2441 double d_s_latency
= (((double)(now
- trp
->tr_timestamp
)) / divisor
);
2442 int s_latency
= (int)d_s_latency
;
2445 if (s_latency
< 100) {
2446 s_usec_10_bins
[s_latency
/10]++;
2448 if (s_latency
< 1000) {
2449 s_usec_100_bins
[s_latency
/100]++;
2450 } else if (s_latency
< 10000) {
2451 s_msec_1_bins
[s_latency
/1000]++;
2452 } else if (s_latency
< 50000) {
2453 s_msec_10_bins
[s_latency
/10000]++;
2458 if (s_latency
> s_max_latency
) {
2459 s_max_latency
= s_latency
;
2461 if (s_latency
< s_min_latency
|| s_total_samples
== 0) {
2462 s_min_latency
= s_latency
;
2464 s_total_latency
+= s_latency
;
2467 if (s_thresh_hold
&& s_latency
> s_thresh_hold
) {
2468 s_exceeded_threshold
++;
2471 *kd_start
= trp
->tr_entry
;
2472 *priority
= trp
->tr_priority
;
2473 *latency
= d_s_latency
;
2478 delete_run_event(*thread
);
2481 return found_latency
;
2486 handle_decrementer(kd_buf
*kd
, int cpunum
)
2488 struct i_latencies
*il
;
2492 if (i_latency_per_cpu
== FALSE
) {
2496 il
= &i_lat
[cpunum
];
2498 if ((long)(kd
->arg1
) >= 0) {
2501 latency
= (((double)(-1 - kd
->arg1
)) / divisor
);
2503 elapsed_usecs
= (long)latency
;
2505 if (elapsed_usecs
< 100) {
2506 il
->i_usec_10_bins
[elapsed_usecs
/10]++;
2509 if (elapsed_usecs
< 1000) {
2510 il
->i_usec_100_bins
[elapsed_usecs
/100]++;
2511 } else if (elapsed_usecs
< 10000) {
2512 il
->i_msec_1_bins
[elapsed_usecs
/1000]++;
2513 } else if (elapsed_usecs
< 50000) {
2514 il
->i_msec_10_bins
[elapsed_usecs
/10000]++;
2519 if (use_high_res_bins
&& elapsed_usecs
< N_HIGH_RES_BINS
) {
2520 i_high_res_bins
[elapsed_usecs
]++;
2522 if (i_thresh_hold
&& elapsed_usecs
> i_thresh_hold
) {
2523 il
->i_exceeded_threshold
++;
2525 if (elapsed_usecs
> il
->i_max_latency
) {
2526 il
->i_max_latency
= elapsed_usecs
;
2528 if (elapsed_usecs
< il
->i_min_latency
|| il
->i_total_samples
== 0) {
2529 il
->i_min_latency
= elapsed_usecs
;
2531 il
->i_total_latency
+= elapsed_usecs
;
2532 il
->i_total_samples
++;
2543 for (i
= 0; i
< num_of_codes
; i
++) {
2544 if (codes_tab
[i
].type
== type
) {
2545 return codes_tab
[i
].name
;
2553 init_code_file(void)
2558 if ((fp
= fopen(code_file
, "r")) == NULL
) {
2560 fprintf(log_fp
, "open of %s failed\n", code_file
);
2564 for (i
= 0; i
< MAX_ENTRIES
; i
++) {
2567 int n
= fscanf(fp
, "%x%127s\n", &code
, name
);
2569 if (n
== 1 && i
== 0) {
2571 * old code file format, just skip
2579 strncpy(codes_tab
[i
].name
, name
, 32);
2580 codes_tab
[i
].type
= code
;
2593 char tmp_nm_file
[128];
2597 bzero(tmp_nm_file
, 128);
2598 bzero(tmpstr
, 1024);
2601 * Build the temporary nm file path
2603 strcpy(tmp_nm_file
,"/tmp/knm.out.XXXXXX");
2605 if (!mktemp(tmp_nm_file
)) {
2606 fprintf(stderr
, "Error in mktemp call\n");
2611 * Build the nm command and create a tmp file with the output
2613 sprintf (tmpstr
, "/usr/bin/nm -f -n -s __TEXT __text %s > %s",
2614 kernelpath
, tmp_nm_file
);
2618 * Parse the output from the nm command
2620 if ((fp
= fopen(tmp_nm_file
, "r")) == NULL
) {
2621 /* Hmmm, let's not treat this as fatal */
2622 fprintf(stderr
, "Failed to open nm symbol file [%s]\n", tmp_nm_file
);
2626 * Count the number of symbols in the nm symbol table
2630 while ((inchr
= getc(fp
)) != -1) {
2631 if (inchr
== '\n') {
2638 * Malloc the space for symbol table
2640 if (kern_sym_count
> 0) {
2641 kern_sym_tbl
= malloc(kern_sym_count
* sizeof(kern_sym_t
));
2643 if (!kern_sym_tbl
) {
2645 * Hmmm, lets not treat this as fatal
2647 fprintf(stderr
, "Can't allocate memory for kernel symbol table\n");
2649 bzero(kern_sym_tbl
, kern_sym_count
* sizeof(kern_sym_t
));
2653 * Hmmm, lets not treat this as fatal
2655 fprintf(stderr
, "No kernel symbol table \n");
2657 for (i
= 0; i
< kern_sym_count
; i
++) {
2658 bzero(tmpstr
, 1024);
2660 if (fscanf(fp
, "%p %c %s", &kern_sym_tbl
[i
].k_sym_addr
, &inchr
, tmpstr
) != 3) {
2663 len
= strlen(tmpstr
);
2664 kern_sym_tbl
[i
].k_sym_name
= malloc(len
+ 1);
2666 if (kern_sym_tbl
[i
].k_sym_name
== NULL
) {
2667 fprintf(stderr
, "Can't allocate memory for symbol name [%s]\n", tmpstr
);
2668 kern_sym_tbl
[i
].k_sym_name
= NULL
;
2671 strcpy(kern_sym_tbl
[i
].k_sym_name
, tmpstr
);
2674 kern_sym_tbl
[i
].k_sym_len
= len
;
2677 if (i
!= kern_sym_count
) {
2679 * Hmmm, didn't build up entire table from nm
2680 * scrap the entire thing
2683 kern_sym_tbl
= NULL
;
2689 * Remove the temporary nm file
2691 unlink(tmp_nm_file
);
2694 * Dump the kernel symbol table
2696 for (i
= 0; i
< kern_sym_count
; i
++) {
2697 if (kern_sym_tbl
[i
].k_sym_name
) {
2698 printf ("[%d] %-16p %s\n", i
,
2699 kern_sym_tbl
[i
].k_sym_addr
, kern_sym_tbl
[i
].k_sym_name
);
2701 printf ("[%d] %-16p %s\n", i
,
2702 kern_sym_tbl
[i
].k_sym_addr
, "No symbol name");
2709 pc_to_string(char *pcstring
, uintptr_t pc
, int max_len
, int mode
)
2714 if (mode
== USER_MODE
) {
2715 sprintf(pcstring
, "%-16lx [usermode addr]", pc
);
2718 ret
= binary_search(kern_sym_tbl
, 0, kern_sym_count
-1, pc
);
2720 if (ret
== -1 || kern_sym_tbl
[ret
].k_sym_name
== NULL
) {
2721 sprintf(pcstring
, "%-16lx", pc
);
2724 if ((len
= kern_sym_tbl
[ret
].k_sym_len
) > (max_len
- 8)) {
2728 memcpy(pcstring
, kern_sym_tbl
[ret
].k_sym_name
, len
);
2730 sprintf(&pcstring
[len
], "+0x%-5lx", pc
- (uintptr_t)kern_sym_tbl
[ret
].k_sym_addr
);
2735 * Return -1 if not found, else return index
2738 binary_search(kern_sym_t
*list
, int low
, int high
, uintptr_t addr
)
2742 if (kern_sym_count
== 0) {
2747 return -1; /* failed */
2750 if (low
+ 1 == high
) {
2751 if ((uintptr_t)list
[low
].k_sym_addr
<= addr
&& addr
< (uintptr_t)list
[high
].k_sym_addr
) {
2753 * We have a range match
2757 if ((uintptr_t)list
[high
].k_sym_addr
<= addr
) {
2765 mid
= (low
+ high
) / 2;
2767 if (addr
< (uintptr_t)list
[mid
].k_sym_addr
) {
2768 return binary_search(list
, low
, mid
, addr
);
2771 return binary_search(list
, mid
, high
, addr
);
2776 open_logfile(const char *path
)
2778 log_fp
= fopen(path
, "a");
2782 * failed to open path
2784 fprintf(stderr
, "latency: failed to open logfile [%s]\n", path
);
2791 open_rawfile(const char *path
)
2793 RAW_fd
= open(path
, O_RDONLY
);
2797 * failed to open path
2799 fprintf(stderr
, "latency: failed to open RAWfile [%s]\n", path
);
2808 mach_timebase_info_data_t info
;
2810 (void)mach_timebase_info(&info
);
2812 divisor
= ((double)info
.denom
/ (double)info
.numer
) * 1000;