2 * Copyright (c) 1999-2007 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 long 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
;
202 kbufinfo_t bufinfo
= {0, 0, 0, 0};
204 int trace_enabled
= 0;
205 int set_remove_flag
= 1;
207 struct kinfo_proc
*kp_buffer
= 0;
210 extern char **environ
;
218 int argtopid(char *);
219 int argtoi(int, char*, char*, int);
226 void leave() /* exit under normal conditions -- INT handler */
229 if (no_screen_refresh
== 0) {
235 set_pidcheck(pid
, 0);
240 void err_leave(s
) /* exit under error conditions */
244 if (no_screen_refresh
== 0) {
250 printf("sc_usage: ");
255 set_pidcheck(pid
, 0);
263 if (no_screen_refresh
== 0)
268 exit_usage(char *myname
) {
270 fprintf(stderr
, "Usage: %s [-c codefile] [-e] [-l] [-sn] pid | cmd | -E execute path\n", myname
);
271 fprintf(stderr
, " -c name of codefile containing mappings for syscalls\n");
272 fprintf(stderr
, " Default is /usr/share/misc/trace.codes\n");
273 fprintf(stderr
, " -e enable sort by call count\n");
274 fprintf(stderr
, " -l turn off top style output\n");
275 fprintf(stderr
, " -sn change sample rate to every n seconds\n");
276 fprintf(stderr
, " pid selects process to sample\n");
277 fprintf(stderr
, " cmd selects command to sample\n");
278 fprintf(stderr
, " -E Execute the given path and optional arguments\n");
284 #define usec_to_1000ths(t) ((t) / 1000)
286 void print_time(char *p
, unsigned int useconds
, unsigned int seconds
)
290 minutes
= seconds
/ 60;
291 hours
= minutes
/ 60;
293 if (minutes
< 100) { // up to 100 minutes
294 sprintf(p
, "%2ld:%02ld.%03ld", minutes
, (unsigned long)(seconds
% 60),
295 (unsigned long)usec_to_1000ths(useconds
));
297 else if (hours
< 100) { // up to 100 hours
298 sprintf(p
, "%2ld:%02ld:%02ld ", hours
, (minutes
% 60),
299 (unsigned long)(seconds
% 60));
302 sprintf(p
, "%4ld hrs ", hours
);
311 char *myname
= "sc_usage";
312 char *codefile
= "/usr/share/misc/trace.codes";
315 int delay
= Default_DELAY
;
316 void screen_update();
320 void reset_counters();
322 if ( geteuid() != 0 ) {
323 printf("'sc_usage' must be run as root...\n");
329 if ((myname
= rindex(argv
[0], '/')) == 0) {
337 while ((ch
= getopt(argc
, argv
, "c:els:d:E")) != EOF
) {
340 delay
= argtoi('s', "decimal number", optarg
, 10);
346 no_screen_refresh
= 1;
355 /* exit_usage(myname);*/
356 exit_usage("default");
362 sc_tab_init(codefile
);
368 /* parse a pid or a command */
369 if((pid
= argtopid(argv
[optind
])) < 0 )
373 { /* execute this command */
378 ptr
= strrchr(argv
[optind
], '/');
384 strncpy(proc_name
, ptr
, sizeof(proc_name
)-1);
385 proc_name
[sizeof(proc_name
)-1] = '\0';
395 fprintf(stderr
, "Starting program: %s\n", argv
[optind
]);
398 switch ((pid
= vfork()))
405 ptrace(0,(pid_t
)0,(caddr_t
)0,0); /* PT_TRACE_ME */
406 execve(argv
[optind
], &argv
[optind
], environ
);
421 if (no_screen_refresh
== 0) {
423 /* initializes curses and screen (last) */
424 if (initscr() == (WINDOW
*) 0)
426 printf("Unrecognized TERM type, try vt100\n");
438 /* set up signal handlers */
439 signal(SIGINT
, leave
);
440 signal(SIGQUIT
, leave
);
441 signal(SIGHUP
, leave
);
442 signal(SIGTERM
, leave
);
443 signal(SIGWINCH
, sigwinch
);
445 if (no_screen_refresh
== 0)
446 topn
= LINES
- Header_lines
;
451 if ((my_buffer
= malloc(SAMPLE_SIZE
* sizeof(kd_buf
))) == (char *)0)
452 quit("can't allocate memory for tracing info\n");
455 set_numbufs(SAMPLE_SIZE
);
457 set_pidcheck(pid
, 1);
460 ptrace(7, pid
, (caddr_t
)1, 0); /* PT_CONTINUE */
465 if ((sort_now
= 10 / delay
) < 2)
469 (void)screen_update();
478 for (i
= 0; i
< (10 * delay
) && newLINES
== 0; i
++) {
480 if (no_screen_refresh
== 0) {
481 if ((c
= getch()) != ERR
&& (char)c
== 'q')
493 No need to check for initscr error return.
494 We won't get here if it fails on the first call.
500 topn
= LINES
- Header_lines
;
503 (void)screen_update();
508 print_row(struct sc_entry
*se
, int no_wtime
) {
513 sprintf(tbuf
, "%-23.23s %8d(%d)", se
->name
, se
->total_count
, se
->delta_count
);
515 sprintf(tbuf
, "%-23.23s %8d", se
->name
, se
->total_count
);
518 memset(&tbuf
[clen
], ' ', 45 - clen
);
520 print_time(&tbuf
[45], (unsigned long)(se
->stime_usecs
), se
->stime_secs
);
523 if (no_wtime
== 0 && (se
->wtime_usecs
|| se
->wtime_secs
)) {
524 sprintf(&tbuf
[clen
], " ");
525 clen
+= strlen(&tbuf
[clen
]);
527 print_time(&tbuf
[clen
], (unsigned long)(se
->wtime_usecs
), se
->wtime_secs
);
528 clen
+= strlen(&tbuf
[clen
]);
530 if (se
->waiting
|| se
->delta_wtime_usecs
|| se
->delta_wtime_secs
) {
532 sprintf(&tbuf
[clen
], "(");
533 clen
+= strlen(&tbuf
[clen
]);
535 print_time(&tbuf
[clen
], (unsigned long)(se
->delta_wtime_usecs
),
536 se
->delta_wtime_secs
);
537 clen
+= strlen(&tbuf
[clen
]);
539 sprintf(&tbuf
[clen
], ")");
540 clen
+= strlen(&tbuf
[clen
]);
543 if (se
->waiting
== 1)
544 sprintf(&tbuf
[clen
], " W");
546 sprintf(&tbuf
[clen
], " %d", se
->waiting
);
547 clen
+= strlen(&tbuf
[clen
]);
551 sprintf(&tbuf
[clen
], "\n");
553 if (tbuf
[COLS
-2] != '\n') {
557 if (no_screen_refresh
)
580 if (no_screen_refresh
== 0) {
581 /* clear for new display */
587 sprintf(tbuf
, "%-14.14s", proc_name
);
595 p2
= "context switch ";
597 p2
= "context switches";
598 if (num_of_threads
== 1)
603 sprintf(&tbuf
[clen
], " %4d %s %4d %s %4d %s",
604 preempted
, p1
, csw
, p2
, num_of_threads
, p3
);
605 clen
+= strlen(&tbuf
[clen
]);
608 * Display the current time.
609 * "ctime" always returns a string that looks like this:
611 * Sun Sep 16 01:03:52 1973
612 * 012345678901234567890123
615 * We want indices 11 thru 18 (length 8).
617 curr_time
= time((long *)0);
620 start_time
= curr_time
;
622 elapsed_secs
= curr_time
- start_time
;
623 minutes
= elapsed_secs
/ 60;
624 hours
= minutes
/ 60;
626 memset(&tbuf
[clen
], ' ', 78 - clen
);
630 sprintf(&tbuf
[clen
], "%-8.8s\n", &(ctime(&curr_time
)[11]));
632 if (tbuf
[COLS
-2] != '\n') {
636 if (no_screen_refresh
)
641 if (total_faults
== 1)
649 sprintf(tbuf
, " %4d %s %4d %s",
650 total_faults
, p1
, scalls
, p2
);
653 sprintf(&tbuf
[clen
], " %3ld:%02ld:%02ld\n",
654 (long)hours
, (long)(minutes
% 60), (long)(elapsed_secs
% 60));
656 if (tbuf
[COLS
-2] != '\n') {
660 if (no_screen_refresh
)
667 sprintf(tbuf
, "\nTYPE NUMBER CPU_TIME WAIT_TIME\n");
669 if (tbuf
[COLS
-2] != '\n') {
673 if (no_screen_refresh
)
678 sprintf(tbuf
, "------------------------------------------------------------------------------\n");
679 if (tbuf
[COLS
-2] != '\n') {
683 if (no_screen_refresh
)
691 sprintf(tbuf
, "System Idle ");
694 print_time(&tbuf
[clen
], (unsigned long)(itime_usecs
), itime_secs
);
695 clen
+= strlen(&tbuf
[clen
]);
697 if (delta_itime_usecs
|| delta_itime_secs
) {
699 sprintf(&tbuf
[clen
], "(");
700 clen
+= strlen(&tbuf
[clen
]);
702 print_time(&tbuf
[clen
], (unsigned long)(delta_itime_usecs
), delta_itime_secs
);
703 clen
+= strlen(&tbuf
[clen
]);
705 sprintf(&tbuf
[clen
], ")");
706 clen
+= strlen(&tbuf
[clen
]);
708 sprintf(&tbuf
[clen
], "\n");
710 if (tbuf
[COLS
-2] != '\n') {
714 if (no_screen_refresh
)
722 sprintf(tbuf
, "System Busy ");
725 print_time(&tbuf
[clen
], (unsigned long)(otime_usecs
), otime_secs
);
726 clen
+= strlen(&tbuf
[clen
]);
728 if (delta_otime_usecs
|| delta_otime_secs
) {
730 sprintf(&tbuf
[clen
], "(");
731 clen
+= strlen(&tbuf
[clen
]);
733 print_time(&tbuf
[clen
], (unsigned long)(delta_otime_usecs
), delta_otime_secs
);
734 clen
+= strlen(&tbuf
[clen
]);
736 sprintf(&tbuf
[clen
], ")");
737 clen
+= strlen(&tbuf
[clen
]);
739 sprintf(&tbuf
[clen
], "\n");
741 if (tbuf
[COLS
-2] != '\n') {
745 if (no_screen_refresh
)
752 sprintf(tbuf
, "%-14.14s Usermode ", proc_name
);
755 print_time(&tbuf
[clen
], (unsigned long)(utime_usecs
), utime_secs
);
756 clen
+= strlen(&tbuf
[clen
]);
757 sprintf(&tbuf
[clen
], "\n");
759 if (tbuf
[COLS
-2] != '\n') {
763 if (no_screen_refresh
)
770 max_rows
= topn
- (num_of_threads
+ 3);
774 for (output_lf
= 1, n
= 1; rows
< max_rows
&& n
< MAX_FAULTS
; n
++) {
777 if (se
->total_count
== 0)
779 if (output_lf
== 1) {
781 if (no_screen_refresh
)
787 if (rows
>= max_rows
)
796 if (no_screen_refresh
)
802 for (i
= 0; rows
< max_rows
; i
++) {
804 n
= sort_by_count
[i
];
806 n
= sort_by_wtime
[i
];
809 print_row(&sc_tab
[n
], 0);
812 if (no_screen_refresh
== 0) {
815 while (rows
++ < max_rows
)
820 if (num_of_threads
) {
821 sprintf(tbuf
, "\nCURRENT_TYPE LAST_PATHNAME_WAITED_FOR CUR_WAIT_TIME THRD# PRI\n");
823 if (tbuf
[COLS
-2] != '\n') {
827 if (no_screen_refresh
)
832 sprintf(tbuf
, "------------------------------------------------------------------------------\n");
833 if (tbuf
[COLS
-2] != '\n') {
837 if (no_screen_refresh
)
844 for (i
= 0; i
< num_of_threads
; i
++, ti
++) {
848 int secs
, time_secs
, time_usecs
;
850 now
= mach_absolute_time();
852 while (ti
->thread
== 0 && ti
< &th_state
[MAX_THREADS
])
854 if (ti
== &th_state
[MAX_THREADS
])
858 te
= &ti
->th_entry
[ti
->depth
- 1];
860 if (te
->sc_state
== WAITING
) {
862 sprintf(tbuf
, "%-23.23s", sc_tab
[te
->code
].name
);
864 sprintf(tbuf
, "%-23.23s", "vm_fault");
866 sprintf(tbuf
, "%-23.23s", state_name
[te
->sc_state
]);
868 te
= &ti
->th_entry
[0];
869 sprintf(tbuf
, "%-23.23s", state_name
[te
->sc_state
]);
873 /* print the tail end of the pathname */
874 p
= (char *)ti
->pathname
;
881 sprintf(&tbuf
[clen
], " %-34.34s ", &p
[plen
]);
883 clen
+= strlen(&tbuf
[clen
]);
885 time_usecs
= (unsigned long)(((double)now
- te
->otime
) / divisor
);
886 secs
= time_usecs
/ 1000000;
887 time_usecs
-= secs
* 1000000;
890 print_time(&tbuf
[clen
], time_usecs
, time_secs
);
891 clen
+= strlen(&tbuf
[clen
]);
892 sprintf(&tbuf
[clen
], " %2d %3d\n", i
, ti
->curpri
);
894 if (tbuf
[COLS
-2] != '\n') {
898 if (no_screen_refresh
)
903 if (no_screen_refresh
== 0) {
907 printf("\n=================\n");
911 for (i
= 0; i
< (MAX_SC
+ msgcode_cnt
); i
++) {
912 if ((n
= sort_by_count
[i
]) == -1)
914 sc_tab
[n
].delta_count
= 0;
915 sc_tab
[n
].waiting
= 0;
916 sc_tab
[n
].delta_wtime_usecs
= 0;
917 sc_tab
[n
].delta_wtime_secs
= 0;
919 for (i
= 1; i
< MAX_FAULTS
; i
++) {
920 faults
[i
].delta_count
= 0;
921 faults
[i
].waiting
= 0;
922 faults
[i
].delta_wtime_usecs
= 0;
923 faults
[i
].delta_wtime_secs
= 0;
929 delta_itime_secs
= 0;
930 delta_itime_usecs
= 0;
931 delta_otime_secs
= 0;
932 delta_otime_usecs
= 0;
939 for (i
= 0; i
< (MAX_SC
+ msgcode_cnt
) ; i
++) {
940 sc_tab
[i
].delta_count
= 0;
941 sc_tab
[i
].total_count
= 0;
942 sc_tab
[i
].waiting
= 0;
943 sc_tab
[i
].delta_wtime_usecs
= 0;
944 sc_tab
[i
].delta_wtime_secs
= 0;
945 sc_tab
[i
].wtime_usecs
= 0;
946 sc_tab
[i
].wtime_secs
= 0;
947 sc_tab
[i
].stime_usecs
= 0;
948 sc_tab
[i
].stime_secs
= 0;
950 for (i
= 1; i
< MAX_FAULTS
; i
++) {
951 faults
[i
].delta_count
= 0;
952 faults
[i
].total_count
= 0;
953 faults
[i
].waiting
= 0;
954 faults
[i
].delta_wtime_usecs
= 0;
955 faults
[i
].delta_wtime_secs
= 0;
956 faults
[i
].wtime_usecs
= 0;
957 faults
[i
].wtime_secs
= 0;
958 faults
[i
].stime_usecs
= 0;
959 faults
[i
].stime_secs
= 0;
971 delta_itime_secs
= 0;
972 delta_itime_usecs
= 0;
975 delta_otime_secs
= 0;
976 delta_otime_usecs
= 0;
980 sc_tab_init(char *codefile
) {
987 if ((fp
= fopen(codefile
,"r")) == (FILE *)0) {
988 printf("Failed to open code description file %s\n", codefile
);
992 n
= fscanf(fp
, "%d\n", &cnt
);
996 /* Count Mach message MSG_ codes */
997 for (msgcode_cnt
=0;;) {
998 n
= fscanf(fp
, "%x%55s\n", &code
, &name
[0]);
1001 if (strncmp ("MSG_", &name
[0], 4) == 0)
1003 if (strcmp("USER_TEST", &name
[0]) == 0)
1007 sc_tab
= (struct sc_entry
*)malloc((MAX_SC
+msgcode_cnt
) * sizeof (struct sc_entry
));
1009 quit("can't allocate memory for system call table\n");
1010 bzero(sc_tab
,(MAX_SC
+msgcode_cnt
) * sizeof (struct sc_entry
));
1012 msgcode_tab
= (int *)malloc(msgcode_cnt
* sizeof(int));
1014 quit("can't allocate memory for msgcode table\n");
1015 bzero(msgcode_tab
,(msgcode_cnt
* sizeof(int)));
1017 sort_by_count
= (int *)malloc((MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
1019 quit("can't allocate memory for sort_by_count table\n");
1020 bzero(sort_by_count
,(MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
1022 sort_by_wtime
= (int *)malloc((MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
1024 quit("can't allocate memory for sort_by_wtime table\n");
1025 bzero(sort_by_wtime
, (MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
1030 n
= fscanf(fp
, "%d\n", &cnt
);
1036 n
= fscanf(fp
, "%x%55s\n", &code
, &name
[0]);
1041 if (strcmp("MACH_vmfault", &name
[0]) == 0) {
1042 mach_vmfault
= code
;
1045 if (strcmp("MACH_SCHED", &name
[0]) == 0) {
1049 if (strcmp("MACH_STKHANDOFF", &name
[0]) == 0) {
1050 mach_stkhandoff
= code
;
1053 if (strcmp("MACH_IDLE", &name
[0]) == 0) {
1057 if (strcmp("VFS_LOOKUP", &name
[0]) == 0) {
1061 if (strcmp("BSC_SysCall", &name
[0]) == 0) {
1065 if (strcmp("MACH_SysCall", &name
[0]) == 0) {
1069 if (strcmp("BSC_exit", &name
[0]) == 0) {
1073 if (strncmp("MSG_", &name
[0], 4) == 0) {
1074 msgcode_tab
[msgcode_indx
] = ((code
& 0x00ffffff) >>2);
1075 n
= MAX_SC
+ msgcode_indx
;
1076 strncpy(&sc_tab
[n
].name
[0], &name
[4], 31 );
1080 if (strncmp("MSC_", &name
[0], 4) == 0) {
1081 n
= 512 + ((code
>>2) & 0x1ff);
1082 strcpy(&sc_tab
[n
].name
[0], &name
[4]);
1085 if (strncmp("BSC_", &name
[0], 4) == 0) {
1086 n
= (code
>>2) & 0x1ff;
1087 strcpy(&sc_tab
[n
].name
[0], &name
[4]);
1090 if (strcmp("USER_TEST", &name
[0]) == 0)
1093 strcpy(&faults
[1].name
[0], "zero_fill");
1094 strcpy(&faults
[2].name
[0], "pagein");
1095 strcpy(&faults
[3].name
[0], "copy_on_write");
1096 strcpy(&faults
[4].name
[0], "cache_hit");
1103 struct kinfo_proc
*kp
;
1107 mib
[2] = KERN_PROC_ALL
;
1110 if (sysctl(mib
, 4, NULL
, &bufSize
, NULL
, 0) < 0)
1111 quit("trace facility failure, KERN_PROC_ALL\n");
1113 if((kp
= (struct kinfo_proc
*)malloc(bufSize
)) == (struct kinfo_proc
*)0)
1114 quit("can't allocate memory for proc buffer\n");
1116 if (sysctl(mib
, 4, kp
, &bufSize
, NULL
, 0) < 0)
1117 quit("trace facility failure, KERN_PROC_ALL\n");
1119 kp_nentries
= bufSize
/ sizeof(struct kinfo_proc
);
1123 struct th_info
*find_thread(int thread
) {
1126 for (ti
= th_state
; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1127 if (ti
->thread
== thread
)
1130 return ((struct th_info
*)0);
1135 cmp_wtime(struct sc_entry
*s1
, struct sc_entry
*s2
) {
1137 if (s1
->wtime_secs
< s2
->wtime_secs
)
1139 if (s1
->wtime_secs
> s2
->wtime_secs
)
1141 if (s1
->wtime_usecs
<= s2
->wtime_usecs
)
1149 int i
, n
, k
, cnt
, secs
;
1151 struct sc_entry
*se
;
1155 now
= mach_absolute_time();
1157 for (ti
= th_state
; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1158 if (ti
->thread
== 0)
1162 te
= &ti
->th_entry
[ti
->depth
-1];
1164 if (te
->sc_state
== WAITING
) {
1166 se
= &sc_tab
[te
->code
];
1168 se
= &faults
[DBG_PAGEIN_FAULT
];
1170 se
->wtime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1171 se
->delta_wtime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1172 te
->stime
= (double)now
;
1174 secs
= se
->wtime_usecs
/ 1000000;
1175 se
->wtime_usecs
-= secs
* 1000000;
1176 se
->wtime_secs
+= secs
;
1178 secs
= se
->delta_wtime_usecs
/ 1000000;
1179 se
->delta_wtime_usecs
-= secs
* 1000000;
1180 se
->delta_wtime_secs
+= secs
;
1183 te
= &ti
->th_entry
[0];
1185 if (te
->sc_state
== PREEMPTED
) {
1186 if ((unsigned long)(((double)now
- te
->otime
) / divisor
) > 5000000) {
1189 ti
->pathptr
= (long *)NULL
;
1190 ti
->pathname
[0] = 0;
1196 if ((called
% sort_now
) == 0) {
1197 sort_by_count
[0] = -1;
1198 sort_by_wtime
[0] = -1;
1199 for (cnt
= 1, n
= 1; n
< (MAX_SC
+ msgcode_cnt
); n
++) {
1200 if (sc_tab
[n
].total_count
) {
1201 for (i
= 0; i
< cnt
; i
++) {
1202 if ((k
= sort_by_count
[i
]) == -1 ||
1203 sc_tab
[n
].total_count
> sc_tab
[k
].total_count
) {
1205 for (k
= cnt
- 1; k
>= i
; k
--)
1206 sort_by_count
[k
+1] = sort_by_count
[k
];
1207 sort_by_count
[i
] = n
;
1211 if (how_to_sort
== 0) {
1212 for (i
= 0; i
< cnt
; i
++) {
1213 if ((k
= sort_by_wtime
[i
]) == -1 ||
1214 cmp_wtime(&sc_tab
[n
], &sc_tab
[k
])) {
1216 for (k
= cnt
- 1; k
>= i
; k
--)
1217 sort_by_wtime
[k
+1] = sort_by_wtime
[k
];
1218 sort_by_wtime
[i
] = n
;
1234 mib
[1] = KERN_KDEBUG
;
1235 mib
[2] = KERN_KDENABLE
; /* protocol */
1238 mib
[5] = 0; /* no flags */
1239 if (sysctl(mib
, 4, NULL
, &needed
, NULL
, 0) < 0)
1240 quit("trace facility failure, KERN_KDENABLE\n");
1249 set_numbufs(int nbufs
)
1252 mib
[1] = KERN_KDEBUG
;
1253 mib
[2] = KERN_KDSETBUF
;
1256 mib
[5] = 0; /* no flags */
1257 if (sysctl(mib
, 4, NULL
, &needed
, NULL
, 0) < 0)
1258 quit("trace facility failure, KERN_KDSETBUF\n");
1261 mib
[1] = KERN_KDEBUG
;
1262 mib
[2] = KERN_KDSETUP
;
1265 mib
[5] = 0; /* no flags */
1266 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1267 quit("trace facility failure, KERN_KDSETUP\n");
1272 set_pidcheck(int pid
, int on_off
)
1276 kr
.type
= KDBG_TYPENONE
;
1279 needed
= sizeof(kd_regtype
);
1281 mib
[1] = KERN_KDEBUG
;
1282 mib
[2] = KERN_KDPIDTR
;
1286 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0) {
1288 printf("pid %d does not exist\n", pid
);
1296 get_bufinfo(kbufinfo_t
*val
)
1298 needed
= sizeof (*val
);
1300 mib
[1] = KERN_KDEBUG
;
1301 mib
[2] = KERN_KDGETBUF
;
1304 mib
[5] = 0; /* no flags */
1305 if (sysctl(mib
, 3, val
, &needed
, 0, 0) < 0)
1306 quit("trace facility failure, KERN_KDGETBUF\n");
1318 mib
[1] = KERN_KDEBUG
;
1319 mib
[2] = KERN_KDREMOVE
; /* protocol */
1322 mib
[5] = 0; /* no flags */
1324 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1326 set_remove_flag
= 0;
1329 quit("the trace facility is currently in use...\n fs_usage, sc_usage, and latency use this feature.\n\n");
1331 quit("trace facility failure, KERN_KDREMOVE\n");
1339 kr
.type
= KDBG_RANGETYPE
;
1342 needed
= sizeof(kd_regtype
);
1344 mib
[1] = KERN_KDEBUG
;
1345 mib
[2] = KERN_KDSETREG
;
1348 mib
[5] = 0; /* no flags */
1349 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0)
1350 quit("trace facility failure, KERN_KDSETREG\n");
1353 mib
[1] = KERN_KDEBUG
;
1354 mib
[2] = KERN_KDSETUP
;
1357 mib
[5] = 0; /* no flags */
1358 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1359 quit("trace facility failure, KERN_KDSETUP\n");
1371 /* Get kernel buffer information */
1372 get_bufinfo(&bufinfo
);
1376 needed
= bufinfo
.nkdbufs
* sizeof(kd_buf
);
1378 mib
[1] = KERN_KDEBUG
;
1379 mib
[2] = KERN_KDREADTR
;
1382 mib
[5] = 0; /* no flags */
1383 if (sysctl(mib
, 3, my_buffer
, &needed
, NULL
, 0) < 0)
1384 quit("trace facility failure, KERN_KDREADTR\n");
1387 if (bufinfo
.flags
& KDBG_WRAPPED
) {
1388 for (i
= 0; i
< MAX_THREADS
; i
++) {
1389 th_state
[i
].depth
= 0;
1390 th_state
[i
].thread
= 0;
1391 th_state
[i
].vfslookup
= 0;
1392 th_state
[i
].pathptr
= (long *)NULL
;
1393 th_state
[i
].pathname
[0] = 0;
1400 set_pidcheck(pid
, 1);
1401 set_enable(1); /* re-enable kernel logging */
1403 kd
= (kd_buf
*)my_buffer
;
1405 for (i
= 0; i
< count
; i
++) {
1406 int debugid
, baseid
, thread
;
1409 struct th_info
*ti
, *switched_out
, *switched_in
;
1410 struct sc_entry
*se
;
1413 thread
= kd
[i
].arg5
;
1414 debugid
= kd
[i
].debugid
;
1415 type
= kd
[i
].debugid
& DBG_FUNC_MASK
;
1418 switched_out
= (struct th_info
*)0;
1419 switched_in
= (struct th_info
*)0;
1421 now
= kd
[i
].timestamp
& KDBG_TIMESTAMP_MASK
;
1423 baseid
= debugid
& 0xffff0000;
1425 if (type
== vfs_lookup
) {
1428 if ((ti
= find_thread(thread
)) == (struct th_info
*)0)
1431 if (ti
->vfslookup
== 1) {
1433 sargptr
= ti
->pathname
;
1435 *sargptr
++ = kd
[i
].arg2
;
1436 *sargptr
++ = kd
[i
].arg3
;
1437 *sargptr
++ = kd
[i
].arg4
;
1439 * NULL terminate the 'string'
1443 ti
->pathptr
= sargptr
;
1445 } else if (ti
->vfslookup
> 1) {
1447 sargptr
= ti
->pathptr
;
1450 We don't want to overrun our pathname buffer if the
1451 kernel sends us more VFS_LOOKUP entries than we can
1455 if (sargptr
>= &ti
->pathname
[NUMPARMS
])
1459 We need to detect consecutive vfslookup entries.
1460 So, if we get here and find a START entry,
1461 fake the pathptr so we can bypass all further
1465 if (debugid
& DBG_FUNC_START
)
1467 ti
->pathptr
= &ti
->pathname
[NUMPARMS
];
1471 *sargptr
++ = kd
[i
].arg1
;
1472 *sargptr
++ = kd
[i
].arg2
;
1473 *sargptr
++ = kd
[i
].arg3
;
1474 *sargptr
++ = kd
[i
].arg4
;
1476 * NULL terminate the 'string'
1480 ti
->pathptr
= sargptr
;
1484 } else if (baseid
== bsc_base
)
1485 code
= (debugid
>> 2) & 0x1ff;
1486 else if (baseid
== msc_base
)
1487 code
= 512 + ((debugid
>> 2) & 0x1ff);
1488 else if (type
== mach_idle
) {
1489 if (debugid
& DBG_FUNC_START
) {
1490 switched_out
= find_thread(kd
[i
].arg5
);
1494 if (debugid
& DBG_FUNC_END
) {
1495 switched_in
= find_thread(kd
[i
].arg5
);
1500 itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1501 delta_itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1503 } else if (in_other
) {
1504 otime_usecs
+= ((double)now
- other_start
) / divisor
;
1505 delta_otime_usecs
+= ((double)now
- other_start
) / divisor
;
1508 if ( !switched_in
) {
1510 * not one of the target proc's threads
1512 if (now_collect_cpu_time
) {
1514 idle_start
= (double)now
;
1518 if (now_collect_cpu_time
) {
1521 other_start
= (double)now
;
1524 if ( !switched_in
&& !switched_out
)
1528 else if (type
== mach_sched
|| type
== mach_stkhandoff
) {
1529 switched_out
= find_thread(kd
[i
].arg5
);
1530 switched_in
= find_thread(kd
[i
].arg2
);
1533 itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1534 delta_itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1536 } else if (in_other
) {
1537 otime_usecs
+= ((double)now
- other_start
) / divisor
;
1538 delta_otime_usecs
+= ((double)now
- other_start
) / divisor
;
1541 if ( !switched_in
) {
1543 * not one of the target proc's threads
1545 if (now_collect_cpu_time
) {
1547 other_start
= (double)now
;
1550 if ( !switched_in
&& !switched_out
)
1554 else if ((baseid
& 0xff000000) == 0xff000000) {
1555 code
= find_msgcode (debugid
);
1558 } else if (baseid
!= mach_vmfault
)
1561 if (switched_out
|| switched_in
) {
1564 ti
->curpri
= kd
[i
].arg3
;
1567 te
= &ti
->th_entry
[ti
->depth
-1];
1569 if (te
->sc_state
== KERNEL_MODE
)
1570 te
->ctime
+= (double)now
- te
->stime
;
1571 te
->sc_state
= WAITING
;
1576 te
= &ti
->th_entry
[0];
1578 if (te
->sc_state
== USER_MODE
)
1579 utime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1580 te
->sc_state
= PREEMPTED
;
1583 te
->stime
= (double)now
;
1584 te
->otime
= (double)now
;
1585 now_collect_cpu_time
= 1;
1590 ti
->curpri
= kd
[i
].arg4
;
1593 te
= &ti
->th_entry
[ti
->depth
-1];
1595 if (te
->sc_state
== WAITING
)
1596 te
->wtime
+= (double)now
- te
->stime
;
1597 te
->sc_state
= KERNEL_MODE
;
1599 te
= &ti
->th_entry
[0];
1601 te
->sc_state
= USER_MODE
;
1603 te
->stime
= (double)now
;
1604 te
->otime
= (double)now
;
1608 if ((ti
= find_thread(thread
)) == (struct th_info
*)0) {
1609 for (ti
= &th_state
[0]; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1610 if (ti
->thread
== 0) {
1611 ti
->thread
= thread
;
1616 if (ti
== &th_state
[MAX_THREADS
])
1619 if (debugid
& DBG_FUNC_START
) {
1623 te
= &ti
->th_entry
[ti
->depth
-1];
1625 if (te
->sc_state
== KERNEL_MODE
)
1626 te
->ctime
+= (double)now
- te
->stime
;
1628 te
= &ti
->th_entry
[0];
1630 if (te
->sc_state
== USER_MODE
)
1631 utime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1633 te
->stime
= (double)now
;
1634 te
->otime
= (double)now
;
1636 if (ti
->depth
< MAX_NESTED
) {
1637 te
= &ti
->th_entry
[ti
->depth
];
1639 te
->sc_state
= KERNEL_MODE
;
1642 te
->stime
= (double)now
;
1643 te
->otime
= (double)now
;
1644 te
->ctime
= (double)0;
1645 te
->wtime
= (double)0;
1649 } else if (debugid
& DBG_FUNC_END
) {
1654 se
= &faults
[kd
[i
].arg4
];
1657 if (se
->total_count
== 0)
1663 te
= &ti
->th_entry
[ti
->depth
-1];
1665 if (te
->type
== type
) {
1666 se
->stime_usecs
+= te
->ctime
/ divisor
;
1667 se
->stime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1669 se
->wtime_usecs
+= te
->wtime
/ divisor
;
1670 se
->delta_wtime_usecs
+= te
->wtime
/ divisor
;
1672 secs
= se
->stime_usecs
/ 1000000;
1673 se
->stime_usecs
-= secs
* 1000000;
1674 se
->stime_secs
+= secs
;
1676 secs
= se
->wtime_usecs
/ 1000000;
1677 se
->wtime_usecs
-= secs
* 1000000;
1678 se
->wtime_secs
+= secs
;
1680 secs
= se
->delta_wtime_usecs
/ 1000000;
1681 se
->delta_wtime_usecs
-= secs
* 1000000;
1682 se
->delta_wtime_secs
+= secs
;
1686 if (ti
->depth
== 0) {
1688 * headed back to user mode
1689 * start the time accumulation
1691 te
= &ti
->th_entry
[0];
1692 te
->sc_state
= USER_MODE
;
1694 te
= &ti
->th_entry
[ti
->depth
-1];
1696 te
->stime
= (double)now
;
1697 te
->otime
= (double)now
;
1703 if (ti
->depth
== 0) {
1705 * headed back to user mode
1706 * start the time accumulation
1708 te
= &ti
->th_entry
[0];
1709 te
->sc_state
= USER_MODE
;
1710 te
->stime
= (double)now
;
1711 te
->otime
= (double)now
;
1716 secs
= utime_usecs
/ 1000000;
1717 utime_usecs
-= secs
* 1000000;
1720 secs
= itime_usecs
/ 1000000;
1721 itime_usecs
-= secs
* 1000000;
1724 secs
= delta_itime_usecs
/ 1000000;
1725 delta_itime_usecs
-= secs
* 1000000;
1726 delta_itime_secs
+= secs
;
1728 secs
= otime_usecs
/ 1000000;
1729 otime_usecs
-= secs
* 1000000;
1732 secs
= delta_otime_usecs
/ 1000000;
1733 delta_otime_usecs
-= secs
* 1000000;
1734 delta_otime_secs
+= secs
;
1744 This flag is turned off when calling
1745 quit() due to a set_remove() failure.
1747 if (set_remove_flag
)
1750 printf("sc_usage: ");
1759 mach_timebase_info_data_t info
;
1761 (void) mach_timebase_info (&info
);
1763 divisor
= ( (double)info
.denom
/ (double)info
.numer
) * 1000;
1779 ret
= (int)strtol(str
, &cp
, 10);
1780 if (cp
== str
|| *cp
) {
1781 /* Assume this is a command string and find first matching pid */
1782 for (i
=0; i
< kp_nentries
; i
++) {
1783 if(kp_buffer
[i
].kp_proc
.p_stat
== 0)
1786 if(!strcmp(str
, kp_buffer
[i
].kp_proc
.p_comm
))
1788 strncpy(proc_name
, kp_buffer
[i
].kp_proc
.p_comm
, sizeof(proc_name
)-1);
1789 proc_name
[sizeof(proc_name
)-1] = '\0';
1790 return(kp_buffer
[i
].kp_proc
.p_pid
);
1797 for (i
=0; i
< kp_nentries
; i
++)
1799 if(kp_buffer
[i
].kp_proc
.p_stat
== 0)
1801 else if (kp_buffer
[i
].kp_proc
.p_pid
== ret
) {
1802 strncpy(proc_name
, kp_buffer
[i
].kp_proc
.p_comm
, sizeof(proc_name
)-1);
1803 proc_name
[sizeof(proc_name
)-1] = '\0';
1804 return(kp_buffer
[i
].kp_proc
.p_pid
);
1812 /* Returns index into sc_tab for a mach msg entry */
1814 find_msgcode(int debugid
)
1819 for (indx
=0; indx
< msgcode_cnt
; indx
++)
1821 if (msgcode_tab
[indx
] == ((debugid
& 0x00ffffff) >>2))
1822 return (MAX_SC
+indx
);
1828 argtoi(flag
, req
, str
, base
)
1836 ret
= (int)strtol(str
, &cp
, base
);
1837 if (cp
== str
|| *cp
)
1838 errx(EINVAL
, "-%c flag requires a %s", flag
, req
);