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>
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
93 /* If NUMPARMS from kernel changes, it will be reflected in PATHLENGTH as well */
95 #define PATHLENGTH (NUMPARMS*sizeof(long))
97 char *state_name
[] = {
106 #define KERNEL_MODE 1
127 char pathname
[PATHLENGTH
+ 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
;
175 int mach_stkhandoff
= 0;
177 int mach_vmfault
= 0;
184 #define DBG_FUNC_ALL (DBG_FUNC_START | DBG_FUNC_END)
185 #define DBG_FUNC_MASK 0xfffffffc
192 /* Default divisor */
193 #define DIVISOR 16.6666 /* Trace divisor converts to microseconds */
194 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;
209 extern char **environ
;
217 int argtopid(char *);
218 int argtoi(int, char*, char*, int);
225 void leave() /* exit under normal conditions -- INT handler */
228 if (no_screen_refresh
== 0) {
234 set_pidcheck(pid
, 0);
239 void err_leave(s
) /* exit under error conditions */
243 if (no_screen_refresh
== 0) {
249 printf("sc_usage: ");
254 set_pidcheck(pid
, 0);
262 if (no_screen_refresh
== 0)
267 exit_usage(char *myname
) {
269 fprintf(stderr
, "Usage: %s [-c codefile] [-e] [-l] [-sn] pid | cmd | -E execute path\n", myname
);
270 fprintf(stderr
, " -c name of codefile containing mappings for syscalls\n");
271 fprintf(stderr
, " Default is /usr/share/misc/trace.codes\n");
272 fprintf(stderr
, " -e enable sort by call count\n");
273 fprintf(stderr
, " -l turn off top style output\n");
274 fprintf(stderr
, " -sn change sample rate to every n seconds\n");
275 fprintf(stderr
, " pid selects process to sample\n");
276 fprintf(stderr
, " cmd selects command to sample\n");
277 fprintf(stderr
, " -E Execute the given path and optional arguments\n");
283 #define usec_to_1000ths(t) ((t) / 1000)
285 void print_time(char *p
, unsigned int useconds
, unsigned int seconds
)
289 minutes
= seconds
/ 60;
290 hours
= minutes
/ 60;
292 if (minutes
< 100) { // up to 100 minutes
293 sprintf(p
, "%2ld:%02ld.%03ld", minutes
, (unsigned long)(seconds
% 60),
294 (unsigned long)usec_to_1000ths(useconds
));
296 else if (hours
< 100) { // up to 100 hours
297 sprintf(p
, "%2ld:%02ld:%02ld ", hours
, (minutes
% 60),
298 (unsigned long)(seconds
% 60));
301 sprintf(p
, "%4ld hrs ", hours
);
310 char *myname
= "sc_usage";
311 char *codefile
= "/usr/share/misc/trace.codes";
314 int delay
= Default_DELAY
;
315 void screen_update();
319 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,(pid_t
)0,(caddr_t
)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
, (caddr_t
)1, 0); /* PT_CONTINUE */
464 if ((sort_now
= 10 / delay
) < 2)
468 (void)screen_update();
477 for (i
= 0; i
< (10 * delay
) && newLINES
== 0; i
++) {
479 if (no_screen_refresh
== 0) {
480 if ((c
= getch()) != ERR
&& (char)c
== 'q')
492 No need to check for initscr error return.
493 We won't get here if it fails on the first call.
499 topn
= LINES
- Header_lines
;
502 (void)screen_update();
507 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 long)(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 long)(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 long)(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");
552 if (tbuf
[COLS
-2] != '\n') {
556 if (no_screen_refresh
)
579 if (no_screen_refresh
== 0) {
580 /* clear for new display */
586 sprintf(tbuf
, "%-14.14s", proc_name
);
594 p2
= "context switch ";
596 p2
= "context switches";
597 if (num_of_threads
== 1)
602 sprintf(&tbuf
[clen
], " %4d %s %4d %s %4d %s",
603 preempted
, p1
, csw
, p2
, num_of_threads
, p3
);
604 clen
+= strlen(&tbuf
[clen
]);
607 * Display the current time.
608 * "ctime" always returns a string that looks like this:
610 * Sun Sep 16 01:03:52 1973
611 * 012345678901234567890123
614 * We want indices 11 thru 18 (length 8).
616 curr_time
= time((long *)0);
619 start_time
= curr_time
;
621 elapsed_secs
= curr_time
- start_time
;
622 minutes
= elapsed_secs
/ 60;
623 hours
= minutes
/ 60;
625 memset(&tbuf
[clen
], ' ', 78 - clen
);
629 sprintf(&tbuf
[clen
], "%-8.8s\n", &(ctime(&curr_time
)[11]));
631 if (tbuf
[COLS
-2] != '\n') {
635 if (no_screen_refresh
)
640 if (total_faults
== 1)
648 sprintf(tbuf
, " %4d %s %4d %s",
649 total_faults
, p1
, scalls
, p2
);
652 sprintf(&tbuf
[clen
], " %3ld:%02ld:%02ld\n",
653 (long)hours
, (long)(minutes
% 60), (long)(elapsed_secs
% 60));
655 if (tbuf
[COLS
-2] != '\n') {
659 if (no_screen_refresh
)
666 sprintf(tbuf
, "\nTYPE NUMBER CPU_TIME WAIT_TIME\n");
668 if (tbuf
[COLS
-2] != '\n') {
672 if (no_screen_refresh
)
677 sprintf(tbuf
, "------------------------------------------------------------------------------\n");
678 if (tbuf
[COLS
-2] != '\n') {
682 if (no_screen_refresh
)
690 sprintf(tbuf
, "System Idle ");
693 print_time(&tbuf
[clen
], (unsigned long)(itime_usecs
), itime_secs
);
694 clen
+= strlen(&tbuf
[clen
]);
696 if (delta_itime_usecs
|| delta_itime_secs
) {
698 sprintf(&tbuf
[clen
], "(");
699 clen
+= strlen(&tbuf
[clen
]);
701 print_time(&tbuf
[clen
], (unsigned long)(delta_itime_usecs
), delta_itime_secs
);
702 clen
+= strlen(&tbuf
[clen
]);
704 sprintf(&tbuf
[clen
], ")");
705 clen
+= strlen(&tbuf
[clen
]);
707 sprintf(&tbuf
[clen
], "\n");
709 if (tbuf
[COLS
-2] != '\n') {
713 if (no_screen_refresh
)
721 sprintf(tbuf
, "System Busy ");
724 print_time(&tbuf
[clen
], (unsigned long)(otime_usecs
), otime_secs
);
725 clen
+= strlen(&tbuf
[clen
]);
727 if (delta_otime_usecs
|| delta_otime_secs
) {
729 sprintf(&tbuf
[clen
], "(");
730 clen
+= strlen(&tbuf
[clen
]);
732 print_time(&tbuf
[clen
], (unsigned long)(delta_otime_usecs
), delta_otime_secs
);
733 clen
+= strlen(&tbuf
[clen
]);
735 sprintf(&tbuf
[clen
], ")");
736 clen
+= strlen(&tbuf
[clen
]);
738 sprintf(&tbuf
[clen
], "\n");
740 if (tbuf
[COLS
-2] != '\n') {
744 if (no_screen_refresh
)
751 sprintf(tbuf
, "%-14.14s Usermode ", proc_name
);
754 print_time(&tbuf
[clen
], (unsigned long)(utime_usecs
), utime_secs
);
755 clen
+= strlen(&tbuf
[clen
]);
756 sprintf(&tbuf
[clen
], "\n");
758 if (tbuf
[COLS
-2] != '\n') {
762 if (no_screen_refresh
)
769 max_rows
= topn
- (num_of_threads
+ 3);
773 for (output_lf
= 1, n
= 1; rows
< max_rows
&& n
< MAX_FAULTS
; n
++) {
776 if (se
->total_count
== 0)
778 if (output_lf
== 1) {
780 if (no_screen_refresh
)
786 if (rows
>= max_rows
)
795 if (no_screen_refresh
)
801 for (i
= 0; rows
< max_rows
; i
++) {
803 n
= sort_by_count
[i
];
805 n
= sort_by_wtime
[i
];
808 print_row(&sc_tab
[n
], 0);
811 if (no_screen_refresh
== 0) {
814 while (rows
++ < max_rows
)
819 if (num_of_threads
) {
820 sprintf(tbuf
, "\nCURRENT_TYPE LAST_PATHNAME_WAITED_FOR CUR_WAIT_TIME THRD# PRI\n");
822 if (tbuf
[COLS
-2] != '\n') {
826 if (no_screen_refresh
)
831 sprintf(tbuf
, "------------------------------------------------------------------------------\n");
832 if (tbuf
[COLS
-2] != '\n') {
836 if (no_screen_refresh
)
843 for (i
= 0; i
< num_of_threads
; i
++, ti
++) {
846 int secs
, time_secs
, time_usecs
;
848 now
= mach_absolute_time();
850 while (ti
->thread
== 0 && ti
< &th_state
[MAX_THREADS
])
852 if (ti
== &th_state
[MAX_THREADS
])
856 te
= &ti
->th_entry
[ti
->depth
- 1];
858 if (te
->sc_state
== WAITING
) {
860 sprintf(tbuf
, "%-23.23s", sc_tab
[te
->code
].name
);
862 sprintf(tbuf
, "%-23.23s", "vm_fault");
864 sprintf(tbuf
, "%-23.23s", state_name
[te
->sc_state
]);
866 te
= &ti
->th_entry
[0];
867 sprintf(tbuf
, "%-23.23s", state_name
[te
->sc_state
]);
871 /* print the tail end of the pathname */
872 plen
= strlen(ti
->pathname
);
877 sprintf(&tbuf
[clen
], " %-34.34s ", &ti
->pathname
[plen
]);
879 clen
+= strlen(&tbuf
[clen
]);
881 time_usecs
= (unsigned long)(((double)now
- te
->otime
) / divisor
);
882 secs
= time_usecs
/ 1000000;
883 time_usecs
-= secs
* 1000000;
886 print_time(&tbuf
[clen
], time_usecs
, time_secs
);
887 clen
+= strlen(&tbuf
[clen
]);
888 sprintf(&tbuf
[clen
], " %2d %3d\n", i
, ti
->curpri
);
890 if (tbuf
[COLS
-2] != '\n') {
894 if (no_screen_refresh
)
899 if (no_screen_refresh
== 0) {
903 printf("\n=================\n");
907 for (i
= 0; i
< (MAX_SC
+ msgcode_cnt
); i
++) {
908 if ((n
= sort_by_count
[i
]) == -1)
910 sc_tab
[n
].delta_count
= 0;
911 sc_tab
[n
].waiting
= 0;
912 sc_tab
[n
].delta_wtime_usecs
= 0;
913 sc_tab
[n
].delta_wtime_secs
= 0;
915 for (i
= 1; i
< MAX_FAULTS
; i
++) {
916 faults
[i
].delta_count
= 0;
917 faults
[i
].waiting
= 0;
918 faults
[i
].delta_wtime_usecs
= 0;
919 faults
[i
].delta_wtime_secs
= 0;
925 delta_itime_secs
= 0;
926 delta_itime_usecs
= 0;
927 delta_otime_secs
= 0;
928 delta_otime_usecs
= 0;
935 for (i
= 0; i
< (MAX_SC
+ msgcode_cnt
) ; i
++) {
936 sc_tab
[i
].delta_count
= 0;
937 sc_tab
[i
].total_count
= 0;
938 sc_tab
[i
].waiting
= 0;
939 sc_tab
[i
].delta_wtime_usecs
= 0;
940 sc_tab
[i
].delta_wtime_secs
= 0;
941 sc_tab
[i
].wtime_usecs
= 0;
942 sc_tab
[i
].wtime_secs
= 0;
943 sc_tab
[i
].stime_usecs
= 0;
944 sc_tab
[i
].stime_secs
= 0;
946 for (i
= 1; i
< MAX_FAULTS
; i
++) {
947 faults
[i
].delta_count
= 0;
948 faults
[i
].total_count
= 0;
949 faults
[i
].waiting
= 0;
950 faults
[i
].delta_wtime_usecs
= 0;
951 faults
[i
].delta_wtime_secs
= 0;
952 faults
[i
].wtime_usecs
= 0;
953 faults
[i
].wtime_secs
= 0;
954 faults
[i
].stime_usecs
= 0;
955 faults
[i
].stime_secs
= 0;
967 delta_itime_secs
= 0;
968 delta_itime_usecs
= 0;
971 delta_otime_secs
= 0;
972 delta_otime_usecs
= 0;
976 sc_tab_init(char *codefile
) {
983 if ((fp
= fopen(codefile
,"r")) == (FILE *)0) {
984 printf("Failed to open code description file %s\n", codefile
);
988 n
= fscanf(fp
, "%d\n", &cnt
);
992 /* Count Mach message MSG_ codes */
993 for (msgcode_cnt
=0;;) {
994 n
= fscanf(fp
, "%x%s\n", &code
, &name
[0]);
997 if (strncmp ("MSG_", &name
[0], 4) == 0)
999 if (strcmp("USER_TEST", &name
[0]) == 0)
1003 sc_tab
= (struct sc_entry
*)malloc((MAX_SC
+msgcode_cnt
) * sizeof (struct sc_entry
));
1005 quit("can't allocate memory for system call table\n");
1006 bzero(sc_tab
,(MAX_SC
+msgcode_cnt
) * sizeof (struct sc_entry
));
1008 msgcode_tab
= (int *)malloc(msgcode_cnt
* sizeof(int));
1010 quit("can't allocate memory for msgcode table\n");
1011 bzero(msgcode_tab
,(msgcode_cnt
* sizeof(int)));
1013 sort_by_count
= (int *)malloc((MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
1015 quit("can't allocate memory for sort_by_count table\n");
1016 bzero(sort_by_count
,(MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
1018 sort_by_wtime
= (int *)malloc((MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
1020 quit("can't allocate memory for sort_by_wtime table\n");
1021 bzero(sort_by_wtime
, (MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
1026 n
= fscanf(fp
, "%d\n", &cnt
);
1032 n
= fscanf(fp
, "%x%s\n", &code
, &name
[0]);
1037 if (strcmp("MACH_vmfault", &name
[0]) == 0) {
1038 mach_vmfault
= code
;
1041 if (strcmp("MACH_SCHED", &name
[0]) == 0) {
1045 if (strcmp("MACH_STKHANDOFF", &name
[0]) == 0) {
1046 mach_stkhandoff
= code
;
1049 if (strcmp("VFS_LOOKUP", &name
[0]) == 0) {
1053 if (strcmp("BSC_SysCall", &name
[0]) == 0) {
1057 if (strcmp("MACH_SysCall", &name
[0]) == 0) {
1061 if (strcmp("BSC_exit", &name
[0]) == 0) {
1065 if (strncmp("MSG_", &name
[0], 4) == 0) {
1066 msgcode_tab
[msgcode_indx
] = ((code
& 0x00ffffff) >>2);
1067 n
= MAX_SC
+ msgcode_indx
;
1068 strncpy(&sc_tab
[n
].name
[0], &name
[4], 31 );
1072 if (strncmp("MSC_", &name
[0], 4) == 0) {
1073 n
= 512 + ((code
>>2) & 0x1ff);
1074 strcpy(&sc_tab
[n
].name
[0], &name
[4]);
1077 if (strncmp("BSC_", &name
[0], 4) == 0) {
1078 n
= (code
>>2) & 0x1ff;
1079 strcpy(&sc_tab
[n
].name
[0], &name
[4]);
1082 if (strcmp("USER_TEST", &name
[0]) == 0)
1085 strcpy(&faults
[1].name
[0], "zero_fill");
1086 strcpy(&faults
[2].name
[0], "pagein");
1087 strcpy(&faults
[3].name
[0], "copy_on_write");
1088 strcpy(&faults
[4].name
[0], "cache_hit");
1095 struct kinfo_proc
*kp
;
1099 mib
[2] = KERN_PROC_ALL
;
1102 if (sysctl(mib
, 4, NULL
, &bufSize
, NULL
, 0) < 0)
1103 quit("trace facility failure, KERN_PROC_ALL\n");
1105 if((kp
= (struct kinfo_proc
*)malloc(bufSize
)) == (struct kinfo_proc
*)0)
1106 quit("can't allocate memory for proc buffer\n");
1108 if (sysctl(mib
, 4, kp
, &bufSize
, NULL
, 0) < 0)
1109 quit("trace facility failure, KERN_PROC_ALL\n");
1111 kp_nentries
= bufSize
/ sizeof(struct kinfo_proc
);
1115 struct th_info
*find_thread(int thread
) {
1118 for (ti
= th_state
; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1119 if (ti
->thread
== thread
)
1122 return ((struct th_info
*)0);
1127 cmp_wtime(struct sc_entry
*s1
, struct sc_entry
*s2
) {
1129 if (s1
->wtime_secs
< s2
->wtime_secs
)
1131 if (s1
->wtime_secs
> s2
->wtime_secs
)
1133 if (s1
->wtime_usecs
<= s2
->wtime_usecs
)
1141 int i
, n
, k
, cnt
, secs
;
1143 struct sc_entry
*se
;
1147 now
= mach_absolute_time();
1149 for (ti
= th_state
; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1150 if (ti
->thread
== 0)
1154 te
= &ti
->th_entry
[ti
->depth
-1];
1156 if (te
->sc_state
== WAITING
) {
1158 se
= &sc_tab
[te
->code
];
1160 se
= &faults
[DBG_PAGEIN_FAULT
];
1162 se
->wtime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1163 se
->delta_wtime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1164 te
->stime
= (double)now
;
1166 secs
= se
->wtime_usecs
/ 1000000;
1167 se
->wtime_usecs
-= secs
* 1000000;
1168 se
->wtime_secs
+= secs
;
1170 secs
= se
->delta_wtime_usecs
/ 1000000;
1171 se
->delta_wtime_usecs
-= secs
* 1000000;
1172 se
->delta_wtime_secs
+= secs
;
1175 te
= &ti
->th_entry
[0];
1177 if (te
->sc_state
== PREEMPTED
) {
1178 if ((unsigned long)(((double)now
- te
->otime
) / divisor
) > 5000000) {
1181 ti
->pathptr
= (long *)0;
1182 ti
->pathname
[0] = 0;
1188 if ((called
% sort_now
) == 0) {
1189 sort_by_count
[0] = -1;
1190 sort_by_wtime
[0] = -1;
1191 for (cnt
= 1, n
= 1; n
< (MAX_SC
+ msgcode_cnt
); n
++) {
1192 if (sc_tab
[n
].total_count
) {
1193 for (i
= 0; i
< cnt
; i
++) {
1194 if ((k
= sort_by_count
[i
]) == -1 ||
1195 sc_tab
[n
].total_count
> sc_tab
[k
].total_count
) {
1197 for (k
= cnt
- 1; k
>= i
; k
--)
1198 sort_by_count
[k
+1] = sort_by_count
[k
];
1199 sort_by_count
[i
] = n
;
1203 if (how_to_sort
== 0) {
1204 for (i
= 0; i
< cnt
; i
++) {
1205 if ((k
= sort_by_wtime
[i
]) == -1 ||
1206 cmp_wtime(&sc_tab
[n
], &sc_tab
[k
])) {
1208 for (k
= cnt
- 1; k
>= i
; k
--)
1209 sort_by_wtime
[k
+1] = sort_by_wtime
[k
];
1210 sort_by_wtime
[i
] = n
;
1226 mib
[1] = KERN_KDEBUG
;
1227 mib
[2] = KERN_KDENABLE
; /* protocol */
1230 mib
[5] = 0; /* no flags */
1231 if (sysctl(mib
, 4, NULL
, &needed
, NULL
, 0) < 0)
1232 quit("trace facility failure, KERN_KDENABLE\n");
1241 set_numbufs(int nbufs
)
1244 mib
[1] = KERN_KDEBUG
;
1245 mib
[2] = KERN_KDSETBUF
;
1248 mib
[5] = 0; /* no flags */
1249 if (sysctl(mib
, 4, NULL
, &needed
, NULL
, 0) < 0)
1250 quit("trace facility failure, KERN_KDSETBUF\n");
1253 mib
[1] = KERN_KDEBUG
;
1254 mib
[2] = KERN_KDSETUP
;
1257 mib
[5] = 0; /* no flags */
1258 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1259 quit("trace facility failure, KERN_KDSETUP\n");
1264 set_pidcheck(int pid
, int on_off
)
1268 kr
.type
= KDBG_TYPENONE
;
1271 needed
= sizeof(kd_regtype
);
1273 mib
[1] = KERN_KDEBUG
;
1274 mib
[2] = KERN_KDPIDTR
;
1278 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0) {
1280 printf("pid %d does not exist\n", pid
);
1288 get_bufinfo(kbufinfo_t
*val
)
1290 needed
= sizeof (*val
);
1292 mib
[1] = KERN_KDEBUG
;
1293 mib
[2] = KERN_KDGETBUF
;
1296 mib
[5] = 0; /* no flags */
1297 if (sysctl(mib
, 3, val
, &needed
, 0, 0) < 0)
1298 quit("trace facility failure, KERN_KDGETBUF\n");
1310 mib
[1] = KERN_KDEBUG
;
1311 mib
[2] = KERN_KDREMOVE
; /* protocol */
1314 mib
[5] = 0; /* no flags */
1316 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1318 set_remove_flag
= 0;
1321 quit("the trace facility is currently in use...\n fs_usage, sc_usage, and latency use this feature.\n\n");
1323 quit("trace facility failure, KERN_KDREMOVE\n");
1331 kr
.type
= KDBG_RANGETYPE
;
1334 needed
= sizeof(kd_regtype
);
1336 mib
[1] = KERN_KDEBUG
;
1337 mib
[2] = KERN_KDSETREG
;
1340 mib
[5] = 0; /* no flags */
1341 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0)
1342 quit("trace facility failure, KERN_KDSETREG\n");
1345 mib
[1] = KERN_KDEBUG
;
1346 mib
[2] = KERN_KDSETUP
;
1349 mib
[5] = 0; /* no flags */
1350 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1351 quit("trace facility failure, KERN_KDSETUP\n");
1363 /* Get kernel buffer information */
1364 get_bufinfo(&bufinfo
);
1368 needed
= bufinfo
.nkdbufs
* sizeof(kd_buf
);
1370 mib
[1] = KERN_KDEBUG
;
1371 mib
[2] = KERN_KDREADTR
;
1374 mib
[5] = 0; /* no flags */
1375 if (sysctl(mib
, 3, my_buffer
, &needed
, NULL
, 0) < 0)
1376 quit("trace facility failure, KERN_KDREADTR\n");
1379 if (bufinfo
.flags
& KDBG_WRAPPED
) {
1380 for (i
= 0; i
< MAX_THREADS
; i
++) {
1381 th_state
[i
].depth
= 0;
1382 th_state
[i
].thread
= 0;
1383 th_state
[i
].vfslookup
= 0;
1384 th_state
[i
].pathptr
= (long *)0;
1385 th_state
[i
].pathname
[0] = 0;
1392 set_pidcheck(pid
, 1);
1393 set_enable(1); /* re-enable kernel logging */
1395 kd
= (kd_buf
*)my_buffer
;
1397 for (i
= 0; i
< count
; i
++) {
1398 int debugid
, baseid
, thread
;
1401 struct th_info
*ti
, *switched_out
, *switched_in
;
1402 struct sc_entry
*se
;
1405 thread
= kd
[i
].arg5
& KDBG_THREAD_MASK
;
1406 debugid
= kd
[i
].debugid
;
1407 type
= kd
[i
].debugid
& DBG_FUNC_MASK
;
1410 switched_out
= (struct th_info
*)0;
1411 switched_in
= (struct th_info
*)0;
1413 now
= kd
[i
].timestamp
;
1415 baseid
= debugid
& 0xffff0000;
1417 if (type
== vfs_lookup
) {
1420 if ((ti
= find_thread(thread
)) == (struct th_info
*)0)
1423 if (ti
->vfslookup
== 1) {
1425 memset(&ti
->pathname
[0], 0, (PATHLENGTH
+ 1));
1426 sargptr
= (long *)&ti
->pathname
[0];
1428 *sargptr
++ = kd
[i
].arg2
;
1429 *sargptr
++ = kd
[i
].arg3
;
1430 *sargptr
++ = kd
[i
].arg4
;
1431 ti
->pathptr
= sargptr
;
1433 } else if (ti
->vfslookup
> 1) {
1435 sargptr
= ti
->pathptr
;
1438 We don't want to overrun our pathname buffer if the
1439 kernel sends us more VFS_LOOKUP entries than we can
1443 if ((long *)sargptr
>= (long *)&ti
->pathname
[PATHLENGTH
])
1447 We need to detect consecutive vfslookup entries.
1448 So, if we get here and find a START entry,
1449 fake the pathptr so we can bypass all further
1453 if (debugid
& DBG_FUNC_START
)
1455 (long *)ti
->pathptr
= (long *)&ti
->pathname
[PATHLENGTH
];
1459 *sargptr
++ = kd
[i
].arg1
;
1460 *sargptr
++ = kd
[i
].arg2
;
1461 *sargptr
++ = kd
[i
].arg3
;
1462 *sargptr
++ = kd
[i
].arg4
;
1463 ti
->pathptr
= sargptr
;
1467 } else if (baseid
== bsc_base
)
1468 code
= (debugid
>> 2) & 0x1ff;
1469 else if (baseid
== msc_base
)
1470 code
= 512 + ((debugid
>> 2) & 0x1ff);
1471 else if (type
== mach_sched
|| type
== mach_stkhandoff
) {
1472 switched_out
= find_thread((kd
[i
].arg5
& KDBG_THREAD_MASK
));
1473 switched_in
= find_thread(kd
[i
].arg2
);
1476 itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1477 delta_itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1479 } else if (in_other
) {
1480 otime_usecs
+= ((double)now
- other_start
) / divisor
;
1481 delta_otime_usecs
+= ((double)now
- other_start
) / divisor
;
1484 if ( !switched_in
) {
1486 * not one of the target proc's threads
1488 if (now_collect_cpu_time
) {
1489 if (kd
[i
].arg4
== 0) {
1491 idle_start
= (double)now
;
1494 other_start
= (double)now
;
1498 if ( !switched_in
&& !switched_out
)
1502 else if ((baseid
& 0xff000000) == 0xff000000) {
1503 code
= find_msgcode (debugid
);
1506 } else if (baseid
!= mach_vmfault
)
1509 if (switched_out
|| switched_in
) {
1512 ti
->curpri
= kd
[i
].arg3
;
1515 te
= &ti
->th_entry
[ti
->depth
-1];
1517 if (te
->sc_state
== KERNEL_MODE
)
1518 te
->ctime
+= (double)now
- te
->stime
;
1519 te
->sc_state
= WAITING
;
1524 te
= &ti
->th_entry
[0];
1526 if (te
->sc_state
== USER_MODE
)
1527 utime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1528 te
->sc_state
= PREEMPTED
;
1531 te
->stime
= (double)now
;
1532 te
->otime
= (double)now
;
1533 now_collect_cpu_time
= 1;
1538 ti
->curpri
= kd
[i
].arg4
;
1541 te
= &ti
->th_entry
[ti
->depth
-1];
1543 if (te
->sc_state
== WAITING
)
1544 te
->wtime
+= (double)now
- te
->stime
;
1545 te
->sc_state
= KERNEL_MODE
;
1547 te
= &ti
->th_entry
[0];
1549 te
->sc_state
= USER_MODE
;
1551 te
->stime
= (double)now
;
1552 te
->otime
= (double)now
;
1556 if ((ti
= find_thread(thread
)) == (struct th_info
*)0) {
1557 for (ti
= &th_state
[0]; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1558 if (ti
->thread
== 0) {
1559 ti
->thread
= thread
;
1564 if (ti
== &th_state
[MAX_THREADS
])
1567 if (debugid
& DBG_FUNC_START
) {
1571 te
= &ti
->th_entry
[ti
->depth
-1];
1573 if (te
->sc_state
== KERNEL_MODE
)
1574 te
->ctime
+= (double)now
- te
->stime
;
1576 te
= &ti
->th_entry
[0];
1578 if (te
->sc_state
== USER_MODE
)
1579 utime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1581 te
->stime
= (double)now
;
1582 te
->otime
= (double)now
;
1584 if (ti
->depth
< MAX_NESTED
) {
1585 te
= &ti
->th_entry
[ti
->depth
];
1587 te
->sc_state
= KERNEL_MODE
;
1590 te
->stime
= (double)now
;
1591 te
->otime
= (double)now
;
1592 te
->ctime
= (double)0;
1593 te
->wtime
= (double)0;
1597 } else if (debugid
& DBG_FUNC_END
) {
1602 se
= &faults
[kd
[i
].arg2
];
1605 if (se
->total_count
== 0)
1611 te
= &ti
->th_entry
[ti
->depth
-1];
1613 if (te
->type
== type
) {
1614 se
->stime_usecs
+= te
->ctime
/ divisor
;
1615 se
->stime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1617 se
->wtime_usecs
+= te
->wtime
/ divisor
;
1618 se
->delta_wtime_usecs
+= te
->wtime
/ divisor
;
1620 secs
= se
->stime_usecs
/ 1000000;
1621 se
->stime_usecs
-= secs
* 1000000;
1622 se
->stime_secs
+= secs
;
1624 secs
= se
->wtime_usecs
/ 1000000;
1625 se
->wtime_usecs
-= secs
* 1000000;
1626 se
->wtime_secs
+= secs
;
1628 secs
= se
->delta_wtime_usecs
/ 1000000;
1629 se
->delta_wtime_usecs
-= secs
* 1000000;
1630 se
->delta_wtime_secs
+= secs
;
1634 if (ti
->depth
== 0) {
1636 * headed back to user mode
1637 * start the time accumulation
1639 te
= &ti
->th_entry
[0];
1640 te
->sc_state
= USER_MODE
;
1642 te
= &ti
->th_entry
[ti
->depth
-1];
1644 te
->stime
= (double)now
;
1645 te
->otime
= (double)now
;
1651 if (ti
->depth
== 0) {
1653 * headed back to user mode
1654 * start the time accumulation
1656 te
= &ti
->th_entry
[0];
1657 te
->sc_state
= USER_MODE
;
1658 te
->stime
= (double)now
;
1659 te
->otime
= (double)now
;
1664 secs
= utime_usecs
/ 1000000;
1665 utime_usecs
-= secs
* 1000000;
1668 secs
= itime_usecs
/ 1000000;
1669 itime_usecs
-= secs
* 1000000;
1672 secs
= delta_itime_usecs
/ 1000000;
1673 delta_itime_usecs
-= secs
* 1000000;
1674 delta_itime_secs
+= secs
;
1676 secs
= otime_usecs
/ 1000000;
1677 otime_usecs
-= secs
* 1000000;
1680 secs
= delta_otime_usecs
/ 1000000;
1681 delta_otime_usecs
-= secs
* 1000000;
1682 delta_otime_secs
+= secs
;
1692 This flag is turned off when calling
1693 quit() due to a set_remove() failure.
1695 if (set_remove_flag
)
1698 printf("sc_usage: ");
1707 mach_timebase_info_data_t info
;
1709 (void) mach_timebase_info (&info
);
1711 divisor
= ( (double)info
.denom
/ (double)info
.numer
) * 1000;
1727 ret
= (int)strtol(str
, &cp
, 10);
1728 if (cp
== str
|| *cp
) {
1729 /* Assume this is a command string and find first matching pid */
1730 for (i
=0; i
< kp_nentries
; i
++) {
1731 if(kp_buffer
[i
].kp_proc
.p_stat
== 0)
1734 if(!strcmp(str
, kp_buffer
[i
].kp_proc
.p_comm
))
1736 strncpy(proc_name
, kp_buffer
[i
].kp_proc
.p_comm
, sizeof(proc_name
)-1);
1737 proc_name
[sizeof(proc_name
)-1] = '\0';
1738 return(kp_buffer
[i
].kp_proc
.p_pid
);
1745 for (i
=0; i
< kp_nentries
; i
++)
1747 if(kp_buffer
[i
].kp_proc
.p_stat
== 0)
1749 else if (kp_buffer
[i
].kp_proc
.p_pid
== ret
) {
1750 strncpy(proc_name
, kp_buffer
[i
].kp_proc
.p_comm
, sizeof(proc_name
)-1);
1751 proc_name
[sizeof(proc_name
)-1] = '\0';
1752 return(kp_buffer
[i
].kp_proc
.p_pid
);
1760 /* Returns index into sc_tab for a mach msg entry */
1762 find_msgcode(int debugid
)
1767 for (indx
=0; indx
< msgcode_cnt
; indx
++)
1769 if (msgcode_tab
[indx
] == ((debugid
& 0x00ffffff) >>2))
1770 return (MAX_SC
+indx
);
1776 argtoi(flag
, req
, str
, base
)
1784 ret
= (int)strtol(str
, &cp
, base
);
1785 if (cp
== str
|| *cp
)
1786 errx(EINVAL
, "-%c flag requires a %s", flag
, req
);