X-Git-Url: https://git.saurik.com/apple/system_cmds.git/blobdiff_plain/b51d5b5f06948960f0bfd4ad974e625ca2350936..a8daac8f698fa38f1ca3aaa2a3ed6ac20e5128e2:/latency.tproj/latency.c diff --git a/latency.tproj/latency.c b/latency.tproj/latency.c index f9bb800..63e92d5 100644 --- a/latency.tproj/latency.c +++ b/latency.tproj/latency.c @@ -24,12 +24,13 @@ /* - cc -I. -DKERNEL_PRIVATE -O -o latency latency.c -lncurses + cc -I. -DPRIVATE -D__APPLE_PRIVATE -O -o latency latency.c -lncurses */ #include #include #include +#include #include #include #include @@ -105,6 +106,11 @@ int my_policy; int my_pri = -1; int num_of_usecs_to_sleep = 1000; +#define N_HIGH_RES_BINS 500 +int use_high_res_bins = false; +int i_high_res_bins[N_HIGH_RES_BINS]; +int i_highest_latency = 0; + char *kernelpath = (char *)0; char *code_file = (char *)0; @@ -141,15 +147,14 @@ int need_new_map = 0; int total_threads = 0; kd_threadmap *mapptr = 0; -#define MAX_ENTRIES 1024 +#define MAX_ENTRIES 4096 struct ct { int type; char name[32]; } codes_tab[MAX_ENTRIES]; -/* If NUMPARMS changes from the kernel, then PATHLENGTH will also reflect the change */ + #define NUMPARMS 23 -#define PATHLENGTH (NUMPARMS*sizeof(long)) struct th_info { int thread; @@ -158,7 +163,7 @@ struct th_info { int arg1; double stime; long *pathptr; - char pathname[PATHLENGTH + 1]; + long pathname[NUMPARMS + 1]; }; #define MAX_THREADS 512 @@ -173,7 +178,7 @@ int cur_max = 0; #define INTERRUPT 0x01050000 #define DECR_TRAP 0x01090000 #define DECR_SET 0x01090004 -#define MACH_vmfault 0x01300000 +#define MACH_vmfault 0x01300008 #define MACH_sched 0x01400000 #define MACH_stkhandoff 0x01400008 #define VFS_LOOKUP 0x03010090 @@ -187,6 +192,8 @@ int cur_max = 0; #define DBG_FUNC_ALL (DBG_FUNC_START | DBG_FUNC_END) #define DBG_FUNC_MASK 0xfffffffc +#define CPU_NUMBER(ts) ((ts & KDBG_CPU_MASK) >> KDBG_CPU_SHIFT) + #define DBG_ZERO_FILL_FAULT 1 #define DBG_PAGEIN_FAULT 2 #define DBG_COW_FAULT 3 @@ -294,7 +301,7 @@ set_pidexclude(int pid, int on_off) sysctl(mib, 3, &kr, &needed, NULL, 0); } -set_rtcdec(decval) +void set_rtcdec(decval) int decval; {kd_regtype kr; int ret; @@ -311,11 +318,14 @@ int decval; mib[5] = 0; /* no flags */ errno = 0; - if ((ret=sysctl(mib, 3, &kr, &needed, NULL, 0)) < 0) { - decrementer_val = 0; - quit("trace facility failure, KERN_KDSETRTCDEC\n"); + decrementer_val = 0; + /* ignore this sysctl error if it's not supported */ + if (errno == ENOENT) + return; + else + quit("trace facility failure, KERN_KDSETRTCDEC\n"); } } @@ -420,6 +430,21 @@ set_init_logging() quit("trace facility failure, KERN_KDSETUP\n"); } +void +write_high_res_latencies() +{ + int i; + FILE *f; + if(use_high_res_bins) + { + f = fopen("latencies.csv","w"); + for(i=0;iarg5 & KDBG_THREAD_MASK; - cpunum = (kd->arg5 & KDBG_CPU_MASK) ? 1: 0; + thread = kd->arg5; + cpunum = CPU_NUMBER(kd->timestamp); debugid = kd->debugid; type = kd->debugid & DBG_FUNC_MASK; @@ -1252,8 +1335,7 @@ void sample_sc(uint64_t start, uint64_t stop) if (type == DECR_TRAP) i_latency = handle_decrementer(kd); - now = (((uint64_t)kd->timestamp.tv_sec) << 32) | - (uint64_t)((unsigned int)(kd->timestamp.tv_nsec)); + now = kd->timestamp & KDBG_TIMESTAMP_MASK; timestamp = ((double)now) / divisor; @@ -1272,7 +1354,7 @@ void sample_sc(uint64_t start, uint64_t stop) } if ((kd->debugid & DBG_FUNC_MASK) == DECR_TRAP) { - cpunum = (kd->arg5 & KDBG_CPU_MASK) ? 1: 0; + cpunum = CPU_NUMBER(kd->timestamp); last_decrementer_kd[cpunum] = kd; } else @@ -1309,7 +1391,7 @@ void sample_sc(uint64_t start, uint64_t stop) } delta = timestamp - last_timestamp; - if (map = find_thread_map(thread)) + if ((map = find_thread_map(thread))) strcpy(command, map->command); else command[0] = 0; @@ -1361,7 +1443,7 @@ void sample_sc(uint64_t start, uint64_t stop) mode = 1; - if (ti = find_thread((kd->arg5 & KDBG_THREAD_MASK), 0, 0)) { + if ((ti = find_thread(kd->arg5, 0, 0))) { if (ti->type == -1 && strcmp(command, "kernel_task")) mode = 0; } @@ -1387,12 +1469,12 @@ void sample_sc(uint64_t start, uint64_t stop) case MACH_stkhandoff: last_mach_sched = kd; - if (map = find_thread_map(kd->arg2)) + if ((map = find_thread_map(kd->arg2))) strcpy(command1, map->command); else sprintf(command1, "%-8x", kd->arg2); - if (ti = find_thread(kd->arg2, 0, 0)) { + if ((ti = find_thread(kd->arg2, 0, 0))) { if (ti->type == -1 && strcmp(command1, "kernel_task")) p = "U"; else @@ -1432,14 +1514,17 @@ void sample_sc(uint64_t start, uint64_t stop) } while ( (kd < end_of_sample) && ((kd->debugid & DBG_FUNC_MASK) == VFS_LOOKUP)) { - if (!ti->pathptr) { + if (ti->pathptr == NULL) { ti->arg1 = kd->arg1; - memset(&ti->pathname[0], 0, (PATHLENGTH + 1)); - sargptr = (long *)&ti->pathname[0]; + sargptr = ti->pathname; *sargptr++ = kd->arg2; *sargptr++ = kd->arg3; *sargptr++ = kd->arg4; + /* + * NULL terminate the 'string' + */ + *sargptr = 0; ti->pathptr = sargptr; } else { @@ -1451,7 +1536,7 @@ void sample_sc(uint64_t start, uint64_t stop) handle. */ - if ((long *)sargptr >= (long *)&ti->pathname[PATHLENGTH]) + if (sargptr >= &ti->pathname[NUMPARMS]) { kd++; continue; @@ -1466,7 +1551,7 @@ void sample_sc(uint64_t start, uint64_t stop) if (kd->debugid & DBG_FUNC_START) { - (long *)ti->pathptr = (long *)&ti->pathname[PATHLENGTH]; + ti->pathptr = &ti->pathname[NUMPARMS]; } else { @@ -1474,16 +1559,22 @@ void sample_sc(uint64_t start, uint64_t stop) *sargptr++ = kd->arg2; *sargptr++ = kd->arg3; *sargptr++ = kd->arg4; + /* + * NULL terminate the 'string' + */ + *sargptr = 0; + ti->pathptr = sargptr; } } kd++; } + p = (char *)ti->pathname; kd--; - /* print the tail end of the pathname */ - len = strlen(ti->pathname); + /* print the tail end of the pathname */ + len = strlen(p); if (len > 42) len -= 42; else @@ -1492,7 +1583,7 @@ void sample_sc(uint64_t start, uint64_t stop) if (log_fp) { fprintf(log_fp, "%9.1f %8.1f\t\t%-14.14s %-42s %-8x %-8x %d %s\n", timestamp - start_bias, delta, "VFS_LOOKUP", - &ti->pathname[len], ti->arg1, thread, cpunum, command); + &p[len], ti->arg1, thread, cpunum, command); } last_timestamp = timestamp; @@ -1535,14 +1626,14 @@ enter_syscall(FILE *fp, kd_buf *kd, int thread, int type, char *command, double int cpunum; char *p; - cpunum = (kd->arg5 & KDBG_CPU_MASK) ? 1: 0; + cpunum = CPU_NUMBER(kd->timestamp); if (print_info && fp) { - if (p = find_code(type)) { + if ((p = find_code(type))) { if (type == INTERRUPT) { int mode = 1; - if (ti = find_thread((kd->arg5 & KDBG_THREAD_MASK), 0, 0)) { + if ((ti = find_thread(kd->arg5, 0, 0))) { if (ti->type == -1 && strcmp(command, "kernel_task")) mode = 0; } @@ -1589,7 +1680,7 @@ enter_syscall(FILE *fp, kd_buf *kd, int thread, int type, char *command, double else ti->type = -1; ti->stime = timestamp; - ti->pathptr = (long *)0; + ti->pathptr = (long *)NULL; #if 0 if (print_info && fp) @@ -1604,8 +1695,10 @@ exit_syscall(FILE *fp, kd_buf *kd, int thread, int type, char *command, double t struct th_info *ti; int cpunum; char *p; + uint64_t user_addr; + + cpunum = CPU_NUMBER(kd->timestamp); - cpunum = (kd->arg5 & KDBG_CPU_MASK) ? 1: 0; ti = find_thread(thread, type, type); #if 0 if (print_info && fp) @@ -1617,12 +1710,14 @@ exit_syscall(FILE *fp, kd_buf *kd, int thread, int type, char *command, double t else fprintf(fp, "%9.1f %8.1f() \t", timestamp - bias, delta); - if (p = find_code(type)) { + if ((p = find_code(type))) { if (type == INTERRUPT) { fprintf(fp, "INTERRUPT %-8x %d %s\n", thread, cpunum, command); - } else if (type == MACH_vmfault && kd->arg2 <= DBG_CACHE_HIT_FAULT) { - fprintf(fp, "%-28.28s %-8.8s %-8x %-8x %d %s\n", - p, fault_name[kd->arg2], kd->arg1, + } else if (type == MACH_vmfault && kd->arg4 <= DBG_CACHE_HIT_FAULT) { + user_addr = ((uint64_t)kd->arg1 << 32) | (uint32_t)kd->arg2; + + fprintf(fp, "%-28.28s %-8.8s %-16qx %-8x %d %s\n", + p, fault_name[kd->arg4], user_addr, thread, cpunum, command); } else { fprintf(fp, "%-28.28s %-8x %-8x %-8x %d %s\n", @@ -1643,7 +1738,7 @@ exit_syscall(FILE *fp, kd_buf *kd, int thread, int type, char *command, double t ti->thread = thread; ti->child_thread = 0; - ti->pathptr = (long *)0; + ti->pathptr = (long *)NULL; } } ti->type = -1; @@ -1658,12 +1753,11 @@ print_entry(FILE *fp, kd_buf *kd, int thread, int type, char *command, double ti if (!fp) return; - cpunum = (kd->arg5 & KDBG_CPU_MASK) ? 1: 0; - + cpunum = CPU_NUMBER(kd->timestamp); #if 0 fprintf(fp, "cur_max = %d, type = %x, thread = %x, cpunum = %d\n", cur_max, type, thread, cpunum); #endif - if (p = find_code(type)) { + if ((p = find_code(type))) { fprintf(fp, "%9.1f %8.1f\t\t%-28.28s %-8x %-8x %-8x %-8x %-8x %d %s\n", timestamp - bias, delta, p, kd->arg1, kd->arg2, kd->arg3, kd->arg4, thread, cpunum, command); @@ -1690,7 +1784,7 @@ check_for_thread_update(int thread, int type, kd_buf *kd) ti->thread = thread; ti->type = -1; - ti->pathptr = (long *)0; + ti->pathptr = (long *)NULL; } ti->child_thread = kd->arg1; return (1); @@ -1718,7 +1812,10 @@ kd_buf *log_decrementer(kd_buf *kd_beg, kd_buf *kd_end, kd_buf *end_of_sample, d { kd_buf *kd, *kd_start, *kd_stop; int kd_count; /* Limit the boundary of kd_start */ - double timestamp, last_timestamp, delta, start_bias; + double timestamp = 0.0; + double last_timestamp = 0.0; + double delta = 0.0; + double start_bias = 0.0; int thread, cpunum; int debugid, type, clen; int len; @@ -1743,51 +1840,50 @@ kd_buf *log_decrementer(kd_buf *kd_beg, kd_buf *kd_end, kd_buf *end_of_sample, d fprintf(log_fp, "RelTime(Us) Delta debugid arg1 arg2 arg3 arg4 thread cpu command\n\n"); - thread = kd_beg->arg5 & KDBG_THREAD_MASK; - cpunum = (kd_end->arg5 & KDBG_CPU_MASK) ? 1: 0; + thread = kd_beg->arg5; + cpunum = CPU_NUMBER(kd_end->timestamp); for (kd_count = 0, kd_start = kd_beg - 1; (kd_start >= (kd_buf *)my_buffer); kd_start--, kd_count++) { if (kd_count == MAX_LOG_COUNT) break; - if((kd_start->arg5 & KDBG_CPU_MASK) != cpunum) + if (CPU_NUMBER(kd_start->timestamp) != cpunum) continue; if ((kd_start->debugid & DBG_FUNC_MASK) == DECR_TRAP) break; - if((kd_start->arg5 & KDBG_THREAD_MASK) != thread) + if (kd_start->arg5 != thread) break; } if (kd_start < (kd_buf *)my_buffer) kd_start = (kd_buf *)my_buffer; - thread = kd_end->arg5 & KDBG_THREAD_MASK; + thread = kd_end->arg5; for (kd_stop = kd_end + 1; kd_stop < end_of_sample; kd_stop++) { if ((kd_stop->debugid & DBG_FUNC_MASK) == DECR_TRAP) break; - if((kd_stop->arg5 & KDBG_CPU_MASK) != cpunum) + if (CPU_NUMBER(kd_stop->timestamp) != cpunum) continue; - if((kd_stop->arg5 & KDBG_THREAD_MASK) != thread) + if (kd_stop->arg5 != thread) break; } if (kd_stop >= end_of_sample) kd_stop = end_of_sample - 1; - now = (((uint64_t)kd_start->timestamp.tv_sec) << 32) | - (uint64_t)((unsigned int)(kd_start->timestamp.tv_nsec)); + now = kd_start->timestamp & KDBG_TIMESTAMP_MASK; timestamp = ((double)now) / divisor; for (kd = kd_start; kd <= kd_stop; kd++) { type = kd->debugid & DBG_FUNC_MASK; - if (ti = find_thread((kd->arg5 & KDBG_THREAD_MASK), type, type)) { + if ((ti = find_thread(kd->arg5, type, type))) { if (ti->stime >= timestamp) ti->type = -1; } @@ -1795,13 +1891,12 @@ kd_buf *log_decrementer(kd_buf *kd_beg, kd_buf *kd_end, kd_buf *end_of_sample, d for (kd = kd_start; kd <= kd_stop; kd++) { int mode; - thread = kd->arg5 & KDBG_THREAD_MASK; - cpunum = (kd->arg5 & KDBG_CPU_MASK) ? 1: 0; + thread = kd->arg5; + cpunum = CPU_NUMBER(kd->timestamp); debugid = kd->debugid; type = kd->debugid & DBG_FUNC_MASK; - now = (((uint64_t)kd->timestamp.tv_sec) << 32) | - (uint64_t)((unsigned int)(kd->timestamp.tv_nsec)); + now = kd->timestamp & KDBG_TIMESTAMP_MASK; timestamp = ((double)now) / divisor; @@ -1811,7 +1906,7 @@ kd_buf *log_decrementer(kd_buf *kd_beg, kd_buf *kd_end, kd_buf *end_of_sample, d } delta = timestamp - last_timestamp; - if (map = find_thread_map(thread)) + if ((map = find_thread_map(thread))) strcpy(command, map->command); else command[0] = 0; @@ -1839,7 +1934,7 @@ kd_buf *log_decrementer(kd_buf *kd_beg, kd_buf *kd_end, kd_buf *end_of_sample, d mode = 1; - if (ti = find_thread((kd->arg5 & KDBG_THREAD_MASK), 0, 0)) { + if ((ti = find_thread(kd->arg5, 0, 0))) { if (ti->type == -1 && strcmp(command, "kernel_task")) mode = 0; } @@ -1859,12 +1954,12 @@ kd_buf *log_decrementer(kd_buf *kd_beg, kd_buf *kd_end, kd_buf *end_of_sample, d case MACH_sched: case MACH_stkhandoff: - if (map = find_thread_map(kd->arg2)) + if ((map = find_thread_map(kd->arg2))) strcpy(command1, map->command); else sprintf(command1, "%-8x", kd->arg2); - if (ti = find_thread(kd->arg2, 0, 0)) { + if ((ti = find_thread(kd->arg2, 0, 0))) { if (ti->type == -1 && strcmp(command1, "kernel_task")) p = "U"; else @@ -1897,20 +1992,24 @@ kd_buf *log_decrementer(kd_buf *kd_beg, kd_buf *kd_end, kd_buf *end_of_sample, d ti->thread = thread; ti->type = -1; - ti->pathptr = (long *)0; + ti->pathptr = (long *)NULL; ti->child_thread = 0; } while ( (kd <= kd_stop) && (kd->debugid & DBG_FUNC_MASK) == VFS_LOOKUP) { - if (!ti->pathptr) { + if (ti->pathptr == NULL) { ti->arg1 = kd->arg1; - memset(&ti->pathname[0], 0, (PATHLENGTH + 1)); - sargptr = (long *)&ti->pathname[0]; + sargptr = ti->pathname; *sargptr++ = kd->arg2; *sargptr++ = kd->arg3; *sargptr++ = kd->arg4; + /* + * NULL terminate the 'string' + */ + *sargptr = 0; + ti->pathptr = sargptr; } else { @@ -1922,7 +2021,7 @@ kd_buf *log_decrementer(kd_buf *kd_beg, kd_buf *kd_end, kd_buf *end_of_sample, d handle. */ - if ((long *)sargptr >= (long *)&ti->pathname[PATHLENGTH]) + if (sargptr >= &ti->pathname[NUMPARMS]) { kd++; continue; @@ -1937,7 +2036,7 @@ kd_buf *log_decrementer(kd_buf *kd_beg, kd_buf *kd_end, kd_buf *end_of_sample, d if (kd->debugid & DBG_FUNC_START) { - (long *)ti->pathptr = (long *)&ti->pathname[PATHLENGTH]; + ti->pathptr = &ti->pathname[NUMPARMS]; } else { @@ -1945,15 +2044,21 @@ kd_buf *log_decrementer(kd_buf *kd_beg, kd_buf *kd_end, kd_buf *end_of_sample, d *sargptr++ = kd->arg2; *sargptr++ = kd->arg3; *sargptr++ = kd->arg4; + /* + * NULL terminate the 'string' + */ + *sargptr = 0; + ti->pathptr = sargptr; } } kd++; } + p = (char *)ti->pathname; kd--; /* print the tail end of the pathname */ - len = strlen(ti->pathname); + len = strlen(p); if (len > 42) len -= 42; else @@ -1961,7 +2066,7 @@ kd_buf *log_decrementer(kd_buf *kd_beg, kd_buf *kd_end, kd_buf *end_of_sample, d fprintf(log_fp, "%9.1f %8.1f\t\t%-14.14s %-42s %-8x %-8x %d %s\n", timestamp - start_bias, delta, "VFS_LOOKUP", - &ti->pathname[len], ti->arg1, thread, cpunum, command); + &p[len], ti->arg1, thread, cpunum, command); last_timestamp = timestamp; break; @@ -2004,6 +2109,12 @@ double handle_decrementer(kd_buf *kd) else i_too_slow++; + if(use_high_res_bins && elapsed_usecs < N_HIGH_RES_BINS) { + if(elapsed_usecs > i_highest_latency) + i_highest_latency = elapsed_usecs; + i_high_res_bins[elapsed_usecs]++; + } + if (i_thresh_hold && elapsed_usecs > i_thresh_hold) i_exceeded_threshold++; if (elapsed_usecs > i_max_latency) @@ -2020,7 +2131,7 @@ double handle_decrementer(kd_buf *kd) void init_code_file() { FILE *fp; - int i, n, cnt, code; + int i, n, code; char name[128]; if ((fp = fopen(code_file, "r")) == (FILE *)0) { @@ -2028,16 +2139,15 @@ void init_code_file() fprintf(log_fp, "open of %s failed\n", code_file); return; } - n = fscanf(fp, "%d\n", &cnt); - - if (n != 1) { - if (log_fp) - fprintf(log_fp, "bad format found in %s\n", code_file); - return; - } for (i = 0; i < MAX_ENTRIES; i++) { - n = fscanf(fp, "%x%s\n", &code, name); + n = fscanf(fp, "%x%127s\n", &code, name); + if (n == 1 && i == 0) { + /* + * old code file format, just skip + */ + continue; + } if (n != 2) break; @@ -2057,13 +2167,17 @@ do_kernel_nm() FILE *fp = (FILE *)0; char tmp_nm_file[128]; char tmpstr[1024]; - int inchr; + char inchr; bzero(tmp_nm_file, 128); bzero(tmpstr, 1024); /* Build the temporary nm file path */ - sprintf(tmp_nm_file, "/tmp/knm.out.%d", getpid()); + strcpy(tmp_nm_file,"/tmp/knm.out.XXXXXX"); + if (!mktemp(tmp_nm_file)) { + fprintf(stderr, "Error in mktemp call\n"); + return; + } /* Build the nm command and create a tmp file with the output*/ sprintf (tmpstr, "/usr/bin/nm -f -n -s __TEXT __text %s > %s", @@ -2109,7 +2223,7 @@ do_kernel_nm() for (i=0; i