2 * Copyright (c) 1999 Apple Computer, 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. -DKERNEL_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>
47 #include <sys/ioctl.h>
49 #ifndef KERNEL_PRIVATE
50 #define KERNEL_PRIVATE
51 #include <sys/kdebug.h>
54 #include <sys/kdebug.h>
55 #endif /*KERNEL_PRIVATE*/
57 #include <sys/sysctl.h>
59 #include <mach/mach_time.h>
63 /* Number of lines of header information on the standard screen */
64 #define HEADER_LINES 5
67 int Header_lines
= HEADER_LINES
;
70 int no_screen_refresh
= 0;
76 int waiting_index
= 0;
77 FILE *dfp
= 0; /*Debug output file */
80 #define SAMPLE_SIZE 20000
82 #define DBG_ZERO_FILL_FAULT 1
83 #define DBG_PAGEIN_FAULT 2
84 #define DBG_COW_FAULT 3
85 #define DBG_CACHE_HIT_FAULT 4
88 #define MAX_THREADS 16
92 /* If NUMPARMS from kernel changes, it will be reflected in PATHLENGTH as well */
94 #define PATHLENGTH (NUMPARMS*sizeof(long))
96 char *state_name
[] = {
105 #define KERNEL_MODE 1
126 char pathname
[PATHLENGTH
+ 1];
127 struct entry th_entry
[MAX_NESTED
];
135 unsigned int stime_secs
;
137 unsigned int wtime_secs
;
139 unsigned int delta_wtime_secs
;
140 double delta_wtime_usecs
;
143 struct th_info th_state
[MAX_THREADS
];
144 struct sc_entry faults
[MAX_FAULTS
];
146 struct sc_entry
*sc_tab
;
148 int msgcode_cnt
; /* number of MSG_ codes */
150 int num_of_threads
= 0;
151 int now_collect_cpu_time
= 0;
153 unsigned int utime_secs
;
157 unsigned int itime_secs
;
159 unsigned int delta_itime_secs
;
160 double delta_itime_usecs
;
164 unsigned int otime_secs
;
166 unsigned int delta_otime_secs
;
167 double delta_otime_usecs
;
174 int mach_stkhandoff
= 0;
176 int mach_vmfault
= 0;
183 #define DBG_FUNC_ALL (DBG_FUNC_START | DBG_FUNC_END)
184 #define DBG_FUNC_MASK 0xfffffffc
191 /* Default divisor */
192 #define DIVISOR 16.6666 /* Trace divisor converts to microseconds */
193 double divisor
= DIVISOR
;
200 kbufinfo_t bufinfo
= {0, 0, 0, 0};
202 int trace_enabled
= 0;
203 int set_remove_flag
= 1;
205 struct kinfo_proc
*kp_buffer
= 0;
208 extern char **environ
;
221 void leave() /* exit under normal conditions -- INT handler */
224 if (no_screen_refresh
== 0) {
230 set_pidcheck(pid
, 0);
235 void err_leave(s
) /* exit under error conditions */
239 if (no_screen_refresh
== 0) {
245 printf("sc_usage: ");
250 set_pidcheck(pid
, 0);
258 if (no_screen_refresh
== 0)
265 fprintf(stderr
, "Usage: %s [-c codefile] [-e] [-l] [-sn] pid | cmd | -E execute path\n", myname
);
266 fprintf(stderr
, " -c name of codefile containing mappings for syscalls\n");
267 fprintf(stderr
, " Default is /usr/share/misc/trace.codes\n");
268 fprintf(stderr
, " -e enable sort by call count\n");
269 fprintf(stderr
, " -l turn off top style output\n");
270 fprintf(stderr
, " -sn change sample rate to every n seconds\n");
271 fprintf(stderr
, " pid selects process to sample\n");
272 fprintf(stderr
, " cmd selects command to sample\n");
273 fprintf(stderr
, " -E Execute the given path and optional arguments\n");
279 #define usec_to_1000ths(t) ((t) / 1000)
281 void print_time(char *p
, unsigned int useconds
, unsigned int seconds
)
285 minutes
= seconds
/ 60;
286 hours
= minutes
/ 60;
288 if (minutes
< 100) { // up to 100 minutes
289 sprintf(p
, "%2ld:%02ld.%03ld", minutes
, seconds
% 60,
290 usec_to_1000ths(useconds
));
292 else if (hours
< 100) { // up to 100 hours
293 sprintf(p
, "%2ld:%02ld:%02ld ", hours
, minutes
% 60,
297 sprintf(p
, "%4ld hrs ", hours
);
306 char *myname
= "sc_usage";
307 char *codefile
= "/usr/share/misc/trace.codes";
310 int delay
= Default_DELAY
;
312 void screen_update();
316 void reset_counters();
321 if ( geteuid() != 0 ) {
322 printf("'sc_usage' must be run as root...\n");
328 if ((myname
= rindex(argv
[0], '/')) == 0) {
336 while ((ch
= getopt(argc
, argv
, "c:els:d:E")) != EOF
) {
339 delay
= argtoi('s', "decimal number", optarg
, 10);
345 no_screen_refresh
= 1;
354 /* exit_usage(myname);*/
355 exit_usage("default");
361 sc_tab_init(codefile
);
367 /* parse a pid or a command */
368 if((pid
= argtopid(argv
[optind
])) < 0 )
372 { /* execute this command */
377 ptr
= strrchr(argv
[optind
], '/');
383 strncpy(proc_name
, ptr
, sizeof(proc_name
)-1);
384 proc_name
[sizeof(proc_name
)-1] = '\0';
394 fprintf(stderr
, "Starting program: %s\n", argv
[optind
]);
397 switch ((pid
= vfork()))
404 ptrace(0,0,0,0); /* PT_TRACE_ME */
405 execve(argv
[optind
], &argv
[optind
], environ
);
420 if (no_screen_refresh
== 0) {
422 /* initializes curses and screen (last) */
423 if (initscr() == (WINDOW
*) 0)
425 printf("Unrecognized TERM type, try vt100\n");
437 /* set up signal handlers */
438 signal(SIGINT
, leave
);
439 signal(SIGQUIT
, leave
);
440 signal(SIGHUP
, leave
);
441 signal(SIGTERM
, leave
);
442 signal(SIGWINCH
, sigwinch
);
444 if (no_screen_refresh
== 0)
445 topn
= LINES
- Header_lines
;
450 if ((my_buffer
= malloc(SAMPLE_SIZE
* sizeof(kd_buf
))) == (char *)0)
451 quit("can't allocate memory for tracing info\n");
454 set_numbufs(SAMPLE_SIZE
);
456 set_pidcheck(pid
, 1);
459 ptrace(7, pid
, 1, 0); /* PT_CONTINUE */
464 if ((sort_now
= 10 / delay
) < 2)
468 (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 hours
, minutes
% 60, 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
++) {
847 int secs
, time_secs
, time_usecs
;
849 now
= mach_absolute_time();
851 while (ti
->thread
== 0 && ti
< &th_state
[MAX_THREADS
])
853 if (ti
== &th_state
[MAX_THREADS
])
857 te
= &ti
->th_entry
[ti
->depth
- 1];
859 if (te
->sc_state
== WAITING
) {
861 sprintf(tbuf
, "%-23.23s", sc_tab
[te
->code
].name
);
863 sprintf(tbuf
, "%-23.23s", "vm_fault");
865 sprintf(tbuf
, "%-23.23s", state_name
[te
->sc_state
]);
867 te
= &ti
->th_entry
[0];
868 sprintf(tbuf
, "%-23.23s", state_name
[te
->sc_state
]);
872 /* print the tail end of the pathname */
873 plen
= strlen(ti
->pathname
);
878 sprintf(&tbuf
[clen
], " %-34.34s ", &ti
->pathname
[plen
]);
880 clen
+= strlen(&tbuf
[clen
]);
882 time_usecs
= (unsigned long)(((double)now
- te
->otime
) / divisor
);
883 secs
= time_usecs
/ 1000000;
884 time_usecs
-= secs
* 1000000;
887 print_time(&tbuf
[clen
], time_usecs
, time_secs
);
888 clen
+= strlen(&tbuf
[clen
]);
889 sprintf(&tbuf
[clen
], " %2d %3d\n", i
, ti
->curpri
);
891 if (tbuf
[COLS
-2] != '\n') {
895 if (no_screen_refresh
)
900 if (no_screen_refresh
== 0) {
904 printf("\n=================\n");
908 for (i
= 0; i
< (MAX_SC
+ msgcode_cnt
); i
++) {
909 if ((n
= sort_by_count
[i
]) == -1)
911 sc_tab
[n
].delta_count
= 0;
912 sc_tab
[n
].waiting
= 0;
913 sc_tab
[n
].delta_wtime_usecs
= 0;
914 sc_tab
[n
].delta_wtime_secs
= 0;
916 for (i
= 1; i
< MAX_FAULTS
; i
++) {
917 faults
[i
].delta_count
= 0;
918 faults
[i
].waiting
= 0;
919 faults
[i
].delta_wtime_usecs
= 0;
920 faults
[i
].delta_wtime_secs
= 0;
926 delta_itime_secs
= 0;
927 delta_itime_usecs
= 0;
928 delta_otime_secs
= 0;
929 delta_otime_usecs
= 0;
936 for (i
= 0; i
< (MAX_SC
+ msgcode_cnt
) ; i
++) {
937 sc_tab
[i
].delta_count
= 0;
938 sc_tab
[i
].total_count
= 0;
939 sc_tab
[i
].waiting
= 0;
940 sc_tab
[i
].delta_wtime_usecs
= 0;
941 sc_tab
[i
].delta_wtime_secs
= 0;
942 sc_tab
[i
].wtime_usecs
= 0;
943 sc_tab
[i
].wtime_secs
= 0;
944 sc_tab
[i
].stime_usecs
= 0;
945 sc_tab
[i
].stime_secs
= 0;
947 for (i
= 1; i
< MAX_FAULTS
; i
++) {
948 faults
[i
].delta_count
= 0;
949 faults
[i
].total_count
= 0;
950 faults
[i
].waiting
= 0;
951 faults
[i
].delta_wtime_usecs
= 0;
952 faults
[i
].delta_wtime_secs
= 0;
953 faults
[i
].wtime_usecs
= 0;
954 faults
[i
].wtime_secs
= 0;
955 faults
[i
].stime_usecs
= 0;
956 faults
[i
].stime_secs
= 0;
968 delta_itime_secs
= 0;
969 delta_itime_usecs
= 0;
972 delta_otime_secs
= 0;
973 delta_otime_usecs
= 0;
977 sc_tab_init(char *codefile
) {
985 if ((fp
= fopen(codefile
,"r")) == (FILE *)0) {
986 printf("Failed to open code description file %s\n", codefile
);
990 n
= fscanf(fp
, "%d\n", &cnt
);
994 /* Count Mach message MSG_ codes */
995 for (msgcode_cnt
=0;;) {
996 n
= fscanf(fp
, "%x%s\n", &code
, &name
[0]);
999 if (strncmp ("MSG_", &name
[0], 4) == 0)
1001 if (strcmp("USER_TEST", &name
[0]) == 0)
1005 sc_tab
= (struct sc_entry
*)malloc((MAX_SC
+msgcode_cnt
) * sizeof (struct sc_entry
));
1007 quit("can't allocate memory for system call table\n");
1008 bzero(sc_tab
,(MAX_SC
+msgcode_cnt
) * sizeof (struct sc_entry
));
1010 msgcode_tab
= (int *)malloc(msgcode_cnt
* sizeof(int));
1012 quit("can't allocate memory for msgcode table\n");
1013 bzero(msgcode_tab
,(msgcode_cnt
* sizeof(int)));
1015 sort_by_count
= (int *)malloc((MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
1017 quit("can't allocate memory for sort_by_count table\n");
1018 bzero(sort_by_count
,(MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
1020 sort_by_wtime
= (int *)malloc((MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
1022 quit("can't allocate memory for sort_by_wtime table\n");
1023 bzero(sort_by_wtime
, (MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
1028 n
= fscanf(fp
, "%d\n", &cnt
);
1034 n
= fscanf(fp
, "%x%s\n", &code
, &name
[0]);
1039 if (strcmp("MACH_vmfault", &name
[0]) == 0) {
1040 mach_vmfault
= code
;
1043 if (strcmp("MACH_SCHED", &name
[0]) == 0) {
1047 if (strcmp("MACH_STKHANDOFF", &name
[0]) == 0) {
1048 mach_stkhandoff
= code
;
1051 if (strcmp("VFS_LOOKUP", &name
[0]) == 0) {
1055 if (strcmp("BSC_SysCall", &name
[0]) == 0) {
1059 if (strcmp("MACH_SysCall", &name
[0]) == 0) {
1063 if (strcmp("BSC_exit", &name
[0]) == 0) {
1067 if (strncmp("MSG_", &name
[0], 4) == 0) {
1068 msgcode_tab
[msgcode_indx
] = ((code
& 0x00ffffff) >>2);
1069 n
= MAX_SC
+ msgcode_indx
;
1070 strncpy(&sc_tab
[n
].name
[0], &name
[4], 31 );
1074 if (strncmp("MSC_", &name
[0], 4) == 0) {
1075 n
= 512 + ((code
>>2) & 0x1ff);
1076 strcpy(&sc_tab
[n
].name
[0], &name
[4]);
1079 if (strncmp("BSC_", &name
[0], 4) == 0) {
1080 n
= (code
>>2) & 0x1ff;
1081 strcpy(&sc_tab
[n
].name
[0], &name
[4]);
1084 if (strcmp("USER_TEST", &name
[0]) == 0)
1087 strcpy(&faults
[1].name
[0], "zero_fill");
1088 strcpy(&faults
[2].name
[0], "pagein");
1089 strcpy(&faults
[3].name
[0], "copy_on_write");
1090 strcpy(&faults
[4].name
[0], "cache_hit");
1097 struct kinfo_proc
*kp
;
1101 mib
[2] = KERN_PROC_ALL
;
1104 if (sysctl(mib
, 4, NULL
, &bufSize
, NULL
, 0) < 0)
1105 quit("trace facility failure, KERN_PROC_ALL\n");
1107 if((kp
= (struct kinfo_proc
*)malloc(bufSize
)) == (struct kinfo_proc
*)0)
1108 quit("can't allocate memory for proc buffer\n");
1110 if (sysctl(mib
, 4, kp
, &bufSize
, NULL
, 0) < 0)
1111 quit("trace facility failure, KERN_PROC_ALL\n");
1113 kp_nentries
= bufSize
/ sizeof(struct kinfo_proc
);
1117 struct th_info
*find_thread(int thread
) {
1120 for (ti
= th_state
; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1121 if (ti
->thread
== thread
)
1124 return ((struct th_info
*)0);
1129 cmp_wtime(struct sc_entry
*s1
, struct sc_entry
*s2
) {
1131 if (s1
->wtime_secs
< s2
->wtime_secs
)
1133 if (s1
->wtime_secs
> s2
->wtime_secs
)
1135 if (s1
->wtime_usecs
<= s2
->wtime_usecs
)
1143 int i
, n
, k
, cnt
, secs
;
1145 struct sc_entry
*se
;
1149 now
= mach_absolute_time();
1151 for (ti
= th_state
; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1152 if (ti
->thread
== 0)
1156 te
= &ti
->th_entry
[ti
->depth
-1];
1158 if (te
->sc_state
== WAITING
) {
1160 se
= &sc_tab
[te
->code
];
1162 se
= &faults
[DBG_PAGEIN_FAULT
];
1164 se
->wtime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1165 se
->delta_wtime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1166 te
->stime
= (double)now
;
1168 secs
= se
->wtime_usecs
/ 1000000;
1169 se
->wtime_usecs
-= secs
* 1000000;
1170 se
->wtime_secs
+= secs
;
1172 secs
= se
->delta_wtime_usecs
/ 1000000;
1173 se
->delta_wtime_usecs
-= secs
* 1000000;
1174 se
->delta_wtime_secs
+= secs
;
1177 te
= &ti
->th_entry
[0];
1179 if (te
->sc_state
== PREEMPTED
) {
1180 if ((unsigned long)(((double)now
- te
->otime
) / divisor
) > 5000000) {
1183 ti
->pathptr
= (long *)0;
1184 ti
->pathname
[0] = 0;
1190 if ((called
% sort_now
) == 0) {
1191 sort_by_count
[0] = -1;
1192 sort_by_wtime
[0] = -1;
1193 for (cnt
= 1, n
= 1; n
< (MAX_SC
+ msgcode_cnt
); n
++) {
1194 if (sc_tab
[n
].total_count
) {
1195 for (i
= 0; i
< cnt
; i
++) {
1196 if ((k
= sort_by_count
[i
]) == -1 ||
1197 sc_tab
[n
].total_count
> sc_tab
[k
].total_count
) {
1199 for (k
= cnt
- 1; k
>= i
; k
--)
1200 sort_by_count
[k
+1] = sort_by_count
[k
];
1201 sort_by_count
[i
] = n
;
1205 if (how_to_sort
== 0) {
1206 for (i
= 0; i
< cnt
; i
++) {
1207 if ((k
= sort_by_wtime
[i
]) == -1 ||
1208 cmp_wtime(&sc_tab
[n
], &sc_tab
[k
])) {
1210 for (k
= cnt
- 1; k
>= i
; k
--)
1211 sort_by_wtime
[k
+1] = sort_by_wtime
[k
];
1212 sort_by_wtime
[i
] = n
;
1228 mib
[1] = KERN_KDEBUG
;
1229 mib
[2] = KERN_KDENABLE
; /* protocol */
1232 mib
[5] = 0; /* no flags */
1233 if (sysctl(mib
, 4, NULL
, &needed
, NULL
, 0) < 0)
1234 quit("trace facility failure, KERN_KDENABLE\n");
1243 set_numbufs(int nbufs
)
1246 mib
[1] = KERN_KDEBUG
;
1247 mib
[2] = KERN_KDSETBUF
;
1250 mib
[5] = 0; /* no flags */
1251 if (sysctl(mib
, 4, NULL
, &needed
, NULL
, 0) < 0)
1252 quit("trace facility failure, KERN_KDSETBUF\n");
1255 mib
[1] = KERN_KDEBUG
;
1256 mib
[2] = KERN_KDSETUP
;
1259 mib
[5] = 0; /* no flags */
1260 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1261 quit("trace facility failure, KERN_KDSETUP\n");
1266 set_pidcheck(int pid
, int on_off
)
1270 kr
.type
= KDBG_TYPENONE
;
1273 needed
= sizeof(kd_regtype
);
1275 mib
[1] = KERN_KDEBUG
;
1276 mib
[2] = KERN_KDPIDTR
;
1280 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0) {
1282 printf("pid %d does not exist\n", pid
);
1290 get_bufinfo(kbufinfo_t
*val
)
1292 needed
= sizeof (*val
);
1294 mib
[1] = KERN_KDEBUG
;
1295 mib
[2] = KERN_KDGETBUF
;
1298 mib
[5] = 0; /* no flags */
1299 if (sysctl(mib
, 3, val
, &needed
, 0, 0) < 0)
1300 quit("trace facility failure, KERN_KDGETBUF\n");
1312 mib
[1] = KERN_KDEBUG
;
1313 mib
[2] = KERN_KDREMOVE
; /* protocol */
1316 mib
[5] = 0; /* no flags */
1318 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1320 set_remove_flag
= 0;
1323 quit("the trace facility is currently in use...\n fs_usage, sc_usage, and latency use this feature.\n\n");
1325 quit("trace facility failure, KERN_KDREMOVE\n");
1333 kr
.type
= KDBG_RANGETYPE
;
1336 needed
= sizeof(kd_regtype
);
1338 mib
[1] = KERN_KDEBUG
;
1339 mib
[2] = KERN_KDSETREG
;
1342 mib
[5] = 0; /* no flags */
1343 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0)
1344 quit("trace facility failure, KERN_KDSETREG\n");
1347 mib
[1] = KERN_KDEBUG
;
1348 mib
[2] = KERN_KDSETUP
;
1351 mib
[5] = 0; /* no flags */
1352 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1353 quit("trace facility failure, KERN_KDSETUP\n");
1365 /* Get kernel buffer information */
1366 get_bufinfo(&bufinfo
);
1370 needed
= bufinfo
.nkdbufs
* sizeof(kd_buf
);
1372 mib
[1] = KERN_KDEBUG
;
1373 mib
[2] = KERN_KDREADTR
;
1376 mib
[5] = 0; /* no flags */
1377 if (sysctl(mib
, 3, my_buffer
, &needed
, NULL
, 0) < 0)
1378 quit("trace facility failure, KERN_KDREADTR\n");
1381 if (bufinfo
.flags
& KDBG_WRAPPED
) {
1382 for (i
= 0; i
< MAX_THREADS
; i
++) {
1383 th_state
[i
].depth
= 0;
1384 th_state
[i
].thread
= 0;
1385 th_state
[i
].vfslookup
= 0;
1386 th_state
[i
].pathptr
= (long *)0;
1387 th_state
[i
].pathname
[0] = 0;
1394 set_pidcheck(pid
, 1);
1395 set_enable(1); /* re-enable kernel logging */
1397 kd
= (kd_buf
*)my_buffer
;
1399 for (i
= 0; i
< count
; i
++) {
1400 int debugid
, baseid
, thread
;
1403 struct th_info
*ti
, *switched_out
, *switched_in
;
1404 struct sc_entry
*se
;
1407 thread
= kd
[i
].arg5
& KDBG_THREAD_MASK
;
1408 debugid
= kd
[i
].debugid
;
1409 type
= kd
[i
].debugid
& DBG_FUNC_MASK
;
1412 switched_out
= (struct th_info
*)0;
1413 switched_in
= (struct th_info
*)0;
1415 now
= (((uint64_t)kd
[i
].timestamp
.tv_sec
) << 32) |
1416 (uint64_t)((unsigned int)(kd
[i
].timestamp
.tv_nsec
));
1417 baseid
= debugid
& 0xffff0000;
1419 if (type
== vfs_lookup
) {
1422 if ((ti
= find_thread(thread
)) == (struct th_info
*)0)
1425 if (ti
->vfslookup
== 1) {
1427 memset(&ti
->pathname
[0], 0, (PATHLENGTH
+ 1));
1428 sargptr
= (long *)&ti
->pathname
[0];
1430 *sargptr
++ = kd
[i
].arg2
;
1431 *sargptr
++ = kd
[i
].arg3
;
1432 *sargptr
++ = kd
[i
].arg4
;
1433 ti
->pathptr
= sargptr
;
1435 } else if (ti
->vfslookup
> 1) {
1437 sargptr
= ti
->pathptr
;
1440 We don't want to overrun our pathname buffer if the
1441 kernel sends us more VFS_LOOKUP entries than we can
1445 if ((long *)sargptr
>= (long *)&ti
->pathname
[PATHLENGTH
])
1449 We need to detect consecutive vfslookup entries.
1450 So, if we get here and find a START entry,
1451 fake the pathptr so we can bypass all further
1455 if (debugid
& DBG_FUNC_START
)
1457 (long *)ti
->pathptr
= (long *)&ti
->pathname
[PATHLENGTH
];
1461 *sargptr
++ = kd
[i
].arg1
;
1462 *sargptr
++ = kd
[i
].arg2
;
1463 *sargptr
++ = kd
[i
].arg3
;
1464 *sargptr
++ = kd
[i
].arg4
;
1465 ti
->pathptr
= sargptr
;
1469 } else if (baseid
== bsc_base
)
1470 code
= (debugid
>> 2) & 0x1ff;
1471 else if (baseid
== msc_base
)
1472 code
= 512 + ((debugid
>> 2) & 0x1ff);
1473 else if (baseid
== mach_sched
|| baseid
== mach_stkhandoff
) {
1474 switched_out
= find_thread(kd
[i
].arg1
);
1475 switched_in
= find_thread(kd
[i
].arg2
);
1478 itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1479 delta_itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1481 } else if (in_other
) {
1482 otime_usecs
+= ((double)now
- other_start
) / divisor
;
1483 delta_otime_usecs
+= ((double)now
- other_start
) / divisor
;
1486 if ( !switched_in
) {
1488 * not one of the target proc's threads
1490 if (now_collect_cpu_time
) {
1491 if (kd
[i
].arg4
== 0) {
1493 idle_start
= (double)now
;
1496 other_start
= (double)now
;
1500 if ( !switched_in
&& !switched_out
)
1504 else if ((baseid
& 0xff000000) == 0xff000000) {
1505 code
= find_msgcode (debugid
);
1508 } else if (baseid
!= mach_vmfault
)
1511 if (switched_out
|| switched_in
) {
1514 ti
->curpri
= kd
[i
].arg3
;
1517 te
= &ti
->th_entry
[ti
->depth
-1];
1519 if (te
->sc_state
== KERNEL_MODE
)
1520 te
->ctime
+= (double)now
- te
->stime
;
1521 te
->sc_state
= WAITING
;
1526 te
= &ti
->th_entry
[0];
1528 if (te
->sc_state
== USER_MODE
)
1529 utime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1530 te
->sc_state
= PREEMPTED
;
1533 te
->stime
= (double)now
;
1534 te
->otime
= (double)now
;
1535 now_collect_cpu_time
= 1;
1540 ti
->curpri
= kd
[i
].arg4
;
1543 te
= &ti
->th_entry
[ti
->depth
-1];
1545 if (te
->sc_state
== WAITING
)
1546 te
->wtime
+= (double)now
- te
->stime
;
1547 te
->sc_state
= KERNEL_MODE
;
1549 te
= &ti
->th_entry
[0];
1551 te
->sc_state
= USER_MODE
;
1553 te
->stime
= (double)now
;
1554 te
->otime
= (double)now
;
1558 if ((ti
= find_thread(thread
)) == (struct th_info
*)0) {
1559 for (ti
= &th_state
[0]; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1560 if (ti
->thread
== 0) {
1561 ti
->thread
= thread
;
1566 if (ti
== &th_state
[MAX_THREADS
])
1569 if (debugid
& DBG_FUNC_START
) {
1573 te
= &ti
->th_entry
[ti
->depth
-1];
1575 if (te
->sc_state
== KERNEL_MODE
)
1576 te
->ctime
+= (double)now
- te
->stime
;
1578 te
= &ti
->th_entry
[0];
1580 if (te
->sc_state
== USER_MODE
)
1581 utime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1583 te
->stime
= (double)now
;
1584 te
->otime
= (double)now
;
1586 if (ti
->depth
< MAX_NESTED
) {
1587 te
= &ti
->th_entry
[ti
->depth
];
1589 te
->sc_state
= KERNEL_MODE
;
1592 te
->stime
= (double)now
;
1593 te
->otime
= (double)now
;
1594 te
->ctime
= (double)0;
1595 te
->wtime
= (double)0;
1599 } else if (debugid
& DBG_FUNC_END
) {
1604 se
= &faults
[kd
[i
].arg2
];
1607 if (se
->total_count
== 0)
1613 te
= &ti
->th_entry
[ti
->depth
-1];
1615 if (te
->type
== type
) {
1616 se
->stime_usecs
+= te
->ctime
/ divisor
;
1617 se
->stime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1619 se
->wtime_usecs
+= te
->wtime
/ divisor
;
1620 se
->delta_wtime_usecs
+= te
->wtime
/ divisor
;
1622 secs
= se
->stime_usecs
/ 1000000;
1623 se
->stime_usecs
-= secs
* 1000000;
1624 se
->stime_secs
+= secs
;
1626 secs
= se
->wtime_usecs
/ 1000000;
1627 se
->wtime_usecs
-= secs
* 1000000;
1628 se
->wtime_secs
+= secs
;
1630 secs
= se
->delta_wtime_usecs
/ 1000000;
1631 se
->delta_wtime_usecs
-= secs
* 1000000;
1632 se
->delta_wtime_secs
+= secs
;
1636 if (ti
->depth
== 0) {
1638 * headed back to user mode
1639 * start the time accumulation
1641 te
= &ti
->th_entry
[0];
1642 te
->sc_state
= USER_MODE
;
1644 te
= &ti
->th_entry
[ti
->depth
-1];
1646 te
->stime
= (double)now
;
1647 te
->otime
= (double)now
;
1653 if (ti
->depth
== 0) {
1655 * headed back to user mode
1656 * start the time accumulation
1658 te
= &ti
->th_entry
[0];
1659 te
->sc_state
= USER_MODE
;
1660 te
->stime
= (double)now
;
1661 te
->otime
= (double)now
;
1666 secs
= utime_usecs
/ 1000000;
1667 utime_usecs
-= secs
* 1000000;
1670 secs
= itime_usecs
/ 1000000;
1671 itime_usecs
-= secs
* 1000000;
1674 secs
= delta_itime_usecs
/ 1000000;
1675 delta_itime_usecs
-= secs
* 1000000;
1676 delta_itime_secs
+= secs
;
1678 secs
= otime_usecs
/ 1000000;
1679 otime_usecs
-= secs
* 1000000;
1682 secs
= delta_otime_usecs
/ 1000000;
1683 delta_otime_usecs
-= secs
* 1000000;
1684 delta_otime_secs
+= secs
;
1695 This flag is turned off when calling
1696 quit() due to a set_remove() failure.
1698 if (set_remove_flag
)
1701 printf("sc_usage: ");
1710 mach_timebase_info_data_t info
;
1712 (void) mach_timebase_info (&info
);
1714 divisor
= ( (double)info
.denom
/ (double)info
.numer
) * 1000;
1729 ret
= (int)strtol(str
, &cp
, 10);
1730 if (cp
== str
|| *cp
) {
1731 /* Assume this is a command string and find first matching pid */
1732 for (i
=0; i
< kp_nentries
; i
++) {
1733 if(kp_buffer
[i
].kp_proc
.p_stat
== 0)
1736 if(!strcmp(str
, kp_buffer
[i
].kp_proc
.p_comm
))
1738 strncpy(proc_name
, kp_buffer
[i
].kp_proc
.p_comm
, sizeof(proc_name
)-1);
1739 proc_name
[sizeof(proc_name
)-1] = '\0';
1740 return(kp_buffer
[i
].kp_proc
.p_pid
);
1747 for (i
=0; i
< kp_nentries
; i
++)
1749 if(kp_buffer
[i
].kp_proc
.p_stat
== 0)
1751 else if (kp_buffer
[i
].kp_proc
.p_pid
== ret
) {
1752 strncpy(proc_name
, kp_buffer
[i
].kp_proc
.p_comm
, sizeof(proc_name
)-1);
1753 proc_name
[sizeof(proc_name
)-1] = '\0';
1754 return(kp_buffer
[i
].kp_proc
.p_pid
);
1762 /* Returns index into sc_tab for a mach msg entry */
1764 find_msgcode(int debugid
)
1769 for (indx
=0; indx
< msgcode_cnt
; indx
++)
1771 if (msgcode_tab
[indx
] == ((debugid
& 0x00ffffff) >>2))
1772 return (MAX_SC
+indx
);
1777 argtoi(flag
, req
, str
, base
)
1785 ret
= (int)strtol(str
, &cp
, base
);
1786 if (cp
== str
|| *cp
)
1787 errx(EINVAL
, "-%c flag requires a %s", flag
, req
);