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
;
162 int sample_generation
= 0;
163 int num_i_latency_cpus
= 1;
168 kd_buf
**last_decrementer_kd
; /* last DECR_TRAP per cpu */
173 typedef struct event
*event_t
;
180 uint64_t ev_timestamp
;
184 typedef struct lookup
*lookup_t
;
192 long lk_pathname
[NUMPARMS
+ 1];
196 typedef struct threadmap
*threadmap_t
;
202 uintptr_t tm_pthread
;
203 char tm_command
[MAXCOMLEN
+ 1];
204 char tm_orig_command
[MAXCOMLEN
+ 1];
208 typedef struct threadrun
*threadrun_t
;
215 uint64_t tr_timestamp
;
219 typedef struct thread_entry
*thread_entry_t
;
221 struct thread_entry
{
222 thread_entry_t te_next
;
228 #define HASH_SIZE 1024
229 #define HASH_MASK 1023
231 event_t event_hash
[HASH_SIZE
];
232 lookup_t lookup_hash
[HASH_SIZE
];
233 threadmap_t threadmap_hash
[HASH_SIZE
];
234 threadrun_t threadrun_hash
[HASH_SIZE
];
236 event_t event_freelist
;
237 lookup_t lookup_freelist
;
238 threadrun_t threadrun_freelist
;
239 threadmap_t threadmap_freelist
;
240 threadmap_t threadmap_temp
;
242 thread_entry_t thread_entry_freelist
;
243 thread_entry_t thread_delete_list
;
244 thread_entry_t thread_reset_list
;
245 thread_entry_t thread_event_list
;
246 thread_entry_t thread_lookup_list
;
247 thread_entry_t thread_run_list
;
258 #define RAW_VERSION0 0x55aa0000
259 #define RAW_VERSION1 0x55aa0101
264 #define KERNEL_MODE 1
267 #define TRACE_DATA_NEWTHREAD 0x07000004
268 #define TRACE_STRING_NEWTHREAD 0x07010004
269 #define TRACE_STRING_EXEC 0x07010008
271 #define INTERRUPT 0x01050000
272 #define DECR_TRAP 0x01090000
273 #define DECR_SET 0x01090004
274 #define MACH_vmfault 0x01300008
275 #define MACH_sched 0x01400000
276 #define MACH_stkhandoff 0x01400008
277 #define MACH_makerunnable 0x01400018
278 #define MACH_idle 0x01400024
279 #define VFS_LOOKUP 0x03010090
280 #define IES_action 0x050b0018
281 #define IES_filter 0x050b001c
282 #define TES_action 0x050c0010
283 #define CQ_action 0x050d0018
284 #define CPUPM_CPUSTER_RUNCOUNT 0x05310144
286 #define BSC_exit 0x040C0004
287 #define BSC_thread_terminate 0x040c05a4
289 #define DBG_FUNC_MASK ~(DBG_FUNC_START | DBG_FUNC_END)
291 #define CPU_NUMBER(kp) kdbg_get_cpu(kp)
293 #define EMPTYSTRING ""
296 const char *fault_name
[] = {
308 const char *sched_reasons
[] = {
328 #define ARRAYSIZE(x) ((int)(sizeof(x) / sizeof(*x)))
329 #define MAX_REASON ARRAYSIZE(sched_reasons)
331 static double handle_decrementer(kd_buf
*, int);
332 static kd_buf
*log_decrementer(kd_buf
*kd_beg
, kd_buf
*kd_end
, kd_buf
*end_of_sample
, double i_latency
);
333 static void read_command_map(void);
334 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
);
335 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
);
336 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
);
337 static void log_info(uint64_t now
, uint64_t idelta
, uint64_t start_bias
, kd_buf
*kd
, kd_buf
*kd_note
);
338 static char *find_code(int);
339 static void pc_to_string(char *pcstring
, uintptr_t pc
, int max_len
, int mode
);
340 static void getdivisor(void);
341 static int sample_sc(void);
342 static void init_code_file(void);
343 static void do_kernel_nm(void);
344 static void open_logfile(const char*);
345 static int binary_search(kern_sym_t
*list
, int low
, int high
, uintptr_t addr
);
347 static void create_map_entry(uintptr_t, char *);
348 static void check_for_thread_update(uintptr_t thread
, int debugid_base
, kd_buf
*kbufp
, char **command
);
349 static void log_scheduler(kd_buf
*kd_start
, kd_buf
*kd_stop
, kd_buf
*end_of_sample
, double s_latency
, uintptr_t thread
);
350 static int check_for_scheduler_latency(int type
, uintptr_t *thread
, uint64_t now
, kd_buf
*kd
, kd_buf
**kd_start
, double *latency
);
351 static void open_rawfile(const char *path
);
353 static void screen_update(FILE *);
355 static void set_enable(int);
356 static void set_remove(void);
366 * This flag is turned off when calling
367 * quit() due to a set_remove() failure.
369 if (set_remove_flag
) {
383 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDENABLE
, val
};
386 if (sysctl(mib
, ARRAYSIZE(mib
), NULL
, &needed
, NULL
, 0) < 0) {
387 quit("trace facility failure, KERN_KDENABLE\n");
392 set_numbufs(int nbufs
)
394 int mib1
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDSETBUF
, nbufs
};
395 int mib2
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDSETUP
};
398 if (sysctl(mib1
, ARRAYSIZE(mib1
), NULL
, &needed
, NULL
, 0) < 0) {
399 quit("trace facility failure, KERN_KDSETBUF\n");
401 if (sysctl(mib2
, ARRAYSIZE(mib2
), NULL
, &needed
, NULL
, 0) < 0) {
402 quit("trace facility failure, KERN_KDSETUP\n");
407 set_pidexclude(int pid
, int on_off
)
409 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDPIDEX
};
410 size_t needed
= sizeof(kd_regtype
);
413 .type
= KDBG_TYPENONE
,
418 sysctl(mib
, ARRAYSIZE(mib
), &kr
, &needed
, NULL
, 0);
422 get_bufinfo(kbufinfo_t
*val
)
424 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDGETBUF
};
425 size_t needed
= sizeof (*val
);
427 if (sysctl(mib
, ARRAYSIZE(mib
), val
, &needed
, 0, 0) < 0) {
428 quit("trace facility failure, KERN_KDGETBUF\n");
435 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDREMOVE
};
440 if (sysctl(mib
, ARRAYSIZE(mib
), NULL
, &needed
, NULL
, 0) < 0) {
442 if (errno
== EBUSY
) {
443 quit("the trace facility is currently in use...\n fs_usage, sc_usage, and latency use this feature.\n\n");
445 quit("trace facility failure, KERN_KDREMOVE\n");
452 write_high_res_latencies(void)
457 if (use_high_res_bins
) {
458 if ((f
= fopen("latencies.csv","w"))) {
459 for (i
= 0; i
< N_HIGH_RES_BINS
; i
++) {
460 fprintf(f
, "%d,%d\n", i
, i_high_res_bins
[i
]);
468 sigintr(int signo
__attribute__((unused
)))
470 write_high_res_latencies();
473 set_pidexclude(getpid(), 0);
474 screen_update(log_fp
);
481 /* exit under normal conditions -- signal handler */
483 leave(int signo
__attribute__((unused
)))
485 write_high_res_latencies();
488 set_pidexclude(getpid(), 0);
496 sigwinch(int signo
__attribute__((unused
)))
502 print_total(FILE *fp
, char *s
, int total
)
507 struct i_latencies
*il
;
510 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
512 itotal
+= il
->i_total
;
514 clen
= sprintf(tbuf
, "%s %10d %9d", s
, total
, itotal
);
516 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
519 if (i_latency_per_cpu
== TRUE
) {
520 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_total
);
525 sprintf(&tbuf
[clen
], "\n");
527 fprintf(fp
, "%s", tbuf
);
536 screen_update(FILE *fp
)
545 int min_lat
, max_lat
;
547 unsigned int average_s_latency
;
548 unsigned int average_i_latency
;
549 struct i_latencies
*il
;
556 fprintf(fp
,"\n\n===================================================================================================\n");
559 * Display the current time.
560 * "ctime" always returns a string that looks like this:
562 * Sun Sep 16 01:03:52 1973
563 * 012345678901234567890123
566 * We want indices 11 thru 18 (length 8).
569 curr_time
= sample_TOD_secs
;
570 elapsed_secs
= ((last_now
- first_now
) / divisor
) / 1000000;
572 elapsed_secs
= curr_time
- start_time
;
575 elapsed_hours
= elapsed_secs
/ 3600;
576 elapsed_secs
-= elapsed_hours
* 3600;
577 elapsed_mins
= elapsed_secs
/ 60;
578 elapsed_secs
-= elapsed_mins
* 60;
580 sprintf(tbuf
, "%-19.19s %2ld:%02ld:%02ld\n", &(ctime(&curr_time
)[0]),
581 (long)elapsed_hours
, (long)elapsed_mins
, (long)elapsed_secs
);
583 fprintf(fp
, "%s", tbuf
);
588 sprintf(tbuf
, " SCHEDULER INTERRUPTS\n");
590 fprintf(fp
, "%s", tbuf
);
595 if (i_latency_per_cpu
== TRUE
) {
596 clen
= sprintf(tbuf
, " Total");
598 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
600 clen
+= sprintf(&tbuf
[clen
], " CPU %d", cpu
);
602 clen
+= sprintf(&tbuf
[clen
], " CPU %d", cpu
);
606 fprintf(fp
, "%s", tbuf
);
611 clen
= sprintf(tbuf
, "\n-------------------------------------------------------");
613 for (cpu
= 1; cpu
< num_i_latency_cpus
; cpu
++) {
614 clen
+= sprintf(&tbuf
[clen
], "----------");
617 fprintf(fp
, "%s", tbuf
);
622 sprintf(tbuf
, "---------------------------------------------");
624 fprintf(fp
, "%s", tbuf
);
629 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
631 itotal
+= il
->i_total_samples
;
633 clen
= sprintf(tbuf
, "\ntotal_samples %10d %9d", s_total_samples
, itotal
);
635 if (i_latency_per_cpu
== TRUE
) {
636 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
639 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_total_samples
);
642 sprintf(&tbuf
[clen
], "\n");
644 fprintf(fp
, "%s", tbuf
);
650 for (stotal
= 0, i
= 0; i
< 10; i
++) {
651 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
654 itotal
+= il
->i_usec_10_bins
[i
];
655 il
->i_total
+= il
->i_usec_10_bins
[i
];
657 clen
= sprintf(tbuf
, "\ndelays < %3d usecs %10d %9d", (i
+ 1) * 10, s_usec_10_bins
[i
], itotal
);
659 stotal
+= s_usec_10_bins
[i
];
661 if (i_latency_per_cpu
== TRUE
) {
662 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
665 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_usec_10_bins
[i
]);
669 fprintf(fp
, "%s", tbuf
);
674 print_total(fp
, "\ntotal < 100 usecs", stotal
);
676 for (stotal
= 0, i
= 1; i
< 10; i
++) {
677 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
680 itotal
+= il
->i_usec_100_bins
[i
];
681 il
->i_total
+= il
->i_usec_100_bins
[i
];
684 clen
= sprintf(tbuf
, "\ndelays < %3d usecs %10d %9d", (i
+ 1) * 100, s_usec_100_bins
[i
], itotal
);
686 clen
= sprintf(tbuf
, "\ndelays < 1 msec %10d %9d", s_usec_100_bins
[i
], itotal
);
689 stotal
+= s_usec_100_bins
[i
];
691 if (i_latency_per_cpu
== TRUE
) {
692 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
695 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_usec_100_bins
[i
]);
699 fprintf(fp
, "%s", tbuf
);
704 print_total(fp
, "\ntotal < 1 msec ", stotal
);
707 for (stotal
= 0, i
= 1; i
< 10; i
++) {
708 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
711 itotal
+= il
->i_msec_1_bins
[i
];
712 il
->i_total
+= il
->i_msec_1_bins
[i
];
714 clen
= sprintf(tbuf
, "\ndelays < %3d msecs %10d %9d", (i
+ 1), s_msec_1_bins
[i
], itotal
);
716 stotal
+= s_msec_1_bins
[i
];
718 if (i_latency_per_cpu
== TRUE
) {
719 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
722 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_msec_1_bins
[i
]);
726 fprintf(fp
, "%s", tbuf
);
731 print_total(fp
, "\ntotal < 10 msecs", stotal
);
733 for (stotal
= 0, i
= 1; i
< 5; i
++) {
734 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
737 itotal
+= il
->i_msec_10_bins
[i
];
738 il
->i_total
+= il
->i_msec_10_bins
[i
];
740 clen
= sprintf(tbuf
, "\ndelays < %3d msecs %10d %9d", (i
+ 1)*10, s_msec_10_bins
[i
], itotal
);
742 stotal
+= s_msec_10_bins
[i
];
744 if (i_latency_per_cpu
== TRUE
) {
745 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
748 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_msec_10_bins
[i
]);
752 fprintf(fp
, "%s", tbuf
);
757 print_total(fp
, "\ntotal < 50 msecs", stotal
);
760 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
762 itotal
+= il
->i_too_slow
;
764 clen
= sprintf(tbuf
, "\ndelays > 50 msecs %10d %9d", s_too_slow
, itotal
);
766 if (i_latency_per_cpu
== TRUE
) {
767 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
770 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_too_slow
);
774 fprintf(fp
, "%s", tbuf
);
779 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
782 if (cpu
== 0 || (il
->i_min_latency
< min_lat
)) {
783 min_lat
= il
->i_min_latency
;
786 clen
= sprintf(tbuf
, "\n\nminimum latency(usecs) %7d %9d", s_min_latency
, min_lat
);
788 if (i_latency_per_cpu
== TRUE
) {
789 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
792 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_min_latency
);
796 fprintf(fp
, "%s", tbuf
);
802 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
805 if (cpu
== 0 || (il
->i_max_latency
> max_lat
)) {
806 max_lat
= il
->i_max_latency
;
809 clen
= sprintf(tbuf
, "\nmaximum latency(usecs) %7d %9d", s_max_latency
, max_lat
);
811 if (i_latency_per_cpu
== TRUE
) {
812 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
815 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_max_latency
);
819 fprintf(fp
, "%s", tbuf
);
824 if (s_total_samples
) {
825 average_s_latency
= (unsigned int)(s_total_latency
/s_total_samples
);
827 average_s_latency
= 0;
830 for (itotal
= 0, tot_lat
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
833 itotal
+= il
->i_total_samples
;
834 tot_lat
+= il
->i_total_latency
;
837 average_i_latency
= (unsigned)(tot_lat
/itotal
);
839 average_i_latency
= 0;
842 clen
= sprintf(tbuf
, "\naverage latency(usecs) %7d %9d", average_s_latency
, average_i_latency
);
844 if (i_latency_per_cpu
== TRUE
) {
845 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
848 if (il
->i_total_samples
) {
849 average_i_latency
= (unsigned int)(il
->i_total_latency
/il
->i_total_samples
);
851 average_i_latency
= 0;
854 clen
+= sprintf(&tbuf
[clen
], " %9d", average_i_latency
);
858 fprintf(fp
, "%s", tbuf
);
863 for (itotal
= 0, cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
866 itotal
+= il
->i_exceeded_threshold
;
868 clen
= sprintf(tbuf
, "\nexceeded threshold %7d %9d", s_exceeded_threshold
, itotal
);
870 if (i_latency_per_cpu
== TRUE
) {
871 for (cpu
= 0; cpu
< num_i_latency_cpus
; cpu
++) {
874 clen
+= sprintf(&tbuf
[clen
], " %9d", il
->i_exceeded_threshold
);
877 sprintf(&tbuf
[clen
], "\n");
880 fprintf(fp
, "%s", tbuf
);
895 fprintf(stderr
, "Usage: latency [-p priority] [-h] [-m] [-st threshold] [-it threshold]\n");
896 fprintf(stderr
, " [-c codefile] [-l logfile] [-R rawfile] [-n kernel]\n\n");
898 fprintf(stderr
, " -p specify scheduling priority to watch... default is realtime\n");
899 fprintf(stderr
, " -h Display high resolution interrupt latencies and write them to latencies.csv (truncate existing file) upon exit.\n");
900 fprintf(stderr
, " -st set scheduler latency threshold in microseconds... if latency exceeds this, then log trace\n");
901 fprintf(stderr
, " -m specify per-CPU interrupt latency reporting\n");
902 fprintf(stderr
, " -it set interrupt latency threshold in microseconds... if latency exceeds this, then log trace\n");
903 fprintf(stderr
, " -c specify name of codes file... default is /usr/share/misc/trace.codes\n");
904 fprintf(stderr
, " -l specify name of file to log trace entries to when the specified threshold is exceeded\n");
905 fprintf(stderr
, " -R specify name of raw trace file to process\n");
906 fprintf(stderr
, " -n specify kernel... default is /mach_kernel\n");
908 fprintf(stderr
, "\nlatency must be run as root\n\n");
915 main(int argc
, char *argv
[])
919 if (0 != reexec_to_match_kernel()) {
920 fprintf(stderr
, "Could not re-execute: %d\n", errno
);
925 if (strcmp(argv
[1], "-R") == 0) {
930 open_rawfile(argv
[1]);
937 } else if (strcmp(argv
[1], "-p") == 0) {
942 watch_priority
= atoi(argv
[1]);
946 } else if (strcmp(argv
[1], "-st") == 0) {
951 s_thresh_hold
= atoi(argv
[1]);
955 } else if (strcmp(argv
[1], "-it") == 0) {
960 i_thresh_hold
= atoi(argv
[1]);
964 } else if (strcmp(argv
[1], "-c") == 0) {
973 } else if (strcmp(argv
[1], "-l") == 0) {
978 open_logfile(argv
[1]);
982 } else if (strcmp(argv
[1], "-n") == 0) {
987 kernelpath
= argv
[1];
991 } else if (strcmp(argv
[1], "-h") == 0) {
992 use_high_res_bins
= TRUE
;
994 } else if (strcmp(argv
[1], "-m") == 0) {
995 i_latency_per_cpu
= TRUE
;
1005 if (geteuid() != 0) {
1006 printf("'latency' must be run as root...\n");
1010 if (kernelpath
== NULL
) {
1011 kernelpath
= "/mach_kernel";
1014 if (code_file
== NULL
) {
1015 code_file
= "/usr/share/misc/trace.codes";
1025 if (initscr() == NULL
) {
1026 printf("Unrecognized TERM type, try vt100\n");
1032 signal(SIGWINCH
, sigwinch
);
1033 signal(SIGINT
, sigintr
);
1034 signal(SIGQUIT
, leave
);
1035 signal(SIGTERM
, leave
);
1036 signal(SIGHUP
, leave
);
1039 * grab the number of cpus and scale the buffer size
1041 int mib
[] = { CTL_HW
, HW_NCPU
};
1042 size_t len
= sizeof(num_cpus
);
1044 sysctl(mib
, ARRAYSIZE(mib
), &num_cpus
, &len
, NULL
, 0);
1047 set_numbufs(SAMPLE_SIZE
* num_cpus
);
1049 get_bufinfo(&bufinfo
);
1053 set_pidexclude(getpid(), 1);
1056 num_entries
= bufinfo
.nkdbufs
;
1058 num_entries
= 50000;
1062 for (cpu_mask
= 0, i
= 0; i
< num_cpus
; i
++)
1063 cpu_mask
|= ((uint64_t)1 << i
);
1065 if ((my_buffer
= malloc(num_entries
* sizeof(kd_buf
))) == NULL
) {
1066 quit("can't allocate memory for tracing info\n");
1069 if ((last_decrementer_kd
= (kd_buf
**)malloc(num_cpus
* sizeof(kd_buf
*))) == NULL
) {
1070 quit("can't allocate memory for decrementer tracing info\n");
1073 if (i_latency_per_cpu
== FALSE
) {
1074 num_i_latency_cpus
= 1;
1076 num_i_latency_cpus
= num_cpus
;
1079 if ((i_lat
= (struct i_latencies
*)malloc(num_i_latency_cpus
* sizeof(struct i_latencies
))) == NULL
) {
1080 quit("can't allocate memory for interrupt latency info\n");
1083 bzero((char *)i_lat
, num_i_latency_cpus
* sizeof(struct i_latencies
));
1086 while (sample_sc()) {
1091 screen_update(log_fp
);
1094 screen_update(stdout
);
1099 double nanosecs_to_sleep
;
1101 nanosecs_to_sleep
= (double)(SAMPLE_TIME_USECS
* 1000);
1102 fdelay
= nanosecs_to_sleep
* (divisor
/1000);
1103 adelay
= (uint64_t)fdelay
;
1107 start_time
= time(NULL
);
1108 refresh_time
= start_time
;
1111 curr_time
= time(NULL
);
1113 if (curr_time
>= refresh_time
) {
1114 screen_update(NULL
);
1115 refresh_time
= curr_time
+ 1;
1117 mach_wait_until(mach_absolute_time() + adelay
);
1123 * No need to check for initscr error return.
1124 * We won't get here if it fails on the first call.
1139 read_command_map(void)
1141 kd_threadmap
*mapptr
= 0;
1142 int total_threads
= 0;
1146 RAW_header header
= {0};
1149 if (read(RAW_fd
, &header
, sizeof(RAW_header
)) != sizeof(RAW_header
)) {
1150 perror("read failed");
1153 if (header
.version_no
!= RAW_VERSION1
) {
1154 header
.version_no
= RAW_VERSION0
;
1155 header
.TOD_secs
= time(NULL
);
1156 header
.TOD_usecs
= 0;
1158 lseek(RAW_fd
, (off_t
)0, SEEK_SET
);
1160 if (read(RAW_fd
, &header
.thread_count
, sizeof(int)) != sizeof(int)) {
1161 perror("read failed");
1165 total_threads
= header
.thread_count
;
1167 sample_TOD_secs
= header
.TOD_secs
;
1168 sample_TOD_usecs
= header
.TOD_usecs
;
1170 if (total_threads
== 0 && header
.version_no
!= RAW_VERSION0
) {
1171 offset
= lseek(RAW_fd
, (off_t
)0, SEEK_CUR
);
1172 offset
= (offset
+ (4095)) & ~4095;
1174 lseek(RAW_fd
, offset
, SEEK_SET
);
1177 total_threads
= bufinfo
.nkdthreads
;
1180 size
= total_threads
* sizeof(kd_threadmap
);
1182 if (size
== 0 || ((mapptr
= (kd_threadmap
*) malloc(size
)) == 0)) {
1185 bzero (mapptr
, size
);
1188 * Now read the threadmap
1191 if (read(RAW_fd
, mapptr
, size
) != size
) {
1192 printf("Can't read the thread map -- this is not fatal\n");
1194 if (header
.version_no
!= RAW_VERSION0
) {
1195 offset
= lseek(RAW_fd
, (off_t
)0, SEEK_CUR
);
1196 offset
= (offset
+ (4095)) & ~4095;
1198 lseek(RAW_fd
, offset
, SEEK_SET
);
1201 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDTHRMAP
};
1202 if (sysctl(mib
, ARRAYSIZE(mib
), mapptr
, &size
, NULL
, 0) < 0) {
1204 * This is not fatal -- just means I cant map command strings
1206 printf("Can't read the thread map -- this is not fatal\n");
1211 for (i
= 0; i
< total_threads
; i
++) {
1212 create_map_entry(mapptr
[i
].thread
, &mapptr
[i
].command
[0]);
1218 create_map_entry(uintptr_t thread
, char *command
)
1222 if ((tme
= threadmap_freelist
)) {
1223 threadmap_freelist
= tme
->tm_next
;
1225 tme
= (threadmap_t
)malloc(sizeof(struct threadmap
));
1228 tme
->tm_thread
= thread
;
1230 (void)strncpy (tme
->tm_command
, command
, MAXCOMLEN
);
1231 tme
->tm_command
[MAXCOMLEN
] = '\0';
1232 tme
->tm_orig_command
[0] = '\0';
1234 int hashid
= thread
& HASH_MASK
;
1236 tme
->tm_next
= threadmap_hash
[hashid
];
1237 threadmap_hash
[hashid
] = tme
;
1241 delete_thread_entry(uintptr_t thread
)
1245 int hashid
= thread
& HASH_MASK
;
1247 if ((tme
= threadmap_hash
[hashid
])) {
1248 if (tme
->tm_thread
== thread
) {
1249 threadmap_hash
[hashid
] = tme
->tm_next
;
1251 threadmap_t tme_prev
= tme
;
1253 for (tme
= tme
->tm_next
; tme
; tme
= tme
->tm_next
) {
1254 if (tme
->tm_thread
== thread
) {
1255 tme_prev
->tm_next
= tme
->tm_next
;
1262 tme
->tm_next
= threadmap_freelist
;
1263 threadmap_freelist
= tme
;
1269 find_and_insert_tmp_map_entry(uintptr_t pthread
, char *command
)
1273 if ((tme
= threadmap_temp
)) {
1274 if (tme
->tm_pthread
== pthread
) {
1275 threadmap_temp
= tme
->tm_next
;
1277 threadmap_t tme_prev
= tme
;
1279 for (tme
= tme
->tm_next
; tme
; tme
= tme
->tm_next
) {
1280 if (tme
->tm_pthread
== pthread
) {
1281 tme_prev
->tm_next
= tme
->tm_next
;
1288 (void)strncpy (tme
->tm_command
, command
, MAXCOMLEN
);
1289 tme
->tm_command
[MAXCOMLEN
] = '\0';
1290 tme
->tm_orig_command
[0] = '\0';
1292 int hashid
= tme
->tm_thread
& HASH_MASK
;
1293 tme
->tm_next
= threadmap_hash
[hashid
];
1294 threadmap_hash
[hashid
] = tme
;
1300 create_tmp_map_entry(uintptr_t thread
, uintptr_t pthread
)
1304 if ((tme
= threadmap_freelist
)) {
1305 threadmap_freelist
= tme
->tm_next
;
1307 tme
= malloc(sizeof(struct threadmap
));
1310 tme
->tm_thread
= thread
;
1311 tme
->tm_pthread
= pthread
;
1312 tme
->tm_command
[0] = '\0';
1313 tme
->tm_orig_command
[0] = '\0';
1315 tme
->tm_next
= threadmap_temp
;
1316 threadmap_temp
= tme
;
1320 find_thread_entry(uintptr_t thread
)
1324 int hashid
= thread
& HASH_MASK
;
1326 for (tme
= threadmap_hash
[hashid
]; tme
; tme
= tme
->tm_next
) {
1327 if (tme
->tm_thread
== thread
) {
1335 find_thread_name(uintptr_t thread
, char **command
)
1339 if ((tme
= find_thread_entry(thread
))) {
1340 *command
= tme
->tm_command
;
1342 *command
= EMPTYSTRING
;
1347 add_thread_entry_to_list(thread_entry_t
*list
, uintptr_t thread
)
1351 if ((te
= thread_entry_freelist
)) {
1352 thread_entry_freelist
= te
->te_next
;
1354 te
= (thread_entry_t
)malloc(sizeof(struct thread_entry
));
1357 te
->te_thread
= thread
;
1358 te
->te_next
= *list
;
1363 exec_thread_entry(uintptr_t thread
, char *command
)
1367 if ((tme
= find_thread_entry(thread
))) {
1368 if (tme
->tm_orig_command
[0] == '\0') {
1369 (void)strncpy (tme
->tm_orig_command
, tme
->tm_command
, MAXCOMLEN
);
1370 tme
->tm_orig_command
[MAXCOMLEN
] = '\0';
1372 (void)strncpy (tme
->tm_command
, command
, MAXCOMLEN
);
1373 tme
->tm_command
[MAXCOMLEN
] = '\0';
1375 add_thread_entry_to_list(&thread_reset_list
, thread
);
1377 create_map_entry(thread
, command
);
1382 record_thread_entry_for_gc(uintptr_t thread
)
1384 add_thread_entry_to_list(&thread_delete_list
, thread
);
1388 gc_thread_entries(void)
1391 thread_entry_t te_next
;
1394 for (te
= thread_delete_list
; te
; te
= te_next
) {
1395 delete_thread_entry(te
->te_thread
);
1397 te_next
= te
->te_next
;
1398 te
->te_next
= thread_entry_freelist
;
1399 thread_entry_freelist
= te
;
1403 thread_delete_list
= 0;
1407 gc_reset_entries(void)
1410 thread_entry_t te_next
;
1413 for (te
= thread_reset_list
; te
; te
= te_next
) {
1414 te_next
= te
->te_next
;
1415 te
->te_next
= thread_entry_freelist
;
1416 thread_entry_freelist
= te
;
1420 thread_reset_list
= 0;
1424 reset_thread_names(void)
1427 thread_entry_t te_next
;
1430 for (te
= thread_reset_list
; te
; te
= te_next
) {
1433 if ((tme
= find_thread_entry(te
->te_thread
))) {
1434 if (tme
->tm_orig_command
[0]) {
1435 (void)strncpy (tme
->tm_command
, tme
->tm_orig_command
, MAXCOMLEN
);
1436 tme
->tm_command
[MAXCOMLEN
] = '\0';
1437 tme
->tm_orig_command
[0] = '\0';
1440 te_next
= te
->te_next
;
1441 te
->te_next
= thread_entry_freelist
;
1442 thread_entry_freelist
= te
;
1446 thread_reset_list
= 0;
1450 delete_all_thread_entries(void)
1452 threadmap_t tme
= 0;
1453 threadmap_t tme_next
= 0;
1456 for (i
= 0; i
< HASH_SIZE
; i
++) {
1457 for (tme
= threadmap_hash
[i
]; tme
; tme
= tme_next
) {
1458 tme_next
= tme
->tm_next
;
1459 tme
->tm_next
= threadmap_freelist
;
1460 threadmap_freelist
= tme
;
1462 threadmap_hash
[i
] = 0;
1470 insert_run_event(uintptr_t thread
, kd_buf
*kd
, uint64_t now
)
1474 int hashid
= thread
& HASH_MASK
;
1476 for (trp
= threadrun_hash
[hashid
]; trp
; trp
= trp
->tr_next
) {
1477 if (trp
->tr_thread
== thread
) {
1482 if ((trp
= threadrun_freelist
)) {
1483 threadrun_freelist
= trp
->tr_next
;
1485 trp
= (threadrun_t
)malloc(sizeof(struct threadrun
));
1488 trp
->tr_thread
= thread
;
1490 trp
->tr_next
= threadrun_hash
[hashid
];
1491 threadrun_hash
[hashid
] = trp
;
1493 add_thread_entry_to_list(&thread_run_list
, thread
);
1496 trp
->tr_timestamp
= now
;
1500 find_run_event(uintptr_t thread
)
1503 int hashid
= thread
& HASH_MASK
;
1505 for (trp
= threadrun_hash
[hashid
]; trp
; trp
= trp
->tr_next
) {
1506 if (trp
->tr_thread
== thread
) {
1514 delete_run_event(uintptr_t thread
)
1516 threadrun_t trp
= 0;
1517 threadrun_t trp_prev
;
1519 int hashid
= thread
& HASH_MASK
;
1521 if ((trp
= threadrun_hash
[hashid
])) {
1522 if (trp
->tr_thread
== thread
) {
1523 threadrun_hash
[hashid
] = trp
->tr_next
;
1527 for (trp
= trp
->tr_next
; trp
; trp
= trp
->tr_next
) {
1528 if (trp
->tr_thread
== thread
) {
1529 trp_prev
->tr_next
= trp
->tr_next
;
1536 trp
->tr_next
= threadrun_freelist
;
1537 threadrun_freelist
= trp
;
1543 gc_run_events(void) {
1545 thread_entry_t te_next
;
1547 threadrun_t trp_next
;
1550 for (te
= thread_run_list
; te
; te
= te_next
) {
1551 int hashid
= te
->te_thread
& HASH_MASK
;
1553 for (trp
= threadrun_hash
[hashid
]; trp
; trp
= trp_next
) {
1554 trp_next
= trp
->tr_next
;
1555 trp
->tr_next
= threadrun_freelist
;
1556 threadrun_freelist
= trp
;
1559 threadrun_hash
[hashid
] = 0;
1561 te_next
= te
->te_next
;
1562 te
->te_next
= thread_entry_freelist
;
1563 thread_entry_freelist
= te
;
1565 thread_run_list
= 0;
1571 insert_start_event(uintptr_t thread
, int type
, uint64_t now
)
1575 int hashid
= thread
& HASH_MASK
;
1577 for (evp
= event_hash
[hashid
]; evp
; evp
= evp
->ev_next
) {
1578 if (evp
->ev_thread
== thread
&& evp
->ev_type
== type
) {
1583 if ((evp
= event_freelist
)) {
1584 event_freelist
= evp
->ev_next
;
1586 evp
= (event_t
)malloc(sizeof(struct event
));
1589 evp
->ev_thread
= thread
;
1590 evp
->ev_type
= type
;
1592 evp
->ev_next
= event_hash
[hashid
];
1593 event_hash
[hashid
] = evp
;
1595 add_thread_entry_to_list(&thread_event_list
, thread
);
1597 evp
->ev_timestamp
= now
;
1602 consume_start_event(uintptr_t thread
, int type
, uint64_t now
)
1606 uint64_t elapsed
= 0;
1608 int hashid
= thread
& HASH_MASK
;
1610 if ((evp
= event_hash
[hashid
])) {
1611 if (evp
->ev_thread
== thread
&& evp
->ev_type
== type
) {
1612 event_hash
[hashid
] = evp
->ev_next
;
1616 for (evp
= evp
->ev_next
; evp
; evp
= evp
->ev_next
) {
1617 if (evp
->ev_thread
== thread
&& evp
->ev_type
== type
) {
1618 evp_prev
->ev_next
= evp
->ev_next
;
1625 elapsed
= now
- evp
->ev_timestamp
;
1627 if (now
< evp
->ev_timestamp
) {
1628 printf("consume: now = %qd, timestamp = %qd\n", now
, evp
->ev_timestamp
);
1631 evp
->ev_next
= event_freelist
;
1632 event_freelist
= evp
;
1639 gc_start_events(void)
1642 thread_entry_t te_next
;
1648 for (te
= thread_event_list
; te
; te
= te_next
) {
1650 hashid
= te
->te_thread
& HASH_MASK
;
1652 for (evp
= event_hash
[hashid
]; evp
; evp
= evp_next
) {
1653 evp_next
= evp
->ev_next
;
1654 evp
->ev_next
= event_freelist
;
1655 event_freelist
= evp
;
1658 event_hash
[hashid
] = 0;
1660 te_next
= te
->te_next
;
1661 te
->te_next
= thread_entry_freelist
;
1662 thread_entry_freelist
= te
;
1664 thread_event_list
= 0;
1668 thread_in_user_mode(uintptr_t thread
, char *command
)
1672 if (strcmp(command
, "kernel_task") == 0) {
1676 int hashid
= thread
& HASH_MASK
;
1678 for (evp
= event_hash
[hashid
]; evp
; evp
= evp
->ev_next
) {
1679 if (evp
->ev_thread
== thread
) {
1689 handle_lookup_event(uintptr_t thread
, int debugid
, kd_buf
*kdp
)
1692 boolean_t first_record
= FALSE
;
1694 int hashid
= thread
& HASH_MASK
;
1696 if (debugid
& DBG_FUNC_START
) {
1697 first_record
= TRUE
;
1700 for (lkp
= lookup_hash
[hashid
]; lkp
; lkp
= lkp
->lk_next
) {
1701 if (lkp
->lk_thread
== thread
) {
1706 if (first_record
== FALSE
) {
1710 if ((lkp
= lookup_freelist
)) {
1711 lookup_freelist
= lkp
->lk_next
;
1713 lkp
= (lookup_t
)malloc(sizeof(struct lookup
));
1716 lkp
->lk_thread
= thread
;
1718 lkp
->lk_next
= lookup_hash
[hashid
];
1719 lookup_hash
[hashid
] = lkp
;
1721 add_thread_entry_to_list(&thread_lookup_list
, thread
);
1724 if (first_record
== TRUE
) {
1725 lkp
->lk_pathptr
= lkp
->lk_pathname
;
1726 lkp
->lk_dvp
= kdp
->arg1
;
1728 if (lkp
->lk_pathptr
> &lkp
->lk_pathname
[NUMPARMS
-4]) {
1731 *lkp
->lk_pathptr
++ = kdp
->arg1
;
1733 *lkp
->lk_pathptr
++ = kdp
->arg2
;
1734 *lkp
->lk_pathptr
++ = kdp
->arg3
;
1735 *lkp
->lk_pathptr
++ = kdp
->arg4
;
1736 *lkp
->lk_pathptr
= 0;
1738 if (debugid
& DBG_FUNC_END
) {
1746 delete_lookup_event(uintptr_t thread
, lookup_t lkp_to_delete
)
1752 hashid
= thread
& HASH_MASK
;
1754 if ((lkp
= lookup_hash
[hashid
])) {
1755 if (lkp
== lkp_to_delete
) {
1756 lookup_hash
[hashid
] = lkp
->lk_next
;
1760 for (lkp
= lkp
->lk_next
; lkp
; lkp
= lkp
->lk_next
) {
1761 if (lkp
== lkp_to_delete
) {
1762 lkp_prev
->lk_next
= lkp
->lk_next
;
1769 lkp
->lk_next
= lookup_freelist
;
1770 lookup_freelist
= lkp
;
1776 gc_lookup_events(void) {
1778 thread_entry_t te_next
;
1784 for (te
= thread_lookup_list
; te
; te
= te_next
) {
1785 hashid
= te
->te_thread
& HASH_MASK
;
1787 for (lkp
= lookup_hash
[hashid
]; lkp
; lkp
= lkp_next
) {
1788 lkp_next
= lkp
->lk_next
;
1789 lkp
->lk_next
= lookup_freelist
;
1790 lookup_freelist
= lkp
;
1793 lookup_hash
[hashid
] = 0;
1795 te_next
= te
->te_next
;
1796 te
->te_next
= thread_entry_freelist
;
1797 thread_entry_freelist
= te
;
1799 thread_lookup_list
= 0;
1805 kd_buf
*kd
, *end_of_sample
;
1811 * Get kernel buffer information
1813 get_bufinfo(&bufinfo
);
1816 delete_all_thread_entries();
1821 uint32_t bytes_read
;
1823 bytes_read
= read(RAW_fd
, my_buffer
, num_entries
* sizeof(kd_buf
));
1825 if (bytes_read
== -1) {
1826 perror("read failed");
1829 count
= bytes_read
/ sizeof(kd_buf
);
1831 if (count
!= num_entries
) {
1836 kd
= (kd_buf
*)my_buffer
;
1837 first_now
= kd
->timestamp
& KDBG_TIMESTAMP_MASK
;
1842 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDREADTR
};
1843 size_t needed
= bufinfo
.nkdbufs
* sizeof(kd_buf
);
1845 if (sysctl(mib
, ARRAYSIZE(mib
), my_buffer
, &needed
, NULL
, 0) < 0) {
1846 quit("trace facility failure, KERN_KDREADTR\n");
1850 sample_generation
++;
1852 if (bufinfo
.flags
& KDBG_WRAPPED
) {
1856 fprintf(log_fp
, "\n\n%-19.19s sample = %d <<<<<<< trace buffer wrapped >>>>>>>\n\n",
1857 &(ctime(&curr_time
)[0]), sample_generation
);
1863 end_of_sample
= &((kd_buf
*)my_buffer
)[count
];
1866 * Always reinitialize the DECR_TRAP array
1868 for (i
= 0; i
< num_cpus
; i
++) {
1869 last_decrementer_kd
[i
] = (kd_buf
*)my_buffer
;
1872 for (kd
= (kd_buf
*)my_buffer
; kd
< end_of_sample
; kd
++) {
1874 uintptr_t thread
= kd
->arg5
;
1875 int type
= kd
->debugid
& DBG_FUNC_MASK
;
1877 (void)check_for_thread_update(thread
, type
, kd
, NULL
);
1879 uint64_t now
= kd
->timestamp
& KDBG_TIMESTAMP_MASK
;
1882 if (type
== DECR_TRAP
) {
1883 int cpunum
= CPU_NUMBER(kd
);
1884 double i_latency
= handle_decrementer(kd
, cpunum
);
1887 if (i_thresh_hold
&& (int)i_latency
> i_thresh_hold
) {
1888 kd_start
= last_decrementer_kd
[cpunum
];
1890 log_decrementer(kd_start
, kd
, end_of_sample
, i_latency
);
1892 last_decrementer_kd
[cpunum
] = kd
;
1896 if (check_for_scheduler_latency(type
, &thread
, now
, kd
, &kd_start
, &s_latency
)) {
1897 log_scheduler(kd_start
, kd
, end_of_sample
, s_latency
, thread
);
1905 gc_thread_entries();
1915 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
)
1922 int cpunum
= CPU_NUMBER(kd
);
1924 if (print_info
&& fp
) {
1925 timestamp
= (double)(now
- start_bias
) / divisor
;
1926 delta
= (double)idelta
/ divisor
;
1928 if ((p
= find_code(type
))) {
1929 if (type
== INTERRUPT
) {
1938 pc_to_string(&pcstring
[0], kd
->arg2
, 58, mode
);
1940 fprintf(fp
, "%9.1f %8.1f\t\tINTERRUPT[%2lx] @ %-58.58s %8x %2d %s\n",
1941 timestamp
, delta
, kd
->arg1
, &pcstring
[0], thread
, cpunum
, command
);
1942 } else if (type
== MACH_vmfault
) {
1943 fprintf(fp
, "%9.1f %8.1f\t\t%-28.28s %8x %2d %s\n",
1944 timestamp
, delta
, p
, thread
, cpunum
, command
);
1946 fprintf(fp
, "%9.1f %8.1f\t\t%-28.28s %-16lx %-16lx %-16lx %-16lx %8x %2d %s\n",
1947 timestamp
, delta
, p
, kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
,
1948 thread
, cpunum
, command
);
1951 fprintf(fp
, "%9.1f %8.1f\t\t%-8x %-16lx %-16lx %-16lx %-16lx %8x %2d %s\n",
1952 timestamp
, delta
, type
, kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
,
1953 thread
, cpunum
, command
);
1956 if (type
!= BSC_thread_terminate
&& type
!= BSC_exit
) {
1957 insert_start_event(thread
, type
, now
);
1963 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
)
1969 double elapsed_timestamp
;
1971 elapsed_timestamp
= (double)consume_start_event(thread
, type
, now
) / divisor
;
1973 if (print_info
&& fp
) {
1974 int cpunum
= CPU_NUMBER(kd
);
1976 timestamp
= (double)(now
- start_bias
) / divisor
;
1977 delta
= (double)idelta
/ divisor
;
1979 fprintf(fp
, "%9.1f %8.1f(%.1f) \t", timestamp
, delta
, elapsed_timestamp
);
1981 if ((p
= find_code(type
))) {
1982 if (type
== INTERRUPT
) {
1983 fprintf(fp
, "INTERRUPT %8x %2d %s\n", thread
, cpunum
, command
);
1984 } else if (type
== MACH_vmfault
&& kd
->arg4
<= DBG_PAGEIND_FAULT
) {
1985 user_addr
= ((uint64_t)kd
->arg1
<< 32) | (uint32_t)kd
->arg2
;
1987 fprintf(fp
, "%-28.28s %-10.10s %-16qx %8x %2d %s\n",
1988 p
, fault_name
[kd
->arg4
], user_addr
,
1989 thread
, cpunum
, command
);
1991 fprintf(fp
, "%-28.28s %-16lx %-16lx %8x %2d %s\n",
1992 p
, kd
->arg1
, kd
->arg2
,
1993 thread
, cpunum
, command
);
1996 fprintf(fp
, "%-8x %-16lx %-16lx %8x %2d %s\n",
1997 type
, kd
->arg1
, kd
->arg2
,
1998 thread
, cpunum
, command
);
2005 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
)
2013 int cpunum
= CPU_NUMBER(kd
);
2015 double timestamp
= (double)(now
- start_bias
) / divisor
;
2016 double delta
= (double)idelta
/ divisor
;
2018 if ((p
= find_code(type
))) {
2019 if (kd
== kd_note
) {
2020 fprintf(fp
, "%9.1f %8.1f\t**\t", timestamp
, delta
);
2022 fprintf(fp
, "%9.1f %8.1f\t\t", timestamp
, delta
);
2024 fprintf(fp
, "%-28.28s %-16lx %-16lx %-16lx %-16lx %8x %2d %s\n",
2025 p
, kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
, thread
, cpunum
, command
);
2027 fprintf(fp
, "%9.1f %8.1f\t\t%-8x %-16lx %-16lx %-16lx %-16lx %8x %2d %s\n",
2028 timestamp
, delta
, type
, kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
,
2029 thread
, cpunum
, command
);
2035 check_for_thread_update(uintptr_t thread
, int debugid_base
, kd_buf
*kbufp
, char **command
)
2037 if (debugid_base
== TRACE_DATA_NEWTHREAD
) {
2039 * Save the create thread data
2041 create_tmp_map_entry(kbufp
->arg1
, thread
);
2042 } else if (debugid_base
== TRACE_STRING_NEWTHREAD
) {
2044 * process new map entry
2046 find_and_insert_tmp_map_entry(thread
, (char *)&kbufp
->arg1
);
2047 } else if (debugid_base
== TRACE_STRING_EXEC
) {
2048 exec_thread_entry(thread
, (char *)&kbufp
->arg1
);
2050 if (debugid_base
== BSC_exit
|| debugid_base
== BSC_thread_terminate
) {
2051 record_thread_entry_for_gc(thread
);
2054 find_thread_name(thread
, command
);
2061 log_info(uint64_t now
, uint64_t idelta
, uint64_t start_bias
, kd_buf
*kd
, kd_buf
*kd_note
)
2069 char command_buf
[32];
2070 char sched_info
[64];
2072 const char *sched_reason
;
2078 int thread
= kd
->arg5
;
2079 int cpunum
= CPU_NUMBER(kd
);
2080 int debugid
= kd
->debugid
;
2081 int type
= kd
->debugid
& DBG_FUNC_MASK
;
2083 (void)check_for_thread_update(thread
, type
, kd
, &command
);
2085 if ((type
>> 24) == DBG_TRACE
) {
2086 if (((type
>> 16) & 0xff) != DBG_TRACE_INFO
) {
2090 timestamp
= (double)(now
- start_bias
) / divisor
;
2091 delta
= (double)idelta
/ divisor
;
2096 pc_to_string(&pcstring
[0], kd
->arg1
, 84, KERNEL_MODE
);
2098 fprintf(log_fp
, "%9.1f %8.1f\t\tCQ_action @ %-84.84s %8x %2d %s\n",
2099 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2103 pc_to_string(&pcstring
[0], kd
->arg1
, 83, KERNEL_MODE
);
2105 fprintf(log_fp
, "%9.1f %8.1f\t\tTES_action @ %-83.83s %8x %2d %s\n",
2106 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2110 pc_to_string(&pcstring
[0], kd
->arg1
, 83, KERNEL_MODE
);
2112 fprintf(log_fp
, "%9.1f %8.1f\t\tIES_action @ %-83.83s %8x %2d %s\n",
2113 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2117 pc_to_string(&pcstring
[0], kd
->arg1
, 83, KERNEL_MODE
);
2119 fprintf(log_fp
, "%9.1f %8.1f\t\tIES_filter @ %-83.83s %8x %2d %s\n",
2120 timestamp
, delta
, &pcstring
[0], thread
, cpunum
, command
);
2124 if ((int)kd
->arg1
>= 0) {
2127 i_latency
= (((double)(-1 - kd
->arg1
)) / divisor
);
2130 if (i_thresh_hold
&& (int)i_latency
> i_thresh_hold
) {
2142 pc_to_string(&pcstring
[0], kd
->arg2
, 84, mode
);
2144 fprintf(log_fp
, "%9.1f %8.1f[%.1f]%s\tDECR_TRAP @ %-84.84s %8x %2d %s\n",
2145 timestamp
, delta
, i_latency
, p
, &pcstring
[0], thread
, cpunum
, command
);
2149 fprintf(log_fp
, "%9.1f %8.1f[%.1f] \t%-28.28s %8x %2d %s\n",
2150 timestamp
, delta
, (double)kd
->arg1
/divisor
, "DECR_SET", thread
, cpunum
, command
);
2154 case MACH_stkhandoff
:
2156 find_thread_name(kd
->arg2
, &command1
);
2158 if (command1
== EMPTYSTRING
) {
2159 command1
= command_buf
;
2160 sprintf(command1
, "%-8lx", kd
->arg2
);
2162 if (thread_in_user_mode(kd
->arg2
, command1
)) {
2170 if (reason
> MAX_REASON
) {
2173 sched_reason
= sched_reasons
[reason
];
2176 if (sched_reason
[0] == '?') {
2177 sprintf(joe
, "%x", reason
);
2180 sprintf(sched_info
, "%16.16s @ pri %3lu --> %16.16s @ pri %3lu%s", command
, kd
->arg3
, command1
, kd
->arg4
, p
);
2182 fprintf(log_fp
, "%9.1f %8.1f\t\t%-10.10s[%s] %s %8x %2d\n",
2183 timestamp
, delta
, "MACH_SCHED", sched_reason
, sched_info
, thread
, cpunum
);
2187 if ((lkp
= handle_lookup_event(thread
, debugid
, kd
))) {
2189 * print the tail end of the pathname
2191 p
= (char *)lkp
->lk_pathname
;
2192 int clen
= strlen(p
);
2200 fprintf(log_fp
, "%9.1f %8.1f\t\t%-14.14s %-59s %-16lx %8x %2d %s\n",
2201 timestamp
, delta
, "VFS_LOOKUP",
2202 &p
[clen
], lkp
->lk_dvp
, thread
, cpunum
, command
);
2204 delete_lookup_event(thread
, lkp
);
2209 if (debugid
& DBG_FUNC_START
) {
2210 enter_syscall(log_fp
, kd
, thread
, type
, command
, now
, idelta
, start_bias
, 1);
2211 } else if (debugid
& DBG_FUNC_END
) {
2212 exit_syscall(log_fp
, kd
, thread
, type
, command
, now
, idelta
, start_bias
, 1);
2214 print_entry(log_fp
, kd
, thread
, type
, command
, now
, idelta
, start_bias
, kd_note
);
2223 log_range(kd_buf
*kd_buffer
, kd_buf
*kd_start
, kd_buf
*kd_stop
, kd_buf
*kd_note
, char *buf1
)
2225 uint64_t last_timestamp
= 0;
2227 uint64_t start_bias
= 0;
2233 clen
= strlen(buf1
);
2234 memset(buf2
, '-', clen
);
2236 fprintf(log_fp
, "\n\n%s\n", buf2
);
2237 fprintf(log_fp
, "%s\n\n", buf1
);
2239 fprintf(log_fp
, "RelTime(Us) Delta debugid arg1 arg2 arg3 arg4 thread cpu command\n\n");
2241 reset_thread_names();
2243 last_timestamp
= kd_start
->timestamp
& KDBG_TIMESTAMP_MASK
;
2244 start_bias
= last_timestamp
;
2246 for (kd
= kd_buffer
; kd
<= kd_stop
; kd
++) {
2247 now
= kd
->timestamp
& KDBG_TIMESTAMP_MASK
;
2249 if (kd
>= kd_start
) {
2250 delta
= now
- last_timestamp
;
2252 log_info(now
, delta
, start_bias
, kd
, kd_note
);
2254 last_timestamp
= now
;
2256 int debugid
= kd
->debugid
;
2257 int thread
= kd
->arg5
;
2258 int type
= kd
->debugid
& DBG_FUNC_MASK
;
2260 if ((type
>> 24) == DBG_TRACE
) {
2261 if (((type
>> 16) & 0xff) != DBG_TRACE_INFO
) {
2265 if (type
== BSC_thread_terminate
|| type
== BSC_exit
) {
2269 if (debugid
& DBG_FUNC_START
) {
2270 insert_start_event(thread
, type
, now
);
2271 } else if (debugid
& DBG_FUNC_END
) {
2272 (void)consume_start_event(thread
, type
, now
);
2282 log_decrementer(kd_buf
*kd_beg
, kd_buf
*kd_end
, kd_buf
*end_of_sample
, double i_latency
)
2284 kd_buf
*kd_start
, *kd_stop
;
2285 int kd_count
; /* Limit the boundary of kd_start */
2287 double sample_timestamp
;
2290 int thread
= kd_beg
->arg5
;
2291 int cpunum
= CPU_NUMBER(kd_end
);
2293 for (kd_count
= 0, kd_start
= kd_beg
- 1; (kd_start
>= (kd_buf
*)my_buffer
); kd_start
--, kd_count
++) {
2294 if (kd_count
== MAX_LOG_COUNT
) {
2298 if (CPU_NUMBER(kd_start
) != cpunum
) {
2302 if ((kd_start
->debugid
& DBG_FUNC_MASK
) == DECR_TRAP
) {
2306 if (kd_start
->arg5
!= thread
) {
2310 if (kd_start
< (kd_buf
*)my_buffer
) {
2311 kd_start
= (kd_buf
*)my_buffer
;
2314 thread
= kd_end
->arg5
;
2316 for (kd_stop
= kd_end
+ 1; kd_stop
< end_of_sample
; kd_stop
++) {
2317 if (CPU_NUMBER(kd_stop
) != cpunum
) {
2321 if ((kd_stop
->debugid
& DBG_FUNC_MASK
) == INTERRUPT
) {
2325 if (kd_stop
->arg5
!= thread
) {
2329 if (kd_stop
>= end_of_sample
) {
2330 kd_stop
= end_of_sample
- 1;
2337 now
= kd_start
->timestamp
& KDBG_TIMESTAMP_MASK
;
2338 sample_timestamp
= (double)(now
- first_now
) / divisor
;
2340 TOD_usecs
= (uint64_t)sample_timestamp
;
2341 TOD_secs
= sample_TOD_secs
+ ((sample_TOD_usecs
+ TOD_usecs
) / 1000000);
2343 sprintf(buf1
, "%-19.19s interrupt latency = %.1fus [timestamp %.1f]", ctime(&TOD_secs
), i_latency
, sample_timestamp
);
2345 sprintf(buf1
, "%-19.19s interrupt latency = %.1fus [sample %d]", &(ctime(&curr_time
)[0]), i_latency
, sample_generation
);
2348 log_range((kd_buf
*)my_buffer
, kd_start
, kd_stop
, 0, buf1
);
2355 log_scheduler(kd_buf
*kd_beg
, kd_buf
*kd_end
, kd_buf
*end_of_sample
, double s_latency
, uintptr_t thread
)
2357 kd_buf
*kd_start
, *kd_stop
;
2362 double sample_timestamp
;
2365 for (count
= 0, kd_start
= kd_beg
; (kd_start
>= (kd_buf
*)my_buffer
); kd_start
--) {
2366 cpunum
= CPU_NUMBER(kd_start
);
2368 cmask
|= ((uint64_t)1 << cpunum
);
2370 if (cmask
== cpu_mask
) {
2375 if (kd_start
< (kd_buf
*)my_buffer
) {
2376 kd_start
= (kd_buf
*)my_buffer
;
2379 for (kd_stop
= kd_end
+ 1; kd_stop
< end_of_sample
; kd_stop
++) {
2380 if (kd_stop
->arg5
== thread
) {
2384 if (kd_stop
>= end_of_sample
) {
2385 kd_stop
= end_of_sample
- 1;
2392 now
= kd_start
->timestamp
& KDBG_TIMESTAMP_MASK
;
2393 sample_timestamp
= (double)(now
- first_now
) / divisor
;
2395 TOD_usecs
= (uint64_t)sample_timestamp
;
2396 TOD_secs
= sample_TOD_secs
+ ((sample_TOD_usecs
+ TOD_usecs
) / 1000000);
2398 sprintf(buf1
, "%-19.19s priority = %d, scheduling latency = %.1fus [timestamp %.1f]", ctime(&TOD_secs
), watch_priority
, s_latency
, sample_timestamp
);
2400 sprintf(buf1
, "%-19.19s priority = %d, scheduling latency = %.1fus [sample %d]", &(ctime(&curr_time
)[0]), watch_priority
, s_latency
, sample_generation
);
2403 log_range((kd_buf
*)my_buffer
, kd_start
, kd_stop
, kd_beg
, buf1
);
2409 check_for_scheduler_latency(int type
, uintptr_t *thread
, uint64_t now
, kd_buf
*kd
, kd_buf
**kd_start
, double *latency
)
2411 int found_latency
= 0;
2413 if (type
== MACH_makerunnable
) {
2414 if (watch_priority
== kd
->arg2
) {
2415 insert_run_event(kd
->arg1
, kd
, now
);
2417 } else if (type
== MACH_sched
|| type
== MACH_stkhandoff
) {
2420 if (type
== MACH_sched
|| type
== MACH_stkhandoff
) {
2424 if ((trp
= find_run_event(*thread
))) {
2425 double d_s_latency
= (((double)(now
- trp
->tr_timestamp
)) / divisor
);
2426 int s_latency
= (int)d_s_latency
;
2429 if (s_latency
< 100) {
2430 s_usec_10_bins
[s_latency
/10]++;
2432 if (s_latency
< 1000) {
2433 s_usec_100_bins
[s_latency
/100]++;
2434 } else if (s_latency
< 10000) {
2435 s_msec_1_bins
[s_latency
/1000]++;
2436 } else if (s_latency
< 50000) {
2437 s_msec_10_bins
[s_latency
/10000]++;
2442 if (s_latency
> s_max_latency
) {
2443 s_max_latency
= s_latency
;
2445 if (s_latency
< s_min_latency
|| s_total_samples
== 0) {
2446 s_min_latency
= s_latency
;
2448 s_total_latency
+= s_latency
;
2451 if (s_thresh_hold
&& s_latency
> s_thresh_hold
) {
2452 s_exceeded_threshold
++;
2455 *kd_start
= trp
->tr_entry
;
2456 *latency
= d_s_latency
;
2461 delete_run_event(*thread
);
2464 return found_latency
;
2469 handle_decrementer(kd_buf
*kd
, int cpunum
)
2471 struct i_latencies
*il
;
2475 if (i_latency_per_cpu
== FALSE
) {
2479 il
= &i_lat
[cpunum
];
2481 if ((long)(kd
->arg1
) >= 0) {
2484 latency
= (((double)(-1 - kd
->arg1
)) / divisor
);
2486 elapsed_usecs
= (long)latency
;
2488 if (elapsed_usecs
< 100) {
2489 il
->i_usec_10_bins
[elapsed_usecs
/10]++;
2492 if (elapsed_usecs
< 1000) {
2493 il
->i_usec_100_bins
[elapsed_usecs
/100]++;
2494 } else if (elapsed_usecs
< 10000) {
2495 il
->i_msec_1_bins
[elapsed_usecs
/1000]++;
2496 } else if (elapsed_usecs
< 50000) {
2497 il
->i_msec_10_bins
[elapsed_usecs
/10000]++;
2502 if (use_high_res_bins
&& elapsed_usecs
< N_HIGH_RES_BINS
) {
2503 i_high_res_bins
[elapsed_usecs
]++;
2505 if (i_thresh_hold
&& elapsed_usecs
> i_thresh_hold
) {
2506 il
->i_exceeded_threshold
++;
2508 if (elapsed_usecs
> il
->i_max_latency
) {
2509 il
->i_max_latency
= elapsed_usecs
;
2511 if (elapsed_usecs
< il
->i_min_latency
|| il
->i_total_samples
== 0) {
2512 il
->i_min_latency
= elapsed_usecs
;
2514 il
->i_total_latency
+= elapsed_usecs
;
2515 il
->i_total_samples
++;
2526 for (i
= 0; i
< num_of_codes
; i
++) {
2527 if (codes_tab
[i
].type
== type
) {
2528 return codes_tab
[i
].name
;
2536 init_code_file(void)
2541 if ((fp
= fopen(code_file
, "r")) == NULL
) {
2543 fprintf(log_fp
, "open of %s failed\n", code_file
);
2547 for (i
= 0; i
< MAX_ENTRIES
; i
++) {
2550 int n
= fscanf(fp
, "%x%127s\n", &code
, name
);
2552 if (n
== 1 && i
== 0) {
2554 * old code file format, just skip
2562 strncpy(codes_tab
[i
].name
, name
, 32);
2563 codes_tab
[i
].type
= code
;
2576 char tmp_nm_file
[128];
2580 bzero(tmp_nm_file
, 128);
2581 bzero(tmpstr
, 1024);
2584 * Build the temporary nm file path
2586 strcpy(tmp_nm_file
,"/tmp/knm.out.XXXXXX");
2588 if (!mktemp(tmp_nm_file
)) {
2589 fprintf(stderr
, "Error in mktemp call\n");
2594 * Build the nm command and create a tmp file with the output
2596 sprintf (tmpstr
, "/usr/bin/nm -f -n -s __TEXT __text %s > %s",
2597 kernelpath
, tmp_nm_file
);
2601 * Parse the output from the nm command
2603 if ((fp
= fopen(tmp_nm_file
, "r")) == NULL
) {
2604 /* Hmmm, let's not treat this as fatal */
2605 fprintf(stderr
, "Failed to open nm symbol file [%s]\n", tmp_nm_file
);
2609 * Count the number of symbols in the nm symbol table
2613 while ((inchr
= getc(fp
)) != -1) {
2614 if (inchr
== '\n') {
2621 * Malloc the space for symbol table
2623 if (kern_sym_count
> 0) {
2624 kern_sym_tbl
= malloc(kern_sym_count
* sizeof(kern_sym_t
));
2626 if (!kern_sym_tbl
) {
2628 * Hmmm, lets not treat this as fatal
2630 fprintf(stderr
, "Can't allocate memory for kernel symbol table\n");
2632 bzero(kern_sym_tbl
, kern_sym_count
* sizeof(kern_sym_t
));
2636 * Hmmm, lets not treat this as fatal
2638 fprintf(stderr
, "No kernel symbol table \n");
2640 for (i
= 0; i
< kern_sym_count
; i
++) {
2641 bzero(tmpstr
, 1024);
2643 if (fscanf(fp
, "%p %c %s", &kern_sym_tbl
[i
].k_sym_addr
, &inchr
, tmpstr
) != 3) {
2646 len
= strlen(tmpstr
);
2647 kern_sym_tbl
[i
].k_sym_name
= malloc(len
+ 1);
2649 if (kern_sym_tbl
[i
].k_sym_name
== NULL
) {
2650 fprintf(stderr
, "Can't allocate memory for symbol name [%s]\n", tmpstr
);
2651 kern_sym_tbl
[i
].k_sym_name
= NULL
;
2654 strcpy(kern_sym_tbl
[i
].k_sym_name
, tmpstr
);
2657 kern_sym_tbl
[i
].k_sym_len
= len
;
2660 if (i
!= kern_sym_count
) {
2662 * Hmmm, didn't build up entire table from nm
2663 * scrap the entire thing
2666 kern_sym_tbl
= NULL
;
2672 * Remove the temporary nm file
2674 unlink(tmp_nm_file
);
2677 * Dump the kernel symbol table
2679 for (i
= 0; i
< kern_sym_count
; i
++) {
2680 if (kern_sym_tbl
[i
].k_sym_name
) {
2681 printf ("[%d] %-16p %s\n", i
,
2682 kern_sym_tbl
[i
].k_sym_addr
, kern_sym_tbl
[i
].k_sym_name
);
2684 printf ("[%d] %-16p %s\n", i
,
2685 kern_sym_tbl
[i
].k_sym_addr
, "No symbol name");
2692 pc_to_string(char *pcstring
, uintptr_t pc
, int max_len
, int mode
)
2697 if (mode
== USER_MODE
) {
2698 sprintf(pcstring
, "%-16lx [usermode addr]", pc
);
2701 ret
= binary_search(kern_sym_tbl
, 0, kern_sym_count
-1, pc
);
2703 if (ret
== -1 || kern_sym_tbl
[ret
].k_sym_name
== NULL
) {
2704 sprintf(pcstring
, "%-16lx", pc
);
2707 if ((len
= kern_sym_tbl
[ret
].k_sym_len
) > (max_len
- 8)) {
2711 memcpy(pcstring
, kern_sym_tbl
[ret
].k_sym_name
, len
);
2713 sprintf(&pcstring
[len
], "+0x%-5lx", pc
- (uintptr_t)kern_sym_tbl
[ret
].k_sym_addr
);
2718 * Return -1 if not found, else return index
2721 binary_search(kern_sym_t
*list
, int low
, int high
, uintptr_t addr
)
2725 if (kern_sym_count
== 0) {
2730 return -1; /* failed */
2733 if (low
+ 1 == high
) {
2734 if ((uintptr_t)list
[low
].k_sym_addr
<= addr
&& addr
< (uintptr_t)list
[high
].k_sym_addr
) {
2736 * We have a range match
2740 if ((uintptr_t)list
[high
].k_sym_addr
<= addr
) {
2748 mid
= (low
+ high
) / 2;
2750 if (addr
< (uintptr_t)list
[mid
].k_sym_addr
) {
2751 return binary_search(list
, low
, mid
, addr
);
2754 return binary_search(list
, mid
, high
, addr
);
2759 open_logfile(const char *path
)
2761 log_fp
= fopen(path
, "a");
2765 * failed to open path
2767 fprintf(stderr
, "latency: failed to open logfile [%s]\n", path
);
2774 open_rawfile(const char *path
)
2776 RAW_fd
= open(path
, O_RDONLY
);
2780 * failed to open path
2782 fprintf(stderr
, "latency: failed to open RAWfile [%s]\n", path
);
2791 mach_timebase_info_data_t info
;
2793 (void)mach_timebase_info(&info
);
2795 divisor
= ((double)info
.denom
/ (double)info
.numer
) * 1000;