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);
221 void get_bufinfo(kbufinfo_t
*);
228 void leave() /* exit under normal conditions -- INT handler */
231 if (no_screen_refresh
== 0) {
237 set_pidcheck(pid
, 0);
242 void err_leave(s
) /* exit under error conditions */
246 if (no_screen_refresh
== 0) {
252 printf("sc_usage: ");
257 set_pidcheck(pid
, 0);
265 if (no_screen_refresh
== 0)
270 exit_usage(char *myname
) {
272 fprintf(stderr
, "Usage: %s [-c codefile] [-e] [-l] [-sn] pid | cmd | -E execute path\n", myname
);
273 fprintf(stderr
, " -c name of codefile containing mappings for syscalls\n");
274 fprintf(stderr
, " Default is /usr/share/misc/trace.codes\n");
275 fprintf(stderr
, " -e enable sort by call count\n");
276 fprintf(stderr
, " -l turn off top style output\n");
277 fprintf(stderr
, " -sn change sample rate to every n seconds\n");
278 fprintf(stderr
, " pid selects process to sample\n");
279 fprintf(stderr
, " cmd selects command to sample\n");
280 fprintf(stderr
, " -E Execute the given path and optional arguments\n");
286 #define usec_to_1000ths(t) ((t) / 1000)
288 void print_time(char *p
, unsigned int useconds
, unsigned int seconds
)
292 minutes
= seconds
/ 60;
293 hours
= minutes
/ 60;
295 if (minutes
< 100) { // up to 100 minutes
296 sprintf(p
, "%02ld:%02ld.%03ld", minutes
, (unsigned long)(seconds
% 60),
297 (unsigned long)usec_to_1000ths(useconds
));
299 else if (hours
< 100) { // up to 100 hours
300 sprintf(p
, "%02ld:%02ld:%02ld ", hours
, (minutes
% 60),
301 (unsigned long)(seconds
% 60));
304 sprintf(p
, "%4ld hrs ", hours
);
313 char *myname
= "sc_usage";
314 char *codefile
= "/usr/share/misc/trace.codes";
317 int delay
= Default_DELAY
;
318 void screen_update();
322 void reset_counters();
324 if ( geteuid() != 0 ) {
325 printf("'sc_usage' must be run as root...\n");
329 if (0 != reexec_to_match_kernel()) {
330 fprintf(stderr
, "Could not re-execute: %d\n", errno
);
336 if ((myname
= rindex(argv
[0], '/')) == 0) {
344 while ((ch
= getopt(argc
, argv
, "c:els:d:E")) != EOF
) {
347 delay
= argtoi('s', "decimal number", optarg
, 10);
353 no_screen_refresh
= 1;
362 /* exit_usage(myname);*/
363 exit_usage("default");
369 sc_tab_init(codefile
);
375 /* parse a pid or a command */
376 if((pid
= argtopid(argv
[optind
])) < 0 )
380 { /* execute this command */
385 ptr
= strrchr(argv
[optind
], '/');
391 strncpy(proc_name
, ptr
, sizeof(proc_name
)-1);
392 proc_name
[sizeof(proc_name
)-1] = '\0';
402 fprintf(stderr
, "Starting program: %s\n", argv
[optind
]);
405 switch ((pid
= vfork()))
412 ptrace(0,(pid_t
)0,(caddr_t
)0,0); /* PT_TRACE_ME */
413 execve(argv
[optind
], &argv
[optind
], environ
);
428 if (no_screen_refresh
== 0) {
430 /* initializes curses and screen (last) */
431 if (initscr() == (WINDOW
*) 0)
433 printf("Unrecognized TERM type, try vt100\n");
445 /* set up signal handlers */
446 signal(SIGINT
, leave
);
447 signal(SIGQUIT
, leave
);
448 signal(SIGHUP
, leave
);
449 signal(SIGTERM
, leave
);
450 signal(SIGWINCH
, sigwinch
);
452 if (no_screen_refresh
== 0)
453 topn
= LINES
- Header_lines
;
460 set_numbufs(SAMPLE_SIZE
);
462 set_pidcheck(pid
, 1);
465 ptrace(7, pid
, (caddr_t
)1, 0); /* PT_CONTINUE */
470 if ((sort_now
= 10 / delay
) < 2)
473 get_bufinfo(&bufinfo
);
475 my_buffer
= malloc(bufinfo
.nkdbufs
* sizeof(kd_buf
));
476 if(my_buffer
== (char *) 0)
477 quit("can't allocate memory for tracing info\n");
480 (void)screen_update();
489 for (i
= 0; i
< (10 * delay
) && newLINES
== 0; i
++) {
491 if (no_screen_refresh
== 0) {
492 if ((c
= getch()) != ERR
&& (char)c
== 'q')
504 No need to check for initscr error return.
505 We won't get here if it fails on the first call.
511 topn
= LINES
- Header_lines
;
514 (void)screen_update();
519 print_row(struct sc_entry
*se
, int no_wtime
) {
524 sprintf(tbuf
, "%-23.23s %8d(%d)", se
->name
, se
->total_count
, se
->delta_count
);
526 sprintf(tbuf
, "%-23.23s %8d", se
->name
, se
->total_count
);
529 memset(&tbuf
[clen
], ' ', 45 - clen
);
531 print_time(&tbuf
[45], (unsigned long)(se
->stime_usecs
), se
->stime_secs
);
534 if (no_wtime
== 0 && (se
->wtime_usecs
|| se
->wtime_secs
)) {
535 sprintf(&tbuf
[clen
], " ");
536 clen
+= strlen(&tbuf
[clen
]);
538 print_time(&tbuf
[clen
], (unsigned long)(se
->wtime_usecs
), se
->wtime_secs
);
539 clen
+= strlen(&tbuf
[clen
]);
541 if (se
->waiting
|| se
->delta_wtime_usecs
|| se
->delta_wtime_secs
) {
543 sprintf(&tbuf
[clen
], "(");
544 clen
+= strlen(&tbuf
[clen
]);
546 print_time(&tbuf
[clen
], (unsigned long)(se
->delta_wtime_usecs
),
547 se
->delta_wtime_secs
);
548 clen
+= strlen(&tbuf
[clen
]);
550 sprintf(&tbuf
[clen
], ")");
551 clen
+= strlen(&tbuf
[clen
]);
554 if (se
->waiting
== 1)
555 sprintf(&tbuf
[clen
], " W");
557 sprintf(&tbuf
[clen
], " %d", se
->waiting
);
558 clen
+= strlen(&tbuf
[clen
]);
562 sprintf(&tbuf
[clen
], "\n");
563 if (no_screen_refresh
)
586 if (no_screen_refresh
== 0) {
587 /* clear for new display */
593 sprintf(tbuf
, "%-14.14s", proc_name
);
601 p2
= "context switch ";
603 p2
= "context switches";
604 if (num_of_threads
== 1)
609 sprintf(&tbuf
[clen
], " %4d %s %4d %s %4d %s",
610 preempted
, p1
, csw
, p2
, num_of_threads
, p3
);
611 clen
+= strlen(&tbuf
[clen
]);
614 * Display the current time.
615 * "ctime" always returns a string that looks like this:
617 * Sun Sep 16 01:03:52 1973
618 * 012345678901234567890123
621 * We want indices 11 thru 18 (length 8).
623 curr_time
= time((long *)0);
626 start_time
= curr_time
;
628 elapsed_secs
= curr_time
- start_time
;
629 minutes
= elapsed_secs
/ 60;
630 hours
= minutes
/ 60;
632 memset(&tbuf
[clen
], ' ', 78 - clen
);
636 sprintf(&tbuf
[clen
], "%-8.8s\n", &(ctime(&curr_time
)[11]));
637 if (no_screen_refresh
)
642 if (total_faults
== 1)
650 sprintf(tbuf
, " %4d %s %4d %s",
651 total_faults
, p1
, scalls
, p2
);
654 sprintf(&tbuf
[clen
], " %3ld:%02ld:%02ld\n",
655 (long)hours
, (long)(minutes
% 60), (long)(elapsed_secs
% 60));
656 if (no_screen_refresh
)
663 sprintf(tbuf
, "\nTYPE NUMBER CPU_TIME WAIT_TIME\n");
664 if (no_screen_refresh
)
669 sprintf(tbuf
, "------------------------------------------------------------------------------\n");
670 if (no_screen_refresh
)
678 sprintf(tbuf
, "System Idle ");
681 print_time(&tbuf
[clen
], (unsigned long)(itime_usecs
), itime_secs
);
682 clen
+= strlen(&tbuf
[clen
]);
684 if (delta_itime_usecs
|| delta_itime_secs
) {
686 sprintf(&tbuf
[clen
], "(");
687 clen
+= strlen(&tbuf
[clen
]);
689 print_time(&tbuf
[clen
], (unsigned long)(delta_itime_usecs
), delta_itime_secs
);
690 clen
+= strlen(&tbuf
[clen
]);
692 sprintf(&tbuf
[clen
], ")");
693 clen
+= strlen(&tbuf
[clen
]);
695 sprintf(&tbuf
[clen
], "\n");
696 if (no_screen_refresh
)
704 sprintf(tbuf
, "System Busy ");
707 print_time(&tbuf
[clen
], (unsigned long)(otime_usecs
), otime_secs
);
708 clen
+= strlen(&tbuf
[clen
]);
710 if (delta_otime_usecs
|| delta_otime_secs
) {
712 sprintf(&tbuf
[clen
], "(");
713 clen
+= strlen(&tbuf
[clen
]);
715 print_time(&tbuf
[clen
], (unsigned long)(delta_otime_usecs
), delta_otime_secs
);
716 clen
+= strlen(&tbuf
[clen
]);
718 sprintf(&tbuf
[clen
], ")");
719 clen
+= strlen(&tbuf
[clen
]);
721 sprintf(&tbuf
[clen
], "\n");
722 if (no_screen_refresh
)
729 sprintf(tbuf
, "%-14.14s Usermode ", proc_name
);
732 print_time(&tbuf
[clen
], (unsigned long)(utime_usecs
), utime_secs
);
733 clen
+= strlen(&tbuf
[clen
]);
735 sprintf(&tbuf
[clen
], "\n");
736 if (no_screen_refresh
)
743 max_rows
= topn
- (num_of_threads
+ 3);
747 for (output_lf
= 1, n
= 1; rows
< max_rows
&& n
< MAX_FAULTS
; n
++) {
750 if (se
->total_count
== 0)
752 if (output_lf
== 1) {
754 if (no_screen_refresh
)
760 if (rows
>= max_rows
)
769 if (no_screen_refresh
)
775 for (i
= 0; rows
< max_rows
; i
++) {
777 n
= sort_by_count
[i
];
779 n
= sort_by_wtime
[i
];
782 print_row(&sc_tab
[n
], 0);
788 if (no_screen_refresh
== 0) {
789 while (rows
++ < max_rows
)
794 if (num_of_threads
) {
795 sprintf(tbuf
, "\nCURRENT_TYPE LAST_PATHNAME_WAITED_FOR CUR_WAIT_TIME THRD# PRI\n");
797 if (no_screen_refresh
)
802 sprintf(tbuf
, "------------------------------------------------------------------------------\n");
803 if (no_screen_refresh
)
810 for (i
= 0; i
< num_of_threads
; i
++, ti
++) {
814 int secs
, time_secs
, time_usecs
;
816 now
= mach_absolute_time();
818 while (ti
->thread
== 0 && ti
< &th_state
[MAX_THREADS
])
820 if (ti
== &th_state
[MAX_THREADS
])
824 te
= &ti
->th_entry
[ti
->depth
- 1];
826 if (te
->sc_state
== WAITING
) {
828 sprintf(tbuf
, "%-23.23s", sc_tab
[te
->code
].name
);
830 sprintf(tbuf
, "%-23.23s", "vm_fault");
832 sprintf(tbuf
, "%-23.23s", state_name
[te
->sc_state
]);
834 te
= &ti
->th_entry
[0];
835 sprintf(tbuf
, "%-23.23s", state_name
[te
->sc_state
]);
839 /* print the tail end of the pathname */
840 p
= (char *)ti
->pathname
;
847 sprintf(&tbuf
[clen
], " %-26.26s ", &p
[plen
]);
849 clen
+= strlen(&tbuf
[clen
]);
851 time_usecs
= (unsigned long)(((double)now
- te
->otime
) / divisor
);
852 secs
= time_usecs
/ 1000000;
853 time_usecs
-= secs
* 1000000;
856 print_time(&tbuf
[clen
], time_usecs
, time_secs
);
857 clen
+= strlen(&tbuf
[clen
]);
858 sprintf(&tbuf
[clen
], " %2d %3d\n", i
, ti
->curpri
);
859 if (no_screen_refresh
)
864 if (no_screen_refresh
== 0) {
868 printf("\n=================\n");
872 for (i
= 0; i
< (MAX_SC
+ msgcode_cnt
); i
++) {
873 if ((n
= sort_by_count
[i
]) == -1)
875 sc_tab
[n
].delta_count
= 0;
876 sc_tab
[n
].waiting
= 0;
877 sc_tab
[n
].delta_wtime_usecs
= 0;
878 sc_tab
[n
].delta_wtime_secs
= 0;
880 for (i
= 1; i
< MAX_FAULTS
; i
++) {
881 faults
[i
].delta_count
= 0;
882 faults
[i
].waiting
= 0;
883 faults
[i
].delta_wtime_usecs
= 0;
884 faults
[i
].delta_wtime_secs
= 0;
890 delta_itime_secs
= 0;
891 delta_itime_usecs
= 0;
892 delta_otime_secs
= 0;
893 delta_otime_usecs
= 0;
900 for (i
= 0; i
< (MAX_SC
+ msgcode_cnt
) ; i
++) {
901 sc_tab
[i
].delta_count
= 0;
902 sc_tab
[i
].total_count
= 0;
903 sc_tab
[i
].waiting
= 0;
904 sc_tab
[i
].delta_wtime_usecs
= 0;
905 sc_tab
[i
].delta_wtime_secs
= 0;
906 sc_tab
[i
].wtime_usecs
= 0;
907 sc_tab
[i
].wtime_secs
= 0;
908 sc_tab
[i
].stime_usecs
= 0;
909 sc_tab
[i
].stime_secs
= 0;
911 for (i
= 1; i
< MAX_FAULTS
; i
++) {
912 faults
[i
].delta_count
= 0;
913 faults
[i
].total_count
= 0;
914 faults
[i
].waiting
= 0;
915 faults
[i
].delta_wtime_usecs
= 0;
916 faults
[i
].delta_wtime_secs
= 0;
917 faults
[i
].wtime_usecs
= 0;
918 faults
[i
].wtime_secs
= 0;
919 faults
[i
].stime_usecs
= 0;
920 faults
[i
].stime_secs
= 0;
932 delta_itime_secs
= 0;
933 delta_itime_usecs
= 0;
936 delta_otime_secs
= 0;
937 delta_otime_usecs
= 0;
941 sc_tab_init(char *codefile
) {
948 if ((fp
= fopen(codefile
,"r")) == (FILE *)0) {
949 printf("Failed to open code description file %s\n", codefile
);
953 /* Count Mach message MSG_ codes */
954 for (msgcode_cnt
=0;;) {
955 n
= fscanf(fp
, "%x%55s\n", &code
, &name
[0]);
958 if (strncmp ("MSG_", &name
[0], 4) == 0)
960 if (strcmp("TRACE_LAST_WRAPPER", &name
[0]) == 0)
964 sc_tab
= (struct sc_entry
*)malloc((MAX_SC
+msgcode_cnt
) * sizeof (struct sc_entry
));
966 quit("can't allocate memory for system call table\n");
967 bzero(sc_tab
,(MAX_SC
+msgcode_cnt
) * sizeof (struct sc_entry
));
969 msgcode_tab
= (int *)malloc(msgcode_cnt
* sizeof(int));
971 quit("can't allocate memory for msgcode table\n");
972 bzero(msgcode_tab
,(msgcode_cnt
* sizeof(int)));
974 sort_by_count
= (int *)malloc((MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
976 quit("can't allocate memory for sort_by_count table\n");
977 bzero(sort_by_count
,(MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
979 sort_by_wtime
= (int *)malloc((MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
981 quit("can't allocate memory for sort_by_wtime table\n");
982 bzero(sort_by_wtime
, (MAX_SC
+ msgcode_cnt
+ 1) * sizeof(int));
988 n
= fscanf(fp
, "%x%55s\n", &code
, &name
[0]);
993 if (strcmp("MACH_vmfault", &name
[0]) == 0) {
997 if (strcmp("MACH_SCHED", &name
[0]) == 0) {
1001 if (strcmp("MACH_STKHANDOFF", &name
[0]) == 0) {
1002 mach_stkhandoff
= code
;
1005 if (strcmp("MACH_IDLE", &name
[0]) == 0) {
1009 if (strcmp("VFS_LOOKUP", &name
[0]) == 0) {
1013 if (strcmp("BSC_SysCall", &name
[0]) == 0) {
1017 if (strcmp("MACH_SysCall", &name
[0]) == 0) {
1021 if (strcmp("BSC_exit", &name
[0]) == 0) {
1025 if (strncmp("MSG_", &name
[0], 4) == 0) {
1026 msgcode_tab
[msgcode_indx
] = ((code
& 0x00ffffff) >>2);
1027 n
= MAX_SC
+ msgcode_indx
;
1028 strncpy(&sc_tab
[n
].name
[0], &name
[4], 31 );
1032 if (strncmp("MSC_", &name
[0], 4) == 0) {
1033 n
= 512 + ((code
>>2) & 0x1ff);
1034 strcpy(&sc_tab
[n
].name
[0], &name
[4]);
1037 if (strncmp("BSC_", &name
[0], 4) == 0) {
1038 n
= (code
>>2) & 0x1ff;
1039 strcpy(&sc_tab
[n
].name
[0], &name
[4]);
1042 if (strcmp("TRACE_LAST_WRAPPER", &name
[0]) == 0)
1045 strcpy(&faults
[1].name
[0], "zero_fill");
1046 strcpy(&faults
[2].name
[0], "pagein");
1047 strcpy(&faults
[3].name
[0], "copy_on_write");
1048 strcpy(&faults
[4].name
[0], "cache_hit");
1055 struct kinfo_proc
*kp
;
1059 mib
[2] = KERN_PROC_ALL
;
1062 if (sysctl(mib
, 4, NULL
, &bufSize
, NULL
, 0) < 0)
1063 quit("trace facility failure, KERN_PROC_ALL\n");
1065 if((kp
= (struct kinfo_proc
*)malloc(bufSize
)) == (struct kinfo_proc
*)0)
1066 quit("can't allocate memory for proc buffer\n");
1068 if (sysctl(mib
, 4, kp
, &bufSize
, NULL
, 0) < 0)
1069 quit("trace facility failure, KERN_PROC_ALL\n");
1071 kp_nentries
= bufSize
/ sizeof(struct kinfo_proc
);
1075 struct th_info
*find_thread(int thread
) {
1078 for (ti
= th_state
; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1079 if (ti
->thread
== thread
)
1082 return ((struct th_info
*)0);
1087 cmp_wtime(struct sc_entry
*s1
, struct sc_entry
*s2
) {
1089 if (s1
->wtime_secs
< s2
->wtime_secs
)
1091 if (s1
->wtime_secs
> s2
->wtime_secs
)
1093 if (s1
->wtime_usecs
<= s2
->wtime_usecs
)
1101 int i
, n
, k
, cnt
, secs
;
1103 struct sc_entry
*se
;
1107 now
= mach_absolute_time();
1109 for (ti
= th_state
; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1110 if (ti
->thread
== 0)
1114 te
= &ti
->th_entry
[ti
->depth
-1];
1116 if (te
->sc_state
== WAITING
) {
1118 se
= &sc_tab
[te
->code
];
1120 se
= &faults
[DBG_PAGEIN_FAULT
];
1122 se
->wtime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1123 se
->delta_wtime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1124 te
->stime
= (double)now
;
1126 secs
= se
->wtime_usecs
/ 1000000;
1127 se
->wtime_usecs
-= secs
* 1000000;
1128 se
->wtime_secs
+= secs
;
1130 secs
= se
->delta_wtime_usecs
/ 1000000;
1131 se
->delta_wtime_usecs
-= secs
* 1000000;
1132 se
->delta_wtime_secs
+= secs
;
1135 te
= &ti
->th_entry
[0];
1137 if (te
->sc_state
== PREEMPTED
) {
1138 if ((unsigned long)(((double)now
- te
->otime
) / divisor
) > 5000000) {
1141 ti
->pathptr
= (long *)NULL
;
1142 ti
->pathname
[0] = 0;
1148 if ((called
% sort_now
) == 0) {
1149 sort_by_count
[0] = -1;
1150 sort_by_wtime
[0] = -1;
1151 for (cnt
= 1, n
= 1; n
< (MAX_SC
+ msgcode_cnt
); n
++) {
1152 if (sc_tab
[n
].total_count
) {
1153 for (i
= 0; i
< cnt
; i
++) {
1154 if ((k
= sort_by_count
[i
]) == -1 ||
1155 sc_tab
[n
].total_count
> sc_tab
[k
].total_count
) {
1157 for (k
= cnt
- 1; k
>= i
; k
--)
1158 sort_by_count
[k
+1] = sort_by_count
[k
];
1159 sort_by_count
[i
] = n
;
1163 if (how_to_sort
== 0) {
1164 for (i
= 0; i
< cnt
; i
++) {
1165 if ((k
= sort_by_wtime
[i
]) == -1 ||
1166 cmp_wtime(&sc_tab
[n
], &sc_tab
[k
])) {
1168 for (k
= cnt
- 1; k
>= i
; k
--)
1169 sort_by_wtime
[k
+1] = sort_by_wtime
[k
];
1170 sort_by_wtime
[i
] = n
;
1186 mib
[1] = KERN_KDEBUG
;
1187 mib
[2] = KERN_KDENABLE
; /* protocol */
1190 mib
[5] = 0; /* no flags */
1191 if (sysctl(mib
, 4, NULL
, &needed
, NULL
, 0) < 0)
1192 quit("trace facility failure, KERN_KDENABLE\n");
1201 set_numbufs(int nbufs
)
1204 mib
[1] = KERN_KDEBUG
;
1205 mib
[2] = KERN_KDSETBUF
;
1208 mib
[5] = 0; /* no flags */
1209 if (sysctl(mib
, 4, NULL
, &needed
, NULL
, 0) < 0)
1210 quit("trace facility failure, KERN_KDSETBUF\n");
1213 mib
[1] = KERN_KDEBUG
;
1214 mib
[2] = KERN_KDSETUP
;
1217 mib
[5] = 0; /* no flags */
1218 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1219 quit("trace facility failure, KERN_KDSETUP\n");
1224 set_pidcheck(int pid
, int on_off
)
1228 kr
.type
= KDBG_TYPENONE
;
1231 needed
= sizeof(kd_regtype
);
1233 mib
[1] = KERN_KDEBUG
;
1234 mib
[2] = KERN_KDPIDTR
;
1238 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0) {
1240 printf("pid %d does not exist\n", pid
);
1248 get_bufinfo(kbufinfo_t
*val
)
1250 needed
= sizeof (*val
);
1252 mib
[1] = KERN_KDEBUG
;
1253 mib
[2] = KERN_KDGETBUF
;
1256 mib
[5] = 0; /* no flags */
1257 if (sysctl(mib
, 3, val
, &needed
, 0, 0) < 0)
1258 quit("trace facility failure, KERN_KDGETBUF\n");
1270 mib
[1] = KERN_KDEBUG
;
1271 mib
[2] = KERN_KDREMOVE
; /* protocol */
1274 mib
[5] = 0; /* no flags */
1276 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1278 set_remove_flag
= 0;
1281 quit("The trace facility is currently in use...\n Note: fs_usage, sc_usage, and latency use this feature.\n\n");
1283 quit("trace facility failure, KERN_KDREMOVE\n");
1291 kr
.type
= KDBG_RANGETYPE
;
1294 needed
= sizeof(kd_regtype
);
1296 mib
[1] = KERN_KDEBUG
;
1297 mib
[2] = KERN_KDSETREG
;
1300 mib
[5] = 0; /* no flags */
1301 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0)
1302 quit("trace facility failure, KERN_KDSETREG\n");
1305 mib
[1] = KERN_KDEBUG
;
1306 mib
[2] = KERN_KDSETUP
;
1309 mib
[5] = 0; /* no flags */
1310 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1311 quit("trace facility failure, KERN_KDSETUP\n");
1328 get_bufinfo(&bufinfo
);
1330 needed
= bufinfo
.nkdbufs
* sizeof(kd_buf
);
1332 mib
[1] = KERN_KDEBUG
;
1333 mib
[2] = KERN_KDREADTR
;
1338 if (sysctl(mib
, 3, my_buffer
, &needed
, NULL
, 0) < 0)
1339 quit("trace facility failure, KERN_KDREADTR\n");
1343 if (bufinfo
.flags
& KDBG_WRAPPED
) {
1344 for (i
= 0; i
< MAX_THREADS
; i
++) {
1345 th_state
[i
].depth
= 0;
1346 th_state
[i
].thread
= 0;
1347 th_state
[i
].vfslookup
= 0;
1348 th_state
[i
].pathptr
= (long *)NULL
;
1349 th_state
[i
].pathname
[0] = 0;
1357 set_pidcheck(pid
, 1);
1358 set_enable(1); /* re-enable kernel logging */
1360 kd
= (kd_buf
*)my_buffer
;
1362 for (i
= 0; i
< count
; i
++) {
1363 int debugid
, baseid
, thread
;
1366 struct th_info
*ti
, *switched_out
, *switched_in
;
1367 struct sc_entry
*se
;
1370 thread
= kd
[i
].arg5
;
1371 debugid
= kd
[i
].debugid
;
1372 type
= kd
[i
].debugid
& DBG_FUNC_MASK
;
1375 switched_out
= (struct th_info
*)0;
1376 switched_in
= (struct th_info
*)0;
1378 now
= kd
[i
].timestamp
& KDBG_TIMESTAMP_MASK
;
1380 baseid
= debugid
& 0xffff0000;
1382 if (type
== vfs_lookup
) {
1385 if ((ti
= find_thread(thread
)) == (struct th_info
*)0)
1388 if (ti
->vfslookup
== 1) {
1390 sargptr
= ti
->pathname
;
1392 *sargptr
++ = kd
[i
].arg2
;
1393 *sargptr
++ = kd
[i
].arg3
;
1394 *sargptr
++ = kd
[i
].arg4
;
1396 * NULL terminate the 'string'
1400 ti
->pathptr
= sargptr
;
1402 } else if (ti
->vfslookup
> 1) {
1404 sargptr
= ti
->pathptr
;
1407 We don't want to overrun our pathname buffer if the
1408 kernel sends us more VFS_LOOKUP entries than we can
1412 if (sargptr
>= &ti
->pathname
[NUMPARMS
])
1416 We need to detect consecutive vfslookup entries.
1417 So, if we get here and find a START entry,
1418 fake the pathptr so we can bypass all further
1422 if (debugid
& DBG_FUNC_START
)
1424 ti
->pathptr
= &ti
->pathname
[NUMPARMS
];
1428 *sargptr
++ = kd
[i
].arg1
;
1429 *sargptr
++ = kd
[i
].arg2
;
1430 *sargptr
++ = kd
[i
].arg3
;
1431 *sargptr
++ = kd
[i
].arg4
;
1433 * NULL terminate the 'string'
1437 ti
->pathptr
= sargptr
;
1441 } else if (baseid
== bsc_base
)
1442 code
= (debugid
>> 2) & 0x1ff;
1443 else if (baseid
== msc_base
)
1444 code
= 512 + ((debugid
>> 2) & 0x1ff);
1445 else if (type
== mach_idle
) {
1446 if (debugid
& DBG_FUNC_START
) {
1447 switched_out
= find_thread(kd
[i
].arg5
);
1451 if (debugid
& DBG_FUNC_END
) {
1452 switched_in
= find_thread(kd
[i
].arg5
);
1457 itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1458 delta_itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1460 } else if (in_other
) {
1461 otime_usecs
+= ((double)now
- other_start
) / divisor
;
1462 delta_otime_usecs
+= ((double)now
- other_start
) / divisor
;
1465 if ( !switched_in
) {
1467 * not one of the target proc's threads
1469 if (now_collect_cpu_time
) {
1471 idle_start
= (double)now
;
1475 if (now_collect_cpu_time
) {
1478 other_start
= (double)now
;
1481 if ( !switched_in
&& !switched_out
)
1485 else if (type
== mach_sched
|| type
== mach_stkhandoff
) {
1486 switched_out
= find_thread(kd
[i
].arg5
);
1487 switched_in
= find_thread(kd
[i
].arg2
);
1490 itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1491 delta_itime_usecs
+= ((double)now
- idle_start
) / divisor
;
1493 } else if (in_other
) {
1494 otime_usecs
+= ((double)now
- other_start
) / divisor
;
1495 delta_otime_usecs
+= ((double)now
- other_start
) / divisor
;
1498 if ( !switched_in
) {
1500 * not one of the target proc's threads
1502 if (now_collect_cpu_time
) {
1504 other_start
= (double)now
;
1507 if ( !switched_in
&& !switched_out
)
1511 else if ((baseid
& 0xff000000) == 0xff000000) {
1512 code
= find_msgcode (debugid
);
1515 } else if (baseid
!= mach_vmfault
)
1518 if (switched_out
|| switched_in
) {
1521 ti
->curpri
= kd
[i
].arg3
;
1524 te
= &ti
->th_entry
[ti
->depth
-1];
1526 if (te
->sc_state
== KERNEL_MODE
)
1527 te
->ctime
+= (double)now
- te
->stime
;
1528 te
->sc_state
= WAITING
;
1533 te
= &ti
->th_entry
[0];
1535 if (te
->sc_state
== USER_MODE
)
1536 utime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1537 te
->sc_state
= PREEMPTED
;
1540 te
->stime
= (double)now
;
1541 te
->otime
= (double)now
;
1542 now_collect_cpu_time
= 1;
1547 ti
->curpri
= kd
[i
].arg4
;
1550 te
= &ti
->th_entry
[ti
->depth
-1];
1552 if (te
->sc_state
== WAITING
)
1553 te
->wtime
+= (double)now
- te
->stime
;
1554 te
->sc_state
= KERNEL_MODE
;
1556 te
= &ti
->th_entry
[0];
1558 te
->sc_state
= USER_MODE
;
1560 te
->stime
= (double)now
;
1561 te
->otime
= (double)now
;
1565 if ((ti
= find_thread(thread
)) == (struct th_info
*)0) {
1566 for (ti
= &th_state
[0]; ti
< &th_state
[MAX_THREADS
]; ti
++) {
1567 if (ti
->thread
== 0) {
1568 ti
->thread
= thread
;
1573 if (ti
== &th_state
[MAX_THREADS
])
1576 if (debugid
& DBG_FUNC_START
) {
1580 te
= &ti
->th_entry
[ti
->depth
-1];
1582 if (te
->sc_state
== KERNEL_MODE
)
1583 te
->ctime
+= (double)now
- te
->stime
;
1585 te
= &ti
->th_entry
[0];
1587 if (te
->sc_state
== USER_MODE
)
1588 utime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1590 te
->stime
= (double)now
;
1591 te
->otime
= (double)now
;
1593 if (ti
->depth
< MAX_NESTED
) {
1594 te
= &ti
->th_entry
[ti
->depth
];
1596 te
->sc_state
= KERNEL_MODE
;
1599 te
->stime
= (double)now
;
1600 te
->otime
= (double)now
;
1601 te
->ctime
= (double)0;
1602 te
->wtime
= (double)0;
1606 } else if (debugid
& DBG_FUNC_END
) {
1611 se
= &faults
[kd
[i
].arg4
];
1614 if (se
->total_count
== 0)
1620 te
= &ti
->th_entry
[ti
->depth
-1];
1622 if (te
->type
== type
) {
1623 se
->stime_usecs
+= te
->ctime
/ divisor
;
1624 se
->stime_usecs
+= ((double)now
- te
->stime
) / divisor
;
1626 se
->wtime_usecs
+= te
->wtime
/ divisor
;
1627 se
->delta_wtime_usecs
+= te
->wtime
/ divisor
;
1629 secs
= se
->stime_usecs
/ 1000000;
1630 se
->stime_usecs
-= secs
* 1000000;
1631 se
->stime_secs
+= secs
;
1633 secs
= se
->wtime_usecs
/ 1000000;
1634 se
->wtime_usecs
-= secs
* 1000000;
1635 se
->wtime_secs
+= secs
;
1637 secs
= se
->delta_wtime_usecs
/ 1000000;
1638 se
->delta_wtime_usecs
-= secs
* 1000000;
1639 se
->delta_wtime_secs
+= secs
;
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
;
1651 te
= &ti
->th_entry
[ti
->depth
-1];
1653 te
->stime
= (double)now
;
1654 te
->otime
= (double)now
;
1660 if (ti
->depth
== 0) {
1662 * headed back to user mode
1663 * start the time accumulation
1665 te
= &ti
->th_entry
[0];
1666 te
->sc_state
= USER_MODE
;
1667 te
->stime
= (double)now
;
1668 te
->otime
= (double)now
;
1673 secs
= utime_usecs
/ 1000000;
1674 utime_usecs
-= secs
* 1000000;
1677 secs
= itime_usecs
/ 1000000;
1678 itime_usecs
-= secs
* 1000000;
1681 secs
= delta_itime_usecs
/ 1000000;
1682 delta_itime_usecs
-= secs
* 1000000;
1683 delta_itime_secs
+= secs
;
1685 secs
= otime_usecs
/ 1000000;
1686 otime_usecs
-= secs
* 1000000;
1689 secs
= delta_otime_usecs
/ 1000000;
1690 delta_otime_usecs
-= secs
* 1000000;
1691 delta_otime_secs
+= secs
;
1701 This flag is turned off when calling
1702 quit() due to a set_remove() failure.
1704 if (set_remove_flag
)
1707 if (no_screen_refresh
== 0) {
1708 /* clear for new display */
1715 printf("sc_usage: ");
1724 mach_timebase_info_data_t info
;
1726 (void) mach_timebase_info (&info
);
1728 divisor
= ( (double)info
.denom
/ (double)info
.numer
) * 1000;
1744 ret
= (int)strtol(str
, &cp
, 10);
1745 if (cp
== str
|| *cp
) {
1746 /* Assume this is a command string and find first matching pid */
1747 for (i
=0; i
< kp_nentries
; i
++) {
1748 if(kp_buffer
[i
].kp_proc
.p_stat
== 0)
1751 if(!strcmp(str
, kp_buffer
[i
].kp_proc
.p_comm
))
1753 strncpy(proc_name
, kp_buffer
[i
].kp_proc
.p_comm
, sizeof(proc_name
)-1);
1754 proc_name
[sizeof(proc_name
)-1] = '\0';
1755 return(kp_buffer
[i
].kp_proc
.p_pid
);
1762 for (i
=0; i
< kp_nentries
; i
++)
1764 if(kp_buffer
[i
].kp_proc
.p_stat
== 0)
1766 else if (kp_buffer
[i
].kp_proc
.p_pid
== ret
) {
1767 strncpy(proc_name
, kp_buffer
[i
].kp_proc
.p_comm
, sizeof(proc_name
)-1);
1768 proc_name
[sizeof(proc_name
)-1] = '\0';
1769 return(kp_buffer
[i
].kp_proc
.p_pid
);
1777 /* Returns index into sc_tab for a mach msg entry */
1779 find_msgcode(int debugid
)
1784 for (indx
=0; indx
< msgcode_cnt
; indx
++)
1786 if (msgcode_tab
[indx
] == ((debugid
& 0x00ffffff) >>2))
1787 return (MAX_SC
+indx
);
1793 argtoi(flag
, req
, str
, base
)
1801 ret
= (int)strtol(str
, &cp
, base
);
1802 if (cp
== str
|| *cp
)
1803 errx(EINVAL
, "-%c flag requires a %s", flag
, req
);