2 * Copyright (c) 1999-2016 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
);
295 main(int argc
, char *argv
[])
297 char *myname
= "sc_usage";
298 char *codefile
= "/usr/share/misc/trace.codes";
301 int delay
= Default_DELAY
;
303 if ( geteuid() != 0 ) {
304 printf("'sc_usage' must be run as root...\n");
308 #if !defined(__arm64__)
309 if (0 != reexec_to_match_kernel()) {
310 fprintf(stderr
, "Could not re-execute: %d\n", errno
);
317 if ((myname
= rindex(argv
[0], '/')) == 0) {
325 while ((ch
= getopt(argc
, argv
, "c:els:d:E")) != EOF
) {
328 delay
= argtoi('s', "decimal number", optarg
, 10);
334 no_screen_refresh
= 1;
343 /* exit_usage(myname);*/
344 exit_usage("default");
350 sc_tab_init(codefile
);
356 /* parse a pid or a command */
357 if((pid
= argtopid(argv
[optind
])) < 0 )
361 { /* execute this command */
366 ptr
= strrchr(argv
[optind
], '/');
372 strncpy(proc_name
, ptr
, sizeof(proc_name
)-1);
373 proc_name
[sizeof(proc_name
)-1] = '\0';
383 fprintf(stderr
, "Starting program: %s\n", argv
[optind
]);
386 switch ((pid
= vfork()))
393 ptrace(0,(pid_t
)0,(caddr_t
)0,0); /* PT_TRACE_ME */
394 execve(argv
[optind
], &argv
[optind
], environ
);
409 if (no_screen_refresh
== 0) {
411 /* initializes curses and screen (last) */
412 if (initscr() == (WINDOW
*) 0)
414 printf("Unrecognized TERM type, try vt100\n");
426 /* set up signal handlers */
427 signal(SIGINT
, leave
);
428 signal(SIGQUIT
, leave
);
429 signal(SIGHUP
, leave
);
430 signal(SIGTERM
, leave
);
431 signal(SIGWINCH
, sigwinch
);
433 if (no_screen_refresh
== 0)
434 topn
= LINES
- Header_lines
;
441 set_numbufs(SAMPLE_SIZE
);
443 set_pidcheck(pid
, 1);
446 ptrace(7, pid
, (caddr_t
)1, 0); /* PT_CONTINUE */
451 if ((sort_now
= 10 / delay
) < 2)
454 get_bufinfo(&bufinfo
);
456 my_buffer
= malloc(bufinfo
.nkdbufs
* sizeof(kd_buf
));
457 if(my_buffer
== (char *) 0)
458 quit("can't allocate memory for tracing info\n");
461 (void)screen_update();
469 for (i
= 0; i
< (10 * delay
) && newLINES
== 0; i
++) {
471 if (no_screen_refresh
== 0) {
472 if ((c
= getch()) != ERR
&& (char)c
== 'q')
484 No need to check for initscr error return.
485 We won't get here if it fails on the first call.
491 topn
= LINES
- Header_lines
;
494 (void)screen_update();
499 print_row(struct sc_entry
*se
, int no_wtime
)
505 sprintf(tbuf
, "%-23.23s %8d(%d)", se
->name
, se
->total_count
, se
->delta_count
);
507 sprintf(tbuf
, "%-23.23s %8d", se
->name
, se
->total_count
);
510 memset(&tbuf
[clen
], ' ', 45 - clen
);
512 print_time(&tbuf
[45], (unsigned int)(se
->stime_usecs
), se
->stime_secs
);
515 if (no_wtime
== 0 && (se
->wtime_usecs
|| se
->wtime_secs
)) {
516 sprintf(&tbuf
[clen
], " ");
517 clen
+= strlen(&tbuf
[clen
]);
519 print_time(&tbuf
[clen
], (unsigned int)(se
->wtime_usecs
), se
->wtime_secs
);
520 clen
+= strlen(&tbuf
[clen
]);
522 if (se
->waiting
|| se
->delta_wtime_usecs
|| se
->delta_wtime_secs
) {
524 sprintf(&tbuf
[clen
], "(");
525 clen
+= strlen(&tbuf
[clen
]);
527 print_time(&tbuf
[clen
], (unsigned int)(se
->delta_wtime_usecs
),
528 se
->delta_wtime_secs
);
529 clen
+= strlen(&tbuf
[clen
]);
531 sprintf(&tbuf
[clen
], ")");
532 clen
+= strlen(&tbuf
[clen
]);
535 if (se
->waiting
== 1)
536 sprintf(&tbuf
[clen
], " W");
538 sprintf(&tbuf
[clen
], " %d", se
->waiting
);
539 clen
+= strlen(&tbuf
[clen
]);
543 sprintf(&tbuf
[clen
], "\n");
544 if (no_screen_refresh
)
567 if (no_screen_refresh
== 0) {
568 /* clear for new display */
574 sprintf(tbuf
, "%-14.14s", proc_name
);
582 p2
= "context switch ";
584 p2
= "context switches";
585 if (num_of_threads
== 1)
590 sprintf(&tbuf
[clen
], " %4d %s %4d %s %4d %s",
591 preempted
, p1
, csw
, p2
, num_of_threads
, p3
);
592 clen
+= strlen(&tbuf
[clen
]);
595 * Display the current time.
596 * "ctime" always returns a string that looks like this:
598 * Sun Sep 16 01:03:52 1973
599 * 012345678901234567890123
602 * We want indices 11 thru 18 (length 8).
604 curr_time
= time((long *)0);
607 start_time
= curr_time
;
609 elapsed_secs
= curr_time
- start_time
;
610 minutes
= elapsed_secs
/ 60;
611 hours
= minutes
/ 60;
613 memset(&tbuf
[clen
], ' ', 78 - clen
);
617 sprintf(&tbuf
[clen
], "%-8.8s\n", &(ctime(&curr_time
)[11]));
618 if (no_screen_refresh
)
623 if (total_faults
== 1)
631 sprintf(tbuf
, " %4d %s %4d %s",
632 total_faults
, p1
, scalls
, p2
);
635 sprintf(&tbuf
[clen
], " %3ld:%02ld:%02ld\n",
636 hours
, minutes
% 60, elapsed_secs
% 60);
637 if (no_screen_refresh
)
644 sprintf(tbuf
, "\nTYPE NUMBER CPU_TIME WAIT_TIME\n");
645 if (no_screen_refresh
)
650 sprintf(tbuf
, "------------------------------------------------------------------------------\n");
651 if (no_screen_refresh
)
659 sprintf(tbuf
, "System Idle ");
662 print_time(&tbuf
[clen
], itime_usecs
, itime_secs
);
663 clen
+= strlen(&tbuf
[clen
]);
665 if (delta_itime_usecs
|| delta_itime_secs
) {
667 sprintf(&tbuf
[clen
], "(");
668 clen
+= strlen(&tbuf
[clen
]);
670 print_time(&tbuf
[clen
], delta_itime_usecs
, delta_itime_secs
);
671 clen
+= strlen(&tbuf
[clen
]);
673 sprintf(&tbuf
[clen
], ")");
674 clen
+= strlen(&tbuf
[clen
]);
676 sprintf(&tbuf
[clen
], "\n");
677 if (no_screen_refresh
)
685 sprintf(tbuf
, "System Busy ");
688 print_time(&tbuf
[clen
], otime_usecs
, otime_secs
);
689 clen
+= strlen(&tbuf
[clen
]);
691 if (delta_otime_usecs
|| delta_otime_secs
) {
693 sprintf(&tbuf
[clen
], "(");
694 clen
+= strlen(&tbuf
[clen
]);
696 print_time(&tbuf
[clen
], delta_otime_usecs
, delta_otime_secs
);
697 clen
+= strlen(&tbuf
[clen
]);
699 sprintf(&tbuf
[clen
], ")");
700 clen
+= strlen(&tbuf
[clen
]);
702 sprintf(&tbuf
[clen
], "\n");
703 if (no_screen_refresh
)
710 sprintf(tbuf
, "%-14.14s Usermode ", proc_name
);
713 print_time(&tbuf
[clen
], utime_usecs
, utime_secs
);
714 clen
+= strlen(&tbuf
[clen
]);
716 sprintf(&tbuf
[clen
], "\n");
717 if (no_screen_refresh
)
724 max_rows
= topn
- (num_of_threads
+ 3);
728 for (output_lf
= 1, n
= 1; rows
< max_rows
&& n
< MAX_FAULTS
; n
++) {
731 if (se
->total_count
== 0)
733 if (output_lf
== 1) {
735 if (no_screen_refresh
)
741 if (rows
>= max_rows
)
750 if (no_screen_refresh
)
756 for (i
= 0; rows
< max_rows
; i
++) {
758 n
= sort_by_count
[i
];
760 n
= sort_by_wtime
[i
];
763 print_row(&sc_tab
[n
], 0);
769 if (no_screen_refresh
== 0) {
770 while (rows
++ < max_rows
)
775 if (num_of_threads
) {
776 sprintf(tbuf
, "\nCURRENT_TYPE LAST_PATHNAME_WAITED_FOR CUR_WAIT_TIME THRD# PRI\n");
778 if (no_screen_refresh
)
783 sprintf(tbuf
, "------------------------------------------------------------------------------\n");
784 if (no_screen_refresh
)
791 for (i
= 0; i
< num_of_threads
; i
++, ti
++) {
795 int secs
, time_secs
, time_usecs
;
797 now
= mach_absolute_time();
799 while (ti
->thread
== 0 && ti
< &th_state
[MAX_THREADS
])
801 if (ti
== &th_state
[MAX_THREADS
])
805 te
= &ti
->th_entry
[ti
->depth
- 1];
807 if (te
->sc_state
== WAITING
) {
809 sprintf(tbuf
, "%-23.23s", sc_tab
[te
->code
].name
);
811 sprintf(tbuf
, "%-23.23s", "vm_fault");
813 sprintf(tbuf
, "%-23.23s", state_name
[te
->sc_state
]);
815 te
= &ti
->th_entry
[0];
816 sprintf(tbuf
, "%-23.23s", state_name
[te
->sc_state
]);
820 /* print the tail end of the pathname */
821 p
= (char *)ti
->pathname
;
828 sprintf(&tbuf
[clen
], " %-26.26s ", &p
[plen
]);
830 clen
+= strlen(&tbuf
[clen
]);
832 time_usecs
= (((double)now
- te
->otime
) / divisor
);
833 secs
= time_usecs
/ 1000000;
834 time_usecs
-= secs
* 1000000;
837 print_time(&tbuf
[clen
], time_usecs
, time_secs
);
838 clen
+= strlen(&tbuf
[clen
]);
839 sprintf(&tbuf
[clen
], " %2d %3d\n", i
, ti
->curpri
);
840 if (no_screen_refresh
)
845 if (no_screen_refresh
== 0) {
849 printf("\n=================\n");
853 for (i
= 0; i
< (MAX_SC
+ msgcode_cnt
); i
++) {
854 if ((n
= sort_by_count
[i
]) == -1)
856 sc_tab
[n
].delta_count
= 0;
857 sc_tab
[n
].waiting
= 0;
858 sc_tab
[n
].delta_wtime_usecs
= 0;
859 sc_tab
[n
].delta_wtime_secs
= 0;
861 for (i
= 1; i
< MAX_FAULTS
; i
++) {
862 faults
[i
].delta_count
= 0;
863 faults
[i
].waiting
= 0;
864 faults
[i
].delta_wtime_usecs
= 0;
865 faults
[i
].delta_wtime_secs
= 0;
871 delta_itime_secs
= 0;
872 delta_itime_usecs
= 0;
873 delta_otime_secs
= 0;
874 delta_otime_usecs
= 0;
882 for (i
= 0; i
< (MAX_SC
+ msgcode_cnt
) ; i
++) {
883 sc_tab
[i
].delta_count
= 0;
884 sc_tab
[i
].total_count
= 0;
885 sc_tab
[i
].waiting
= 0;
886 sc_tab
[i
].delta_wtime_usecs
= 0;
887 sc_tab
[i
].delta_wtime_secs
= 0;
888 sc_tab
[i
].wtime_usecs
= 0;
889 sc_tab
[i
].wtime_secs
= 0;
890 sc_tab
[i
].stime_usecs
= 0;
891 sc_tab
[i
].stime_secs
= 0;
893 for (i
= 1; i
< MAX_FAULTS
; i
++) {
894 faults
[i
].delta_count
= 0;
895 faults
[i
].total_count
= 0;
896 faults
[i
].waiting
= 0;
897 faults
[i
].delta_wtime_usecs
= 0;
898 faults
[i
].delta_wtime_secs
= 0;
899 faults
[i
].wtime_usecs
= 0;
900 faults
[i
].wtime_secs
= 0;
901 faults
[i
].stime_usecs
= 0;
902 faults
[i
].stime_secs
= 0;
914 delta_itime_secs
= 0;
915 delta_itime_usecs
= 0;
918 delta_otime_secs
= 0;
919 delta_otime_usecs
= 0;
923 sc_tab_init(char *codefile
)
931 if ((fp
= fopen(codefile
,"r")) == (FILE *)0) {
932 printf("Failed to open code description file %s\n", codefile
);
936 /* Count Mach message MSG_ codes */
937 for (msgcode_cnt
=0;;) {
938 n
= fscanf(fp
, "%x%55s\n", &code
, &name
[0]);
941 if (strncmp ("MSG_", &name
[0], 4) == 0)
943 if (strcmp("TRACE_LAST_WRAPPER", &name
[0]) == 0)
947 sc_tab
= (struct sc_entry
*)malloc((MAX_SC
+msgcode_cnt
) * sizeof (struct sc_entry
));
949 quit("can't allocate memory for system call table\n");
950 bzero(sc_tab
,(MAX_SC
+msgcode_cnt
) * sizeof (struct sc_entry
));
952 msgcode_tab
= (int *)malloc(msgcode_cnt
* sizeof(int));
954 quit("can't allocate memory for msgcode table\n");
955 bzero(msgcode_tab
,(msgcode_cnt
* sizeof(int)));
957 sort_by_count
= (int *)malloc((MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
959 quit("can't allocate memory for sort_by_count table\n");
960 bzero(sort_by_count
,(MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
962 sort_by_wtime
= (int *)malloc((MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
964 quit("can't allocate memory for sort_by_wtime table\n");
965 bzero(sort_by_wtime
, (MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
971 n
= fscanf(fp
, "%x%55s\n", &code
, &name
[0]);
976 if (strcmp("MACH_vmfault", &name
[0]) == 0) {
980 if (strcmp("MACH_SCHED", &name
[0]) == 0) {
984 if (strcmp("MACH_STKHANDOFF", &name
[0]) == 0) {
985 mach_stkhandoff
= code
;
988 if (strcmp("MACH_IDLE", &name
[0]) == 0) {
992 if (strcmp("VFS_LOOKUP", &name
[0]) == 0) {
996 if (strcmp("BSC_SysCall", &name
[0]) == 0) {
1000 if (strcmp("MACH_SysCall", &name
[0]) == 0) {
1004 if (strcmp("BSC_exit", &name
[0]) == 0) {
1008 if (strncmp("MSG_", &name
[0], 4) == 0) {
1009 msgcode_tab
[msgcode_indx
] = ((code
& 0x00ffffff) >>2);
1010 n
= MAX_SC
+ msgcode_indx
;
1011 strncpy(&sc_tab
[n
].name
[0], &name
[4], 31 );
1015 if (strncmp("MSC_", &name
[0], 4) == 0) {
1016 n
= 512 + ((code
>>2) & 0x1ff);
1017 strcpy(&sc_tab
[n
].name
[0], &name
[4]);
1020 if (strncmp("BSC_", &name
[0], 4) == 0) {
1021 n
= (code
>>2) & 0x1ff;
1022 strcpy(&sc_tab
[n
].name
[0], &name
[4]);
1025 if (strcmp("TRACE_LAST_WRAPPER", &name
[0]) == 0)
1028 strcpy(&faults
[1].name
[0], "zero_fill");
1029 strcpy(&faults
[2].name
[0], "pagein");
1030 strcpy(&faults
[3].name
[0], "copy_on_write");
1031 strcpy(&faults
[4].name
[0], "cache_hit");
1035 find_proc_names(void)
1038 struct kinfo_proc
*kp
;
1042 mib
[2] = KERN_PROC_ALL
;
1045 if (sysctl(mib
, 4, NULL
, &bufSize
, NULL
, 0) < 0)
1046 quit("trace facility failure, KERN_PROC_ALL\n");
1048 if((kp
= (struct kinfo_proc
*)malloc(bufSize
)) == (struct kinfo_proc
*)0)
1049 quit("can't allocate memory for proc buffer\n");
1051 if (sysctl(mib
, 4, kp
, &bufSize
, NULL
, 0) < 0)
1052 quit("trace facility failure, KERN_PROC_ALL\n");
1054 kp_nentries
= bufSize
/ sizeof(struct kinfo_proc
);
1058 static struct th_info
*
1059 find_thread(uint64_t thread
)
1063 for (ti
= th_state
; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1064 if (ti
->thread
== thread
)
1067 return ((struct th_info
*)0);
1071 cmp_wtime(struct sc_entry
*s1
, struct sc_entry
*s2
)
1073 if (s1
->wtime_secs
< s2
->wtime_secs
)
1075 if (s1
->wtime_secs
> s2
->wtime_secs
)
1077 if (s1
->wtime_usecs
<= s2
->wtime_usecs
)
1085 int i
, n
, k
, cnt
, secs
;
1087 struct sc_entry
*se
;
1091 now
= mach_absolute_time();
1093 for (ti
= th_state
; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1094 if (ti
->thread
== 0)
1098 te
= &ti
->th_entry
[ti
->depth
-1];
1100 if (te
->sc_state
== WAITING
) {
1102 se
= &sc_tab
[te
->code
];
1104 se
= &faults
[DBG_PAGEIN_FAULT
];
1106 se
->wtime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1107 se
->delta_wtime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1108 te
->stime
= (double)now
;
1110 secs
= se
->wtime_usecs
/ 1000000;
1111 se
->wtime_usecs
-= secs
* 1000000;
1112 se
->wtime_secs
+= secs
;
1114 secs
= se
->delta_wtime_usecs
/ 1000000;
1115 se
->delta_wtime_usecs
-= secs
* 1000000;
1116 se
->delta_wtime_secs
+= secs
;
1119 te
= &ti
->th_entry
[0];
1121 if (te
->sc_state
== PREEMPTED
) {
1122 if ((unsigned long)(((double)now
- te
->otime
) / divisor
) > 5000000) {
1125 ti
->pathptr
= (int64_t *)NULL
;
1126 ti
->pathname
[0] = 0;
1132 if ((called
% sort_now
) == 0) {
1133 sort_by_count
[0] = -1;
1134 sort_by_wtime
[0] = -1;
1135 for (cnt
= 1, n
= 1; n
< (MAX_SC
+ msgcode_cnt
); n
++) {
1136 if (sc_tab
[n
].total_count
) {
1137 for (i
= 0; i
< cnt
; i
++) {
1138 if ((k
= sort_by_count
[i
]) == -1 ||
1139 sc_tab
[n
].total_count
> sc_tab
[k
].total_count
) {
1141 for (k
= cnt
- 1; k
>= i
; k
--)
1142 sort_by_count
[k
+1] = sort_by_count
[k
];
1143 sort_by_count
[i
] = n
;
1147 if (how_to_sort
== 0) {
1148 for (i
= 0; i
< cnt
; i
++) {
1149 if ((k
= sort_by_wtime
[i
]) == -1 ||
1150 cmp_wtime(&sc_tab
[n
], &sc_tab
[k
])) {
1152 for (k
= cnt
- 1; k
>= i
; k
--)
1153 sort_by_wtime
[k
+1] = sort_by_wtime
[k
];
1154 sort_by_wtime
[i
] = n
;
1170 mib
[1] = KERN_KDEBUG
;
1171 mib
[2] = KERN_KDENABLE
; /* protocol */
1174 mib
[5] = 0; /* no flags */
1175 if (sysctl(mib
, 4, NULL
, &needed
, NULL
, 0) < 0)
1176 quit("trace facility failure, KERN_KDENABLE\n");
1185 set_numbufs(int nbufs
)
1188 mib
[1] = KERN_KDEBUG
;
1189 mib
[2] = KERN_KDSETBUF
;
1192 mib
[5] = 0; /* no flags */
1193 if (sysctl(mib
, 4, NULL
, &needed
, NULL
, 0) < 0)
1194 quit("trace facility failure, KERN_KDSETBUF\n");
1197 mib
[1] = KERN_KDEBUG
;
1198 mib
[2] = KERN_KDSETUP
;
1201 mib
[5] = 0; /* no flags */
1202 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1203 quit("trace facility failure, KERN_KDSETUP\n");
1208 set_pidcheck(int pid
, int on_off
)
1212 kr
.type
= KDBG_TYPENONE
;
1215 needed
= sizeof(kd_regtype
);
1217 mib
[1] = KERN_KDEBUG
;
1218 mib
[2] = KERN_KDPIDTR
;
1222 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0) {
1224 printf("pid %d does not exist\n", pid
);
1232 get_bufinfo(kbufinfo_t
*val
)
1234 needed
= sizeof (*val
);
1236 mib
[1] = KERN_KDEBUG
;
1237 mib
[2] = KERN_KDGETBUF
;
1240 mib
[5] = 0; /* no flags */
1241 if (sysctl(mib
, 3, val
, &needed
, 0, 0) < 0)
1242 quit("trace facility failure, KERN_KDGETBUF\n");
1254 mib
[1] = KERN_KDEBUG
;
1255 mib
[2] = KERN_KDREMOVE
; /* protocol */
1258 mib
[5] = 0; /* no flags */
1260 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1262 set_remove_flag
= 0;
1265 quit("The trace facility is currently in use...\n Note: fs_usage, sc_usage, and latency use this feature.\n\n");
1267 quit("trace facility failure, KERN_KDREMOVE\n");
1276 kr
.type
= KDBG_RANGETYPE
;
1279 needed
= sizeof(kd_regtype
);
1281 mib
[1] = KERN_KDEBUG
;
1282 mib
[2] = KERN_KDSETREG
;
1285 mib
[5] = 0; /* no flags */
1286 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0)
1287 quit("trace facility failure, KERN_KDSETREG\n");
1290 mib
[1] = KERN_KDEBUG
;
1291 mib
[2] = KERN_KDSETUP
;
1294 mib
[5] = 0; /* no flags */
1295 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1296 quit("trace facility failure, KERN_KDSETUP\n");
1310 get_bufinfo(&bufinfo
);
1312 needed
= bufinfo
.nkdbufs
* sizeof(kd_buf
);
1314 mib
[1] = KERN_KDEBUG
;
1315 mib
[2] = KERN_KDREADTR
;
1320 if (sysctl(mib
, 3, my_buffer
, &needed
, NULL
, 0) < 0)
1321 quit("trace facility failure, KERN_KDREADTR\n");
1325 if (bufinfo
.flags
& KDBG_WRAPPED
) {
1326 for (i
= 0; i
< MAX_THREADS
; i
++) {
1327 th_state
[i
].depth
= 0;
1328 th_state
[i
].thread
= 0;
1329 th_state
[i
].vfslookup
= 0;
1330 th_state
[i
].pathptr
= (int64_t *)NULL
;
1331 th_state
[i
].pathname
[0] = 0;
1339 set_pidcheck(pid
, 1);
1340 set_enable(1); /* re-enable kernel logging */
1342 kd
= (kd_buf
*)my_buffer
;
1344 for (i
= 0; i
< count
; i
++) {
1345 int debugid
, baseid
;
1349 struct th_info
*ti
, *switched_out
, *switched_in
;
1350 struct sc_entry
*se
;
1353 thread
= kd
[i
].arg5
;
1354 debugid
= kd
[i
].debugid
;
1355 type
= kd
[i
].debugid
& DBG_FUNC_MASK
;
1358 switched_out
= (struct th_info
*)0;
1359 switched_in
= (struct th_info
*)0;
1361 now
= kd
[i
].timestamp
& KDBG_TIMESTAMP_MASK
;
1363 baseid
= debugid
& 0xffff0000;
1365 if (type
== vfs_lookup
) {
1368 if ((ti
= find_thread(thread
)) == (struct th_info
*)0)
1371 if (ti
->vfslookup
== 1) {
1373 sargptr
= ti
->pathname
;
1375 *sargptr
++ = kd
[i
].arg2
;
1376 *sargptr
++ = kd
[i
].arg3
;
1377 *sargptr
++ = kd
[i
].arg4
;
1379 * NULL terminate the 'string'
1383 ti
->pathptr
= sargptr
;
1385 } else if (ti
->vfslookup
> 1) {
1387 sargptr
= ti
->pathptr
;
1390 We don't want to overrun our pathname buffer if the
1391 kernel sends us more VFS_LOOKUP entries than we can
1395 if (sargptr
>= &ti
->pathname
[NUMPARMS
])
1399 We need to detect consecutive vfslookup entries.
1400 So, if we get here and find a START entry,
1401 fake the pathptr so we can bypass all further
1405 if (debugid
& DBG_FUNC_START
)
1407 ti
->pathptr
= &ti
->pathname
[NUMPARMS
];
1411 *sargptr
++ = kd
[i
].arg1
;
1412 *sargptr
++ = kd
[i
].arg2
;
1413 *sargptr
++ = kd
[i
].arg3
;
1414 *sargptr
++ = kd
[i
].arg4
;
1416 * NULL terminate the 'string'
1420 ti
->pathptr
= sargptr
;
1424 } else if (baseid
== bsc_base
)
1425 code
= (debugid
>> 2) & 0x1ff;
1426 else if (baseid
== msc_base
)
1427 code
= 512 + ((debugid
>> 2) & 0x1ff);
1428 else if (type
== mach_idle
) {
1429 if (debugid
& DBG_FUNC_START
) {
1430 switched_out
= find_thread(kd
[i
].arg5
);
1434 if (debugid
& DBG_FUNC_END
) {
1435 switched_in
= find_thread(kd
[i
].arg5
);
1440 itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1441 delta_itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1443 } else if (in_other
) {
1444 otime_usecs
+= ((double)now
- other_start
) / divisor
;
1445 delta_otime_usecs
+= ((double)now
- other_start
) / divisor
;
1448 if ( !switched_in
) {
1450 * not one of the target proc's threads
1452 if (now_collect_cpu_time
) {
1454 idle_start
= (double)now
;
1458 if (now_collect_cpu_time
) {
1461 other_start
= (double)now
;
1464 if ( !switched_in
&& !switched_out
)
1468 else if (type
== mach_sched
|| type
== mach_stkhandoff
) {
1469 switched_out
= find_thread(kd
[i
].arg5
);
1470 switched_in
= find_thread(kd
[i
].arg2
);
1473 itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1474 delta_itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1476 } else if (in_other
) {
1477 otime_usecs
+= ((double)now
- other_start
) / divisor
;
1478 delta_otime_usecs
+= ((double)now
- other_start
) / divisor
;
1481 if ( !switched_in
) {
1483 * not one of the target proc's threads
1485 if (now_collect_cpu_time
) {
1487 other_start
= (double)now
;
1490 if ( !switched_in
&& !switched_out
)
1494 else if ((baseid
& 0xff000000) == 0xff000000) {
1495 code
= find_msgcode (debugid
);
1498 } else if (baseid
!= mach_vmfault
)
1501 if (switched_out
|| switched_in
) {
1504 ti
->curpri
= (int)kd
[i
].arg3
;
1507 te
= &ti
->th_entry
[ti
->depth
-1];
1509 if (te
->sc_state
== KERNEL_MODE
)
1510 te
->ctime
+= (double)now
- te
->stime
;
1511 te
->sc_state
= WAITING
;
1516 te
= &ti
->th_entry
[0];
1518 if (te
->sc_state
== USER_MODE
)
1519 utime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1520 te
->sc_state
= PREEMPTED
;
1523 te
->stime
= (double)now
;
1524 te
->otime
= (double)now
;
1525 now_collect_cpu_time
= 1;
1530 ti
->curpri
= (int)kd
[i
].arg4
;
1533 te
= &ti
->th_entry
[ti
->depth
-1];
1535 if (te
->sc_state
== WAITING
)
1536 te
->wtime
+= (double)now
- te
->stime
;
1537 te
->sc_state
= KERNEL_MODE
;
1539 te
= &ti
->th_entry
[0];
1541 te
->sc_state
= USER_MODE
;
1543 te
->stime
= (double)now
;
1544 te
->otime
= (double)now
;
1548 if ((ti
= find_thread(thread
)) == (struct th_info
*)0) {
1549 for (ti
= &th_state
[0]; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1550 if (ti
->thread
== 0) {
1551 ti
->thread
= thread
;
1556 if (ti
== &th_state
[MAX_THREADS
])
1559 if (debugid
& DBG_FUNC_START
) {
1563 te
= &ti
->th_entry
[ti
->depth
-1];
1565 if (te
->sc_state
== KERNEL_MODE
)
1566 te
->ctime
+= (double)now
- te
->stime
;
1568 te
= &ti
->th_entry
[0];
1570 if (te
->sc_state
== USER_MODE
)
1571 utime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1573 te
->stime
= (double)now
;
1574 te
->otime
= (double)now
;
1576 if (ti
->depth
< MAX_NESTED
) {
1577 te
= &ti
->th_entry
[ti
->depth
];
1579 te
->sc_state
= KERNEL_MODE
;
1582 te
->stime
= (double)now
;
1583 te
->otime
= (double)now
;
1584 te
->ctime
= (double)0;
1585 te
->wtime
= (double)0;
1589 } else if (debugid
& DBG_FUNC_END
) {
1594 se
= &faults
[kd
[i
].arg4
];
1597 if (se
->total_count
== 0)
1603 te
= &ti
->th_entry
[ti
->depth
-1];
1605 if (te
->type
== type
) {
1606 se
->stime_usecs
+= te
->ctime
/ divisor
;
1607 se
->stime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1609 se
->wtime_usecs
+= te
->wtime
/ divisor
;
1610 se
->delta_wtime_usecs
+= te
->wtime
/ divisor
;
1612 secs
= se
->stime_usecs
/ 1000000;
1613 se
->stime_usecs
-= secs
* 1000000;
1614 se
->stime_secs
+= secs
;
1616 secs
= se
->wtime_usecs
/ 1000000;
1617 se
->wtime_usecs
-= secs
* 1000000;
1618 se
->wtime_secs
+= secs
;
1620 secs
= se
->delta_wtime_usecs
/ 1000000;
1621 se
->delta_wtime_usecs
-= secs
* 1000000;
1622 se
->delta_wtime_secs
+= secs
;
1626 if (ti
->depth
== 0) {
1628 * headed back to user mode
1629 * start the time accumulation
1631 te
= &ti
->th_entry
[0];
1632 te
->sc_state
= USER_MODE
;
1634 te
= &ti
->th_entry
[ti
->depth
-1];
1636 te
->stime
= (double)now
;
1637 te
->otime
= (double)now
;
1643 if (ti
->depth
== 0) {
1645 * headed back to user mode
1646 * start the time accumulation
1648 te
= &ti
->th_entry
[0];
1649 te
->sc_state
= USER_MODE
;
1650 te
->stime
= (double)now
;
1651 te
->otime
= (double)now
;
1656 secs
= utime_usecs
/ 1000000;
1657 utime_usecs
-= secs
* 1000000;
1660 secs
= itime_usecs
/ 1000000;
1661 itime_usecs
-= secs
* 1000000;
1664 secs
= delta_itime_usecs
/ 1000000;
1665 delta_itime_usecs
-= secs
* 1000000;
1666 delta_itime_secs
+= secs
;
1668 secs
= otime_usecs
/ 1000000;
1669 otime_usecs
-= secs
* 1000000;
1672 secs
= delta_otime_usecs
/ 1000000;
1673 delta_otime_usecs
-= secs
* 1000000;
1674 delta_otime_secs
+= secs
;
1684 This flag is turned off when calling
1685 quit() due to a set_remove() failure.
1687 if (set_remove_flag
)
1690 if (no_screen_refresh
== 0) {
1691 /* clear for new display */
1698 printf("sc_usage: ");
1708 mach_timebase_info_data_t info
;
1710 (void) mach_timebase_info (&info
);
1712 divisor
= ( (double)info
.denom
/ (double)info
.numer
) * 1000;
1725 ret
= (int)strtol(str
, &cp
, 10);
1726 if (cp
== str
|| *cp
) {
1727 /* Assume this is a command string and find first matching pid */
1728 for (i
=0; i
< kp_nentries
; i
++) {
1729 if (kp_buffer
[i
].kp_proc
.p_stat
== 0)
1732 if (!strcmp(str
, kp_buffer
[i
].kp_proc
.p_comm
)) {
1734 kp_buffer
[i
].kp_proc
.p_comm
,
1735 sizeof(proc_name
)-1);
1736 proc_name
[sizeof(proc_name
)-1] = '\0';
1737 return (kp_buffer
[i
].kp_proc
.p_pid
);
1742 for (i
=0; i
< kp_nentries
; i
++) {
1743 if (kp_buffer
[i
].kp_proc
.p_stat
== 0)
1745 else if (kp_buffer
[i
].kp_proc
.p_pid
== ret
) {
1747 kp_buffer
[i
].kp_proc
.p_comm
,
1748 sizeof(proc_name
)-1);
1749 proc_name
[sizeof(proc_name
)-1] = '\0';
1750 return (kp_buffer
[i
].kp_proc
.p_pid
);
1757 /* Returns index into sc_tab for a mach msg entry */
1759 find_msgcode(int debugid
)
1763 for (indx
=0; indx
< msgcode_cnt
; indx
++) {
1764 if (msgcode_tab
[indx
] == ((debugid
& 0x00ffffff) >>2))
1765 return (MAX_SC
+indx
);
1771 argtoi(int flag
, char *req
, char *str
, int base
)
1776 ret
= (int)strtol(str
, &cp
, base
);
1777 if (cp
== str
|| *cp
)
1778 errx(EINVAL
, "-%c flag requires a %s", flag
, req
);