2 * Copyright (c) 1999-2019 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
26 cc -I. -DPRIVATE -D__APPLE_PRIVATE -O -o sc_usage sc_usage.c -lncurses
29 #define Default_DELAY 1 /* default delay interval */
39 #include <sys/types.h>
40 #include <sys/param.h>
42 #include <sys/ptrace.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 <sys/sysctl.h>
60 #include <mach/mach_time.h>
64 /* Number of lines of header information on the standard screen */
65 #define HEADER_LINES 5
68 int Header_lines
= HEADER_LINES
;
71 int no_screen_refresh
= 0;
77 int waiting_index
= 0;
78 FILE *dfp
= 0; /*Debug output file */
81 #define SAMPLE_SIZE 20000
83 #define DBG_ZERO_FILL_FAULT 1
84 #define DBG_PAGEIN_FAULT 2
85 #define DBG_COW_FAULT 3
86 #define DBG_CACHE_HIT_FAULT 4
89 #define MAX_THREADS 16
97 char *state_name
[] = {
106 #define KERNEL_MODE 1
127 int64_t pathname
[NUMPARMS
+ 1];
128 struct entry th_entry
[MAX_NESTED
];
136 unsigned int stime_secs
;
138 unsigned int wtime_secs
;
140 unsigned int delta_wtime_secs
;
141 double delta_wtime_usecs
;
144 struct th_info th_state
[MAX_THREADS
];
145 struct sc_entry faults
[MAX_FAULTS
];
147 struct sc_entry
*sc_tab
;
149 int msgcode_cnt
; /* number of MSG_ codes */
151 int num_of_threads
= 0;
152 int now_collect_cpu_time
= 0;
154 unsigned int utime_secs
;
158 unsigned int itime_secs
;
160 unsigned int delta_itime_secs
;
161 double delta_itime_usecs
;
165 unsigned int otime_secs
;
167 unsigned int delta_otime_secs
;
168 double delta_otime_usecs
;
176 int mach_stkhandoff
= 0;
178 int mach_vmfault
= 0;
185 #define DBG_FUNC_ALL (DBG_FUNC_START | DBG_FUNC_END)
186 #define DBG_FUNC_MASK 0xfffffffc
193 /* Default divisor */
194 #define DIVISOR 16.6666 /* Trace divisor converts to microseconds */
195 double divisor
= DIVISOR
;
201 kbufinfo_t bufinfo
= {0, 0, 0, 0};
203 int trace_enabled
= 0;
204 int set_remove_flag
= 1;
206 struct kinfo_proc
*kp_buffer
= 0;
207 size_t kp_nentries
= 0;
209 extern char **environ
;
211 static void set_enable(int);
212 static void set_pidcheck(int, int);
213 static void set_remove(void);
214 static void set_numbufs(int);
215 static void set_init(void);
217 int argtopid(char *);
218 int argtoi(int, char*, char*, int);
220 void get_bufinfo(kbufinfo_t
*);
221 static void reset_counters(void);
222 static void getdivisor(void);
223 static void screen_update(void);
224 static void sc_tab_init(char *);
225 static void sort_scalls(void);
226 static void sample_sc(void);
227 static int find_msgcode(int);
233 /* exit under normal conditions -- INT handler */
235 leave(__unused
int unused
)
237 if (no_screen_refresh
== 0) {
243 set_pidcheck(pid
, 0);
249 sigwinch(__unused
int unused
)
251 if (no_screen_refresh
== 0)
256 exit_usage(char *myname
)
258 fprintf(stderr
, "Usage: %s [-c codefile] [-e] [-l] [-sn] pid | cmd | -E execute path\n", myname
);
259 fprintf(stderr
, " -c name of codefile containing mappings for syscalls\n");
260 fprintf(stderr
, " Default is /usr/share/misc/trace.codes\n");
261 fprintf(stderr
, " -e enable sort by call count\n");
262 fprintf(stderr
, " -l turn off top style output\n");
263 fprintf(stderr
, " -sn change sample rate to every n seconds\n");
264 fprintf(stderr
, " pid selects process to sample\n");
265 fprintf(stderr
, " cmd selects command to sample\n");
266 fprintf(stderr
, " -E Execute the given path and optional arguments\n");
271 #define usec_to_1000ths(t) ((t) / 1000)
274 print_time(char *p
, unsigned int useconds
, unsigned int seconds
)
278 minutes
= seconds
/ 60;
279 hours
= minutes
/ 60;
281 if (minutes
< 100) { // up to 100 minutes
282 sprintf(p
, "%02ld:%02ld.%03ld", minutes
, (unsigned long)(seconds
% 60),
283 (unsigned long)usec_to_1000ths(useconds
));
285 else if (hours
< 100) { // up to 100 hours
286 sprintf(p
, "%02ld:%02ld:%02ld ", hours
, (minutes
% 60),
287 (unsigned long)(seconds
% 60));
290 sprintf(p
, "%4ld hrs ", hours
);
301 main(int argc
, char *argv
[])
303 char *myname
= "sc_usage";
304 char *codefile
= "/usr/share/misc/trace.codes";
307 int delay
= Default_DELAY
;
309 if ( geteuid() != 0 ) {
310 printf("'sc_usage' must be run as root...\n");
314 #if !defined(__arm64__)
315 if (0 != reexec_to_match_kernel()) {
316 fprintf(stderr
, "Could not re-execute: %d\n", errno
);
323 if ((myname
= rindex(argv
[0], '/')) == 0) {
331 while ((ch
= getopt(argc
, argv
, "c:els:d:E")) != EOF
) {
334 delay
= argtoi('s', "decimal number", optarg
, 10);
340 no_screen_refresh
= 1;
349 /* exit_usage(myname);*/
350 exit_usage("default");
356 sc_tab_init(codefile
);
362 /* parse a pid or a command */
363 if((pid
= argtopid(argv
[optind
])) < 0 )
367 { /* execute this command */
372 ptr
= strrchr(argv
[optind
], '/');
378 strncpy(proc_name
, ptr
, sizeof(proc_name
)-1);
379 proc_name
[sizeof(proc_name
)-1] = '\0';
389 fprintf(stderr
, "Starting program: %s\n", argv
[optind
]);
392 switch ((pid
= vfork()))
399 ptrace(0,(pid_t
)0,(caddr_t
)0,0); /* PT_TRACE_ME */
400 execve(argv
[optind
], &argv
[optind
], environ
);
415 if (no_screen_refresh
== 0) {
417 /* initializes curses and screen (last) */
418 if (initscr() == (WINDOW
*) 0)
420 printf("Unrecognized TERM type, try vt100\n");
433 /* set up signal handlers */
434 signal(SIGINT
, leave
);
435 signal(SIGQUIT
, leave
);
436 signal(SIGHUP
, leave
);
437 signal(SIGTERM
, leave
);
438 signal(SIGWINCH
, sigwinch
);
440 if (no_screen_refresh
== 0)
441 topn
= LINES
- Header_lines
;
448 set_numbufs(SAMPLE_SIZE
);
450 set_pidcheck(pid
, 1);
453 ptrace(7, pid
, (caddr_t
)1, 0); /* PT_CONTINUE */
458 if ((sort_now
= 10 / delay
) < 2)
461 get_bufinfo(&bufinfo
);
463 my_buffer
= malloc(bufinfo
.nkdbufs
* sizeof(kd_buf
));
464 if(my_buffer
== (char *) 0)
465 quit("can't allocate memory for tracing info\n");
468 (void)screen_update();
476 for (i
= 0; i
< (10 * delay
) && newLINES
== 0; i
++) {
478 if (no_screen_refresh
== 0) {
479 if ((c
= getch()) != ERR
&& (char)c
== 'q')
491 No need to check for initscr error return.
492 We won't get here if it fails on the first call.
498 topn
= LINES
- Header_lines
;
501 (void)screen_update();
506 print_row(struct sc_entry
*se
, int no_wtime
)
512 sprintf(tbuf
, "%-23.23s %8d(%d)", se
->name
, se
->total_count
, se
->delta_count
);
514 sprintf(tbuf
, "%-23.23s %8d", se
->name
, se
->total_count
);
517 memset(&tbuf
[clen
], ' ', 45 - clen
);
519 print_time(&tbuf
[45], (unsigned int)(se
->stime_usecs
), se
->stime_secs
);
522 if (no_wtime
== 0 && (se
->wtime_usecs
|| se
->wtime_secs
)) {
523 sprintf(&tbuf
[clen
], " ");
524 clen
+= strlen(&tbuf
[clen
]);
526 print_time(&tbuf
[clen
], (unsigned int)(se
->wtime_usecs
), se
->wtime_secs
);
527 clen
+= strlen(&tbuf
[clen
]);
529 if (se
->waiting
|| se
->delta_wtime_usecs
|| se
->delta_wtime_secs
) {
531 sprintf(&tbuf
[clen
], "(");
532 clen
+= strlen(&tbuf
[clen
]);
534 print_time(&tbuf
[clen
], (unsigned int)(se
->delta_wtime_usecs
),
535 se
->delta_wtime_secs
);
536 clen
+= strlen(&tbuf
[clen
]);
538 sprintf(&tbuf
[clen
], ")");
539 clen
+= strlen(&tbuf
[clen
]);
542 if (se
->waiting
== 1)
543 sprintf(&tbuf
[clen
], " W");
545 sprintf(&tbuf
[clen
], " %d", se
->waiting
);
546 clen
+= strlen(&tbuf
[clen
]);
550 sprintf(&tbuf
[clen
], "\n");
551 if (no_screen_refresh
)
574 if (no_screen_refresh
== 0) {
575 /* clear for new display */
581 sprintf(tbuf
, "%-14.14s", proc_name
);
589 p2
= "context switch ";
591 p2
= "context switches";
592 if (num_of_threads
== 1)
597 sprintf(&tbuf
[clen
], " %4d %s %4d %s %4d %s",
598 preempted
, p1
, csw
, p2
, num_of_threads
, p3
);
599 clen
+= strlen(&tbuf
[clen
]);
602 * Display the current time.
603 * "ctime" always returns a string that looks like this:
605 * Sun Sep 16 01:03:52 1973
606 * 012345678901234567890123
609 * We want indices 11 thru 18 (length 8).
611 curr_time
= time((long *)0);
614 start_time
= curr_time
;
616 elapsed_secs
= curr_time
- start_time
;
617 minutes
= elapsed_secs
/ 60;
618 hours
= minutes
/ 60;
620 memset(&tbuf
[clen
], ' ', 78 - clen
);
624 sprintf(&tbuf
[clen
], "%-8.8s\n", &(ctime(&curr_time
)[11]));
625 if (no_screen_refresh
)
630 if (total_faults
== 1)
638 sprintf(tbuf
, " %4d %s %4d %s",
639 total_faults
, p1
, scalls
, p2
);
642 sprintf(&tbuf
[clen
], " %3ld:%02ld:%02ld\n",
643 hours
, minutes
% 60, elapsed_secs
% 60);
644 if (no_screen_refresh
)
651 sprintf(tbuf
, "\nTYPE NUMBER CPU_TIME WAIT_TIME\n");
652 if (no_screen_refresh
)
657 sprintf(tbuf
, "------------------------------------------------------------------------------\n");
658 if (no_screen_refresh
)
666 sprintf(tbuf
, "System Idle ");
669 print_time(&tbuf
[clen
], itime_usecs
, itime_secs
);
670 clen
+= strlen(&tbuf
[clen
]);
672 if (delta_itime_usecs
|| delta_itime_secs
) {
674 sprintf(&tbuf
[clen
], "(");
675 clen
+= strlen(&tbuf
[clen
]);
677 print_time(&tbuf
[clen
], delta_itime_usecs
, delta_itime_secs
);
678 clen
+= strlen(&tbuf
[clen
]);
680 sprintf(&tbuf
[clen
], ")");
681 clen
+= strlen(&tbuf
[clen
]);
683 sprintf(&tbuf
[clen
], "\n");
684 if (no_screen_refresh
)
692 sprintf(tbuf
, "System Busy ");
695 print_time(&tbuf
[clen
], otime_usecs
, otime_secs
);
696 clen
+= strlen(&tbuf
[clen
]);
698 if (delta_otime_usecs
|| delta_otime_secs
) {
700 sprintf(&tbuf
[clen
], "(");
701 clen
+= strlen(&tbuf
[clen
]);
703 print_time(&tbuf
[clen
], delta_otime_usecs
, delta_otime_secs
);
704 clen
+= strlen(&tbuf
[clen
]);
706 sprintf(&tbuf
[clen
], ")");
707 clen
+= strlen(&tbuf
[clen
]);
709 sprintf(&tbuf
[clen
], "\n");
710 if (no_screen_refresh
)
717 sprintf(tbuf
, "%-14.14s Usermode ", proc_name
);
720 print_time(&tbuf
[clen
], utime_usecs
, utime_secs
);
721 clen
+= strlen(&tbuf
[clen
]);
723 sprintf(&tbuf
[clen
], "\n");
724 if (no_screen_refresh
)
731 max_rows
= topn
- (num_of_threads
+ 3);
735 for (output_lf
= 1, n
= 1; rows
< max_rows
&& n
< MAX_FAULTS
; n
++) {
738 if (se
->total_count
== 0)
740 if (output_lf
== 1) {
742 if (no_screen_refresh
)
748 if (rows
>= max_rows
)
757 if (no_screen_refresh
)
763 for (i
= 0; rows
< max_rows
; i
++) {
765 n
= sort_by_count
[i
];
767 n
= sort_by_wtime
[i
];
770 print_row(&sc_tab
[n
], 0);
776 if (no_screen_refresh
== 0) {
777 while (rows
++ < max_rows
)
782 if (num_of_threads
) {
783 sprintf(tbuf
, "\nCURRENT_TYPE LAST_PATHNAME_WAITED_FOR CUR_WAIT_TIME THRD# PRI\n");
785 if (no_screen_refresh
)
790 sprintf(tbuf
, "------------------------------------------------------------------------------\n");
791 if (no_screen_refresh
)
798 for (i
= 0; i
< num_of_threads
; i
++, ti
++) {
802 int secs
, time_secs
, time_usecs
;
804 now
= mach_absolute_time();
806 while (ti
->thread
== 0 && ti
< &th_state
[MAX_THREADS
])
808 if (ti
== &th_state
[MAX_THREADS
])
812 te
= &ti
->th_entry
[ti
->depth
- 1];
814 if (te
->sc_state
== WAITING
) {
816 sprintf(tbuf
, "%-23.23s", sc_tab
[te
->code
].name
);
818 sprintf(tbuf
, "%-23.23s", "vm_fault");
820 sprintf(tbuf
, "%-23.23s", state_name
[te
->sc_state
]);
822 te
= &ti
->th_entry
[0];
823 sprintf(tbuf
, "%-23.23s", state_name
[te
->sc_state
]);
827 /* print the tail end of the pathname */
828 p
= (char *)ti
->pathname
;
835 sprintf(&tbuf
[clen
], " %-26.26s ", &p
[plen
]);
837 clen
+= strlen(&tbuf
[clen
]);
839 time_usecs
= (((double)now
- te
->otime
) / divisor
);
840 secs
= time_usecs
/ 1000000;
841 time_usecs
-= secs
* 1000000;
844 print_time(&tbuf
[clen
], time_usecs
, time_secs
);
845 clen
+= strlen(&tbuf
[clen
]);
846 sprintf(&tbuf
[clen
], " %2d %3d\n", i
, ti
->curpri
);
847 if (no_screen_refresh
)
852 if (no_screen_refresh
== 0) {
856 printf("\n=================\n");
860 for (i
= 0; i
< (MAX_SC
+ msgcode_cnt
); i
++) {
861 if ((n
= sort_by_count
[i
]) == -1)
863 sc_tab
[n
].delta_count
= 0;
864 sc_tab
[n
].waiting
= 0;
865 sc_tab
[n
].delta_wtime_usecs
= 0;
866 sc_tab
[n
].delta_wtime_secs
= 0;
868 for (i
= 1; i
< MAX_FAULTS
; i
++) {
869 faults
[i
].delta_count
= 0;
870 faults
[i
].waiting
= 0;
871 faults
[i
].delta_wtime_usecs
= 0;
872 faults
[i
].delta_wtime_secs
= 0;
878 delta_itime_secs
= 0;
879 delta_itime_usecs
= 0;
880 delta_otime_secs
= 0;
881 delta_otime_usecs
= 0;
889 for (i
= 0; i
< (MAX_SC
+ msgcode_cnt
) ; i
++) {
890 sc_tab
[i
].delta_count
= 0;
891 sc_tab
[i
].total_count
= 0;
892 sc_tab
[i
].waiting
= 0;
893 sc_tab
[i
].delta_wtime_usecs
= 0;
894 sc_tab
[i
].delta_wtime_secs
= 0;
895 sc_tab
[i
].wtime_usecs
= 0;
896 sc_tab
[i
].wtime_secs
= 0;
897 sc_tab
[i
].stime_usecs
= 0;
898 sc_tab
[i
].stime_secs
= 0;
900 for (i
= 1; i
< MAX_FAULTS
; i
++) {
901 faults
[i
].delta_count
= 0;
902 faults
[i
].total_count
= 0;
903 faults
[i
].waiting
= 0;
904 faults
[i
].delta_wtime_usecs
= 0;
905 faults
[i
].delta_wtime_secs
= 0;
906 faults
[i
].wtime_usecs
= 0;
907 faults
[i
].wtime_secs
= 0;
908 faults
[i
].stime_usecs
= 0;
909 faults
[i
].stime_secs
= 0;
921 delta_itime_secs
= 0;
922 delta_itime_usecs
= 0;
925 delta_otime_secs
= 0;
926 delta_otime_usecs
= 0;
930 sc_tab_init(char *codefile
)
938 if ((fp
= fopen(codefile
,"r")) == (FILE *)0) {
939 printf("Failed to open code description file %s\n", codefile
);
943 /* Count Mach message MSG_ codes */
944 for (msgcode_cnt
=0;;) {
945 n
= fscanf(fp
, "%x%55s\n", &code
, &name
[0]);
948 if (strncmp ("MSG_", &name
[0], 4) == 0)
950 if (strcmp("TRACE_LAST_WRAPPER", &name
[0]) == 0)
954 sc_tab
= (struct sc_entry
*)malloc((MAX_SC
+msgcode_cnt
) * sizeof (struct sc_entry
));
956 quit("can't allocate memory for system call table\n");
957 bzero(sc_tab
,(MAX_SC
+msgcode_cnt
) * sizeof (struct sc_entry
));
959 msgcode_tab
= (int *)malloc(msgcode_cnt
* sizeof(int));
961 quit("can't allocate memory for msgcode table\n");
962 bzero(msgcode_tab
,(msgcode_cnt
* sizeof(int)));
964 sort_by_count
= (int *)malloc((MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
966 quit("can't allocate memory for sort_by_count table\n");
967 bzero(sort_by_count
,(MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
969 sort_by_wtime
= (int *)malloc((MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
971 quit("can't allocate memory for sort_by_wtime table\n");
972 bzero(sort_by_wtime
, (MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
978 n
= fscanf(fp
, "%x%55s\n", &code
, &name
[0]);
983 if (strcmp("MACH_vmfault", &name
[0]) == 0) {
987 if (strcmp("MACH_SCHED", &name
[0]) == 0) {
991 if (strcmp("MACH_STKHANDOFF", &name
[0]) == 0) {
992 mach_stkhandoff
= code
;
995 if (strcmp("MACH_IDLE", &name
[0]) == 0) {
999 if (strcmp("VFS_LOOKUP", &name
[0]) == 0) {
1003 if (strcmp("BSC_SysCall", &name
[0]) == 0) {
1007 if (strcmp("MACH_SysCall", &name
[0]) == 0) {
1011 if (strcmp("BSC_exit", &name
[0]) == 0) {
1015 if (strncmp("MSG_", &name
[0], 4) == 0) {
1016 msgcode_tab
[msgcode_indx
] = ((code
& 0x00ffffff) >>2);
1017 n
= MAX_SC
+ msgcode_indx
;
1018 strncpy(&sc_tab
[n
].name
[0], &name
[4], 31 );
1022 if (strncmp("MSC_", &name
[0], 4) == 0) {
1023 n
= 512 + ((code
>>2) & 0x1ff);
1024 strcpy(&sc_tab
[n
].name
[0], &name
[4]);
1027 if (strncmp("BSC_", &name
[0], 4) == 0) {
1028 n
= (code
>>2) & 0x1ff;
1029 strcpy(&sc_tab
[n
].name
[0], &name
[4]);
1032 if (strcmp("TRACE_LAST_WRAPPER", &name
[0]) == 0)
1035 strcpy(&faults
[1].name
[0], "zero_fill");
1036 strcpy(&faults
[2].name
[0], "pagein");
1037 strcpy(&faults
[3].name
[0], "copy_on_write");
1038 strcpy(&faults
[4].name
[0], "cache_hit");
1042 find_proc_names(void)
1045 struct kinfo_proc
*kp
;
1049 mib
[2] = KERN_PROC_ALL
;
1052 if (sysctl(mib
, 4, NULL
, &bufSize
, NULL
, 0) < 0)
1053 quit("trace facility failure, KERN_PROC_ALL\n");
1055 if((kp
= (struct kinfo_proc
*)malloc(bufSize
)) == (struct kinfo_proc
*)0)
1056 quit("can't allocate memory for proc buffer\n");
1058 if (sysctl(mib
, 4, kp
, &bufSize
, NULL
, 0) < 0)
1059 quit("trace facility failure, KERN_PROC_ALL\n");
1061 kp_nentries
= bufSize
/ sizeof(struct kinfo_proc
);
1065 static struct th_info
*
1066 find_thread(uint64_t thread
)
1070 for (ti
= th_state
; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1071 if (ti
->thread
== thread
)
1074 return ((struct th_info
*)0);
1078 cmp_wtime(struct sc_entry
*s1
, struct sc_entry
*s2
)
1080 if (s1
->wtime_secs
< s2
->wtime_secs
)
1082 if (s1
->wtime_secs
> s2
->wtime_secs
)
1084 if (s1
->wtime_usecs
<= s2
->wtime_usecs
)
1092 int i
, n
, k
, cnt
, secs
;
1094 struct sc_entry
*se
;
1098 now
= mach_absolute_time();
1100 for (ti
= th_state
; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1101 if (ti
->thread
== 0)
1105 te
= &ti
->th_entry
[ti
->depth
-1];
1107 if (te
->sc_state
== WAITING
) {
1109 se
= &sc_tab
[te
->code
];
1111 se
= &faults
[DBG_PAGEIN_FAULT
];
1113 se
->wtime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1114 se
->delta_wtime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1115 te
->stime
= (double)now
;
1117 secs
= se
->wtime_usecs
/ 1000000;
1118 se
->wtime_usecs
-= secs
* 1000000;
1119 se
->wtime_secs
+= secs
;
1121 secs
= se
->delta_wtime_usecs
/ 1000000;
1122 se
->delta_wtime_usecs
-= secs
* 1000000;
1123 se
->delta_wtime_secs
+= secs
;
1126 te
= &ti
->th_entry
[0];
1128 if (te
->sc_state
== PREEMPTED
) {
1129 if ((unsigned long)(((double)now
- te
->otime
) / divisor
) > 5000000) {
1132 ti
->pathptr
= (int64_t *)NULL
;
1133 ti
->pathname
[0] = 0;
1139 if ((called
% sort_now
) == 0) {
1140 sort_by_count
[0] = -1;
1141 sort_by_wtime
[0] = -1;
1142 for (cnt
= 1, n
= 1; n
< (MAX_SC
+ msgcode_cnt
); n
++) {
1143 if (sc_tab
[n
].total_count
) {
1144 for (i
= 0; i
< cnt
; i
++) {
1145 if ((k
= sort_by_count
[i
]) == -1 ||
1146 sc_tab
[n
].total_count
> sc_tab
[k
].total_count
) {
1148 for (k
= cnt
- 1; k
>= i
; k
--)
1149 sort_by_count
[k
+1] = sort_by_count
[k
];
1150 sort_by_count
[i
] = n
;
1154 if (how_to_sort
== 0) {
1155 for (i
= 0; i
< cnt
; i
++) {
1156 if ((k
= sort_by_wtime
[i
]) == -1 ||
1157 cmp_wtime(&sc_tab
[n
], &sc_tab
[k
])) {
1159 for (k
= cnt
- 1; k
>= i
; k
--)
1160 sort_by_wtime
[k
+1] = sort_by_wtime
[k
];
1161 sort_by_wtime
[i
] = n
;
1177 mib
[1] = KERN_KDEBUG
;
1178 mib
[2] = KERN_KDENABLE
; /* protocol */
1181 mib
[5] = 0; /* no flags */
1182 if (sysctl(mib
, 4, NULL
, &needed
, NULL
, 0) < 0)
1183 quit("trace facility failure, KERN_KDENABLE\n");
1192 set_numbufs(int nbufs
)
1195 mib
[1] = KERN_KDEBUG
;
1196 mib
[2] = KERN_KDSETBUF
;
1199 mib
[5] = 0; /* no flags */
1200 if (sysctl(mib
, 4, NULL
, &needed
, NULL
, 0) < 0)
1201 quit("trace facility failure, KERN_KDSETBUF\n");
1204 mib
[1] = KERN_KDEBUG
;
1205 mib
[2] = KERN_KDSETUP
;
1208 mib
[5] = 0; /* no flags */
1209 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1210 quit("trace facility failure, KERN_KDSETUP\n");
1215 set_pidcheck(int pid
, int on_off
)
1219 kr
.type
= KDBG_TYPENONE
;
1222 needed
= sizeof(kd_regtype
);
1224 mib
[1] = KERN_KDEBUG
;
1225 mib
[2] = KERN_KDPIDTR
;
1229 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0) {
1231 printf("pid %d does not exist\n", pid
);
1239 get_bufinfo(kbufinfo_t
*val
)
1241 needed
= sizeof (*val
);
1243 mib
[1] = KERN_KDEBUG
;
1244 mib
[2] = KERN_KDGETBUF
;
1247 mib
[5] = 0; /* no flags */
1248 if (sysctl(mib
, 3, val
, &needed
, 0, 0) < 0)
1249 quit("trace facility failure, KERN_KDGETBUF\n");
1261 mib
[1] = KERN_KDEBUG
;
1262 mib
[2] = KERN_KDREMOVE
; /* protocol */
1265 mib
[5] = 0; /* no flags */
1267 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1269 set_remove_flag
= 0;
1272 quit("The trace facility is currently in use...\n Note: fs_usage, sc_usage, and latency use this feature.\n\n");
1274 quit("trace facility failure, KERN_KDREMOVE\n");
1283 kr
.type
= KDBG_RANGETYPE
;
1286 needed
= sizeof(kd_regtype
);
1288 mib
[1] = KERN_KDEBUG
;
1289 mib
[2] = KERN_KDSETREG
;
1292 mib
[5] = 0; /* no flags */
1293 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0)
1294 quit("trace facility failure, KERN_KDSETREG\n");
1297 mib
[1] = KERN_KDEBUG
;
1298 mib
[2] = KERN_KDSETUP
;
1301 mib
[5] = 0; /* no flags */
1302 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1303 quit("trace facility failure, KERN_KDSETUP\n");
1317 get_bufinfo(&bufinfo
);
1319 needed
= bufinfo
.nkdbufs
* sizeof(kd_buf
);
1321 mib
[1] = KERN_KDEBUG
;
1322 mib
[2] = KERN_KDREADTR
;
1327 if (sysctl(mib
, 3, my_buffer
, &needed
, NULL
, 0) < 0)
1328 quit("trace facility failure, KERN_KDREADTR\n");
1332 if (bufinfo
.flags
& KDBG_WRAPPED
) {
1333 for (i
= 0; i
< MAX_THREADS
; i
++) {
1334 th_state
[i
].depth
= 0;
1335 th_state
[i
].thread
= 0;
1336 th_state
[i
].vfslookup
= 0;
1337 th_state
[i
].pathptr
= (int64_t *)NULL
;
1338 th_state
[i
].pathname
[0] = 0;
1346 set_pidcheck(pid
, 1);
1347 set_enable(1); /* re-enable kernel logging */
1349 kd
= (kd_buf
*)my_buffer
;
1351 for (i
= 0; i
< count
; i
++) {
1352 int debugid
, baseid
;
1356 struct th_info
*ti
, *switched_out
, *switched_in
;
1357 struct sc_entry
*se
;
1360 thread
= kd
[i
].arg5
;
1361 debugid
= kd
[i
].debugid
;
1362 type
= kd
[i
].debugid
& DBG_FUNC_MASK
;
1365 switched_out
= (struct th_info
*)0;
1366 switched_in
= (struct th_info
*)0;
1368 now
= kd
[i
].timestamp
& KDBG_TIMESTAMP_MASK
;
1370 baseid
= debugid
& 0xffff0000;
1372 if (type
== vfs_lookup
) {
1375 if ((ti
= find_thread(thread
)) == (struct th_info
*)0)
1378 if (ti
->vfslookup
== 1) {
1380 sargptr
= ti
->pathname
;
1382 *sargptr
++ = kd
[i
].arg2
;
1383 *sargptr
++ = kd
[i
].arg3
;
1384 *sargptr
++ = kd
[i
].arg4
;
1386 * NULL terminate the 'string'
1390 ti
->pathptr
= sargptr
;
1392 } else if (ti
->vfslookup
> 1) {
1394 sargptr
= ti
->pathptr
;
1397 We don't want to overrun our pathname buffer if the
1398 kernel sends us more VFS_LOOKUP entries than we can
1402 if (sargptr
>= &ti
->pathname
[NUMPARMS
])
1406 We need to detect consecutive vfslookup entries.
1407 So, if we get here and find a START entry,
1408 fake the pathptr so we can bypass all further
1412 if (debugid
& DBG_FUNC_START
)
1414 ti
->pathptr
= &ti
->pathname
[NUMPARMS
];
1418 *sargptr
++ = kd
[i
].arg1
;
1419 *sargptr
++ = kd
[i
].arg2
;
1420 *sargptr
++ = kd
[i
].arg3
;
1421 *sargptr
++ = kd
[i
].arg4
;
1423 * NULL terminate the 'string'
1427 ti
->pathptr
= sargptr
;
1431 } else if (baseid
== bsc_base
)
1432 code
= (debugid
>> 2) & 0x1ff;
1433 else if (baseid
== msc_base
)
1434 code
= 512 + ((debugid
>> 2) & 0x1ff);
1435 else if (type
== mach_idle
) {
1436 if (debugid
& DBG_FUNC_START
) {
1437 switched_out
= find_thread(kd
[i
].arg5
);
1441 if (debugid
& DBG_FUNC_END
) {
1442 switched_in
= find_thread(kd
[i
].arg5
);
1447 itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1448 delta_itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1450 } else if (in_other
) {
1451 otime_usecs
+= ((double)now
- other_start
) / divisor
;
1452 delta_otime_usecs
+= ((double)now
- other_start
) / divisor
;
1455 if ( !switched_in
) {
1457 * not one of the target proc's threads
1459 if (now_collect_cpu_time
) {
1461 idle_start
= (double)now
;
1465 if (now_collect_cpu_time
) {
1468 other_start
= (double)now
;
1471 if ( !switched_in
&& !switched_out
)
1475 else if (type
== mach_sched
|| type
== mach_stkhandoff
) {
1476 switched_out
= find_thread(kd
[i
].arg5
);
1477 switched_in
= find_thread(kd
[i
].arg2
);
1480 itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1481 delta_itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1483 } else if (in_other
) {
1484 otime_usecs
+= ((double)now
- other_start
) / divisor
;
1485 delta_otime_usecs
+= ((double)now
- other_start
) / divisor
;
1488 if ( !switched_in
) {
1490 * not one of the target proc's threads
1492 if (now_collect_cpu_time
) {
1494 other_start
= (double)now
;
1497 if ( !switched_in
&& !switched_out
)
1501 else if ((baseid
& 0xff000000) == 0xff000000) {
1502 code
= find_msgcode (debugid
);
1505 } else if (baseid
!= mach_vmfault
)
1508 if (switched_out
|| switched_in
) {
1511 ti
->curpri
= (int)kd
[i
].arg3
;
1514 te
= &ti
->th_entry
[ti
->depth
-1];
1516 if (te
->sc_state
== KERNEL_MODE
)
1517 te
->ctime
+= (double)now
- te
->stime
;
1518 te
->sc_state
= WAITING
;
1523 te
= &ti
->th_entry
[0];
1525 if (te
->sc_state
== USER_MODE
)
1526 utime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1527 te
->sc_state
= PREEMPTED
;
1530 te
->stime
= (double)now
;
1531 te
->otime
= (double)now
;
1532 now_collect_cpu_time
= 1;
1537 ti
->curpri
= (int)kd
[i
].arg4
;
1540 te
= &ti
->th_entry
[ti
->depth
-1];
1542 if (te
->sc_state
== WAITING
)
1543 te
->wtime
+= (double)now
- te
->stime
;
1544 te
->sc_state
= KERNEL_MODE
;
1546 te
= &ti
->th_entry
[0];
1548 te
->sc_state
= USER_MODE
;
1550 te
->stime
= (double)now
;
1551 te
->otime
= (double)now
;
1555 if ((ti
= find_thread(thread
)) == (struct th_info
*)0) {
1556 for (ti
= &th_state
[0]; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1557 if (ti
->thread
== 0) {
1558 ti
->thread
= thread
;
1563 if (ti
== &th_state
[MAX_THREADS
])
1566 if (debugid
& DBG_FUNC_START
) {
1570 te
= &ti
->th_entry
[ti
->depth
-1];
1572 if (te
->sc_state
== KERNEL_MODE
)
1573 te
->ctime
+= (double)now
- te
->stime
;
1575 te
= &ti
->th_entry
[0];
1577 if (te
->sc_state
== USER_MODE
)
1578 utime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1580 te
->stime
= (double)now
;
1581 te
->otime
= (double)now
;
1583 if (ti
->depth
< MAX_NESTED
) {
1584 te
= &ti
->th_entry
[ti
->depth
];
1586 te
->sc_state
= KERNEL_MODE
;
1589 te
->stime
= (double)now
;
1590 te
->otime
= (double)now
;
1591 te
->ctime
= (double)0;
1592 te
->wtime
= (double)0;
1596 } else if (debugid
& DBG_FUNC_END
) {
1601 se
= &faults
[kd
[i
].arg4
];
1604 if (se
->total_count
== 0)
1610 te
= &ti
->th_entry
[ti
->depth
-1];
1612 if (te
->type
== type
) {
1613 se
->stime_usecs
+= te
->ctime
/ divisor
;
1614 se
->stime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1616 se
->wtime_usecs
+= te
->wtime
/ divisor
;
1617 se
->delta_wtime_usecs
+= te
->wtime
/ divisor
;
1619 secs
= se
->stime_usecs
/ 1000000;
1620 se
->stime_usecs
-= secs
* 1000000;
1621 se
->stime_secs
+= secs
;
1623 secs
= se
->wtime_usecs
/ 1000000;
1624 se
->wtime_usecs
-= secs
* 1000000;
1625 se
->wtime_secs
+= secs
;
1627 secs
= se
->delta_wtime_usecs
/ 1000000;
1628 se
->delta_wtime_usecs
-= secs
* 1000000;
1629 se
->delta_wtime_secs
+= secs
;
1633 if (ti
->depth
== 0) {
1635 * headed back to user mode
1636 * start the time accumulation
1638 te
= &ti
->th_entry
[0];
1639 te
->sc_state
= USER_MODE
;
1641 te
= &ti
->th_entry
[ti
->depth
-1];
1643 te
->stime
= (double)now
;
1644 te
->otime
= (double)now
;
1650 if (ti
->depth
== 0) {
1652 * headed back to user mode
1653 * start the time accumulation
1655 te
= &ti
->th_entry
[0];
1656 te
->sc_state
= USER_MODE
;
1657 te
->stime
= (double)now
;
1658 te
->otime
= (double)now
;
1663 secs
= utime_usecs
/ 1000000;
1664 utime_usecs
-= secs
* 1000000;
1667 secs
= itime_usecs
/ 1000000;
1668 itime_usecs
-= secs
* 1000000;
1671 secs
= delta_itime_usecs
/ 1000000;
1672 delta_itime_usecs
-= secs
* 1000000;
1673 delta_itime_secs
+= secs
;
1675 secs
= otime_usecs
/ 1000000;
1676 otime_usecs
-= secs
* 1000000;
1679 secs
= delta_otime_usecs
/ 1000000;
1680 delta_otime_usecs
-= secs
* 1000000;
1681 delta_otime_secs
+= secs
;
1691 This flag is turned off when calling
1692 quit() due to a set_remove() failure.
1694 if (set_remove_flag
)
1697 if (no_screen_refresh
== 0) {
1698 /* clear for new display */
1705 printf("sc_usage: ");
1715 mach_timebase_info_data_t info
;
1717 (void) mach_timebase_info (&info
);
1719 divisor
= ( (double)info
.denom
/ (double)info
.numer
) * 1000;
1732 ret
= (int)strtol(str
, &cp
, 10);
1733 if (cp
== str
|| *cp
) {
1734 /* Assume this is a command string and find first matching pid */
1735 for (i
=0; i
< kp_nentries
; i
++) {
1736 if (kp_buffer
[i
].kp_proc
.p_stat
== 0)
1739 if (!strcmp(str
, kp_buffer
[i
].kp_proc
.p_comm
)) {
1741 kp_buffer
[i
].kp_proc
.p_comm
,
1742 sizeof(proc_name
)-1);
1743 proc_name
[sizeof(proc_name
)-1] = '\0';
1744 return (kp_buffer
[i
].kp_proc
.p_pid
);
1749 for (i
=0; i
< kp_nentries
; i
++) {
1750 if (kp_buffer
[i
].kp_proc
.p_stat
== 0)
1752 else if (kp_buffer
[i
].kp_proc
.p_pid
== ret
) {
1754 kp_buffer
[i
].kp_proc
.p_comm
,
1755 sizeof(proc_name
)-1);
1756 proc_name
[sizeof(proc_name
)-1] = '\0';
1757 return (kp_buffer
[i
].kp_proc
.p_pid
);
1764 /* Returns index into sc_tab for a mach msg entry */
1766 find_msgcode(int debugid
)
1770 for (indx
=0; indx
< msgcode_cnt
; indx
++) {
1771 if (msgcode_tab
[indx
] == ((debugid
& 0x00ffffff) >>2))
1772 return (MAX_SC
+indx
);
1778 argtoi(int flag
, char *req
, char *str
, int base
)
1783 ret
= (int)strtol(str
, &cp
, base
);
1784 if (cp
== str
|| *cp
)
1785 errx(EINVAL
, "-%c flag requires a %s", flag
, req
);