Commit c19b373c authored by Russ Cox's avatar Russ Cox

runtime: cpu profiling support

R=r
CC=golang-dev
https://golang.org/cl/4306043
parent f9fc1ddf
......@@ -53,6 +53,7 @@ OFILES=\
cgocall.$O\
chan.$O\
closure.$O\
cpuprof.$O\
float.$O\
complex.$O\
hashmap.$O\
......
......@@ -18,8 +18,8 @@ void runtime·morestack(void);
// as well as the runtime.Callers function (pcbuf != nil).
// A little clunky to merge the two but avoids duplicating
// the code and all its subtlety.
static int32
gentraceback(byte *pc0, byte *sp, G *g, int32 skip, uintptr *pcbuf, int32 max)
int32
runtime·gentraceback(byte *pc0, byte *sp, byte *lr0, G *g, int32 skip, uintptr *pcbuf, int32 max)
{
byte *p;
int32 i, n, iter, sawnewstack;
......@@ -28,6 +28,7 @@ gentraceback(byte *pc0, byte *sp, G *g, int32 skip, uintptr *pcbuf, int32 max)
Stktop *stk;
Func *f;
USED(lr0);
pc = (uintptr)pc0;
lr = 0;
fp = nil;
......@@ -199,7 +200,7 @@ gentraceback(byte *pc0, byte *sp, G *g, int32 skip, uintptr *pcbuf, int32 max)
void
runtime·traceback(byte *pc0, byte *sp, byte*, G *g)
{
gentraceback(pc0, sp, g, 0, nil, 100);
runtime·gentraceback(pc0, sp, nil, g, 0, nil, 100);
}
int32
......@@ -211,7 +212,7 @@ runtime·callers(int32 skip, uintptr *pcbuf, int32 m)
sp = (byte*)&skip;
pc = runtime·getcallerpc(&skip);
return gentraceback(pc, sp, g, skip, pcbuf, m);
return runtime·gentraceback(pc, sp, nil, g, skip, pcbuf, m);
}
static uintptr
......
......@@ -15,7 +15,7 @@ void _divu(void);
void _modu(void);
static int32
gentraceback(byte *pc0, byte *sp, byte *lr0, G *g, int32 skip, uintptr *pcbuf, int32 max)
runtime·gentraceback(byte *pc0, byte *sp, byte *lr0, G *g, int32 skip, uintptr *pcbuf, int32 max)
{
int32 i, n, iter;
uintptr pc, lr, tracepc, x;
......@@ -189,11 +189,10 @@ gentraceback(byte *pc0, byte *sp, byte *lr0, G *g, int32 skip, uintptr *pcbuf, i
return n;
}
void
runtime·traceback(byte *pc0, byte *sp, byte *lr, G *g)
{
gentraceback(pc0, sp, lr, g, 0, nil, 100);
runtime·gentraceback(pc0, sp, lr, g, 0, nil, 100);
}
// func caller(n int) (pc uintptr, file string, line int, ok bool)
......@@ -205,5 +204,5 @@ runtime·callers(int32 skip, uintptr *pcbuf, int32 m)
sp = runtime·getcallersp(&skip);
pc = runtime·getcallerpc(&skip);
return gentraceback(pc, sp, 0, g, skip, pcbuf, m);
return runtime·gentraceback(pc, sp, 0, g, skip, pcbuf, m);
}
This diff is collapsed.
......@@ -46,6 +46,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp)
mc = uc->uc_mcontext;
r = &mc->ss;
if(sig == SIGPROF) {
runtime·sigprof((uint8*)r->eip, (uint8*)r->esp, nil, gp);
return;
}
if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) {
// Work around Leopard bug that doesn't set FPE_INTDIV.
// Look at instruction to see if it is a divide.
......@@ -126,31 +131,58 @@ runtime·signalstack(byte *p, int32 n)
runtime·sigaltstack(&st, nil);
}
static void
sigaction(int32 i, void (*fn)(int32, Siginfo*, void*, G*), bool restart)
{
Sigaction sa;
runtime·memclr((byte*)&sa, sizeof sa);
sa.sa_flags = SA_SIGINFO|SA_ONSTACK;
if(restart)
sa.sa_flags |= SA_RESTART;
sa.sa_mask = ~0U;
sa.sa_tramp = (uintptr)runtime·sigtramp; // runtime·sigtramp's job is to call into real handler
sa.__sigaction_u.__sa_sigaction = (uintptr)fn;
runtime·sigaction(i, &sa, nil);
}
void
runtime·initsig(int32 queue)
{
int32 i;
static Sigaction sa;
void *fn;
runtime·siginit();
sa.sa_flags |= SA_SIGINFO|SA_ONSTACK;
sa.sa_mask = 0xFFFFFFFFU;
sa.sa_tramp = runtime·sigtramp; // runtime·sigtramp's job is to call into real handler
for(i = 0; i<NSIG; i++) {
if(runtime·sigtab[i].flags) {
if((runtime·sigtab[i].flags & SigQueue) != queue)
continue;
if(runtime·sigtab[i].flags & (SigCatch | SigQueue)) {
sa.__sigaction_u.__sa_sigaction = runtime·sighandler;
} else {
sa.__sigaction_u.__sa_sigaction = runtime·sigignore;
}
if(runtime·sigtab[i].flags & SigRestart)
sa.sa_flags |= SA_RESTART;
if(runtime·sigtab[i].flags & (SigCatch | SigQueue))
fn = runtime·sighandler;
else
sa.sa_flags &= ~SA_RESTART;
runtime·sigaction(i, &sa, nil);
fn = runtime·sigignore;
sigaction(i, fn, (runtime·sigtab[i].flags & SigRestart) != 0);
}
}
}
void
runtime·resetcpuprofiler(int32 hz)
{
Sigaction sa;
Itimerval it;
runtime·memclr((byte*)&it, sizeof it);
if(hz == 0) {
runtime·setitimer(ITIMER_PROF, &it, nil);
sigaction(SIGPROF, SIG_IGN, true);
} else {
sigaction(SIGPROF, runtime·sighandler, true);
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000000 / hz;
it.it_value = it.it_interval;
runtime·setitimer(ITIMER_PROF, &it, nil);
}
m->profilehz = hz;
}
......@@ -54,6 +54,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp)
mc = uc->uc_mcontext;
r = &mc->ss;
if(sig == SIGPROF) {
runtime·sigprof((uint8*)r->rip, (uint8*)r->rsp, nil, gp);
return;
}
if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) {
// Work around Leopard bug that doesn't set FPE_INTDIV.
// Look at instruction to see if it is a divide.
......@@ -136,31 +141,58 @@ runtime·signalstack(byte *p, int32 n)
runtime·sigaltstack(&st, nil);
}
static void
sigaction(int32 i, void (*fn)(int32, Siginfo*, void*, G*), bool restart)
{
Sigaction sa;
runtime·memclr((byte*)&sa, sizeof sa);
sa.sa_flags = SA_SIGINFO|SA_ONSTACK;
if(restart)
sa.sa_flags |= SA_RESTART;
sa.sa_mask = ~0ULL;
sa.sa_tramp = (uintptr)runtime·sigtramp; // runtime·sigtramp's job is to call into real handler
sa.__sigaction_u.__sa_sigaction = (uintptr)fn;
runtime·sigaction(i, &sa, nil);
}
void
runtime·initsig(int32 queue)
{
int32 i;
static Sigaction sa;
void *fn;
runtime·siginit();
sa.sa_flags |= SA_SIGINFO|SA_ONSTACK;
sa.sa_mask = 0xFFFFFFFFU;
sa.sa_tramp = runtime·sigtramp; // runtime·sigtramp's job is to call into real handler
for(i = 0; i<NSIG; i++) {
if(runtime·sigtab[i].flags) {
if((runtime·sigtab[i].flags & SigQueue) != queue)
continue;
if(runtime·sigtab[i].flags & (SigCatch | SigQueue)) {
sa.__sigaction_u.__sa_sigaction = runtime·sighandler;
} else {
sa.__sigaction_u.__sa_sigaction = runtime·sigignore;
}
if(runtime·sigtab[i].flags & SigRestart)
sa.sa_flags |= SA_RESTART;
if(runtime·sigtab[i].flags & (SigCatch | SigQueue))
fn = runtime·sighandler;
else
sa.sa_flags &= ~SA_RESTART;
runtime·sigaction(i, &sa, nil);
fn = runtime·sigignore;
sigaction(i, fn, (runtime·sigtab[i].flags & SigRestart) != 0);
}
}
}
void
runtime·resetcpuprofiler(int32 hz)
{
Sigaction sa;
Itimerval it;
runtime·memclr((byte*)&it, sizeof it);
if(hz == 0) {
runtime·setitimer(ITIMER_PROF, &it, nil);
sigaction(SIGPROF, SIG_IGN, true);
} else {
sigaction(SIGPROF, runtime·sighandler, true);
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000000 / hz;
it.it_value = it.it_interval;
runtime·setitimer(ITIMER_PROF, &it, nil);
}
m->profilehz = hz;
}
......@@ -92,4 +92,24 @@ func (r *MemProfileRecord) Stack() []uintptr {
// where r.AllocBytes > 0 but r.AllocBytes == r.FreeBytes.
// These are sites where memory was allocated, but it has all
// been released back to the runtime.
// Most clients should use the runtime/pprof package or
// the testing package's -test.memprofile flag instead
// of calling MemProfile directly.
func MemProfile(p []MemProfileRecord, inuseZero bool) (n int, ok bool)
// CPUProfile returns the next chunk of binary CPU profiling stack trace data,
// blocking until data is available. If profiling is turned off and all the profile
// data accumulated while it was on has been returned, CPUProfile returns nil.
// The caller must save the returned data before calling CPUProfile again.
// Most clients should use the runtime/pprof package or
// the testing package's -test.cpuprofile flag instead of calling
// CPUProfile directly.
func CPUProfile() []byte
// SetCPUProfileRate sets the CPU profiling rate to hz samples per second.
// If hz <= 0, SetCPUProfileRate turns off profiling.
// If the profiler is on, the rate cannot be changed without first turning it off.
// Most clients should use the runtime/pprof package or
// the testing package's -test.cpuprofile flag instead of calling
// SetCPUProfileRate directly.
func SetCPUProfileRate(hz int)
......@@ -54,6 +54,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp)
uc = context;
r = &uc->uc_mcontext;
if(sig == SIGPROF) {
runtime·sigprof((uint8*)r->mc_eip, (uint8*)r->mc_esp, nil, gp);
return;
}
if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) {
// Make it look like a call to the signal func.
// Have to pass arguments out of band since
......@@ -122,32 +127,57 @@ runtime·signalstack(byte *p, int32 n)
runtime·sigaltstack(&st, nil);
}
static void
sigaction(int32 i, void (*fn)(int32, Siginfo*, void*, G*), bool restart)
{
Sigaction sa;
runtime·memclr((byte*)&sa, sizeof sa);
sa.sa_flags = SA_SIGINFO|SA_ONSTACK;
if(restart)
sa.sa_flags |= SA_RESTART;
sa.sa_mask = ~0ULL;
sa.__sigaction_u.__sa_sigaction = (uintptr)fn;
runtime·sigaction(i, &sa, nil);
}
void
runtime·initsig(int32 queue)
{
static Sigaction sa;
int32 i;
void *fn;
runtime·siginit();
int32 i;
sa.sa_flags |= SA_ONSTACK | SA_SIGINFO;
sa.sa_mask = ~0x0ull;
for(i = 0; i < NSIG; i++) {
for(i = 0; i<NSIG; i++) {
if(runtime·sigtab[i].flags) {
if((runtime·sigtab[i].flags & SigQueue) != queue)
continue;
if(runtime·sigtab[i].flags & (SigCatch | SigQueue))
sa.__sigaction_u.__sa_sigaction = (void*) runtime·sigtramp;
fn = runtime·sighandler;
else
sa.__sigaction_u.__sa_sigaction = (void*) runtime·sigignore;
if(runtime·sigtab[i].flags & SigRestart)
sa.sa_flags |= SA_RESTART;
else
sa.sa_flags &= ~SA_RESTART;
runtime·sigaction(i, &sa, nil);
fn = runtime·sigignore;
sigaction(i, fn, (runtime·sigtab[i].flags & SigRestart) != 0);
}
}
}
void
runtime·resetcpuprofiler(int32 hz)
{
Sigaction sa;
Itimerval it;
runtime·memclr((byte*)&it, sizeof it);
if(hz == 0) {
runtime·setitimer(ITIMER_PROF, &it, nil);
sigaction(SIGPROF, SIG_IGN, true);
} else {
sigaction(SIGPROF, runtime·sighandler, true);
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000000 / hz;
it.it_value = it.it_interval;
runtime·setitimer(ITIMER_PROF, &it, nil);
}
m->profilehz = hz;
}
......@@ -62,6 +62,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp)
uc = context;
r = &uc->uc_mcontext;
if(sig == SIGPROF) {
runtime·sigprof((uint8*)r->mc_rip, (uint8*)r->mc_rsp, nil, gp);
return;
}
if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) {
// Make it look like a call to the signal func.
// Have to pass arguments out of band since
......@@ -130,32 +135,57 @@ runtime·signalstack(byte *p, int32 n)
runtime·sigaltstack(&st, nil);
}
static void
sigaction(int32 i, void (*fn)(int32, Siginfo*, void*, G*), bool restart)
{
Sigaction sa;
runtime·memclr((byte*)&sa, sizeof sa);
sa.sa_flags = SA_SIGINFO|SA_ONSTACK;
if(restart)
sa.sa_flags |= SA_RESTART;
sa.sa_mask = ~0ULL;
sa.__sigaction_u.__sa_sigaction = (uintptr)fn;
runtime·sigaction(i, &sa, nil);
}
void
runtime·initsig(int32 queue)
{
static Sigaction sa;
int32 i;
void *fn;
runtime·siginit();
int32 i;
sa.sa_flags |= SA_ONSTACK | SA_SIGINFO;
sa.sa_mask = ~0x0ull;
for(i = 0; i < NSIG; i++) {
for(i = 0; i<NSIG; i++) {
if(runtime·sigtab[i].flags) {
if((runtime·sigtab[i].flags & SigQueue) != queue)
continue;
if(runtime·sigtab[i].flags & (SigCatch | SigQueue))
sa.__sigaction_u.__sa_sigaction = (void*) runtime·sigtramp;
fn = runtime·sighandler;
else
sa.__sigaction_u.__sa_sigaction = (void*) runtime·sigignore;
if(runtime·sigtab[i].flags & SigRestart)
sa.sa_flags |= SA_RESTART;
else
sa.sa_flags &= ~SA_RESTART;
runtime·sigaction(i, &sa, nil);
fn = runtime·sigignore;
sigaction(i, fn, (runtime·sigtab[i].flags & SigRestart) != 0);
}
}
}
void
runtime·resetcpuprofiler(int32 hz)
{
Sigaction sa;
Itimerval it;
runtime·memclr((byte*)&it, sizeof it);
if(hz == 0) {
runtime·setitimer(ITIMER_PROF, &it, nil);
sigaction(SIGPROF, SIG_IGN, true);
} else {
sigaction(SIGPROF, runtime·sighandler, true);
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000000 / hz;
it.it_value = it.it_interval;
runtime·setitimer(ITIMER_PROF, &it, nil);
}
m->profilehz = hz;
}
......@@ -51,6 +51,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp)
uc = context;
r = &uc->uc_mcontext;
if(sig == SIGPROF) {
runtime·sigprof((uint8*)r->eip, (uint8*)r->esp, nil, gp);
return;
}
if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) {
// Make it look like a call to the signal func.
// Have to pass arguments out of band since
......@@ -114,30 +119,59 @@ runtime·signalstack(byte *p, int32 n)
runtime·sigaltstack(&st, nil);
}
static void
sigaction(int32 i, void (*fn)(int32, Siginfo*, void*, G*), bool restart)
{
Sigaction sa;
runtime·memclr((byte*)&sa, sizeof sa);
sa.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESTORER;
if(restart)
sa.sa_flags |= SA_RESTART;
sa.sa_mask = ~0ULL;
sa.sa_restorer = (void*)runtime·sigreturn;
if(fn == runtime·sighandler)
fn = (void*)runtime·sigtramp;
sa.k_sa_handler = fn;
runtime·rt_sigaction(i, &sa, nil, 8);
}
void
runtime·initsig(int32 queue)
{
static Sigaction sa;
int32 i;
void *fn;
runtime·siginit();
int32 i;
sa.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESTORER;
sa.sa_mask = 0xFFFFFFFFFFFFFFFFULL;
sa.sa_restorer = (void*)runtime·sigreturn;
for(i = 0; i<NSIG; i++) {
if(runtime·sigtab[i].flags) {
if((runtime·sigtab[i].flags & SigQueue) != queue)
continue;
if(runtime·sigtab[i].flags & (SigCatch | SigQueue))
sa.k_sa_handler = (void*)runtime·sigtramp;
fn = runtime·sighandler;
else
sa.k_sa_handler = (void*)runtime·sigignore;
if(runtime·sigtab[i].flags & SigRestart)
sa.sa_flags |= SA_RESTART;
else
sa.sa_flags &= ~SA_RESTART;
runtime·rt_sigaction(i, &sa, nil, 8);
fn = runtime·sigignore;
sigaction(i, fn, (runtime·sigtab[i].flags & SigRestart) != 0);
}
}
}
void
runtime·resetcpuprofiler(int32 hz)
{
Itimerval it;
runtime·memclr((byte*)&it, sizeof it);
if(hz == 0) {
runtime·setitimer(ITIMER_PROF, &it, nil);
sigaction(SIGPROF, SIG_IGN, true);
} else {
sigaction(SIGPROF, runtime·sighandler, true);
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000000 / hz;
it.it_value = it.it_interval;
runtime·setitimer(ITIMER_PROF, &it, nil);
}
m->profilehz = hz;
}
......@@ -61,6 +61,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp)
mc = &uc->uc_mcontext;
r = (Sigcontext*)mc; // same layout, more conveient names
if(sig == SIGPROF) {
runtime·sigprof((uint8*)r->rip, (uint8*)r->rsp, nil, gp);
return;
}
if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) {
// Make it look like a call to the signal func.
// Have to pass arguments out of band since
......@@ -124,30 +129,59 @@ runtime·signalstack(byte *p, int32 n)
runtime·sigaltstack(&st, nil);
}
static void
sigaction(int32 i, void (*fn)(int32, Siginfo*, void*, G*), bool restart)
{
Sigaction sa;
runtime·memclr((byte*)&sa, sizeof sa);
sa.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESTORER;
if(restart)
sa.sa_flags |= SA_RESTART;
sa.sa_mask = ~0ULL;
sa.sa_restorer = (void*)runtime·sigreturn;
if(fn == runtime·sighandler)
fn = (void*)runtime·sigtramp;
sa.sa_handler = fn;
runtime·rt_sigaction(i, &sa, nil, 8);
}
void
runtime·initsig(int32 queue)
{
static Sigaction sa;
int32 i;
void *fn;
runtime·siginit();
int32 i;
sa.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESTORER;
sa.sa_mask = 0xFFFFFFFFFFFFFFFFULL;
sa.sa_restorer = (void*)runtime·sigreturn;
for(i = 0; i<NSIG; i++) {
if(runtime·sigtab[i].flags) {
if((runtime·sigtab[i].flags & SigQueue) != queue)
continue;
if(runtime·sigtab[i].flags & (SigCatch | SigQueue))
sa.sa_handler = (void*)runtime·sigtramp;
fn = runtime·sighandler;
else
sa.sa_handler = (void*)runtime·sigignore;
if(runtime·sigtab[i].flags & SigRestart)
sa.sa_flags |= SA_RESTART;
else
sa.sa_flags &= ~SA_RESTART;
runtime·rt_sigaction(i, &sa, nil, 8);
fn = runtime·sigignore;
sigaction(i, fn, (runtime·sigtab[i].flags & SigRestart) != 0);
}
}
}
void
runtime·resetcpuprofiler(int32 hz)
{
Itimerval it;
runtime·memclr((byte*)&it, sizeof it);
if(hz == 0) {
runtime·setitimer(ITIMER_PROF, &it, nil);
sigaction(SIGPROF, SIG_IGN, true);
} else {
sigaction(SIGPROF, runtime·sighandler, true);
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000000 / hz;
it.it_value = it.it_interval;
runtime·setitimer(ITIMER_PROF, &it, nil);
}
m->profilehz = hz;
}
......@@ -58,6 +58,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp)
uc = context;
r = &uc->uc_mcontext;
if(sig == SIGPROF) {
runtime·sigprof((uint8*)r->arm_pc, (uint8*)r->arm_sp, (uint8*)r->arm_lr, gp);
return;
}
if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) {
// Make it look like a call to the signal func.
// Have to pass arguments out of band since
......@@ -119,31 +124,58 @@ runtime·signalstack(byte *p, int32 n)
runtime·sigaltstack(&st, nil);
}
static void
sigaction(int32 i, void (*fn)(int32, Siginfo*, void*, G*), bool restart)
{
Sigaction sa;
runtime·memclr((byte*)&sa, sizeof sa);
sa.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESTORER;
if(restart)
sa.sa_flags |= SA_RESTART;
sa.sa_mask = ~0ULL;
sa.sa_restorer = (void*)runtime·sigreturn;
sa.k_sa_handler = fn;
runtime·rt_sigaction(i, &sa, nil, 8);
}
void
runtime·initsig(int32 queue)
{
static Sigaction sa;
int32 i;
void *fn;
runtime·siginit();
int32 i;
sa.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESTORER;
sa.sa_mask.sig[0] = 0xFFFFFFFF;
sa.sa_mask.sig[1] = 0xFFFFFFFF;
sa.sa_restorer = (void*)runtime·sigreturn;
for(i = 0; i<NSIG; i++) {
if(runtime·sigtab[i].flags) {
if((runtime·sigtab[i].flags & SigQueue) != queue)
continue;
if(runtime·sigtab[i].flags & (SigCatch | SigQueue))
sa.sa_handler = (void*)runtime·sigtramp;
fn = runtime·sighandler;
else
sa.sa_handler = (void*)runtime·sigignore;
if(runtime·sigtab[i].flags & SigRestart)
sa.sa_flags |= SA_RESTART;
else
sa.sa_flags &= ~SA_RESTART;
runtime·rt_sigaction(i, &sa, nil, 8);
fn = runtime·sigignore;
sigaction(i, fn, (runtime·sigtab[i].flags & SigRestart) != 0);
}
}
}
void
runtime·resetcpuprofiler(int32 hz)
{
Sigaction sa;
Itimerval it;
runtime·memclr((byte*)&it, sizeof it);
if(hz == 0) {
runtime·setitimer(ITIMER_PROF, &it, nil);
sigaction(SIGPROF, SIG_IGN, true);
} else {
sigaction(SIGPROF, runtime·sighandler, true);
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000000 / hz;
it.it_value = it.it_interval;
runtime·setitimer(ITIMER_PROF, &it, nil);
}
m->profilehz = hz;
}
......@@ -14,3 +14,11 @@ runtime·signame(int32)
{
return runtime·emptystring;
}
void
runtime·resetcpuprofiler(int32 hz)
{
// TODO: Enable profiling interrupts.
m->profilehz = hz;
}
......@@ -70,6 +70,7 @@ struct Sched {
int32 msyscall; // number of ms in system calls
int32 predawn; // running initialization, don't run new gs.
int32 profilehz; // cpu profiling rate
Note stopped; // one g can wait here for ms to stop
int32 waitstop; // after setting this flag
......@@ -96,9 +97,6 @@ static void matchmg(void); // match ms to gs
static void readylocked(G*); // ready, but sched is locked
static void mnextg(M*, G*);
// Scheduler loop.
static void scheduler(void);
// The bootstrap sequence is:
//
// call osinit
......@@ -529,6 +527,8 @@ matchmg(void)
static void
schedule(G *gp)
{
int32 hz;
schedlock();
if(gp != nil) {
if(runtime·sched.predawn)
......@@ -574,6 +574,12 @@ schedule(G *gp)
gp->status = Grunning;
m->curg = gp;
gp->m = m;
// Check whether the profiler needs to be turned on or off.
hz = runtime·sched.profilehz;
if(m->profilehz != hz)
runtime·resetcpuprofiler(hz);
if(gp->sched.pc == (byte*)runtime·goexit) { // kickoff
runtime·gogocall(&gp->sched, (void(*)(void))gp->entry);
}
......@@ -640,7 +646,7 @@ runtime·exitsyscall(void)
runtime·sched.msyscall--;
runtime·sched.mcpu++;
// Fast path - if there's room for this m, we're done.
if(runtime·sched.mcpu <= runtime·sched.mcpumax) {
if(m->profilehz == runtime·sched.profilehz && runtime·sched.mcpu <= runtime·sched.mcpumax) {
g->status = Grunning;
schedunlock();
return;
......@@ -1251,3 +1257,57 @@ runtime·badmcall2(void) // called from assembly
{
runtime·throw("runtime: mcall function returned");
}
static struct {
Lock;
void (*fn)(uintptr*, int32);
int32 hz;
uintptr pcbuf[100];
} prof;
void
runtime·sigprof(uint8 *pc, uint8 *sp, uint8 *lr, G *gp)
{
int32 n;
if(prof.fn == nil || prof.hz == 0)
return;
runtime·lock(&prof);
if(prof.fn == nil) {
runtime·unlock(&prof);
return;
}
n = runtime·gentraceback(pc, sp, lr, gp, 0, prof.pcbuf, nelem(prof.pcbuf));
if(n > 0)
prof.fn(prof.pcbuf, n);
runtime·unlock(&prof);
}
void
runtime·setcpuprofilerate(void (*fn)(uintptr*, int32), int32 hz)
{
// Force sane arguments.
if(hz < 0)
hz = 0;
if(hz == 0)
fn = nil;
if(fn == nil)
hz = 0;
// Stop profiler on this cpu so that it is safe to lock prof.
// if a profiling signal came in while we had prof locked,
// it would deadlock.
runtime·resetcpuprofiler(0);
runtime·lock(&prof);
prof.fn = fn;
prof.hz = hz;
runtime·unlock(&prof);
runtime·lock(&runtime·sched);
runtime·sched.profilehz = hz;
runtime·unlock(&runtime·sched);
if(hz != 0)
runtime·resetcpuprofiler(hz);
}
......@@ -225,6 +225,7 @@ struct M
int32 nomemprof;
int32 waitnextg;
int32 dying;
int32 profilehz;
Note havenextg;
G* nextg;
M* alllink; // on allm
......@@ -453,9 +454,13 @@ void runtime·siginit(void);
bool runtime·sigsend(int32 sig);
void runtime·gettime(int64*, int32*);
int32 runtime·callers(int32, uintptr*, int32);
int32 runtime·gentraceback(byte*, byte*, byte*, G*, int32, uintptr*, int32);
int64 runtime·nanotime(void);
void runtime·dopanic(int32);
void runtime·startpanic(void);
void runtime·sigprof(uint8 *pc, uint8 *sp, uint8 *lr, G *gp);
void runtime·resetcpuprofiler(int32);
void runtime·setcpuprofilerate(void(*)(uintptr*, int32), int32);
#pragma varargck argpos runtime·printf 1
#pragma varargck type "d" int32
......
......@@ -88,3 +88,11 @@ runtime·sighandler(ExceptionRecord *info, void *frame, Context *r)
runtime·exit(2);
return 0;
}
void
runtime·resetcpuprofiler(int32 hz)
{
// TODO: Enable profiling interrupts.
m->profilehz = hz;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment