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
= 97;
112 char *kernelpath
= NULL
;
115 void *k_sym_addr
; /* kernel symbol address from nm */
116 u_int k_sym_len
; /* length of kernel symbol string */
117 char *k_sym_name
; /* kernel symbol string from nm */
120 kern_sym_t
*kern_sym_tbl
; /* pointer to the nm table */
121 int kern_sym_count
; /* number of entries in nm table */
125 #define MAX_ENTRIES 4096
129 } codes_tab
[MAX_ENTRIES
];
131 char *code_file
= NULL
;
132 int num_of_codes
= 0;
136 sig_atomic_t gotSIGWINCH
= 0;
137 int trace_enabled
= 0;
138 int need_new_map
= 1;
139 int set_remove_flag
= 1; /* By default, remove trace buffer */
144 uint64_t first_now
= 0;
145 uint64_t last_now
= 0;
149 #define SAMPLE_TIME_USECS 50000
150 #define SAMPLE_SIZE 300000
151 #define MAX_LOG_COUNT 30 /* limits the number of entries dumped in log_decrementer */
153 kbufinfo_t bufinfo
= {0, 0, 0};
157 uint64_t sample_TOD_secs
;
158 uint32_t sample_TOD_usecs
;
160 int sample_generation
= 0;
161 int num_i_latency_cpus
= 1;
166 kd_buf
**last_decrementer_kd
; /* last DECR_TRAP per cpu */
171 typedef struct event
*event_t
;
178 uint64_t ev_timestamp
;
182 typedef struct lookup
*lookup_t
;
190 long lk_pathname
[NUMPARMS
+ 1];
194 typedef struct threadmap
*threadmap_t
;
200 uintptr_t tm_pthread
;
201 char tm_command
[MAXCOMLEN
+ 1];
202 char tm_orig_command
[MAXCOMLEN
+ 1];
206 typedef struct threadrun
*threadrun_t
;
213 uint64_t tr_timestamp
;
217 typedef struct thread_entry
*thread_entry_t
;
219 struct thread_entry
{
220 thread_entry_t te_next
;
226 #define HASH_SIZE 1024
227 #define HASH_MASK 1023
229 event_t event_hash
[HASH_SIZE
];
230 lookup_t lookup_hash
[HASH_SIZE
];
231 threadmap_t threadmap_hash
[HASH_SIZE
];
232 threadrun_t threadrun_hash
[HASH_SIZE
];
234 event_t event_freelist
;
235 lookup_t lookup_freelist
;
236 threadrun_t threadrun_freelist
;
237 threadmap_t threadmap_freelist
;
238 threadmap_t threadmap_temp
;
240 thread_entry_t thread_entry_freelist
;
241 thread_entry_t thread_delete_list
;
242 thread_entry_t thread_reset_list
;
243 thread_entry_t thread_event_list
;
244 thread_entry_t thread_lookup_list
;
245 thread_entry_t thread_run_list
;
256 #define RAW_VERSION0 0x55aa0000
257 #define RAW_VERSION1 0x55aa0101
262 #define KERNEL_MODE 1
265 #define TRACE_DATA_NEWTHREAD 0x07000004
266 #define TRACE_STRING_NEWTHREAD 0x07010004
267 #define TRACE_STRING_EXEC 0x07010008
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 VFS_LOOKUP 0x03010090
278 #define IES_action 0x050b0018
279 #define IES_filter 0x050b001c
280 #define TES_action 0x050c0010
281 #define CQ_action 0x050d0018
282 #define CPUPM_CPUSTER_RUNCOUNT 0x05310144
284 #define BSC_exit 0x040C0004
285 #define BSC_thread_terminate 0x040c05a4
287 #define DBG_FUNC_MASK ~(DBG_FUNC_START | DBG_FUNC_END)
289 #define CPU_NUMBER(kp) kdbg_get_cpu(kp)
291 #define EMPTYSTRING ""
294 const char *fault_name
[] = {
306 const char *sched_reasons
[] = {
326 #define ARRAYSIZE(x) ((int)(sizeof(x) / sizeof(*x)))
327 #define MAX_REASON ARRAYSIZE(sched_reasons)
329 static double handle_decrementer(kd_buf
*, int);
330 static kd_buf
*log_decrementer(kd_buf
*kd_beg
, kd_buf
*kd_end
, kd_buf
*end_of_sample
, double i_latency
);
331 static void read_command_map(void);
332 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
);
333 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
);
334 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
);
335 static void log_info(uint64_t now
, uint64_t idelta
, uint64_t start_bias
, kd_buf
*kd
, kd_buf
*kd_note
);
336 static char *find_code(int);
337 static void pc_to_string(char *pcstring
, uintptr_t pc
, int max_len
, int mode
);
338 static void getdivisor(void);
339 static int sample_sc(void);
340 static void init_code_file(void);
341 static void do_kernel_nm(void);
342 static void open_logfile(const char*);
343 static int binary_search(kern_sym_t
*list
, int low
, int high
, uintptr_t addr
);
345 static void create_map_entry(uintptr_t, char *);
346 static void check_for_thread_update(uintptr_t thread
, int debugid_base
, kd_buf
*kbufp
, char **command
);
347 static void log_scheduler(kd_buf
*kd_start
, kd_buf
*kd_stop
, kd_buf
*end_of_sample
, double s_latency
, uintptr_t thread
);
348 static int check_for_scheduler_latency(int type
, uintptr_t *thread
, uint64_t now
, kd_buf
*kd
, kd_buf
**kd_start
, double *latency
);
349 static void open_rawfile(const char *path
);
351 static void screen_update(FILE *);
353 static void set_enable(int);
354 static void set_remove(void);
364 * This flag is turned off when calling
365 * quit() due to a set_remove() failure.
367 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 int 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
= 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 %9d", 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
], " %9d", 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 %9d", 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
], " %9d", 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\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 /mach_kernel\n");
906 fprintf(stderr
, "\nlatency must be run as root\n\n");
913 main(int argc
, char *argv
[])
915 if (0 != reexec_to_match_kernel()) {
916 fprintf(stderr
, "Could not re-execute: %d\n", errno
);
921 if (strcmp(argv
[1], "-R") == 0) {
926 open_rawfile(argv
[1]);
933 } else if (strcmp(argv
[1], "-p") == 0) {
938 watch_priority
= atoi(argv
[1]);
942 } else if (strcmp(argv
[1], "-st") == 0) {
947 s_thresh_hold
= atoi(argv
[1]);
951 } else if (strcmp(argv
[1], "-it") == 0) {
956 i_thresh_hold
= atoi(argv
[1]);
960 } else if (strcmp(argv
[1], "-c") == 0) {
969 } else if (strcmp(argv
[1], "-l") == 0) {
974 open_logfile(argv
[1]);
978 } else if (strcmp(argv
[1], "-n") == 0) {
983 kernelpath
= argv
[1];
987 } else if (strcmp(argv
[1], "-h") == 0) {
988 use_high_res_bins
= TRUE
;
990 } else if (strcmp(argv
[1], "-m") == 0) {
991 i_latency_per_cpu
= TRUE
;
1001 if (geteuid() != 0) {
1002 printf("'latency' must be run as root...\n");
1006 if (kernelpath
== NULL
) {
1007 kernelpath
= "/mach_kernel";
1010 if (code_file
== NULL
) {
1011 code_file
= "/usr/share/misc/trace.codes";
1021 if (initscr() == NULL
) {
1022 printf("Unrecognized TERM type, try vt100\n");
1028 signal(SIGWINCH
, sigwinch
);
1029 signal(SIGINT
, sigintr
);
1030 signal(SIGQUIT
, leave
);
1031 signal(SIGTERM
, leave
);
1032 signal(SIGHUP
, leave
);
1035 * grab the number of cpus and scale the buffer size
1037 int mib
[] = { CTL_HW
, HW_NCPU
};
1038 size_t len
= sizeof(num_cpus
);
1040 sysctl(mib
, ARRAYSIZE(mib
), &num_cpus
, &len
, NULL
, 0);
1043 set_numbufs(SAMPLE_SIZE
* num_cpus
);
1045 get_bufinfo(&bufinfo
);
1049 set_pidexclude(getpid(), 1);
1052 num_entries
= bufinfo
.nkdbufs
;
1054 num_entries
= 50000;
1058 if ((my_buffer
= malloc(num_entries
* sizeof(kd_buf
))) == NULL
) {
1059 quit("can't allocate memory for tracing info\n");
1062 if ((last_decrementer_kd
= (kd_buf
**)malloc(num_cpus
* sizeof(kd_buf
*))) == NULL
) {
1063 quit("can't allocate memory for decrementer tracing info\n");
1066 if (i_latency_per_cpu
== FALSE
) {
1067 num_i_latency_cpus
= 1;
1069 num_i_latency_cpus
= num_cpus
;
1072 if ((i_lat
= (struct i_latencies
*)malloc(num_i_latency_cpus
* sizeof(struct i_latencies
))) == NULL
) {
1073 quit("can't allocate memory for interrupt latency info\n");
1076 bzero((char *)i_lat
, num_i_latency_cpus
* sizeof(struct i_latencies
));
1079 while (sample_sc()) {
1084 screen_update(log_fp
);
1087 screen_update(stdout
);
1092 double nanosecs_to_sleep
;
1094 nanosecs_to_sleep
= (double)(SAMPLE_TIME_USECS
* 1000);
1095 fdelay
= nanosecs_to_sleep
* (divisor
/1000);
1096 adelay
= (uint64_t)fdelay
;
1100 start_time
= time(NULL
);
1101 refresh_time
= start_time
;
1104 curr_time
= time(NULL
);
1106 if (curr_time
>= refresh_time
) {
1107 screen_update(NULL
);
1108 refresh_time
= curr_time
+ 1;
1110 mach_wait_until(mach_absolute_time() + adelay
);
1116 * No need to check for initscr error return.
1117 * We won't get here if it fails on the first call.
1132 read_command_map(void)
1134 kd_threadmap
*mapptr
= 0;
1135 int total_threads
= 0;
1139 RAW_header header
= {0};
1142 if (read(RAW_fd
, &header
, sizeof(RAW_header
)) != sizeof(RAW_header
)) {
1143 perror("read failed");
1146 if (header
.version_no
!= RAW_VERSION1
) {
1147 header
.version_no
= RAW_VERSION0
;
1148 header
.TOD_secs
= time(NULL
);
1149 header
.TOD_usecs
= 0;
1151 lseek(RAW_fd
, (off_t
)0, SEEK_SET
);
1153 if (read(RAW_fd
, &header
.thread_count
, sizeof(int)) != sizeof(int)) {
1154 perror("read failed");
1158 total_threads
= header
.thread_count
;
1160 sample_TOD_secs
= header
.TOD_secs
;
1161 sample_TOD_usecs
= header
.TOD_usecs
;
1163 if (total_threads
== 0 && header
.version_no
!= RAW_VERSION0
) {
1164 offset
= lseek(RAW_fd
, (off_t
)0, SEEK_CUR
);
1165 offset
= (offset
+ (4095)) & ~4095;
1167 lseek(RAW_fd
, offset
, SEEK_SET
);
1170 total_threads
= bufinfo
.nkdthreads
;
1173 size
= total_threads
* sizeof(kd_threadmap
);
1175 if (size
== 0 || ((mapptr
= (kd_threadmap
*) malloc(size
)) == 0)) {
1178 bzero (mapptr
, size
);
1181 * Now read the threadmap
1184 if (read(RAW_fd
, mapptr
, size
) != size
) {
1185 printf("Can't read the thread map -- this is not fatal\n");
1187 if (header
.version_no
!= RAW_VERSION0
) {
1188 offset
= lseek(RAW_fd
, (off_t
)0, SEEK_CUR
);
1189 offset
= (offset
+ (4095)) & ~4095;
1191 lseek(RAW_fd
, offset
, SEEK_SET
);
1194 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDTHRMAP
};
1195 if (sysctl(mib
, ARRAYSIZE(mib
), mapptr
, &size
, NULL
, 0) < 0) {
1197 * This is not fatal -- just means I cant map command strings
1199 printf("Can't read the thread map -- this is not fatal\n");
1204 for (i
= 0; i
< total_threads
; i
++) {
1205 create_map_entry(mapptr
[i
].thread
, &mapptr
[i
].command
[0]);
1211 create_map_entry(uintptr_t thread
, char *command
)
1215 if ((tme
= threadmap_freelist
)) {
1216 threadmap_freelist
= tme
->tm_next
;
1218 tme
= (threadmap_t
)malloc(sizeof(struct threadmap
));
1221 tme
->tm_thread
= thread
;
1223 (void)strncpy (tme
->tm_command
, command
, MAXCOMLEN
);
1224 tme
->tm_command
[MAXCOMLEN
] = '\0';
1225 tme
->tm_orig_command
[0] = '\0';
1227 int hashid
= thread
& HASH_MASK
;
1229 tme
->tm_next
= threadmap_hash
[hashid
];
1230 threadmap_hash
[hashid
] = tme
;
1234 delete_thread_entry(uintptr_t thread
)
1238 int hashid
= thread
& HASH_MASK
;
1240 if ((tme
= threadmap_hash
[hashid
])) {
1241 if (tme
->tm_thread
== thread
) {
1242 threadmap_hash
[hashid
] = tme
->tm_next
;
1244 threadmap_t tme_prev
= tme
;
1246 for (tme
= tme
->tm_next
; tme
; tme
= tme
->tm_next
) {
1247 if (tme
->tm_thread
== thread
) {
1248 tme_prev
->tm_next
= tme
->tm_next
;
1255 tme
->tm_next
= threadmap_freelist
;
1256 threadmap_freelist
= tme
;
1262 find_and_insert_tmp_map_entry(uintptr_t pthread
, char *command
)
1266 if ((tme
= threadmap_temp
)) {
1267 if (tme
->tm_pthread
== pthread
) {
1268 threadmap_temp
= tme
->tm_next
;
1270 threadmap_t tme_prev
= tme
;
1272 for (tme
= tme
->tm_next
; tme
; tme
= tme
->tm_next
) {
1273 if (tme
->tm_pthread
== pthread
) {
1274 tme_prev
->tm_next
= tme
->tm_next
;
1281 (void)strncpy (tme
->tm_command
, command
, MAXCOMLEN
);
1282 tme
->tm_command
[MAXCOMLEN
] = '\0';
1283 tme
->tm_orig_command
[0] = '\0';
1285 int hashid
= tme
->tm_thread
& HASH_MASK
;
1286 tme
->tm_next
= threadmap_hash
[hashid
];
1287 threadmap_hash
[hashid
] = tme
;
1293 create_tmp_map_entry(uintptr_t thread
, uintptr_t pthread
)
1297 if ((tme
= threadmap_freelist
)) {
1298 threadmap_freelist
= tme
->tm_next
;
1300 tme
= malloc(sizeof(struct threadmap
));
1303 tme
->tm_thread
= thread
;
1304 tme
->tm_pthread
= pthread
;
1305 tme
->tm_command
[0] = '\0';
1306 tme
->tm_orig_command
[0] = '\0';
1308 tme
->tm_next
= threadmap_temp
;
1309 threadmap_temp
= tme
;
1313 find_thread_entry(uintptr_t thread
)
1317 int hashid
= thread
& HASH_MASK
;
1319 for (tme
= threadmap_hash
[hashid
]; tme
; tme
= tme
->tm_next
) {
1320 if (tme
->tm_thread
== thread
) {
1328 find_thread_name(uintptr_t thread
, char **command
)
1332 if ((tme
= find_thread_entry(thread
))) {
1333 *command
= tme
->tm_command
;
1335 *command
= EMPTYSTRING
;
1340 add_thread_entry_to_list(thread_entry_t
*list
, uintptr_t thread
)
1344 if ((te
= thread_entry_freelist
)) {
1345 thread_entry_freelist
= te
->te_next
;
1347 te
= (thread_entry_t
)malloc(sizeof(struct thread_entry
));
1350 te
->te_thread
= thread
;
1351 te
->te_next
= *list
;
1356 exec_thread_entry(uintptr_t thread
, char *command
)
1360 if ((tme
= find_thread_entry(thread
))) {
1361 if (tme
->tm_orig_command
[0] == '\0') {
1362 (void)strncpy (tme
->tm_orig_command
, tme
->tm_command
, MAXCOMLEN
);
1363 tme
->tm_orig_command
[MAXCOMLEN
] = '\0';
1365 (void)strncpy (tme
->tm_command
, command
, MAXCOMLEN
);
1366 tme
->tm_command
[MAXCOMLEN
] = '\0';
1368 add_thread_entry_to_list(&thread_reset_list
, thread
);
1370 create_map_entry(thread
, command
);
1375 record_thread_entry_for_gc(uintptr_t thread
)
1377 add_thread_entry_to_list(&thread_delete_list
, thread
);
1381 gc_thread_entries(void)
1384 thread_entry_t te_next
;
1387 for (te
= thread_delete_list
; te
; te
= te_next
) {
1388 delete_thread_entry(te
->te_thread
);
1390 te_next
= te
->te_next
;
1391 te
->te_next
= thread_entry_freelist
;
1392 thread_entry_freelist
= te
;
1396 thread_delete_list
= 0;
1400 gc_reset_entries(void)
1403 thread_entry_t te_next
;
1406 for (te
= thread_reset_list
; te
; te
= te_next
) {
1407 te_next
= te
->te_next
;
1408 te
->te_next
= thread_entry_freelist
;
1409 thread_entry_freelist
= te
;
1413 thread_reset_list
= 0;
1417 reset_thread_names(void)
1420 thread_entry_t te_next
;
1423 for (te
= thread_reset_list
; te
; te
= te_next
) {
1426 if ((tme
= find_thread_entry(te
->te_thread
))) {
1427 if (tme
->tm_orig_command
[0]) {
1428 (void)strncpy (tme
->tm_command
, tme
->tm_orig_command
, MAXCOMLEN
);
1429 tme
->tm_command
[MAXCOMLEN
] = '\0';
1430 tme
->tm_orig_command
[0] = '\0';
1433 te_next
= te
->te_next
;
1434 te
->te_next
= thread_entry_freelist
;
1435 thread_entry_freelist
= te
;
1439 thread_reset_list
= 0;
1443 delete_all_thread_entries(void)
1445 threadmap_t tme
= 0;
1446 threadmap_t tme_next
= 0;
1449 for (i
= 0; i
< HASH_SIZE
; i
++) {
1450 for (tme
= threadmap_hash
[i
]; tme
; tme
= tme_next
) {
1451 tme_next
= tme
->tm_next
;
1452 tme
->tm_next
= threadmap_freelist
;
1453 threadmap_freelist
= tme
;
1455 threadmap_hash
[i
] = 0;
1463 insert_run_event(uintptr_t thread
, kd_buf
*kd
, uint64_t now
)
1467 int hashid
= thread
& HASH_MASK
;
1469 for (trp
= threadrun_hash
[hashid
]; trp
; trp
= trp
->tr_next
) {
1470 if (trp
->tr_thread
== thread
) {
1475 if ((trp
= threadrun_freelist
)) {
1476 threadrun_freelist
= trp
->tr_next
;
1478 trp
= (threadrun_t
)malloc(sizeof(struct threadrun
));
1481 trp
->tr_thread
= thread
;
1483 trp
->tr_next
= threadrun_hash
[hashid
];
1484 threadrun_hash
[hashid
] = trp
;
1486 add_thread_entry_to_list(&thread_run_list
, thread
);
1489 trp
->tr_timestamp
= now
;
1493 find_run_event(uintptr_t thread
)
1496 int hashid
= thread
& HASH_MASK
;
1498 for (trp
= threadrun_hash
[hashid
]; trp
; trp
= trp
->tr_next
) {
1499 if (trp
->tr_thread
== thread
) {
1507 delete_run_event(uintptr_t thread
)
1509 threadrun_t trp
= 0;
1510 threadrun_t trp_prev
;
1512 int hashid
= thread
& HASH_MASK
;
1514 if ((trp
= threadrun_hash
[hashid
])) {
1515 if (trp
->tr_thread
== thread
) {
1516 threadrun_hash
[hashid
] = trp
->tr_next
;
1520 for (trp
= trp
->tr_next
; trp
; trp
= trp
->tr_next
) {
1521 if (trp
->tr_thread
== thread
) {
1522 trp_prev
->tr_next
= trp
->tr_next
;
1529 trp
->tr_next
= threadrun_freelist
;
1530 threadrun_freelist
= trp
;
1536 gc_run_events(void) {
1538 thread_entry_t te_next
;
1540 threadrun_t trp_next
;
1543 for (te
= thread_run_list
; te
; te
= te_next
) {
1544 int hashid
= te
->te_thread
& HASH_MASK
;
1546 for (trp
= threadrun_hash
[hashid
]; trp
; trp
= trp_next
) {
1547 trp_next
= trp
->tr_next
;
1548 trp
->tr_next
= threadrun_freelist
;
1549 threadrun_freelist
= trp
;
1552 threadrun_hash
[hashid
] = 0;
1554 te_next
= te
->te_next
;
1555 te
->te_next
= thread_entry_freelist
;
1556 thread_entry_freelist
= te
;
1558 thread_run_list
= 0;
1564 insert_start_event(uintptr_t thread
, int type
, uint64_t now
)
1568 int hashid
= thread
& HASH_MASK
;
1570 for (evp
= event_hash
[hashid
]; evp
; evp
= evp
->ev_next
) {
1571 if (evp
->ev_thread
== thread
&& evp
->ev_type
== type
) {
1576 if ((evp
= event_freelist
)) {
1577 event_freelist
= evp
->ev_next
;
1579 evp
= (event_t
)malloc(sizeof(struct event
));
1582 evp
->ev_thread
= thread
;
1583 evp
->ev_type
= type
;
1585 evp
->ev_next
= event_hash
[hashid
];
1586 event_hash
[hashid
] = evp
;
1588 add_thread_entry_to_list(&thread_event_list
, thread
);
1590 evp
->ev_timestamp
= now
;
1595 consume_start_event(uintptr_t thread
, int type
, uint64_t now
)
1599 uint64_t elapsed
= 0;
1601 int hashid
= thread
& HASH_MASK
;
1603 if ((evp
= event_hash
[hashid
])) {
1604 if (evp
->ev_thread
== thread
&& evp
->ev_type
== type
) {
1605 event_hash
[hashid
] = evp
->ev_next
;
1609 for (evp
= evp
->ev_next
; evp
; evp
= evp
->ev_next
) {
1610 if (evp
->ev_thread
== thread
&& evp
->ev_type
== type
) {
1611 evp_prev
->ev_next
= evp
->ev_next
;
1618 elapsed
= now
- evp
->ev_timestamp
;
1620 if (now
< evp
->ev_timestamp
) {
1621 printf("consume: now = %qd, timestamp = %qd\n", now
, evp
->ev_timestamp
);
1624 evp
->ev_next
= event_freelist
;
1625 event_freelist
= evp
;
1632 gc_start_events(void)
1635 thread_entry_t te_next
;
1641 for (te
= thread_event_list
; te
; te
= te_next
) {
1643 hashid
= te
->te_thread
& HASH_MASK
;
1645 for (evp
= event_hash
[hashid
]; evp
; evp
= evp_next
) {
1646 evp_next
= evp
->ev_next
;
1647 evp
->ev_next
= event_freelist
;
1648 event_freelist
= evp
;
1651 event_hash
[hashid
] = 0;
1653 te_next
= te
->te_next
;
1654 te
->te_next
= thread_entry_freelist
;
1655 thread_entry_freelist
= te
;
1657 thread_event_list
= 0;
1661 thread_in_user_mode(uintptr_t thread
, char *command
)
1665 if (strcmp(command
, "kernel_task") == 0) {
1669 int hashid
= thread
& HASH_MASK
;
1671 for (evp
= event_hash
[hashid
]; evp
; evp
= evp
->ev_next
) {
1672 if (evp
->ev_thread
== thread
) {
1682 handle_lookup_event(uintptr_t thread
, int debugid
, kd_buf
*kdp
)
1685 boolean_t first_record
= FALSE
;
1687 int hashid
= thread
& HASH_MASK
;
1689 if (debugid
& DBG_FUNC_START
) {
1690 first_record
= TRUE
;
1693 for (lkp
= lookup_hash
[hashid
]; lkp
; lkp
= lkp
->lk_next
) {
1694 if (lkp
->lk_thread
== thread
) {
1699 if (first_record
== FALSE
) {
1703 if ((lkp
= lookup_freelist
)) {
1704 lookup_freelist
= lkp
->lk_next
;
1706 lkp
= (lookup_t
)malloc(sizeof(struct lookup
));
1709 lkp
->lk_thread
= thread
;
1711 lkp
->lk_next
= lookup_hash
[hashid
];
1712 lookup_hash
[hashid
] = lkp
;
1714 add_thread_entry_to_list(&thread_lookup_list
, thread
);
1717 if (first_record
== TRUE
) {
1718 lkp
->lk_pathptr
= lkp
->lk_pathname
;
1719 lkp
->lk_dvp
= kdp
->arg1
;
1721 if (lkp
->lk_pathptr
> &lkp
->lk_pathname
[NUMPARMS
-4]) {
1724 *lkp
->lk_pathptr
++ = kdp
->arg1
;
1726 *lkp
->lk_pathptr
++ = kdp
->arg2
;
1727 *lkp
->lk_pathptr
++ = kdp
->arg3
;
1728 *lkp
->lk_pathptr
++ = kdp
->arg4
;
1729 *lkp
->lk_pathptr
= 0;
1731 if (debugid
& DBG_FUNC_END
) {
1739 delete_lookup_event(uintptr_t thread
, lookup_t lkp_to_delete
)
1745 hashid
= thread
& HASH_MASK
;
1747 if ((lkp
= lookup_hash
[hashid
])) {
1748 if (lkp
== lkp_to_delete
) {
1749 lookup_hash
[hashid
] = lkp
->lk_next
;
1753 for (lkp
= lkp
->lk_next
; lkp
; lkp
= lkp
->lk_next
) {
1754 if (lkp
== lkp_to_delete
) {
1755 lkp_prev
->lk_next
= lkp
->lk_next
;
1762 lkp
->lk_next
= lookup_freelist
;
1763 lookup_freelist
= lkp
;
1769 gc_lookup_events(void) {
1771 thread_entry_t te_next
;
1777 for (te
= thread_lookup_list
; te
; te
= te_next
) {
1778 hashid
= te
->te_thread
& HASH_MASK
;
1780 for (lkp
= lookup_hash
[hashid
]; lkp
; lkp
= lkp_next
) {
1781 lkp_next
= lkp
->lk_next
;
1782 lkp
->lk_next
= lookup_freelist
;
1783 lookup_freelist
= lkp
;
1786 lookup_hash
[hashid
] = 0;
1788 te_next
= te
->te_next
;
1789 te
->te_next
= thread_entry_freelist
;
1790 thread_entry_freelist
= te
;
1792 thread_lookup_list
= 0;
1798 kd_buf
*kd
, *end_of_sample
;
1804 * Get kernel buffer information
1806 get_bufinfo(&bufinfo
);
1809 delete_all_thread_entries();
1814 uint32_t bytes_read
;
1816 bytes_read
= read(RAW_fd
, my_buffer
, num_entries
* sizeof(kd_buf
));
1818 if (bytes_read
== -1) {
1819 perror("read failed");
1822 count
= bytes_read
/ sizeof(kd_buf
);
1824 if (count
!= num_entries
) {
1829 kd
= (kd_buf
*)my_buffer
;
1830 first_now
= kd
->timestamp
& KDBG_TIMESTAMP_MASK
;
1835 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDREADTR
};
1836 size_t needed
= bufinfo
.nkdbufs
* sizeof(kd_buf
);
1838 if (sysctl(mib
, ARRAYSIZE(mib
), my_buffer
, &needed
, NULL
, 0) < 0) {
1839 quit("trace facility failure, KERN_KDREADTR\n");
1843 sample_generation
++;
1845 if (bufinfo
.flags
& KDBG_WRAPPED
) {
1849 fprintf(log_fp
, "\n\n%-19.19s sample = %d <<<<<<< trace buffer wrapped >>>>>>>\n\n",
1850 &(ctime(&curr_time
)[0]), sample_generation
);
1856 end_of_sample
= &((kd_buf
*)my_buffer
)[count
];
1859 * Always reinitialize the DECR_TRAP array
1861 for (i
= 0; i
< num_cpus
; i
++) {
1862 last_decrementer_kd
[i
] = (kd_buf
*)my_buffer
;
1865 for (kd
= (kd_buf
*)my_buffer
; kd
< end_of_sample
; kd
++) {
1867 uintptr_t thread
= kd
->arg5
;
1868 int type
= kd
->debugid
& DBG_FUNC_MASK
;
1870 (void)check_for_thread_update(thread
, type
, kd
, NULL
);
1872 uint64_t now
= kd
->timestamp
& KDBG_TIMESTAMP_MASK
;
1875 if (type
== DECR_TRAP
) {
1876 int cpunum
= CPU_NUMBER(kd
);
1877 double i_latency
= handle_decrementer(kd
, cpunum
);
1880 if (i_thresh_hold
&& (int)i_latency
> i_thresh_hold
) {
1881 kd_start
= last_decrementer_kd
[cpunum
];
1883 log_decrementer(kd_start
, kd
, end_of_sample
, i_latency
);
1885 last_decrementer_kd
[cpunum
] = kd
;
1889 if (check_for_scheduler_latency(type
, &thread
, now
, kd
, &kd_start
, &s_latency
)) {
1890 log_scheduler(kd_start
, kd
, end_of_sample
, s_latency
, thread
);
1898 gc_thread_entries();
1908 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
)
1915 int cpunum
= CPU_NUMBER(kd
);
1917 if (print_info
&& fp
) {
1918 timestamp
= (double)(now
- start_bias
) / divisor
;
1919 delta
= (double)idelta
/ divisor
;
1921 if ((p
= find_code(type
))) {
1922 if (type
== INTERRUPT
) {
1931 pc_to_string(&pcstring
[0], kd
->arg2
, 58, mode
);
1933 fprintf(fp
, "%9.1f %8.1f\t\tINTERRUPT[%2lx] @ %-58.58s %-8x %d %s\n",
1934 timestamp
, delta
, kd
->arg1
, &pcstring
[0], thread
, cpunum
, command
);
1935 } else if (type
== MACH_vmfault
) {
1936 fprintf(fp
, "%9.1f %8.1f\t\t%-28.28s %-8x %d %s\n",
1937 timestamp
, delta
, p
, thread
, cpunum
, command
);
1939 fprintf(fp
, "%9.1f %8.1f\t\t%-28.28s %-8lx %-8lx %-8lx %-8lx %-8x %d %s\n",
1940 timestamp
, delta
, p
, kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
,
1941 thread
, cpunum
, command
);
1944 fprintf(fp
, "%9.1f %8.1f\t\t%-8x %-8lx %-8lx %-8lx %-8lx %-8x %d %s\n",
1945 timestamp
, delta
, type
, kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
,
1946 thread
, cpunum
, command
);
1949 if (type
!= BSC_thread_terminate
&& type
!= BSC_exit
) {
1950 insert_start_event(thread
, type
, now
);
1956 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
)
1962 double elapsed_timestamp
;
1964 elapsed_timestamp
= (double)consume_start_event(thread
, type
, now
) / divisor
;
1966 if (print_info
&& fp
) {
1967 int cpunum
= CPU_NUMBER(kd
);
1969 timestamp
= (double)(now
- start_bias
) / divisor
;
1970 delta
= (double)idelta
/ divisor
;
1972 fprintf(fp
, "%9.1f %8.1f(%.1f) \t", timestamp
, delta
, elapsed_timestamp
);
1974 if ((p
= find_code(type
))) {
1975 if (type
== INTERRUPT
) {
1976 fprintf(fp
, "INTERRUPT %-8x %d %s\n", thread
, cpunum
, command
);
1977 } else if (type
== MACH_vmfault
&& kd
->arg4
<= DBG_PAGEIND_FAULT
) {
1978 user_addr
= ((uint64_t)kd
->arg1
<< 32) | (uint32_t)kd
->arg2
;
1980 fprintf(fp
, "%-28.28s %-10.10s %-16qx %-8x %d %s\n",
1981 p
, fault_name
[kd
->arg4
], user_addr
,
1982 thread
, cpunum
, command
);
1984 fprintf(fp
, "%-28.28s %-8lx %-8lx %-8x %d %s\n",
1985 p
, kd
->arg1
, kd
->arg2
,
1986 thread
, cpunum
, command
);
1989 fprintf(fp
, "%-8x %-8lx %-8lx %-8x %d %s\n",
1990 type
, kd
->arg1
, kd
->arg2
,
1991 thread
, cpunum
, command
);
1998 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
)
2006 int cpunum
= CPU_NUMBER(kd
);
2008 double timestamp
= (double)(now
- start_bias
) / divisor
;
2009 double delta
= (double)idelta
/ divisor
;
2011 if ((p
= find_code(type
))) {
2012 if (kd
== kd_note
) {
2013 fprintf(fp
, "%9.1f %8.1f\t**\t", timestamp
, delta
);
2015 fprintf(fp
, "%9.1f %8.1f\t\t", timestamp
, delta
);
2017 fprintf(fp
, "%-28.28s %-8lx %-8lx %-8lx %-8lx %-8x %d %s\n",
2018 p
, kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
, thread
, cpunum
, command
);
2020 fprintf(fp
, "%9.1f %8.1f\t\t%-8x %-8lx %-8lx %-8lx %-8lx %-8x %d %s\n",
2021 timestamp
, delta
, type
, kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
,
2022 thread
, cpunum
, command
);
2028 check_for_thread_update(uintptr_t thread
, int debugid_base
, kd_buf
*kbufp
, char **command
)
2030 if (debugid_base
== TRACE_DATA_NEWTHREAD
) {
2032 * Save the create thread data
2034 create_tmp_map_entry(kbufp
->arg1
, thread
);
2035 } else if (debugid_base
== TRACE_STRING_NEWTHREAD
) {
2037 * process new map entry
2039 find_and_insert_tmp_map_entry(thread
, (char *)&kbufp
->arg1
);
2040 } else if (debugid_base
== TRACE_STRING_EXEC
) {
2041 exec_thread_entry(thread
, (char *)&kbufp
->arg1
);
2043 if (debugid_base
== BSC_exit
|| debugid_base
== BSC_thread_terminate
) {
2044 record_thread_entry_for_gc(thread
);
2047 find_thread_name(thread
, command
);
2054 log_info(uint64_t now
, uint64_t idelta
, uint64_t start_bias
, kd_buf
*kd
, kd_buf
*kd_note
)
2062 char command_buf
[32];
2063 char sched_info
[64];
2065 const char *sched_reason
;
2071 int thread
= kd
->arg5
;
2072 int cpunum
= CPU_NUMBER(kd
);
2073 int debugid
= kd
->debugid
;
2074 int type
= kd
->debugid
& DBG_FUNC_MASK
;
2076 (void)check_for_thread_update(thread
, type
, kd
, &command
);
2078 if ((type
>> 24) == DBG_TRACE
) {
2079 if (((type
>> 16) & 0xff) != DBG_TRACE_INFO
) {
2083 timestamp
= (double)(now
- start_bias
) / divisor
;
2084 delta
= (double)idelta
/ divisor
;
2089 pc_to_string(&pcstring
[0], kd
->arg1
, 62, KERNEL_MODE
);
2091 fprintf(log_fp
, "%9.1f %8.1f\t\tCQ_action @ %-62.62s %-8x %d %s\n",
2092 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2096 pc_to_string(&pcstring
[0], kd
->arg1
, 61, KERNEL_MODE
);
2098 fprintf(log_fp
, "%9.1f %8.1f\t\tTES_action @ %-61.61s %-8x %d %s\n",
2099 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2103 pc_to_string(&pcstring
[0], kd
->arg1
, 61, KERNEL_MODE
);
2105 fprintf(log_fp
, "%9.1f %8.1f\t\tIES_action @ %-61.61s %-8x %d %s\n",
2106 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2110 pc_to_string(&pcstring
[0], kd
->arg1
, 61, KERNEL_MODE
);
2112 fprintf(log_fp
, "%9.1f %8.1f\t\tIES_filter @ %-61.61s %-8x %d %s\n",
2113 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2117 if ((int)kd
->arg1
>= 0) {
2120 i_latency
= (((double)(-1 - kd
->arg1
)) / divisor
);
2123 if (i_thresh_hold
&& (int)i_latency
> i_thresh_hold
) {
2135 pc_to_string(&pcstring
[0], kd
->arg2
, 62, mode
);
2137 fprintf(log_fp
, "%9.1f %8.1f[%.1f]%s\tDECR_TRAP @ %-62.62s %-8x %d %s\n",
2138 timestamp
, delta
, i_latency
, p
, &pcstring
[0], thread
, cpunum
, command
);
2142 fprintf(log_fp
, "%9.1f %8.1f[%.1f] \t%-28.28s %-8x %d %s\n",
2143 timestamp
, delta
, (double)kd
->arg1
/divisor
, "DECR_SET", thread
, cpunum
, command
);
2147 case MACH_stkhandoff
:
2149 find_thread_name(kd
->arg2
, &command1
);
2151 if (command1
== EMPTYSTRING
) {
2152 command1
= command_buf
;
2153 sprintf(command1
, "%-8lx", kd
->arg2
);
2155 if (thread_in_user_mode(kd
->arg2
, command1
)) {
2163 if (reason
> MAX_REASON
) {
2166 sched_reason
= sched_reasons
[reason
];
2169 if (sched_reason
[0] == '?') {
2170 sprintf(joe
, "%x", reason
);
2173 sprintf(sched_info
, "%14.14s @ pri %3lu --> %14.14s @ pri %3lu%s", command
, kd
->arg3
, command1
, kd
->arg4
, p
);
2175 fprintf(log_fp
, "%9.1f %8.1f\t\t%-10.10s[%s] %s %-8x %d\n",
2176 timestamp
, delta
, "MACH_SCHED", sched_reason
, sched_info
, thread
, cpunum
);
2180 if ((lkp
= handle_lookup_event(thread
, debugid
, kd
))) {
2182 * print the tail end of the pathname
2184 p
= (char *)lkp
->lk_pathname
;
2185 int clen
= strlen(p
);
2193 fprintf(log_fp
, "%9.1f %8.1f\t\t%-14.14s %-45s %-8lx %-8x %d %s\n",
2194 timestamp
, delta
, "VFS_LOOKUP",
2195 &p
[clen
], lkp
->lk_dvp
, thread
, cpunum
, command
);
2197 delete_lookup_event(thread
, lkp
);
2202 if (debugid
& DBG_FUNC_START
) {
2203 enter_syscall(log_fp
, kd
, thread
, type
, command
, now
, idelta
, start_bias
, 1);
2204 } else if (debugid
& DBG_FUNC_END
) {
2205 exit_syscall(log_fp
, kd
, thread
, type
, command
, now
, idelta
, start_bias
, 1);
2207 print_entry(log_fp
, kd
, thread
, type
, command
, now
, idelta
, start_bias
, kd_note
);
2216 log_range(kd_buf
*kd_buffer
, kd_buf
*kd_start
, kd_buf
*kd_stop
, kd_buf
*kd_note
, char *buf1
)
2218 uint64_t last_timestamp
= 0;
2220 uint64_t start_bias
= 0;
2226 clen
= strlen(buf1
);
2227 memset(buf2
, '-', clen
);
2229 fprintf(log_fp
, "\n\n%s\n", buf2
);
2230 fprintf(log_fp
, "%s\n\n", buf1
);
2232 fprintf(log_fp
, "RelTime(Us) Delta debugid arg1 arg2 arg3 arg4 thread cpu command\n\n");
2234 reset_thread_names();
2236 last_timestamp
= kd_start
->timestamp
& KDBG_TIMESTAMP_MASK
;
2237 start_bias
= last_timestamp
;
2239 for (kd
= kd_buffer
; kd
<= kd_stop
; kd
++) {
2240 now
= kd
->timestamp
& KDBG_TIMESTAMP_MASK
;
2242 if (kd
>= kd_start
) {
2243 delta
= now
- last_timestamp
;
2245 log_info(now
, delta
, start_bias
, kd
, kd_note
);
2247 last_timestamp
= now
;
2249 int debugid
= kd
->debugid
;
2250 int thread
= kd
->arg5
;
2251 int type
= kd
->debugid
& DBG_FUNC_MASK
;
2253 if ((type
>> 24) == DBG_TRACE
) {
2254 if (((type
>> 16) & 0xff) != DBG_TRACE_INFO
) {
2258 if (type
== BSC_thread_terminate
|| type
== BSC_exit
) {
2262 if (debugid
& DBG_FUNC_START
) {
2263 insert_start_event(thread
, type
, now
);
2264 } else if (debugid
& DBG_FUNC_END
) {
2265 (void)consume_start_event(thread
, type
, now
);
2275 log_decrementer(kd_buf
*kd_beg
, kd_buf
*kd_end
, kd_buf
*end_of_sample
, double i_latency
)
2277 kd_buf
*kd_start
, *kd_stop
;
2278 int kd_count
; /* Limit the boundary of kd_start */
2280 double sample_timestamp
;
2283 int thread
= kd_beg
->arg5
;
2284 int cpunum
= CPU_NUMBER(kd_end
);
2286 for (kd_count
= 0, kd_start
= kd_beg
- 1; (kd_start
>= (kd_buf
*)my_buffer
); kd_start
--, kd_count
++) {
2287 if (kd_count
== MAX_LOG_COUNT
) {
2291 if (CPU_NUMBER(kd_start
) != cpunum
) {
2295 if ((kd_start
->debugid
& DBG_FUNC_MASK
) == DECR_TRAP
) {
2299 if (kd_start
->arg5
!= thread
) {
2303 if (kd_start
< (kd_buf
*)my_buffer
) {
2304 kd_start
= (kd_buf
*)my_buffer
;
2307 thread
= kd_end
->arg5
;
2309 for (kd_stop
= kd_end
+ 1; kd_stop
< end_of_sample
; kd_stop
++) {
2310 if (CPU_NUMBER(kd_stop
) != cpunum
) {
2314 if ((kd_stop
->debugid
& DBG_FUNC_MASK
) == INTERRUPT
) {
2318 if (kd_stop
->arg5
!= thread
) {
2322 if (kd_stop
>= end_of_sample
) {
2323 kd_stop
= end_of_sample
- 1;
2330 now
= kd_start
->timestamp
& KDBG_TIMESTAMP_MASK
;
2331 sample_timestamp
= (double)(now
- first_now
) / divisor
;
2333 TOD_usecs
= (uint64_t)sample_timestamp
;
2334 TOD_secs
= sample_TOD_secs
+ ((sample_TOD_usecs
+ TOD_usecs
) / 1000000);
2336 sprintf(buf1
, "%-19.19s interrupt latency = %.1fus [timestamp %.1f]", ctime(&TOD_secs
), i_latency
, sample_timestamp
);
2338 sprintf(buf1
, "%-19.19s interrupt latency = %.1fus [sample %d]", &(ctime(&curr_time
)[0]), i_latency
, sample_generation
);
2341 log_range((kd_buf
*)my_buffer
, kd_start
, kd_stop
, 0, buf1
);
2348 log_scheduler(kd_buf
*kd_beg
, kd_buf
*kd_end
, kd_buf
*end_of_sample
, double s_latency
, uintptr_t thread
)
2350 kd_buf
*kd_start
, *kd_stop
;
2352 double sample_timestamp
;
2355 int cpunum
= CPU_NUMBER(kd_end
);
2357 for (kd_start
= kd_beg
; (kd_start
>= (kd_buf
*)my_buffer
); kd_start
--) {
2358 if (CPU_NUMBER(kd_start
) == cpunum
) {
2362 if (kd_start
< (kd_buf
*)my_buffer
) {
2363 kd_start
= (kd_buf
*)my_buffer
;
2366 for (kd_stop
= kd_end
+ 1; kd_stop
< end_of_sample
; kd_stop
++) {
2367 if (kd_stop
->arg5
== thread
) {
2371 if (kd_stop
>= end_of_sample
) {
2372 kd_stop
= end_of_sample
- 1;
2379 now
= kd_start
->timestamp
& KDBG_TIMESTAMP_MASK
;
2380 sample_timestamp
= (double)(now
- first_now
) / divisor
;
2382 TOD_usecs
= (uint64_t)sample_timestamp
;
2383 TOD_secs
= sample_TOD_secs
+ ((sample_TOD_usecs
+ TOD_usecs
) / 1000000);
2385 sprintf(buf1
, "%-19.19s priority = %d, scheduling latency = %.1fus [timestamp %.1f]", ctime(&TOD_secs
), watch_priority
, s_latency
, sample_timestamp
);
2387 sprintf(buf1
, "%-19.19s priority = %d, scheduling latency = %.1fus [sample %d]", &(ctime(&curr_time
)[0]), watch_priority
, s_latency
, sample_generation
);
2390 log_range((kd_buf
*)my_buffer
, kd_start
, kd_stop
, kd_beg
, buf1
);
2396 check_for_scheduler_latency(int type
, uintptr_t *thread
, uint64_t now
, kd_buf
*kd
, kd_buf
**kd_start
, double *latency
)
2398 int found_latency
= 0;
2400 if (type
== MACH_makerunnable
) {
2401 if (watch_priority
== kd
->arg2
) {
2402 insert_run_event(kd
->arg1
, kd
, now
);
2404 } else if (type
== MACH_sched
|| type
== MACH_stkhandoff
) {
2407 if (type
== MACH_sched
|| type
== MACH_stkhandoff
) {
2411 if ((trp
= find_run_event(*thread
))) {
2412 double d_s_latency
= (((double)(now
- trp
->tr_timestamp
)) / divisor
);
2413 int s_latency
= (int)d_s_latency
;
2416 if (s_latency
< 100) {
2417 s_usec_10_bins
[s_latency
/10]++;
2419 if (s_latency
< 1000) {
2420 s_usec_100_bins
[s_latency
/100]++;
2421 } else if (s_latency
< 10000) {
2422 s_msec_1_bins
[s_latency
/1000]++;
2423 } else if (s_latency
< 50000) {
2424 s_msec_10_bins
[s_latency
/10000]++;
2429 if (s_latency
> s_max_latency
) {
2430 s_max_latency
= s_latency
;
2432 if (s_latency
< s_min_latency
|| s_total_samples
== 0) {
2433 s_min_latency
= s_latency
;
2435 s_total_latency
+= s_latency
;
2438 if (s_thresh_hold
&& s_latency
> s_thresh_hold
) {
2439 s_exceeded_threshold
++;
2442 *kd_start
= trp
->tr_entry
;
2443 *latency
= d_s_latency
;
2448 delete_run_event(*thread
);
2451 return found_latency
;
2456 handle_decrementer(kd_buf
*kd
, int cpunum
)
2458 struct i_latencies
*il
;
2462 if (i_latency_per_cpu
== FALSE
) {
2466 il
= &i_lat
[cpunum
];
2468 if ((long)(kd
->arg1
) >= 0) {
2471 latency
= (((double)(-1 - kd
->arg1
)) / divisor
);
2473 elapsed_usecs
= (long)latency
;
2475 if (elapsed_usecs
< 100) {
2476 il
->i_usec_10_bins
[elapsed_usecs
/10]++;
2479 if (elapsed_usecs
< 1000) {
2480 il
->i_usec_100_bins
[elapsed_usecs
/100]++;
2481 } else if (elapsed_usecs
< 10000) {
2482 il
->i_msec_1_bins
[elapsed_usecs
/1000]++;
2483 } else if (elapsed_usecs
< 50000) {
2484 il
->i_msec_10_bins
[elapsed_usecs
/10000]++;
2489 if (use_high_res_bins
&& elapsed_usecs
< N_HIGH_RES_BINS
) {
2490 i_high_res_bins
[elapsed_usecs
]++;
2492 if (i_thresh_hold
&& elapsed_usecs
> i_thresh_hold
) {
2493 il
->i_exceeded_threshold
++;
2495 if (elapsed_usecs
> il
->i_max_latency
) {
2496 il
->i_max_latency
= elapsed_usecs
;
2498 if (elapsed_usecs
< il
->i_min_latency
|| il
->i_total_samples
== 0) {
2499 il
->i_min_latency
= elapsed_usecs
;
2501 il
->i_total_latency
+= elapsed_usecs
;
2502 il
->i_total_samples
++;
2513 for (i
= 0; i
< num_of_codes
; i
++) {
2514 if (codes_tab
[i
].type
== type
) {
2515 return codes_tab
[i
].name
;
2523 init_code_file(void)
2528 if ((fp
= fopen(code_file
, "r")) == NULL
) {
2530 fprintf(log_fp
, "open of %s failed\n", code_file
);
2534 for (i
= 0; i
< MAX_ENTRIES
; i
++) {
2537 int n
= fscanf(fp
, "%x%127s\n", &code
, name
);
2539 if (n
== 1 && i
== 0) {
2541 * old code file format, just skip
2549 strncpy(codes_tab
[i
].name
, name
, 32);
2550 codes_tab
[i
].type
= code
;
2563 char tmp_nm_file
[128];
2567 bzero(tmp_nm_file
, 128);
2568 bzero(tmpstr
, 1024);
2571 * Build the temporary nm file path
2573 strcpy(tmp_nm_file
,"/tmp/knm.out.XXXXXX");
2575 if (!mktemp(tmp_nm_file
)) {
2576 fprintf(stderr
, "Error in mktemp call\n");
2581 * Build the nm command and create a tmp file with the output
2583 sprintf (tmpstr
, "/usr/bin/nm -f -n -s __TEXT __text %s > %s",
2584 kernelpath
, tmp_nm_file
);
2588 * Parse the output from the nm command
2590 if ((fp
= fopen(tmp_nm_file
, "r")) == NULL
) {
2591 /* Hmmm, let's not treat this as fatal */
2592 fprintf(stderr
, "Failed to open nm symbol file [%s]\n", tmp_nm_file
);
2596 * Count the number of symbols in the nm symbol table
2600 while ((inchr
= getc(fp
)) != -1) {
2601 if (inchr
== '\n') {
2608 * Malloc the space for symbol table
2610 if (kern_sym_count
> 0) {
2611 kern_sym_tbl
= malloc(kern_sym_count
* sizeof(kern_sym_t
));
2613 if (!kern_sym_tbl
) {
2615 * Hmmm, lets not treat this as fatal
2617 fprintf(stderr
, "Can't allocate memory for kernel symbol table\n");
2619 bzero(kern_sym_tbl
, kern_sym_count
* sizeof(kern_sym_t
));
2623 * Hmmm, lets not treat this as fatal
2625 fprintf(stderr
, "No kernel symbol table \n");
2627 for (i
= 0; i
< kern_sym_count
; i
++) {
2628 bzero(tmpstr
, 1024);
2630 if (fscanf(fp
, "%p %c %s", &kern_sym_tbl
[i
].k_sym_addr
, &inchr
, tmpstr
) != 3) {
2633 len
= strlen(tmpstr
);
2634 kern_sym_tbl
[i
].k_sym_name
= malloc(len
+ 1);
2636 if (kern_sym_tbl
[i
].k_sym_name
== NULL
) {
2637 fprintf(stderr
, "Can't allocate memory for symbol name [%s]\n", tmpstr
);
2638 kern_sym_tbl
[i
].k_sym_name
= NULL
;
2641 strcpy(kern_sym_tbl
[i
].k_sym_name
, tmpstr
);
2644 kern_sym_tbl
[i
].k_sym_len
= len
;
2647 if (i
!= kern_sym_count
) {
2649 * Hmmm, didn't build up entire table from nm
2650 * scrap the entire thing
2653 kern_sym_tbl
= NULL
;
2659 * Remove the temporary nm file
2661 unlink(tmp_nm_file
);
2664 * Dump the kernel symbol table
2666 for (i
= 0; i
< kern_sym_count
; i
++) {
2667 if (kern_sym_tbl
[i
].k_sym_name
) {
2668 printf ("[%d] %-16p %s\n", i
,
2669 kern_sym_tbl
[i
].k_sym_addr
, kern_sym_tbl
[i
].k_sym_name
);
2671 printf ("[%d] %-16p %s\n", i
,
2672 kern_sym_tbl
[i
].k_sym_addr
, "No symbol name");
2679 pc_to_string(char *pcstring
, uintptr_t pc
, int max_len
, int mode
)
2684 if (mode
== USER_MODE
) {
2685 sprintf(pcstring
, "%-16lx [usermode addr]", pc
);
2688 ret
= binary_search(kern_sym_tbl
, 0, kern_sym_count
-1, pc
);
2690 if (ret
== -1 || kern_sym_tbl
[ret
].k_sym_name
== NULL
) {
2691 sprintf(pcstring
, "%-16lx", pc
);
2694 if ((len
= kern_sym_tbl
[ret
].k_sym_len
) > (max_len
- 8)) {
2698 memcpy(pcstring
, kern_sym_tbl
[ret
].k_sym_name
, len
);
2700 sprintf(&pcstring
[len
], "+0x%-5lx", pc
- (uintptr_t)kern_sym_tbl
[ret
].k_sym_addr
);
2705 * Return -1 if not found, else return index
2708 binary_search(kern_sym_t
*list
, int low
, int high
, uintptr_t addr
)
2712 if (kern_sym_count
== 0) {
2717 return -1; /* failed */
2720 if (low
+ 1 == high
) {
2721 if ((uintptr_t)list
[low
].k_sym_addr
<= addr
&& addr
< (uintptr_t)list
[high
].k_sym_addr
) {
2723 * We have a range match
2727 if ((uintptr_t)list
[high
].k_sym_addr
<= addr
) {
2735 mid
= (low
+ high
) / 2;
2737 if (addr
< (uintptr_t)list
[mid
].k_sym_addr
) {
2738 return binary_search(list
, low
, mid
, addr
);
2741 return binary_search(list
, mid
, high
, addr
);
2746 open_logfile(const char *path
)
2748 log_fp
= fopen(path
, "a");
2752 * failed to open path
2754 fprintf(stderr
, "latency: failed to open logfile [%s]\n", path
);
2761 open_rawfile(const char *path
)
2763 RAW_fd
= open(path
, O_RDONLY
);
2767 * failed to open path
2769 fprintf(stderr
, "latency: failed to open RAWfile [%s]\n", path
);
2778 mach_timebase_info_data_t info
;
2780 (void)mach_timebase_info(&info
);
2782 divisor
= ((double)info
.denom
/ (double)info
.numer
) * 1000;