Skip to content

Instantly share code, notes, and snippets.

@brendangregg
Created October 8, 2013 01:03
Show Gist options
  • Save brendangregg/6877796 to your computer and use it in GitHub Desktop.
Save brendangregg/6877796 to your computer and use it in GitHub Desktop.
mac os x instruments: thread states
#!/usr/sbin/dtrace -C -Z -s
/*
To run this script, please type the following command while in the same
directory (or specify the full pather rather than ./):
sudo ./threadstates -o dtrace_output.txt <pid>
After the output file has been generated (here, it's dtrace_output.txt but you
may call it whatever you like), call up the trace document where
this script was exported from and choose the "DTrace Data Import..." option
from the "File" pulldown menu to import that data.
*/
#pragma D option switchrate=1msec
#pragma D option bufsize=25m
#pragma D option stackindent=0
#pragma D option quiet
syscall::getpriority:entry/execname=="Instruments"/ {}
BEGIN{printf("<dtrace_output_begin/>\n %-14d\n8321B938-7EA2-4D1F-9924-9B970E19183A\n", walltimestamp);}
sched*:mach_kernel:*:sleep/((((self->state[curlwpsinfo->pr_lwpid] != curthread->state) && !(curthread->state & 0x10)) && ((pid == $1))))/{
self->state[curlwpsinfo->pr_lwpid] = curthread->state;
printf("<e> -1 0 %d %u %-14d\n%u\n%d\n%d\n%d \n</e>\n",cpu, tid, walltimestamp, curlwpsinfo->pr_lwpid, curthread->state, curcpu->cpu_id, vtimestamp/1000000);printf("<s>\n");ustack(128);printf("</s>\n");
}
sched*:mach_kernel:*:wakeup/((((pid == 1178) && (args[1]->pr_pid == 1178)) && ((pid == $1))))/{
printf("<e> -1 1 %d %u %-14d\n%u\n%d\n%d\n%d\n%s \n</e>\n",cpu, tid, walltimestamp, (id_t)args[0]->pr_lwpid, 0x04, curcpu->cpu_id, pid, execname);printf("<s>\n");ustack(128);printf("</s>\n");
}
sched*:mach_kernel:*:on-cpu/((pid == $1))/{
self->state[curlwpsinfo->pr_lwpid] = curthread->state;
printf("<e> -1 2 %d %u %-14d\n%u\n%d\n%d \n</e>\n",cpu, tid, walltimestamp, curlwpsinfo->pr_lwpid, curthread->state, curcpu->cpu_id);printf("<s>\n");ustack(128);printf("</s>\n");
}
sched*:mach_kernel:*:off-cpu/((((curthread->state != 0x04) && !(curthread->state & 0x10)) && ((pid == $1))))/{
self->state[curlwpsinfo->pr_lwpid] = curthread->state;
printf("<e> -1 3 %d %u %-14d\n%u\n%d\n%d\n%d \n</e>\n",cpu, tid, walltimestamp, curlwpsinfo->pr_lwpid, curthread->state, curcpu->cpu_id, vtimestamp/1000000);printf("<s>\n");ustack(128);printf("</s>\n");
}
sched*:mach_kernel:*:off-cpu/((((curthread->state == 0x04)) && ((pid == $1))))/{
self->state[curlwpsinfo->pr_lwpid] = curthread->state;
printf("<e> -1 4 %d %u %-14d\n%u\n%d\n%d\n%d \n</e>\n",cpu, tid, walltimestamp, curlwpsinfo->pr_lwpid, 0x01, curcpu->cpu_id, vtimestamp/1000000);printf("<s>\n");ustack(128);printf("</s>\n");
}
fbt::thread_timer_event:entry/((((self->state[curlwpsinfo->pr_lwpid] != curthread->state) && !(curthread->state & 0x10)) && ((pid == $1))))/{
self->state[curlwpsinfo->pr_lwpid] = curthread->state;
printf("<e> -1 5 %d %u %-14d\n%u\n%d\n%d\n%d \n</e>\n",cpu, tid, walltimestamp, curlwpsinfo->pr_lwpid, curthread->state, curcpu->cpu_id, vtimestamp/1000000);printf("<s>\n");ustack(128);printf("</s>\n");
}
proc:::lwp-create/((((execname != "kernel_task")) && ((pid == $1))))/{
printf("<e> -1 6 %d %u %-14d\n%u\n%d\n%d\n%u\n%s \n</e>\n",cpu, tid, walltimestamp, args[0]->pr_lwpid, args[0]->pr_state, curcpu->cpu_id, tid, execname);printf("<s>\n");ustack(128);printf("</s>\n");
}
proc:::lwp-create/((((execname == "kernel_task")) && ((pid == $1))))/{
printf("<e> -1 7 %d %u %-14d\n%u\n%d\n%d\n%u\n%s\n%d \n</e>\n",cpu, tid, walltimestamp, args[0]->pr_lwpid, args[0]->pr_state, curcpu->cpu_id, tid, execname, pid);printf("<s>\n");stack(128);printf("</s>\n");
}
proc:::lwp-exit/((pid == $1))/{
printf("<e> -1 8 %d %u %-14d\n%u\n%d\n%d\n%d \n</e>\n",cpu, tid, walltimestamp, tid, 0x10, curcpu->cpu_id, vtimestamp/1000000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment