OSDN Git Service

bbdf894f49d1a68a328db439d7cac07da79967c5
[pf3gnuchains/gcc-fork.git] / libgo / runtime / proc.c
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 #include "runtime.h"
6 #include "malloc.h"     /* so that acid generated from proc.c includes malloc data structures */
7
8 typedef struct Sched Sched;
9
10 M       m0;
11
12 #ifdef __rtems__
13 #define __thread
14 #endif
15
16 __thread M *m = &m0;
17
18 static struct {
19         Lock;
20         void (*fn)(uintptr*, int32);
21         int32 hz;
22         uintptr pcbuf[100];
23 } prof;
24
25 void
26 runtime_sigprof(uint8 *pc __attribute__ ((unused)),
27                 uint8 *sp __attribute__ ((unused)),
28                 uint8 *lr __attribute__ ((unused)))
29 {
30         int32 n;
31         
32         if(prof.fn == nil || prof.hz == 0)
33                 return;
34         
35         runtime_lock(&prof);
36         if(prof.fn == nil) {
37                 runtime_unlock(&prof);
38                 return;
39         }
40         n = 0;
41         // n = runtime·gentraceback(pc, sp, lr, gp, 0, prof.pcbuf, nelem(prof.pcbuf));
42         if(n > 0)
43                 prof.fn(prof.pcbuf, n);
44         runtime_unlock(&prof);
45 }
46
47 void
48 runtime_setcpuprofilerate(void (*fn)(uintptr*, int32), int32 hz)
49 {
50         // Force sane arguments.
51         if(hz < 0)
52                 hz = 0;
53         if(hz == 0)
54                 fn = nil;
55         if(fn == nil)
56                 hz = 0;
57
58         // Stop profiler on this cpu so that it is safe to lock prof.
59         // if a profiling signal came in while we had prof locked,
60         // it would deadlock.
61         runtime_resetcpuprofiler(0);
62
63         runtime_lock(&prof);
64         prof.fn = fn;
65         prof.hz = hz;
66         runtime_unlock(&prof);
67         // runtime_lock(&runtime_sched);
68         // runtime_sched.profilehz = hz;
69         // runtime_unlock(&runtime_sched);
70         
71         if(hz != 0)
72                 runtime_resetcpuprofiler(hz);
73 }