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>
45 #include <sys/types.h>
46 #include <sys/param.h>
48 #include <sys/sysctl.h>
49 #include <sys/ioctl.h>
51 #ifndef KERNEL_PRIVATE
52 #define KERNEL_PRIVATE
53 #include <sys/kdebug.h>
56 #include <sys/kdebug.h>
57 #endif /*KERNEL_PRIVATE*/
59 #include <mach/mach_error.h>
60 #include <mach/mach_types.h>
61 #include <mach/message.h>
62 #include <mach/mach_syscalls.h>
63 #include <mach/clock_types.h>
64 #include <mach/mach_time.h>
66 #include <libkern/OSTypes.h>
69 int s_usec_10_bins
[10];
70 int s_usec_100_bins
[10];
71 int s_msec_1_bins
[10];
72 int s_msec_10_bins
[5];
75 int s_min_latency
= 0;
76 long long s_total_latency
= 0;
77 int s_total_samples
= 0;
79 int s_exceeded_threshold
= 0;
82 #define N_HIGH_RES_BINS 500
83 int use_high_res_bins
= false;
86 int i_usec_10_bins
[10];
87 int i_usec_100_bins
[10];
88 int i_msec_1_bins
[10];
89 int i_msec_10_bins
[5];
95 int i_exceeded_threshold
;
96 uint64_t i_total_latency
;
99 struct i_latencies
*i_lat
;
100 boolean_t i_latency_per_cpu
= FALSE
;
102 int i_high_res_bins
[N_HIGH_RES_BINS
];
106 int watch_priority_min
= 97;
107 int watch_priority_max
= 97;
114 char *kernelpath
= NULL
;
117 void *k_sym_addr
; /* kernel symbol address from nm */
118 size_t k_sym_len
; /* length of kernel symbol string */
119 char *k_sym_name
; /* kernel symbol string from nm */
122 kern_sym_t
*kern_sym_tbl
; /* pointer to the nm table */
123 int kern_sym_count
; /* number of entries in nm table */
127 #define MAX_ENTRIES 4096
131 } codes_tab
[MAX_ENTRIES
];
133 char *code_file
= NULL
;
134 int num_of_codes
= 0;
138 sig_atomic_t gotSIGWINCH
= 0;
139 int trace_enabled
= 0;
140 int need_new_map
= 1;
141 int set_remove_flag
= 1; /* By default, remove trace buffer */
146 uint64_t first_now
= 0;
147 uint64_t last_now
= 0;
151 #define SAMPLE_TIME_USECS 50000
152 #define SAMPLE_SIZE 300000
153 #define MAX_LOG_COUNT 30 /* limits the number of entries dumped in log_decrementer */
155 kbufinfo_t bufinfo
= {0, 0, 0};
159 uint64_t sample_TOD_secs
;
160 uint32_t sample_TOD_usecs
;
164 int sample_generation
= 0;
165 int num_i_latency_cpus
= 1;
170 kd_buf
**last_decrementer_kd
; /* last DECR_TRAP per cpu */
175 typedef struct event
*event_t
;
182 uint64_t ev_timestamp
;
186 typedef struct lookup
*lookup_t
;
194 int64_t lk_pathname
[NUMPARMS
+ 1];
198 typedef struct threadmap
*threadmap_t
;
205 char tm_command
[MAXCOMLEN
+ 1];
206 char tm_orig_command
[MAXCOMLEN
+ 1];
210 typedef struct threadrun
*threadrun_t
;
217 uint64_t tr_timestamp
;
222 typedef struct thread_entry
*thread_entry_t
;
224 struct thread_entry
{
225 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 INTERRUPT 0x01050000
270 #define DECR_TRAP 0x01090000
271 #define DECR_SET 0x01090004
272 #define MACH_vmfault 0x01300008
273 #define MACH_sched 0x01400000
274 #define MACH_stkhandoff 0x01400008
275 #define MACH_makerunnable 0x01400018
276 #define MACH_idle 0x01400024
277 #define IES_action 0x050b0018
278 #define IES_filter 0x050b001c
279 #define TES_action 0x050c0010
280 #define CQ_action 0x050d0018
281 #define CPUPM_CPUSTER_RUNCOUNT 0x05310144
283 #define BSC_exit 0x040C0004
284 #define BSC_thread_terminate 0x040c05a4
286 #define DBG_FUNC_MASK ~(DBG_FUNC_START | DBG_FUNC_END)
288 #define CPU_NUMBER(kp) kdbg_get_cpu(kp)
290 #define EMPTYSTRING ""
292 const char *fault_name
[] = {
304 const char *sched_reasons
[] = {
324 #define ARRAYSIZE(x) ((int)(sizeof(x) / sizeof(*x)))
325 #define MAX_REASON ARRAYSIZE(sched_reasons)
327 static double handle_decrementer(kd_buf
*, int);
328 static kd_buf
*log_decrementer(kd_buf
*kd_beg
, kd_buf
*kd_end
, kd_buf
*end_of_sample
, double i_latency
);
329 static void read_command_map(void);
330 static void enter_syscall(FILE *fp
, kd_buf
*kd
, uint64_t thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, int print_info
);
331 static void exit_syscall(FILE *fp
, kd_buf
*kd
, uint64_t thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, int print_info
);
332 static void print_entry(FILE *fp
, kd_buf
*kd
, uint64_t thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, kd_buf
*kd_note
);
333 static void log_info(uint64_t now
, uint64_t idelta
, uint64_t start_bias
, kd_buf
*kd
, kd_buf
*kd_note
);
334 static char *find_code(int);
335 static void pc_to_string(char *pcstring
, uint64_t pc
, int max_len
, int mode
);
336 static void getdivisor(void);
337 static int sample_sc(void);
338 static void init_code_file(void);
339 static void do_kernel_nm(void);
340 static void open_logfile(const char*);
341 static int binary_search(kern_sym_t
*list
, int low
, int high
, uint64_t addr
);
343 static void create_map_entry(uint64_t, char *);
344 static void check_for_thread_update(uint64_t thread
, int debugid_base
, kd_buf
*kbufp
, char **command
);
345 static void log_scheduler(kd_buf
*kd_start
, kd_buf
*kd_stop
, kd_buf
*end_of_sample
, int s_priority
, double s_latency
, uint64_t thread
);
346 static int check_for_scheduler_latency(int type
, uint64_t *thread
, uint64_t now
, kd_buf
*kd
, kd_buf
**kd_start
, int *priority
, double *latency
);
347 static void open_rawfile(const char *path
);
349 static void screen_update(FILE *);
351 static void set_enable(int);
352 static void set_remove(void);
362 * This flag is turned off when calling
363 * quit() due to a set_remove() failure.
365 if (set_remove_flag
) {
381 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDENABLE
, val
};
384 if (sysctl(mib
, ARRAYSIZE(mib
), NULL
, &needed
, NULL
, 0) < 0) {
385 quit("trace facility failure, KERN_KDENABLE\n");
390 set_numbufs(int nbufs
)
392 int mib1
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDSETBUF
, nbufs
};
393 int mib2
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDSETUP
};
396 if (sysctl(mib1
, ARRAYSIZE(mib1
), NULL
, &needed
, NULL
, 0) < 0) {
397 quit("trace facility failure, KERN_KDSETBUF\n");
399 if (sysctl(mib2
, ARRAYSIZE(mib2
), NULL
, &needed
, NULL
, 0) < 0) {
400 quit("trace facility failure, KERN_KDSETUP\n");
405 set_pidexclude(int pid
, int on_off
)
407 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDPIDEX
};
408 size_t needed
= sizeof(kd_regtype
);
411 .type
= KDBG_TYPENONE
,
416 sysctl(mib
, ARRAYSIZE(mib
), &kr
, &needed
, NULL
, 0);
420 get_bufinfo(kbufinfo_t
*val
)
422 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDGETBUF
};
423 size_t needed
= sizeof (*val
);
425 if (sysctl(mib
, ARRAYSIZE(mib
), val
, &needed
, 0, 0) < 0) {
426 quit("trace facility failure, KERN_KDGETBUF\n");
433 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDREMOVE
};
438 if (sysctl(mib
, ARRAYSIZE(mib
), NULL
, &needed
, NULL
, 0) < 0) {
440 if (errno
== EBUSY
) {
441 quit("the trace facility is currently in use...\n fs_usage, sc_usage, and latency use this feature.\n\n");
443 quit("trace facility failure, KERN_KDREMOVE\n");
450 write_high_res_latencies(void)
455 if (use_high_res_bins
) {
456 if ((f
= fopen("latencies.csv","w"))) {
457 for (i
= 0; i
< N_HIGH_RES_BINS
; i
++) {
458 fprintf(f
, "%d,%d\n", i
, i_high_res_bins
[i
]);
466 sigintr(int signo
__attribute__((unused
)))
468 write_high_res_latencies();
471 set_pidexclude(getpid(), 0);
472 screen_update(log_fp
);
479 /* exit under normal conditions -- signal handler */
481 leave(int signo
__attribute__((unused
)))
483 write_high_res_latencies();
486 set_pidexclude(getpid(), 0);
494 sigwinch(int signo
__attribute__((unused
)))
500 print_total(FILE *fp
, char *s
, int total
)
505 struct i_latencies
*il
;
508 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
510 itotal
+= il
->i_total
;
512 clen
= sprintf(tbuf
, "%s %10d %9d", s
, total
, itotal
);
514 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
517 if (i_latency_per_cpu
== TRUE
) {
518 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_total
);
523 sprintf(&tbuf
[clen
], "\n");
525 fprintf(fp
, "%s", tbuf
);
534 screen_update(FILE *fp
)
543 long min_lat
, max_lat
;
545 unsigned int average_s_latency
;
546 unsigned int average_i_latency
;
547 struct i_latencies
*il
;
554 fprintf(fp
,"\n\n===================================================================================================\n");
557 * Display the current time.
558 * "ctime" always returns a string that looks like this:
560 * Sun Sep 16 01:03:52 1973
561 * 012345678901234567890123
564 * We want indices 11 thru 18 (length 8).
567 curr_time
= (unsigned long)sample_TOD_secs
;
568 elapsed_secs
= ((last_now
- first_now
) / divisor
) / 1000000;
570 elapsed_secs
= curr_time
- start_time
;
573 elapsed_hours
= elapsed_secs
/ 3600;
574 elapsed_secs
-= elapsed_hours
* 3600;
575 elapsed_mins
= elapsed_secs
/ 60;
576 elapsed_secs
-= elapsed_mins
* 60;
578 sprintf(tbuf
, "%-19.19s %2ld:%02ld:%02ld\n", &(ctime(&curr_time
)[0]),
579 (long)elapsed_hours
, (long)elapsed_mins
, (long)elapsed_secs
);
581 fprintf(fp
, "%s", tbuf
);
586 sprintf(tbuf
, " SCHEDULER INTERRUPTS\n");
588 fprintf(fp
, "%s", tbuf
);
593 if (i_latency_per_cpu
== TRUE
) {
594 clen
= sprintf(tbuf
, " Total");
596 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
598 clen
+= sprintf(&tbuf
[clen
], " CPU %d", cpu
);
600 clen
+= sprintf(&tbuf
[clen
], " CPU %d", cpu
);
604 fprintf(fp
, "%s", tbuf
);
609 clen
= sprintf(tbuf
, "\n-------------------------------------------------------");
611 for (cpu
= 1; cpu
< num_i_latency_cpus
; cpu
++) {
612 clen
+= sprintf(&tbuf
[clen
], "----------");
615 fprintf(fp
, "%s", tbuf
);
620 sprintf(tbuf
, "---------------------------------------------");
622 fprintf(fp
, "%s", tbuf
);
627 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
629 itotal
+= il
->i_total_samples
;
631 clen
= sprintf(tbuf
, "\ntotal_samples %10d %9d", s_total_samples
, itotal
);
633 if (i_latency_per_cpu
== TRUE
) {
634 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
637 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_total_samples
);
640 sprintf(&tbuf
[clen
], "\n");
642 fprintf(fp
, "%s", tbuf
);
648 for (stotal
= 0, i
= 0; i
< 10; i
++) {
649 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
652 itotal
+= il
->i_usec_10_bins
[i
];
653 il
->i_total
+= il
->i_usec_10_bins
[i
];
655 clen
= sprintf(tbuf
, "\ndelays < %3d usecs %10d %9d", (i
+ 1) * 10, s_usec_10_bins
[i
], itotal
);
657 stotal
+= s_usec_10_bins
[i
];
659 if (i_latency_per_cpu
== TRUE
) {
660 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
663 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_usec_10_bins
[i
]);
667 fprintf(fp
, "%s", tbuf
);
672 print_total(fp
, "\ntotal < 100 usecs", stotal
);
674 for (stotal
= 0, i
= 1; i
< 10; i
++) {
675 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
678 itotal
+= il
->i_usec_100_bins
[i
];
679 il
->i_total
+= il
->i_usec_100_bins
[i
];
682 clen
= sprintf(tbuf
, "\ndelays < %3d usecs %10d %9d", (i
+ 1) * 100, s_usec_100_bins
[i
], itotal
);
684 clen
= sprintf(tbuf
, "\ndelays < 1 msec %10d %9d", s_usec_100_bins
[i
], itotal
);
687 stotal
+= s_usec_100_bins
[i
];
689 if (i_latency_per_cpu
== TRUE
) {
690 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
693 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_usec_100_bins
[i
]);
697 fprintf(fp
, "%s", tbuf
);
702 print_total(fp
, "\ntotal < 1 msec ", stotal
);
705 for (stotal
= 0, i
= 1; i
< 10; i
++) {
706 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
709 itotal
+= il
->i_msec_1_bins
[i
];
710 il
->i_total
+= il
->i_msec_1_bins
[i
];
712 clen
= sprintf(tbuf
, "\ndelays < %3d msecs %10d %9d", (i
+ 1), s_msec_1_bins
[i
], itotal
);
714 stotal
+= s_msec_1_bins
[i
];
716 if (i_latency_per_cpu
== TRUE
) {
717 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
720 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_msec_1_bins
[i
]);
724 fprintf(fp
, "%s", tbuf
);
729 print_total(fp
, "\ntotal < 10 msecs", stotal
);
731 for (stotal
= 0, i
= 1; i
< 5; i
++) {
732 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
735 itotal
+= il
->i_msec_10_bins
[i
];
736 il
->i_total
+= il
->i_msec_10_bins
[i
];
738 clen
= sprintf(tbuf
, "\ndelays < %3d msecs %10d %9d", (i
+ 1)*10, s_msec_10_bins
[i
], itotal
);
740 stotal
+= s_msec_10_bins
[i
];
742 if (i_latency_per_cpu
== TRUE
) {
743 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
746 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_msec_10_bins
[i
]);
750 fprintf(fp
, "%s", tbuf
);
755 print_total(fp
, "\ntotal < 50 msecs", stotal
);
758 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
760 itotal
+= il
->i_too_slow
;
762 clen
= sprintf(tbuf
, "\ndelays > 50 msecs %10d %9d", s_too_slow
, itotal
);
764 if (i_latency_per_cpu
== TRUE
) {
765 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
768 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_too_slow
);
772 fprintf(fp
, "%s", tbuf
);
777 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
780 if (cpu
== 0 || (il
->i_min_latency
< min_lat
)) {
781 min_lat
= il
->i_min_latency
;
784 clen
= sprintf(tbuf
, "\n\nminimum latency(usecs) %7d %9ld", s_min_latency
, min_lat
);
786 if (i_latency_per_cpu
== TRUE
) {
787 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
790 clen
+= sprintf(&tbuf
[clen
], " %9ld", il
->i_min_latency
);
794 fprintf(fp
, "%s", tbuf
);
800 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
803 if (cpu
== 0 || (il
->i_max_latency
> max_lat
)) {
804 max_lat
= il
->i_max_latency
;
807 clen
= sprintf(tbuf
, "\nmaximum latency(usecs) %7d %9ld", s_max_latency
, max_lat
);
809 if (i_latency_per_cpu
== TRUE
) {
810 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
813 clen
+= sprintf(&tbuf
[clen
], " %9ld", il
->i_max_latency
);
817 fprintf(fp
, "%s", tbuf
);
822 if (s_total_samples
) {
823 average_s_latency
= (unsigned int)(s_total_latency
/s_total_samples
);
825 average_s_latency
= 0;
828 for (itotal
= 0, tot_lat
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
831 itotal
+= il
->i_total_samples
;
832 tot_lat
+= il
->i_total_latency
;
835 average_i_latency
= (unsigned)(tot_lat
/itotal
);
837 average_i_latency
= 0;
840 clen
= sprintf(tbuf
, "\naverage latency(usecs) %7d %9d", average_s_latency
, average_i_latency
);
842 if (i_latency_per_cpu
== TRUE
) {
843 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
846 if (il
->i_total_samples
) {
847 average_i_latency
= (unsigned int)(il
->i_total_latency
/il
->i_total_samples
);
849 average_i_latency
= 0;
852 clen
+= sprintf(&tbuf
[clen
], " %9d", average_i_latency
);
856 fprintf(fp
, "%s", tbuf
);
861 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
864 itotal
+= il
->i_exceeded_threshold
;
866 clen
= sprintf(tbuf
, "\nexceeded threshold %7d %9d", s_exceeded_threshold
, itotal
);
868 if (i_latency_per_cpu
== TRUE
) {
869 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
872 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_exceeded_threshold
);
875 sprintf(&tbuf
[clen
], "\n");
878 fprintf(fp
, "%s", tbuf
);
893 fprintf(stderr
, "Usage: latency [-p <priority>] [-h] [-m] [-st <threshold>] [-it <threshold>]\n");
894 fprintf(stderr
, " [-c <codefile>] [-l <logfile>] [-R <rawfile>] [-n <kernel>]\n\n");
896 fprintf(stderr
, " -p specify scheduling priority to watch... default is realtime. Can also be a range, e.g. \"31-47\".\n");
897 fprintf(stderr
, " -h Display high resolution interrupt latencies and write them to latencies.csv (truncate existing file) upon exit.\n");
898 fprintf(stderr
, " -st set scheduler latency threshold in microseconds... if latency exceeds this, then log trace\n");
899 fprintf(stderr
, " -m specify per-CPU interrupt latency reporting\n");
900 fprintf(stderr
, " -it set interrupt latency threshold in microseconds... if latency exceeds this, then log trace\n");
901 fprintf(stderr
, " -c specify name of codes file... default is /usr/share/misc/trace.codes\n");
902 fprintf(stderr
, " -l specify name of file to log trace entries to when the specified threshold is exceeded\n");
903 fprintf(stderr
, " -R specify name of raw trace file to process\n");
904 fprintf(stderr
, " -n specify kernel... default is /System/Library/Kernels/kernel.development\n");
906 fprintf(stderr
, "\nlatency must be run as root\n\n");
912 main(int argc
, char *argv
[])
916 if (0 != reexec_to_match_kernel()) {
917 fprintf(stderr
, "Could not re-execute: %d\n", errno
);
922 if (strcmp(argv
[1], "-R") == 0) {
927 open_rawfile(argv
[1]);
934 } else if (strcmp(argv
[1], "-p") == 0) {
939 if (2 == sscanf(argv
[1], "%d-%d", &watch_priority_min
, &watch_priority_max
)) {
940 if (watch_priority_min
> watch_priority_max
) {
942 } else if (watch_priority_min
< 0) {
946 if (1 == sscanf(argv
[1], "%d", &watch_priority_min
)) {
947 watch_priority_max
= watch_priority_min
;
955 } else if (strcmp(argv
[1], "-st") == 0) {
960 s_thresh_hold
= atoi(argv
[1]);
964 } else if (strcmp(argv
[1], "-it") == 0) {
969 i_thresh_hold
= atoi(argv
[1]);
973 } else if (strcmp(argv
[1], "-c") == 0) {
982 } else if (strcmp(argv
[1], "-l") == 0) {
987 open_logfile(argv
[1]);
991 } else if (strcmp(argv
[1], "-n") == 0) {
996 kernelpath
= argv
[1];
1000 } else if (strcmp(argv
[1], "-h") == 0) {
1001 use_high_res_bins
= TRUE
;
1003 } else if (strcmp(argv
[1], "-m") == 0) {
1004 i_latency_per_cpu
= TRUE
;
1014 if (geteuid() != 0) {
1015 printf("'latency' must be run as root...\n");
1019 if (kernelpath
== NULL
) {
1020 kernelpath
= "/System/Library/Kernels/kernel.development";
1023 if (code_file
== NULL
) {
1024 code_file
= "/usr/share/misc/trace.codes";
1034 if (initscr() == NULL
) {
1035 printf("Unrecognized TERM type, try vt100\n");
1041 signal(SIGWINCH
, sigwinch
);
1042 signal(SIGINT
, sigintr
);
1043 signal(SIGQUIT
, leave
);
1044 signal(SIGTERM
, leave
);
1045 signal(SIGHUP
, leave
);
1048 * grab the number of cpus and scale the buffer size
1050 int mib
[] = { CTL_HW
, HW_NCPU
};
1051 size_t len
= sizeof(num_cpus
);
1053 sysctl(mib
, ARRAYSIZE(mib
), &num_cpus
, &len
, NULL
, 0);
1056 set_numbufs(SAMPLE_SIZE
* num_cpus
);
1058 get_bufinfo(&bufinfo
);
1062 set_pidexclude(getpid(), 1);
1065 num_entries
= bufinfo
.nkdbufs
;
1067 num_entries
= 50000;
1071 for (cpu_mask
= 0, i
= 0; i
< num_cpus
; i
++)
1072 cpu_mask
|= ((uint64_t)1 << i
);
1074 if ((my_buffer
= malloc(num_entries
* sizeof(kd_buf
))) == NULL
) {
1075 quit("can't allocate memory for tracing info\n");
1078 if ((last_decrementer_kd
= (kd_buf
**)malloc(num_cpus
* sizeof(kd_buf
*))) == NULL
) {
1079 quit("can't allocate memory for decrementer tracing info\n");
1082 if (i_latency_per_cpu
== FALSE
) {
1083 num_i_latency_cpus
= 1;
1085 num_i_latency_cpus
= num_cpus
;
1088 if ((i_lat
= (struct i_latencies
*)malloc(num_i_latency_cpus
* sizeof(struct i_latencies
))) == NULL
) {
1089 quit("can't allocate memory for interrupt latency info\n");
1092 bzero((char *)i_lat
, num_i_latency_cpus
* sizeof(struct i_latencies
));
1095 while (sample_sc()) {
1100 screen_update(log_fp
);
1103 screen_update(stdout
);
1108 double nanosecs_to_sleep
;
1110 nanosecs_to_sleep
= (double)(SAMPLE_TIME_USECS
* 1000);
1111 fdelay
= nanosecs_to_sleep
* (divisor
/1000);
1112 adelay
= (uint64_t)fdelay
;
1116 start_time
= time(NULL
);
1117 refresh_time
= start_time
;
1120 curr_time
= time(NULL
);
1122 if (curr_time
>= refresh_time
) {
1123 screen_update(NULL
);
1124 refresh_time
= curr_time
+ 1;
1126 mach_wait_until(mach_absolute_time() + adelay
);
1132 * No need to check for initscr error return.
1133 * We won't get here if it fails on the first call.
1146 read_command_map(void)
1148 kd_threadmap
*mapptr
= 0;
1149 int total_threads
= 0;
1153 RAW_header header
= {0};
1156 if (read(RAW_fd
, &header
, sizeof(RAW_header
)) != sizeof(RAW_header
)) {
1157 perror("read failed");
1160 if (header
.version_no
!= RAW_VERSION1
) {
1161 header
.version_no
= RAW_VERSION0
;
1162 header
.TOD_secs
= time(NULL
);
1163 header
.TOD_usecs
= 0;
1165 lseek(RAW_fd
, (off_t
)0, SEEK_SET
);
1167 if (read(RAW_fd
, &header
.thread_count
, sizeof(int)) != sizeof(int)) {
1168 perror("read failed");
1172 total_threads
= header
.thread_count
;
1174 sample_TOD_secs
= header
.TOD_secs
;
1175 sample_TOD_usecs
= header
.TOD_usecs
;
1177 if (total_threads
== 0 && header
.version_no
!= RAW_VERSION0
) {
1178 offset
= lseek(RAW_fd
, (off_t
)0, SEEK_CUR
);
1179 offset
= (offset
+ (4095)) & ~4095;
1181 lseek(RAW_fd
, offset
, SEEK_SET
);
1184 total_threads
= bufinfo
.nkdthreads
;
1187 size
= total_threads
* sizeof(kd_threadmap
);
1189 if (size
== 0 || ((mapptr
= (kd_threadmap
*) malloc(size
)) == 0)) {
1192 bzero (mapptr
, size
);
1195 * Now read the threadmap
1198 if (read(RAW_fd
, mapptr
, size
) != size
) {
1199 printf("Can't read the thread map -- this is not fatal\n");
1201 if (header
.version_no
!= RAW_VERSION0
) {
1202 offset
= lseek(RAW_fd
, (off_t
)0, SEEK_CUR
);
1203 offset
= (offset
+ (4095)) & ~4095;
1205 lseek(RAW_fd
, offset
, SEEK_SET
);
1208 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDTHRMAP
};
1209 if (sysctl(mib
, ARRAYSIZE(mib
), mapptr
, &size
, NULL
, 0) < 0) {
1211 * This is not fatal -- just means I cant map command strings
1213 printf("Can't read the thread map -- this is not fatal\n");
1218 for (i
= 0; i
< total_threads
; i
++) {
1219 create_map_entry(mapptr
[i
].thread
, &mapptr
[i
].command
[0]);
1225 create_map_entry(uint64_t thread
, char *command
)
1229 if ((tme
= threadmap_freelist
)) {
1230 threadmap_freelist
= tme
->tm_next
;
1232 tme
= (threadmap_t
)malloc(sizeof(struct threadmap
));
1235 tme
->tm_thread
= thread
;
1237 (void)strncpy (tme
->tm_command
, command
, MAXCOMLEN
);
1238 tme
->tm_command
[MAXCOMLEN
] = '\0';
1239 tme
->tm_orig_command
[0] = '\0';
1241 int hashid
= thread
& HASH_MASK
;
1243 tme
->tm_next
= threadmap_hash
[hashid
];
1244 threadmap_hash
[hashid
] = tme
;
1248 delete_thread_entry(uint64_t thread
)
1252 int hashid
= thread
& HASH_MASK
;
1254 if ((tme
= threadmap_hash
[hashid
])) {
1255 if (tme
->tm_thread
== thread
) {
1256 threadmap_hash
[hashid
] = tme
->tm_next
;
1258 threadmap_t tme_prev
= tme
;
1260 for (tme
= tme
->tm_next
; tme
; tme
= tme
->tm_next
) {
1261 if (tme
->tm_thread
== thread
) {
1262 tme_prev
->tm_next
= tme
->tm_next
;
1269 tme
->tm_next
= threadmap_freelist
;
1270 threadmap_freelist
= tme
;
1276 find_and_insert_tmp_map_entry(uint64_t pthread
, char *command
)
1280 if ((tme
= threadmap_temp
)) {
1281 if (tme
->tm_pthread
== pthread
) {
1282 threadmap_temp
= tme
->tm_next
;
1284 threadmap_t tme_prev
= tme
;
1286 for (tme
= tme
->tm_next
; tme
; tme
= tme
->tm_next
) {
1287 if (tme
->tm_pthread
== pthread
) {
1288 tme_prev
->tm_next
= tme
->tm_next
;
1295 (void)strncpy (tme
->tm_command
, command
, MAXCOMLEN
);
1296 tme
->tm_command
[MAXCOMLEN
] = '\0';
1297 tme
->tm_orig_command
[0] = '\0';
1299 int hashid
= tme
->tm_thread
& HASH_MASK
;
1300 tme
->tm_next
= threadmap_hash
[hashid
];
1301 threadmap_hash
[hashid
] = tme
;
1307 create_tmp_map_entry(uint64_t thread
, uint64_t pthread
)
1311 if ((tme
= threadmap_freelist
)) {
1312 threadmap_freelist
= tme
->tm_next
;
1314 tme
= malloc(sizeof(struct threadmap
));
1317 tme
->tm_thread
= thread
;
1318 tme
->tm_pthread
= pthread
;
1319 tme
->tm_command
[0] = '\0';
1320 tme
->tm_orig_command
[0] = '\0';
1322 tme
->tm_next
= threadmap_temp
;
1323 threadmap_temp
= tme
;
1327 find_thread_entry(uint64_t thread
)
1331 int hashid
= thread
& HASH_MASK
;
1333 for (tme
= threadmap_hash
[hashid
]; tme
; tme
= tme
->tm_next
) {
1334 if (tme
->tm_thread
== thread
) {
1342 find_thread_name(uint64_t thread
, char **command
)
1346 if ((tme
= find_thread_entry(thread
))) {
1347 *command
= tme
->tm_command
;
1349 *command
= EMPTYSTRING
;
1354 add_thread_entry_to_list(thread_entry_t
*list
, uint64_t thread
)
1358 if ((te
= thread_entry_freelist
)) {
1359 thread_entry_freelist
= te
->te_next
;
1361 te
= (thread_entry_t
)malloc(sizeof(struct thread_entry
));
1364 te
->te_thread
= thread
;
1365 te
->te_next
= *list
;
1370 exec_thread_entry(uint64_t thread
, char *command
)
1374 if ((tme
= find_thread_entry(thread
))) {
1375 if (tme
->tm_orig_command
[0] == '\0') {
1376 (void)strncpy (tme
->tm_orig_command
, tme
->tm_command
, MAXCOMLEN
);
1377 tme
->tm_orig_command
[MAXCOMLEN
] = '\0';
1379 (void)strncpy (tme
->tm_command
, command
, MAXCOMLEN
);
1380 tme
->tm_command
[MAXCOMLEN
] = '\0';
1382 add_thread_entry_to_list(&thread_reset_list
, thread
);
1384 create_map_entry(thread
, command
);
1389 record_thread_entry_for_gc(uint64_t thread
)
1391 add_thread_entry_to_list(&thread_delete_list
, thread
);
1395 gc_thread_entries(void)
1398 thread_entry_t te_next
;
1401 for (te
= thread_delete_list
; te
; te
= te_next
) {
1402 delete_thread_entry(te
->te_thread
);
1404 te_next
= te
->te_next
;
1405 te
->te_next
= thread_entry_freelist
;
1406 thread_entry_freelist
= te
;
1410 thread_delete_list
= 0;
1414 gc_reset_entries(void)
1417 thread_entry_t te_next
;
1420 for (te
= thread_reset_list
; te
; te
= te_next
) {
1421 te_next
= te
->te_next
;
1422 te
->te_next
= thread_entry_freelist
;
1423 thread_entry_freelist
= te
;
1427 thread_reset_list
= 0;
1431 reset_thread_names(void)
1434 thread_entry_t te_next
;
1437 for (te
= thread_reset_list
; te
; te
= te_next
) {
1440 if ((tme
= find_thread_entry(te
->te_thread
))) {
1441 if (tme
->tm_orig_command
[0]) {
1442 (void)strncpy (tme
->tm_command
, tme
->tm_orig_command
, MAXCOMLEN
);
1443 tme
->tm_command
[MAXCOMLEN
] = '\0';
1444 tme
->tm_orig_command
[0] = '\0';
1447 te_next
= te
->te_next
;
1448 te
->te_next
= thread_entry_freelist
;
1449 thread_entry_freelist
= te
;
1453 thread_reset_list
= 0;
1457 delete_all_thread_entries(void)
1459 threadmap_t tme
= 0;
1460 threadmap_t tme_next
= 0;
1463 for (i
= 0; i
< HASH_SIZE
; i
++) {
1464 for (tme
= threadmap_hash
[i
]; tme
; tme
= tme_next
) {
1465 tme_next
= tme
->tm_next
;
1466 tme
->tm_next
= threadmap_freelist
;
1467 threadmap_freelist
= tme
;
1469 threadmap_hash
[i
] = 0;
1474 insert_run_event(uint64_t thread
, int priority
, kd_buf
*kd
, uint64_t now
)
1478 int hashid
= thread
& HASH_MASK
;
1480 for (trp
= threadrun_hash
[hashid
]; trp
; trp
= trp
->tr_next
) {
1481 if (trp
->tr_thread
== thread
) {
1486 if ((trp
= threadrun_freelist
)) {
1487 threadrun_freelist
= trp
->tr_next
;
1489 trp
= (threadrun_t
)malloc(sizeof(struct threadrun
));
1492 trp
->tr_thread
= thread
;
1494 trp
->tr_next
= threadrun_hash
[hashid
];
1495 threadrun_hash
[hashid
] = trp
;
1497 add_thread_entry_to_list(&thread_run_list
, thread
);
1500 trp
->tr_timestamp
= now
;
1501 trp
->tr_priority
= priority
;
1505 find_run_event(uint64_t thread
)
1508 int hashid
= thread
& HASH_MASK
;
1510 for (trp
= threadrun_hash
[hashid
]; trp
; trp
= trp
->tr_next
) {
1511 if (trp
->tr_thread
== thread
) {
1519 delete_run_event(uint64_t thread
)
1521 threadrun_t trp
= 0;
1522 threadrun_t trp_prev
;
1524 int hashid
= thread
& HASH_MASK
;
1526 if ((trp
= threadrun_hash
[hashid
])) {
1527 if (trp
->tr_thread
== thread
) {
1528 threadrun_hash
[hashid
] = trp
->tr_next
;
1532 for (trp
= trp
->tr_next
; trp
; trp
= trp
->tr_next
) {
1533 if (trp
->tr_thread
== thread
) {
1534 trp_prev
->tr_next
= trp
->tr_next
;
1541 trp
->tr_next
= threadrun_freelist
;
1542 threadrun_freelist
= trp
;
1551 thread_entry_t te_next
;
1553 threadrun_t trp_next
;
1556 for (te
= thread_run_list
; te
; te
= te_next
) {
1557 int hashid
= te
->te_thread
& HASH_MASK
;
1559 for (trp
= threadrun_hash
[hashid
]; trp
; trp
= trp_next
) {
1560 trp_next
= trp
->tr_next
;
1561 trp
->tr_next
= threadrun_freelist
;
1562 threadrun_freelist
= trp
;
1565 threadrun_hash
[hashid
] = 0;
1567 te_next
= te
->te_next
;
1568 te
->te_next
= thread_entry_freelist
;
1569 thread_entry_freelist
= te
;
1571 thread_run_list
= 0;
1577 insert_start_event(uint64_t thread
, int type
, uint64_t now
)
1581 int hashid
= thread
& HASH_MASK
;
1583 for (evp
= event_hash
[hashid
]; evp
; evp
= evp
->ev_next
) {
1584 if (evp
->ev_thread
== thread
&& evp
->ev_type
== type
) {
1589 if ((evp
= event_freelist
)) {
1590 event_freelist
= evp
->ev_next
;
1592 evp
= (event_t
)malloc(sizeof(struct event
));
1595 evp
->ev_thread
= thread
;
1596 evp
->ev_type
= type
;
1598 evp
->ev_next
= event_hash
[hashid
];
1599 event_hash
[hashid
] = evp
;
1601 add_thread_entry_to_list(&thread_event_list
, thread
);
1603 evp
->ev_timestamp
= now
;
1608 consume_start_event(uint64_t thread
, int type
, uint64_t now
)
1612 uint64_t elapsed
= 0;
1614 int hashid
= thread
& HASH_MASK
;
1616 if ((evp
= event_hash
[hashid
])) {
1617 if (evp
->ev_thread
== thread
&& evp
->ev_type
== type
) {
1618 event_hash
[hashid
] = evp
->ev_next
;
1622 for (evp
= evp
->ev_next
; evp
; evp
= evp
->ev_next
) {
1623 if (evp
->ev_thread
== thread
&& evp
->ev_type
== type
) {
1624 evp_prev
->ev_next
= evp
->ev_next
;
1631 elapsed
= now
- evp
->ev_timestamp
;
1633 if (now
< evp
->ev_timestamp
) {
1634 printf("consume: now = %qd, timestamp = %qd\n", now
, evp
->ev_timestamp
);
1637 evp
->ev_next
= event_freelist
;
1638 event_freelist
= evp
;
1645 gc_start_events(void)
1648 thread_entry_t te_next
;
1654 for (te
= thread_event_list
; te
; te
= te_next
) {
1656 hashid
= te
->te_thread
& HASH_MASK
;
1658 for (evp
= event_hash
[hashid
]; evp
; evp
= evp_next
) {
1659 evp_next
= evp
->ev_next
;
1660 evp
->ev_next
= event_freelist
;
1661 event_freelist
= evp
;
1664 event_hash
[hashid
] = 0;
1666 te_next
= te
->te_next
;
1667 te
->te_next
= thread_entry_freelist
;
1668 thread_entry_freelist
= te
;
1670 thread_event_list
= 0;
1674 thread_in_user_mode(uint64_t thread
, char *command
)
1678 if (strcmp(command
, "kernel_task") == 0) {
1682 int hashid
= thread
& HASH_MASK
;
1684 for (evp
= event_hash
[hashid
]; evp
; evp
= evp
->ev_next
) {
1685 if (evp
->ev_thread
== thread
) {
1693 handle_lookup_event(uint64_t thread
, int debugid
, kd_buf
*kdp
)
1696 boolean_t first_record
= FALSE
;
1698 int hashid
= thread
& HASH_MASK
;
1700 if (debugid
& DBG_FUNC_START
) {
1701 first_record
= TRUE
;
1704 for (lkp
= lookup_hash
[hashid
]; lkp
; lkp
= lkp
->lk_next
) {
1705 if (lkp
->lk_thread
== thread
) {
1710 if (first_record
== FALSE
) {
1714 if ((lkp
= lookup_freelist
)) {
1715 lookup_freelist
= lkp
->lk_next
;
1717 lkp
= (lookup_t
)malloc(sizeof(struct lookup
));
1720 lkp
->lk_thread
= thread
;
1722 lkp
->lk_next
= lookup_hash
[hashid
];
1723 lookup_hash
[hashid
] = lkp
;
1725 add_thread_entry_to_list(&thread_lookup_list
, thread
);
1728 if (first_record
== TRUE
) {
1729 lkp
->lk_pathptr
= lkp
->lk_pathname
;
1730 lkp
->lk_dvp
= kdp
->arg1
;
1732 if (lkp
->lk_pathptr
> &lkp
->lk_pathname
[NUMPARMS
-4]) {
1735 *lkp
->lk_pathptr
++ = kdp
->arg1
;
1737 *lkp
->lk_pathptr
++ = kdp
->arg2
;
1738 *lkp
->lk_pathptr
++ = kdp
->arg3
;
1739 *lkp
->lk_pathptr
++ = kdp
->arg4
;
1740 *lkp
->lk_pathptr
= 0;
1742 if (debugid
& DBG_FUNC_END
) {
1750 delete_lookup_event(uint64_t thread
, lookup_t lkp_to_delete
)
1756 hashid
= thread
& HASH_MASK
;
1758 if ((lkp
= lookup_hash
[hashid
])) {
1759 if (lkp
== lkp_to_delete
) {
1760 lookup_hash
[hashid
] = lkp
->lk_next
;
1764 for (lkp
= lkp
->lk_next
; lkp
; lkp
= lkp
->lk_next
) {
1765 if (lkp
== lkp_to_delete
) {
1766 lkp_prev
->lk_next
= lkp
->lk_next
;
1773 lkp
->lk_next
= lookup_freelist
;
1774 lookup_freelist
= lkp
;
1780 gc_lookup_events(void)
1783 thread_entry_t te_next
;
1789 for (te
= thread_lookup_list
; te
; te
= te_next
) {
1790 hashid
= te
->te_thread
& HASH_MASK
;
1792 for (lkp
= lookup_hash
[hashid
]; lkp
; lkp
= lkp_next
) {
1793 lkp_next
= lkp
->lk_next
;
1794 lkp
->lk_next
= lookup_freelist
;
1795 lookup_freelist
= lkp
;
1798 lookup_hash
[hashid
] = 0;
1800 te_next
= te
->te_next
;
1801 te
->te_next
= thread_entry_freelist
;
1802 thread_entry_freelist
= te
;
1804 thread_lookup_list
= 0;
1810 kd_buf
*kd
, *end_of_sample
;
1817 * Get kernel buffer information
1819 get_bufinfo(&bufinfo
);
1822 delete_all_thread_entries();
1829 bytes_read
= read(RAW_fd
, my_buffer
, num_entries
* sizeof(kd_buf
));
1831 if (bytes_read
== -1) {
1832 perror("read failed");
1835 count
= bytes_read
/ sizeof(kd_buf
);
1837 if (count
!= num_entries
) {
1842 kd
= (kd_buf
*)my_buffer
;
1843 first_now
= kd
->timestamp
& KDBG_TIMESTAMP_MASK
;
1848 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDREADTR
};
1849 size_t needed
= bufinfo
.nkdbufs
* sizeof(kd_buf
);
1851 if (sysctl(mib
, ARRAYSIZE(mib
), my_buffer
, &needed
, NULL
, 0) < 0) {
1852 quit("trace facility failure, KERN_KDREADTR\n");
1856 sample_generation
++;
1858 if (bufinfo
.flags
& KDBG_WRAPPED
) {
1862 fprintf(log_fp
, "\n\n%-19.19s sample = %d <<<<<<< trace buffer wrapped >>>>>>>\n\n",
1863 &(ctime(&curr_time
)[0]), sample_generation
);
1869 end_of_sample
= &((kd_buf
*)my_buffer
)[count
];
1872 * Always reinitialize the DECR_TRAP array
1874 for (i
= 0; i
< num_cpus
; i
++) {
1875 last_decrementer_kd
[i
] = (kd_buf
*)my_buffer
;
1878 for (kd
= (kd_buf
*)my_buffer
; kd
< end_of_sample
; kd
++) {
1880 uint64_t thread
= kd
->arg5
;
1881 int type
= kd
->debugid
& DBG_FUNC_MASK
;
1883 (void)check_for_thread_update(thread
, type
, kd
, NULL
);
1885 uint64_t now
= kd
->timestamp
& KDBG_TIMESTAMP_MASK
;
1888 if (type
== DECR_TRAP
) {
1889 int cpunum
= CPU_NUMBER(kd
);
1890 double i_latency
= handle_decrementer(kd
, cpunum
);
1893 if (i_thresh_hold
&& (int)i_latency
> i_thresh_hold
) {
1894 kd_start
= last_decrementer_kd
[cpunum
];
1896 log_decrementer(kd_start
, kd
, end_of_sample
, i_latency
);
1898 last_decrementer_kd
[cpunum
] = kd
;
1903 if (check_for_scheduler_latency(type
, &thread
, now
, kd
, &kd_start
, &s_priority
, &s_latency
)) {
1904 log_scheduler(kd_start
, kd
, end_of_sample
, s_priority
, s_latency
, thread
);
1912 gc_thread_entries();
1920 enter_syscall(FILE *fp
, kd_buf
*kd
, uint64_t thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, int print_info
)
1927 int cpunum
= CPU_NUMBER(kd
);
1929 if (print_info
&& fp
) {
1930 timestamp
= (double)(now
- start_bias
) / divisor
;
1931 delta
= (double)idelta
/ divisor
;
1933 if ((p
= find_code(type
))) {
1934 if (type
== INTERRUPT
) {
1943 pc_to_string(&pcstring
[0], kd
->arg2
, 58, mode
);
1945 fprintf(fp
, "%9.1f %8.1f\t\tINTERRUPT[%2" PRIx64
"] @ %-58.58s %8" PRIx64
" %2d %s\n",
1946 timestamp
, delta
, (uint64_t)kd
->arg1
, &pcstring
[0], thread
, cpunum
, command
);
1947 } else if (type
== MACH_vmfault
) {
1948 fprintf(fp
, "%9.1f %8.1f\t\t%-28.28s %8" PRIx64
" %2d %s\n",
1949 timestamp
, delta
, p
, thread
, cpunum
, command
);
1951 fprintf(fp
, "%9.1f %8.1f\t\t%-28.28s %-16" PRIx64
" %-16" PRIx64
" %-16" PRIx64
" %-16" PRIx64
" %8" PRIx64
" %2d %s\n",
1952 timestamp
, delta
, p
, (uint64_t)kd
->arg1
, (uint64_t)kd
->arg2
, (uint64_t)kd
->arg3
, (uint64_t)kd
->arg4
,
1953 thread
, cpunum
, command
);
1956 fprintf(fp
, "%9.1f %8.1f\t\t%-8x %-16" PRIx64
" %-16" PRIx64
" %-16" PRIx64
" %-16" PRIx64
" %8" PRIx64
" %2d %s\n",
1957 timestamp
, delta
, type
, (uint64_t)kd
->arg1
, (uint64_t)kd
->arg2
, (uint64_t)kd
->arg3
, (uint64_t)kd
->arg4
,
1958 thread
, cpunum
, command
);
1961 if (type
!= BSC_thread_terminate
&& type
!= BSC_exit
) {
1962 insert_start_event(thread
, type
, now
);
1967 exit_syscall(FILE *fp
, kd_buf
*kd
, uint64_t thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, int print_info
)
1973 double elapsed_timestamp
;
1975 elapsed_timestamp
= (double)consume_start_event(thread
, type
, now
) / divisor
;
1977 if (print_info
&& fp
) {
1978 int cpunum
= CPU_NUMBER(kd
);
1980 timestamp
= (double)(now
- start_bias
) / divisor
;
1981 delta
= (double)idelta
/ divisor
;
1983 fprintf(fp
, "%9.1f %8.1f(%.1f) \t", timestamp
, delta
, elapsed_timestamp
);
1985 if ((p
= find_code(type
))) {
1986 if (type
== INTERRUPT
) {
1987 fprintf(fp
, "INTERRUPT %8" PRIx64
" %2d %s\n", thread
, cpunum
, command
);
1988 } else if (type
== MACH_vmfault
&& kd
->arg4
<= DBG_PAGEIND_FAULT
) {
1989 user_addr
= ((uint64_t)kd
->arg1
<< 32) | (uint32_t)kd
->arg2
;
1991 fprintf(fp
, "%-28.28s %-10.10s %-16qx %8" PRIx64
" %2d %s\n",
1992 p
, fault_name
[kd
->arg4
], user_addr
,
1993 thread
, cpunum
, command
);
1995 fprintf(fp
, "%-28.28s %-16" PRIx64
" %-16" PRIx64
" %8" PRIx64
" %2d %s\n",
1996 p
, (uint64_t)kd
->arg1
, (uint64_t)kd
->arg2
,
1997 thread
, cpunum
, command
);
2000 fprintf(fp
, "%-8x %-16" PRIx64
" %-16" PRIx64
" %8" PRIx64
" %2d %s\n",
2001 type
, (uint64_t)kd
->arg1
, (uint64_t)kd
->arg2
,
2002 thread
, cpunum
, command
);
2008 print_entry(FILE *fp
, kd_buf
*kd
, uint64_t thread
, int type
, char *command
, uint64_t now
, uint64_t idelta
, uint64_t start_bias
, kd_buf
*kd_note
)
2016 int cpunum
= CPU_NUMBER(kd
);
2018 double timestamp
= (double)(now
- start_bias
) / divisor
;
2019 double delta
= (double)idelta
/ divisor
;
2021 if ((p
= find_code(type
))) {
2022 if (kd
== kd_note
) {
2023 fprintf(fp
, "%9.1f %8.1f\t**\t", timestamp
, delta
);
2025 fprintf(fp
, "%9.1f %8.1f\t\t", timestamp
, delta
);
2027 fprintf(fp
, "%-28.28s %-16" PRIx64
" %-16" PRIx64
" %-16" PRIx64
" %-16" PRIx64
" %8" PRIx64
" %2d %s\n",
2028 p
, (uint64_t)kd
->arg1
, (uint64_t)kd
->arg2
, (uint64_t)kd
->arg3
, (uint64_t)kd
->arg4
, thread
, cpunum
, command
);
2030 fprintf(fp
, "%9.1f %8.1f\t\t%-8x %-16" PRIx64
" %-16" PRIx64
" %-16" PRIx64
" %-16" PRIx64
" %8" PRIx64
" %2d %s\n",
2031 timestamp
, delta
, type
, (uint64_t)kd
->arg1
, (uint64_t)kd
->arg2
, (uint64_t)kd
->arg3
, (uint64_t)kd
->arg4
,
2032 thread
, cpunum
, command
);
2037 check_for_thread_update(uint64_t thread
, int debugid_base
, kd_buf
*kbufp
, char **command
)
2039 if (debugid_base
== TRACE_DATA_NEWTHREAD
) {
2041 * Save the create thread data
2043 create_tmp_map_entry(kbufp
->arg1
, thread
);
2044 } else if (debugid_base
== TRACE_STRING_NEWTHREAD
) {
2046 * process new map entry
2048 find_and_insert_tmp_map_entry(thread
, (char *)&kbufp
->arg1
);
2049 } else if (debugid_base
== TRACE_STRING_EXEC
) {
2050 exec_thread_entry(thread
, (char *)&kbufp
->arg1
);
2052 if (debugid_base
== BSC_exit
|| debugid_base
== BSC_thread_terminate
) {
2053 record_thread_entry_for_gc(thread
);
2056 find_thread_name(thread
, command
);
2062 log_info(uint64_t now
, uint64_t idelta
, uint64_t start_bias
, kd_buf
*kd
, kd_buf
*kd_note
)
2070 char command_buf
[32];
2071 char sched_info
[64];
2073 const char *sched_reason
;
2079 uint64_t thread
= kd
->arg5
;
2080 int cpunum
= CPU_NUMBER(kd
);
2081 int debugid
= kd
->debugid
;
2082 int type
= kd
->debugid
& DBG_FUNC_MASK
;
2084 (void)check_for_thread_update(thread
, type
, kd
, &command
);
2086 if ((type
>> 24) == DBG_TRACE
) {
2087 if (((type
>> 16) & 0xff) != DBG_TRACE_INFO
) {
2091 timestamp
= (double)(now
- start_bias
) / divisor
;
2092 delta
= (double)idelta
/ divisor
;
2097 pc_to_string(&pcstring
[0], kd
->arg1
, 84, KERNEL_MODE
);
2099 fprintf(log_fp
, "%9.1f %8.1f\t\tCQ_action @ %-84.84s %8" PRIx64
" %2d %s\n",
2100 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2104 pc_to_string(&pcstring
[0], kd
->arg1
, 83, KERNEL_MODE
);
2106 fprintf(log_fp
, "%9.1f %8.1f\t\tTES_action @ %-83.83s %8" PRIx64
" %2d %s\n",
2107 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2111 pc_to_string(&pcstring
[0], kd
->arg1
, 83, KERNEL_MODE
);
2113 fprintf(log_fp
, "%9.1f %8.1f\t\tIES_action @ %-83.83s %8" PRIx64
" %2d %s\n",
2114 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2118 pc_to_string(&pcstring
[0], kd
->arg1
, 83, KERNEL_MODE
);
2120 fprintf(log_fp
, "%9.1f %8.1f\t\tIES_filter @ %-83.83s %8" PRIx64
" %2d %s\n",
2121 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2125 if ((int)kd
->arg1
>= 0) {
2128 i_latency
= (((double)(-1 - kd
->arg1
)) / divisor
);
2131 if (i_thresh_hold
&& (int)i_latency
> i_thresh_hold
) {
2143 pc_to_string(&pcstring
[0], kd
->arg2
, 84, mode
);
2145 fprintf(log_fp
, "%9.1f %8.1f[%.1f]%s\tDECR_TRAP @ %-84.84s %8" PRIx64
" %2d %s\n",
2146 timestamp
, delta
, i_latency
, p
, &pcstring
[0], thread
, cpunum
, command
);
2150 fprintf(log_fp
, "%9.1f %8.1f[%.1f] \t%-28.28s %8" PRIx64
" %2d %s\n",
2151 timestamp
, delta
, (double)kd
->arg1
/divisor
, "DECR_SET", thread
, cpunum
, command
);
2155 case MACH_stkhandoff
:
2157 find_thread_name(kd
->arg2
, &command1
);
2159 if (command1
== EMPTYSTRING
) {
2160 command1
= command_buf
;
2161 sprintf(command1
, "%-8" PRIx64
, (uint64_t)kd
->arg2
);
2163 if (thread_in_user_mode(kd
->arg2
, command1
)) {
2171 if (reason
> MAX_REASON
) {
2174 sched_reason
= sched_reasons
[reason
];
2177 if (sched_reason
[0] == '?') {
2178 sprintf(joe
, "%" PRIx64
, reason
);
2181 sprintf(sched_info
, "%16.16s @ pri %3" PRIu64
" --> %16.16s @ pri %3" PRIu64
"%s", command
, (uint64_t)kd
->arg3
, command1
, (uint64_t)kd
->arg4
, p
);
2183 fprintf(log_fp
, "%9.1f %8.1f\t\t%-10.10s[%s] %s %8" PRIx64
" %2d\n",
2184 timestamp
, delta
, "MACH_SCHED", sched_reason
, sched_info
, thread
, cpunum
);
2188 if ((lkp
= handle_lookup_event(thread
, debugid
, kd
))) {
2190 * print the tail end of the pathname
2192 p
= (char *)lkp
->lk_pathname
;
2193 size_t clen
= strlen(p
);
2201 fprintf(log_fp
, "%9.1f %8.1f\t\t%-14.14s %-59s %-16" PRIx64
" %8" PRIx64
" %2d %s\n",
2202 timestamp
, delta
, "VFS_LOOKUP",
2203 &p
[clen
], lkp
->lk_dvp
, thread
, cpunum
, command
);
2205 delete_lookup_event(thread
, lkp
);
2210 if (debugid
& DBG_FUNC_START
) {
2211 enter_syscall(log_fp
, kd
, thread
, type
, command
, now
, idelta
, start_bias
, 1);
2212 } else if (debugid
& DBG_FUNC_END
) {
2213 exit_syscall(log_fp
, kd
, thread
, type
, command
, now
, idelta
, start_bias
, 1);
2215 print_entry(log_fp
, kd
, thread
, type
, command
, now
, idelta
, start_bias
, kd_note
);
2222 log_range(kd_buf
*kd_buffer
, kd_buf
*kd_start
, kd_buf
*kd_stop
, kd_buf
*kd_note
, char *buf1
)
2224 uint64_t last_timestamp
= 0;
2226 uint64_t start_bias
= 0;
2232 clen
= strlen(buf1
);
2233 memset(buf2
, '-', clen
);
2235 fprintf(log_fp
, "\n\n%s\n", buf2
);
2236 fprintf(log_fp
, "%s\n\n", buf1
);
2238 fprintf(log_fp
, "RelTime(Us) Delta debugid arg1 arg2 arg3 arg4 thread cpu command\n\n");
2240 reset_thread_names();
2242 last_timestamp
= kd_start
->timestamp
& KDBG_TIMESTAMP_MASK
;
2243 start_bias
= last_timestamp
;
2245 for (kd
= kd_buffer
; kd
<= kd_stop
; kd
++) {
2246 now
= kd
->timestamp
& KDBG_TIMESTAMP_MASK
;
2248 if (kd
>= kd_start
) {
2249 delta
= now
- last_timestamp
;
2251 log_info(now
, delta
, start_bias
, kd
, kd_note
);
2253 last_timestamp
= now
;
2255 int debugid
= kd
->debugid
;
2256 uint64_t thread
= kd
->arg5
;
2257 int type
= kd
->debugid
& DBG_FUNC_MASK
;
2259 if ((type
>> 24) == DBG_TRACE
) {
2260 if (((type
>> 16) & 0xff) != DBG_TRACE_INFO
) {
2264 if (type
== BSC_thread_terminate
|| type
== BSC_exit
) {
2268 if (debugid
& DBG_FUNC_START
) {
2269 insert_start_event(thread
, type
, now
);
2270 } else if (debugid
& DBG_FUNC_END
) {
2271 (void)consume_start_event(thread
, type
, now
);
2280 log_decrementer(kd_buf
*kd_beg
, kd_buf
*kd_end
, kd_buf
*end_of_sample
, double i_latency
)
2282 kd_buf
*kd_start
, *kd_stop
;
2283 int kd_count
; /* Limit the boundary of kd_start */
2285 double sample_timestamp
;
2288 uint64_t thread
= kd_beg
->arg5
;
2289 int cpunum
= CPU_NUMBER(kd_end
);
2291 for (kd_count
= 0, kd_start
= kd_beg
- 1; (kd_start
>= (kd_buf
*)my_buffer
); kd_start
--, kd_count
++) {
2292 if (kd_count
== MAX_LOG_COUNT
) {
2296 if (CPU_NUMBER(kd_start
) != cpunum
) {
2300 if ((kd_start
->debugid
& DBG_FUNC_MASK
) == DECR_TRAP
) {
2304 if (kd_start
->arg5
!= thread
) {
2308 if (kd_start
< (kd_buf
*)my_buffer
) {
2309 kd_start
= (kd_buf
*)my_buffer
;
2312 thread
= kd_end
->arg5
;
2314 for (kd_stop
= kd_end
+ 1; kd_stop
< end_of_sample
; kd_stop
++) {
2315 if (CPU_NUMBER(kd_stop
) != cpunum
) {
2319 if ((kd_stop
->debugid
& DBG_FUNC_MASK
) == INTERRUPT
) {
2323 if (kd_stop
->arg5
!= thread
) {
2327 if (kd_stop
>= end_of_sample
) {
2328 kd_stop
= end_of_sample
- 1;
2335 now
= kd_start
->timestamp
& KDBG_TIMESTAMP_MASK
;
2336 sample_timestamp
= (double)(now
- first_now
) / divisor
;
2338 TOD_usecs
= (uint64_t)sample_timestamp
;
2339 TOD_secs
= (unsigned long)sample_TOD_secs
+ (unsigned long)((sample_TOD_usecs
+ TOD_usecs
) / 1000000);
2341 sprintf(buf1
, "%-19.19s interrupt latency = %.1fus [timestamp %.1f]", ctime(&TOD_secs
), i_latency
, sample_timestamp
);
2343 sprintf(buf1
, "%-19.19s interrupt latency = %.1fus [sample %d]", &(ctime(&curr_time
)[0]), i_latency
, sample_generation
);
2346 log_range((kd_buf
*)my_buffer
, kd_start
, kd_stop
, 0, buf1
);
2353 log_scheduler(kd_buf
*kd_beg
, kd_buf
*kd_end
, kd_buf
*end_of_sample
, int s_priority
, double s_latency
, uint64_t thread
)
2355 kd_buf
*kd_start
, *kd_stop
;
2360 double sample_timestamp
;
2363 for (count
= 0, kd_start
= kd_beg
; (kd_start
>= (kd_buf
*)my_buffer
); kd_start
--) {
2364 cpunum
= CPU_NUMBER(kd_start
);
2366 cmask
|= ((uint64_t)1 << cpunum
);
2368 if (cmask
== cpu_mask
) {
2373 if (kd_start
< (kd_buf
*)my_buffer
) {
2374 kd_start
= (kd_buf
*)my_buffer
;
2377 for (kd_stop
= kd_end
+ 1; kd_stop
< end_of_sample
; kd_stop
++) {
2378 if (kd_stop
->arg5
== thread
) {
2382 if (kd_stop
>= end_of_sample
) {
2383 kd_stop
= end_of_sample
- 1;
2390 now
= kd_start
->timestamp
& KDBG_TIMESTAMP_MASK
;
2391 sample_timestamp
= (double)(now
- first_now
) / divisor
;
2393 TOD_usecs
= (uint64_t)sample_timestamp
;
2394 TOD_secs
= (unsigned long)sample_TOD_secs
+ (unsigned long)((sample_TOD_usecs
+ TOD_usecs
) / 1000000);
2396 sprintf(buf1
, "%-19.19s priority = %d, scheduling latency = %.1fus [timestamp %.1f]", ctime(&TOD_secs
), s_priority
, s_latency
, sample_timestamp
);
2398 sprintf(buf1
, "%-19.19s priority = %d, scheduling latency = %.1fus [sample %d]", &(ctime(&curr_time
)[0]), s_priority
, s_latency
, sample_generation
);
2401 log_range((kd_buf
*)my_buffer
, kd_start
, kd_stop
, kd_beg
, buf1
);
2405 check_for_scheduler_latency(int type
, uint64_t *thread
, uint64_t now
, kd_buf
*kd
, kd_buf
**kd_start
, int *priority
, double *latency
)
2407 int found_latency
= 0;
2409 if (type
== MACH_makerunnable
) {
2410 if (watch_priority_min
<= kd
->arg2
&& kd
->arg2
<= watch_priority_max
) {
2411 insert_run_event(kd
->arg1
, (int)kd
->arg2
, kd
, now
);
2413 } else if (type
== MACH_sched
|| type
== MACH_stkhandoff
) {
2414 threadrun_t trp
= find_run_event(kd
->arg2
);
2416 if (type
== MACH_sched
|| type
== MACH_stkhandoff
) {
2420 if ((trp
= find_run_event(*thread
))) {
2421 double d_s_latency
= (((double)(now
- trp
->tr_timestamp
)) / divisor
);
2422 int s_latency
= (int)d_s_latency
;
2425 if (s_latency
< 100) {
2426 s_usec_10_bins
[s_latency
/10]++;
2428 if (s_latency
< 1000) {
2429 s_usec_100_bins
[s_latency
/100]++;
2430 } else if (s_latency
< 10000) {
2431 s_msec_1_bins
[s_latency
/1000]++;
2432 } else if (s_latency
< 50000) {
2433 s_msec_10_bins
[s_latency
/10000]++;
2438 if (s_latency
> s_max_latency
) {
2439 s_max_latency
= s_latency
;
2441 if (s_latency
< s_min_latency
|| s_total_samples
== 0) {
2442 s_min_latency
= s_latency
;
2444 s_total_latency
+= s_latency
;
2447 if (s_thresh_hold
&& s_latency
> s_thresh_hold
) {
2448 s_exceeded_threshold
++;
2451 *kd_start
= trp
->tr_entry
;
2452 *priority
= trp
->tr_priority
;
2453 *latency
= d_s_latency
;
2458 delete_run_event(*thread
);
2461 return found_latency
;
2465 handle_decrementer(kd_buf
*kd
, int cpunum
)
2467 struct i_latencies
*il
;
2471 if (i_latency_per_cpu
== FALSE
) {
2475 il
= &i_lat
[cpunum
];
2477 if ((long)(kd
->arg1
) >= 0) {
2480 latency
= (((double)(-1 - kd
->arg1
)) / divisor
);
2482 elapsed_usecs
= (long)latency
;
2484 if (elapsed_usecs
< 100) {
2485 il
->i_usec_10_bins
[elapsed_usecs
/10]++;
2488 if (elapsed_usecs
< 1000) {
2489 il
->i_usec_100_bins
[elapsed_usecs
/100]++;
2490 } else if (elapsed_usecs
< 10000) {
2491 il
->i_msec_1_bins
[elapsed_usecs
/1000]++;
2492 } else if (elapsed_usecs
< 50000) {
2493 il
->i_msec_10_bins
[elapsed_usecs
/10000]++;
2498 if (use_high_res_bins
&& elapsed_usecs
< N_HIGH_RES_BINS
) {
2499 i_high_res_bins
[elapsed_usecs
]++;
2501 if (i_thresh_hold
&& elapsed_usecs
> i_thresh_hold
) {
2502 il
->i_exceeded_threshold
++;
2504 if (elapsed_usecs
> il
->i_max_latency
) {
2505 il
->i_max_latency
= elapsed_usecs
;
2507 if (elapsed_usecs
< il
->i_min_latency
|| il
->i_total_samples
== 0) {
2508 il
->i_min_latency
= elapsed_usecs
;
2510 il
->i_total_latency
+= elapsed_usecs
;
2511 il
->i_total_samples
++;
2520 for (i
= 0; i
< num_of_codes
; i
++) {
2521 if (codes_tab
[i
].type
== type
) {
2522 return codes_tab
[i
].name
;
2529 init_code_file(void)
2534 if ((fp
= fopen(code_file
, "r")) == NULL
) {
2536 fprintf(log_fp
, "open of %s failed\n", code_file
);
2540 for (i
= 0; i
< MAX_ENTRIES
; i
++) {
2543 int n
= fscanf(fp
, "%x%127s\n", &code
, name
);
2545 if (n
== 1 && i
== 0) {
2547 * old code file format, just skip
2555 strncpy(codes_tab
[i
].name
, name
, 32);
2556 codes_tab
[i
].type
= code
;
2569 char tmp_nm_file
[128];
2573 bzero(tmp_nm_file
, 128);
2574 bzero(tmpstr
, 1024);
2577 * Build the temporary nm file path
2579 strcpy(tmp_nm_file
,"/tmp/knm.out.XXXXXX");
2581 if (!mktemp(tmp_nm_file
)) {
2582 fprintf(stderr
, "Error in mktemp call\n");
2587 * Build the nm command and create a tmp file with the output
2589 sprintf (tmpstr
, "/usr/bin/nm -n %s -s __TEXT __text > %s",
2590 kernelpath
, tmp_nm_file
);
2594 * Parse the output from the nm command
2596 if ((fp
= fopen(tmp_nm_file
, "r")) == NULL
) {
2597 /* Hmmm, let's not treat this as fatal */
2598 fprintf(stderr
, "Failed to open nm symbol file [%s]\n", tmp_nm_file
);
2602 * Count the number of symbols in the nm symbol table
2606 while ((inchr
= getc(fp
)) != -1) {
2607 if (inchr
== '\n') {
2614 * Malloc the space for symbol table
2616 if (kern_sym_count
> 0) {
2617 kern_sym_tbl
= malloc(kern_sym_count
* sizeof(kern_sym_t
));
2619 if (!kern_sym_tbl
) {
2621 * Hmmm, lets not treat this as fatal
2623 fprintf(stderr
, "Can't allocate memory for kernel symbol table\n");
2625 bzero(kern_sym_tbl
, kern_sym_count
* sizeof(kern_sym_t
));
2629 * Hmmm, lets not treat this as fatal
2631 fprintf(stderr
, "No kernel symbol table \n");
2633 for (i
= 0; i
< kern_sym_count
; i
++) {
2634 bzero(tmpstr
, 1024);
2636 if (fscanf(fp
, "%p %c %s", &kern_sym_tbl
[i
].k_sym_addr
, &inchr
, tmpstr
) != 3) {
2639 len
= strlen(tmpstr
);
2640 kern_sym_tbl
[i
].k_sym_name
= malloc(len
+ 1);
2642 if (kern_sym_tbl
[i
].k_sym_name
== NULL
) {
2643 fprintf(stderr
, "Can't allocate memory for symbol name [%s]\n", tmpstr
);
2644 kern_sym_tbl
[i
].k_sym_name
= NULL
;
2647 strcpy(kern_sym_tbl
[i
].k_sym_name
, tmpstr
);
2650 kern_sym_tbl
[i
].k_sym_len
= len
;
2653 if (i
!= kern_sym_count
) {
2655 * Hmmm, didn't build up entire table from nm
2656 * scrap the entire thing
2659 kern_sym_tbl
= NULL
;
2665 * Remove the temporary nm file
2667 unlink(tmp_nm_file
);
2670 * Dump the kernel symbol table
2672 for (i
= 0; i
< kern_sym_count
; i
++) {
2673 if (kern_sym_tbl
[i
].k_sym_name
) {
2674 printf ("[%d] %-16p %s\n", i
,
2675 kern_sym_tbl
[i
].k_sym_addr
, kern_sym_tbl
[i
].k_sym_name
);
2677 printf ("[%d] %-16p %s\n", i
,
2678 kern_sym_tbl
[i
].k_sym_addr
, "No symbol name");
2685 pc_to_string(char *pcstring
, uint64_t pc
, int max_len
, int mode
)
2690 if (mode
== USER_MODE
) {
2691 sprintf(pcstring
, "%-16" PRIx64
" [usermode addr]", pc
);
2694 ret
= binary_search(kern_sym_tbl
, 0, kern_sym_count
-1, pc
);
2696 if (ret
== -1 || kern_sym_tbl
[ret
].k_sym_name
== NULL
) {
2697 sprintf(pcstring
, "%-16" PRIx64
, pc
);
2700 if ((len
= kern_sym_tbl
[ret
].k_sym_len
) > (max_len
- 8)) {
2704 memcpy(pcstring
, kern_sym_tbl
[ret
].k_sym_name
, len
);
2706 sprintf(&pcstring
[len
], "+0x%-5" PRIx64
, pc
- (uint64_t)kern_sym_tbl
[ret
].k_sym_addr
);
2711 * Return -1 if not found, else return index
2714 binary_search(kern_sym_t
*list
, int low
, int high
, uint64_t addr
)
2718 if (kern_sym_count
== 0) {
2723 return -1; /* failed */
2726 if (low
+ 1 == high
) {
2727 if ((uint64_t)list
[low
].k_sym_addr
<= addr
&& addr
< (uint64_t)list
[high
].k_sym_addr
) {
2729 * We have a range match
2733 if ((uint64_t)list
[high
].k_sym_addr
<= addr
) {
2741 mid
= (low
+ high
) / 2;
2743 if (addr
< (uint64_t)list
[mid
].k_sym_addr
) {
2744 return binary_search(list
, low
, mid
, addr
);
2747 return binary_search(list
, mid
, high
, addr
);
2751 open_logfile(const char *path
)
2753 log_fp
= fopen(path
, "a");
2757 * failed to open path
2759 fprintf(stderr
, "latency: failed to open logfile [%s]\n", path
);
2765 open_rawfile(const char *path
)
2767 RAW_fd
= open(path
, O_RDONLY
);
2771 * failed to open path
2773 fprintf(stderr
, "latency: failed to open RAWfile [%s]\n", path
);
2781 mach_timebase_info_data_t info
;
2783 (void)mach_timebase_info(&info
);
2785 divisor
= ((double)info
.denom
/ (double)info
.numer
) * 1000;