runtime: copy cpuprof code from Go 1.7 runtime
[official-gcc.git] / libgo / runtime / runtime.h
blob96f550ced0b140c29af66a9e250dd610b82576fd
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.
5 #include "config.h"
7 #include "go-assert.h"
8 #include <complex.h>
9 #include <signal.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <pthread.h>
18 #include <semaphore.h>
19 #include <ucontext.h>
21 #ifdef HAVE_SYS_MMAN_H
22 #include <sys/mman.h>
23 #endif
25 #include "interface.h"
26 #include "go-alloc.h"
28 #define _STRINGIFY2_(x) #x
29 #define _STRINGIFY_(x) _STRINGIFY2_(x)
30 #define GOSYM_PREFIX _STRINGIFY_(__USER_LABEL_PREFIX__)
32 /* This file supports C files copied from the 6g runtime library.
33 This is a version of the 6g runtime.h rewritten for gccgo's version
34 of the code. */
36 typedef signed int int8 __attribute__ ((mode (QI)));
37 typedef unsigned int uint8 __attribute__ ((mode (QI)));
38 typedef signed int int16 __attribute__ ((mode (HI)));
39 typedef unsigned int uint16 __attribute__ ((mode (HI)));
40 typedef signed int int32 __attribute__ ((mode (SI)));
41 typedef unsigned int uint32 __attribute__ ((mode (SI)));
42 typedef signed int int64 __attribute__ ((mode (DI)));
43 typedef unsigned int uint64 __attribute__ ((mode (DI)));
44 typedef float float32 __attribute__ ((mode (SF)));
45 typedef double float64 __attribute__ ((mode (DF)));
46 typedef signed int intptr __attribute__ ((mode (pointer)));
47 typedef unsigned int uintptr __attribute__ ((mode (pointer)));
49 typedef intptr intgo; // Go's int
50 typedef uintptr uintgo; // Go's uint
52 typedef uintptr uintreg;
54 /* Defined types. */
56 typedef uint8 bool;
57 typedef uint8 byte;
58 typedef struct Func Func;
59 typedef struct g G;
60 typedef struct mutex Lock;
61 typedef struct m M;
62 typedef struct p P;
63 typedef struct note Note;
64 typedef struct String String;
65 typedef struct FuncVal FuncVal;
66 typedef struct SigTab SigTab;
67 typedef struct mcache MCache;
68 typedef struct FixAlloc FixAlloc;
69 typedef struct hchan Hchan;
70 typedef struct Timers Timers;
71 typedef struct Timer Timer;
72 typedef struct gcstats GCStats;
73 typedef struct LFNode LFNode;
74 typedef struct ParFor ParFor;
75 typedef struct ParForThread ParForThread;
76 typedef struct cgoMal CgoMal;
77 typedef struct PollDesc PollDesc;
78 typedef struct sudog SudoG;
80 typedef struct __go_open_array Slice;
81 typedef struct __go_interface Iface;
82 typedef struct __go_empty_interface Eface;
83 typedef struct __go_type_descriptor Type;
84 typedef struct _defer Defer;
85 typedef struct _panic Panic;
87 typedef struct __go_ptr_type PtrType;
88 typedef struct __go_func_type FuncType;
89 typedef struct __go_interface_type InterfaceType;
90 typedef struct __go_map_type MapType;
91 typedef struct __go_channel_type ChanType;
93 typedef struct traceback Traceback;
95 typedef struct location Location;
97 struct String
99 const byte* str;
100 intgo len;
103 struct FuncVal
105 void (*fn)(void);
106 // variable-size, fn-specific data here
109 #include "array.h"
110 #include "interface.h"
112 // Rename Go types generated by mkrsysinfo.sh from C types, to avoid
113 // the name conflict.
114 #define timeval go_timeval
115 #define timespec go_timespec
117 #include "runtime.inc"
119 #undef timeval
120 #undef timespec
123 * Per-CPU declaration.
125 extern M* runtime_m(void);
126 extern G* runtime_g(void)
127 __asm__(GOSYM_PREFIX "runtime.getg");
129 extern M runtime_m0;
130 extern G runtime_g0;
132 enum
134 true = 1,
135 false = 0,
137 enum
139 PtrSize = sizeof(void*),
141 enum
143 // Per-M stack segment cache size.
144 StackCacheSize = 32,
145 // Global <-> per-M stack segment cache transfer batch size.
146 StackCacheBatch = 16,
149 struct SigTab
151 int32 sig;
152 int32 flags;
153 void* fwdsig;
156 // Layout of in-memory per-function information prepared by linker
157 // See http://golang.org/s/go12symtab.
158 // Keep in sync with linker and with ../../libmach/sym.c
159 // and with package debug/gosym.
160 struct Func
162 String name;
163 uintptr entry; // entry pc
166 #ifdef GOOS_nacl
167 enum {
168 NaCl = 1,
170 #else
171 enum {
172 NaCl = 0,
174 #endif
176 #ifdef GOOS_windows
177 enum {
178 Windows = 1
180 #else
181 enum {
182 Windows = 0
184 #endif
185 #ifdef GOOS_solaris
186 enum {
187 Solaris = 1
189 #else
190 enum {
191 Solaris = 0
193 #endif
195 struct Timers
197 Lock;
198 G *timerproc;
199 bool sleeping;
200 bool rescheduling;
201 Note waitnote;
202 Timer **t;
203 int32 len;
204 int32 cap;
207 // Package time knows the layout of this structure.
208 // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
209 // For GOOS=nacl, package syscall knows the layout of this structure.
210 // If this struct changes, adjust ../syscall/net_nacl.go:/runtimeTimer.
211 struct Timer
213 intgo i; // heap index
215 // Timer wakes up at when, and then at when+period, ... (period > 0 only)
216 // each time calling f(now, arg) in the timer goroutine, so f must be
217 // a well-behaved function and not block.
218 int64 when;
219 int64 period;
220 FuncVal *fv;
221 Eface arg;
222 uintptr seq;
225 // Lock-free stack node.
226 struct LFNode
228 LFNode *next;
229 uintptr pushcnt;
232 // Parallel for descriptor.
233 struct ParFor
235 const FuncVal *body; // executed for each element
236 uint32 done; // number of idle threads
237 uint32 nthr; // total number of threads
238 uint32 nthrmax; // maximum number of threads
239 uint32 thrseq; // thread id sequencer
240 uint32 cnt; // iteration space [0, cnt)
241 bool wait; // if true, wait while all threads finish processing,
242 // otherwise parfor may return while other threads are still working
243 ParForThread *thr; // array of thread descriptors
244 // stats
245 uint64 nsteal;
246 uint64 nstealcnt;
247 uint64 nprocyield;
248 uint64 nosyield;
249 uint64 nsleep;
252 extern bool runtime_precisestack;
253 extern bool runtime_copystack;
256 * defined macros
257 * you need super-gopher-guru privilege
258 * to add this list.
260 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
261 #define nil ((void*)0)
262 #define USED(v) ((void) v)
263 #define ROUND(x, n) (((x)+(n)-1)&~(uintptr)((n)-1)) /* all-caps to mark as macro: it evaluates n twice */
265 byte* runtime_startup_random_data;
266 uint32 runtime_startup_random_data_len;
267 void runtime_get_random_data(byte**, int32*);
269 enum {
270 // hashinit wants this many random bytes
271 HashRandomBytes = 32
273 void runtime_hashinit(void);
275 void runtime_traceback(void);
276 void runtime_tracebackothers(G*);
277 enum
279 // The maximum number of frames we print for a traceback
280 TracebackMaxFrames = 100,
284 * external data
286 extern uintptr runtime_zerobase;
287 extern G** runtime_allg;
288 extern uintptr runtime_allglen;
289 extern G* runtime_lastg;
290 extern M* runtime_allm;
291 extern P** runtime_allp;
292 extern int32 runtime_gomaxprocs;
293 extern uint32 runtime_needextram;
294 extern uint32 runtime_panicking;
295 extern int8* runtime_goos;
296 extern int32 runtime_ncpu;
297 extern void (*runtime_sysargs)(int32, uint8**);
298 extern struct debugVars runtime_debug;
299 extern uintptr runtime_maxstacksize;
301 extern bool runtime_isstarted;
302 extern bool runtime_isarchive;
305 * common functions and data
307 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
308 #define runtime_strncmp(s1, s2, n) __builtin_strncmp((s1), (s2), (n))
309 #define runtime_strstr(s1, s2) __builtin_strstr((s1), (s2))
310 intgo runtime_findnull(const byte*)
311 __asm__ (GOSYM_PREFIX "runtime.findnull");
313 void runtime_gogo(G*);
314 struct __go_func_type;
315 void runtime_args(int32, byte**)
316 __asm__ (GOSYM_PREFIX "runtime.args");
317 void runtime_osinit();
318 void runtime_goargs(void)
319 __asm__ (GOSYM_PREFIX "runtime.goargs");
320 void runtime_goenvs(void);
321 void runtime_goenvs_unix(void)
322 __asm__ (GOSYM_PREFIX "runtime.goenvs_unix");
323 void runtime_throw(const char*) __attribute__ ((noreturn));
324 void runtime_panicstring(const char*) __attribute__ ((noreturn));
325 bool runtime_canpanic(G*);
326 void runtime_printf(const char*, ...);
327 int32 runtime_snprintf(byte*, int32, const char*, ...);
328 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
329 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
330 void* runtime_mal(uintptr);
331 String runtime_gostringnocopy(const byte*)
332 __asm__ (GOSYM_PREFIX "runtime.gostringnocopy");
333 void runtime_schedinit(void);
334 void runtime_initsig(bool);
335 void runtime_sigenable(uint32 sig);
336 void runtime_sigdisable(uint32 sig);
337 void runtime_sigignore(uint32 sig);
338 int32 runtime_gotraceback(bool *crash);
339 void runtime_goroutineheader(G*);
340 void runtime_printtrace(Location*, int32, bool);
341 #define runtime_open(p, f, m) open((p), (f), (m))
342 #define runtime_read(d, v, n) read((d), (v), (n))
343 #define runtime_write(d, v, n) write((d), (v), (n))
344 #define runtime_close(d) close(d)
345 void runtime_ready(G*);
346 String runtime_getenv(const char*);
347 int32 runtime_atoi(const byte*, intgo);
348 void* runtime_mstart(void*);
349 G* runtime_malg(int32, byte**, uintptr*);
350 void runtime_mpreinit(M*);
351 void runtime_minit(void);
352 void runtime_unminit(void);
353 void runtime_needm(void);
354 void runtime_dropm(void);
355 void runtime_signalstack(byte*, int32);
356 MCache* runtime_allocmcache(void);
357 void runtime_freemcache(MCache*);
358 void runtime_mallocinit(void);
359 void runtime_mprofinit(void);
360 #define runtime_malloc(s) __go_alloc(s)
361 #define runtime_free(p) __go_free(p)
362 #define runtime_getcallersp(p) __builtin_frame_address(1)
363 int32 runtime_mcount(void);
364 int32 runtime_gcount(void);
365 void runtime_mcall(void(*)(G*));
366 uint32 runtime_fastrand1(void) __asm__ (GOSYM_PREFIX "runtime.fastrand1");
367 int32 runtime_timediv(int64, int32, int32*)
368 __asm__ (GOSYM_PREFIX "runtime.timediv");
369 int32 runtime_round2(int32 x); // round x up to a power of 2.
371 // atomic operations
372 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
373 #define runtime_cas64(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
374 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
375 // Don't confuse with XADD x86 instruction,
376 // this one is actually 'addx', that is, add-and-fetch.
377 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
378 #define runtime_xadd64(p, v) __sync_add_and_fetch (p, v)
379 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
380 #define runtime_xchg64(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
381 #define runtime_xchgp(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
382 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
383 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
384 #define runtime_atomicstore64(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
385 #define runtime_atomicload64(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
386 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
387 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
389 void runtime_setg(G*);
390 void runtime_newextram(void);
391 #define runtime_exit(s) exit(s)
392 #define runtime_breakpoint() __builtin_trap()
393 void runtime_gosched(void);
394 void runtime_gosched0(G*);
395 void runtime_schedtrace(bool);
396 void runtime_park(bool(*)(G*, void*), void*, const char*);
397 void runtime_parkunlock(Lock*, const char*);
398 void runtime_tsleep(int64, const char*);
399 M* runtime_newm(void);
400 void runtime_goexit(void);
401 void runtime_entersyscall(int32)
402 __asm__ (GOSYM_PREFIX "runtime.entersyscall");
403 void runtime_entersyscallblock(int32)
404 __asm__ (GOSYM_PREFIX "runtime.entersyscallblock");
405 void runtime_exitsyscall(int32)
406 __asm__ (GOSYM_PREFIX "runtime.exitsyscall");
407 G* __go_go(void (*pfn)(void*), void*);
408 void siginit(void);
409 bool __go_sigsend(int32 sig);
410 int32 runtime_callers(int32, Location*, int32, bool keep_callers);
411 int64 runtime_nanotime(void) // monotonic time
412 __asm__(GOSYM_PREFIX "runtime.nanotime");
413 int64 runtime_unixnanotime(void); // real time, can skip
414 void runtime_dopanic(int32) __attribute__ ((noreturn));
415 void runtime_startpanic(void);
416 void runtime_freezetheworld(void);
417 void runtime_unwindstack(G*, byte*);
418 void runtime_sigprof();
419 void runtime_resetcpuprofiler(int32);
420 void runtime_setcpuprofilerate_m(int32)
421 __asm__ (GOSYM_PREFIX "runtime.setcpuprofilerate_m");
422 void runtime_cpuprofAdd(Slice)
423 __asm__ (GOSYM_PREFIX "runtime.cpuprofAdd");
424 void runtime_usleep(uint32)
425 __asm__ (GOSYM_PREFIX "runtime.usleep");
426 int64 runtime_cputicks(void)
427 __asm__ (GOSYM_PREFIX "runtime.cputicks");
428 int64 runtime_tickspersecond(void)
429 __asm__ (GOSYM_PREFIX "runtime.tickspersecond");
430 void runtime_blockevent(int64, int32);
431 extern int64 runtime_blockprofilerate;
432 void runtime_addtimer(Timer*);
433 bool runtime_deltimer(Timer*);
434 G* runtime_netpoll(bool);
435 void runtime_netpollinit(void);
436 int32 runtime_netpollopen(uintptr, PollDesc*);
437 int32 runtime_netpollclose(uintptr);
438 void runtime_netpollready(G**, PollDesc*, int32);
439 uintptr runtime_netpollfd(PollDesc*);
440 void runtime_netpollarm(PollDesc*, int32);
441 void** runtime_netpolluser(PollDesc*);
442 bool runtime_netpollclosing(PollDesc*);
443 void runtime_netpolllock(PollDesc*);
444 void runtime_netpollunlock(PollDesc*);
445 void runtime_crash(void);
446 void runtime_parsedebugvars(void)
447 __asm__(GOSYM_PREFIX "runtime.parsedebugvars");
448 void _rt0_go(void);
449 void* runtime_funcdata(Func*, int32);
450 int32 runtime_setmaxthreads(int32);
451 G* runtime_timejump(void);
452 void runtime_iterate_finq(void (*callback)(FuncVal*, void*, const FuncType*, const PtrType*));
454 void runtime_stopTheWorldWithSema(void)
455 __asm__(GOSYM_PREFIX "runtime.stopTheWorldWithSema");
456 void runtime_startTheWorldWithSema(void)
457 __asm__(GOSYM_PREFIX "runtime.startTheWorldWithSema");
458 void runtime_acquireWorldsema(void)
459 __asm__(GOSYM_PREFIX "runtime.acquireWorldsema");
460 void runtime_releaseWorldsema(void)
461 __asm__(GOSYM_PREFIX "runtime.releaseWorldsema");
464 * mutual exclusion locks. in the uncontended case,
465 * as fast as spin locks (just a few user-level instructions),
466 * but on the contention path they sleep in the kernel.
467 * a zeroed Lock is unlocked (no need to initialize each lock).
469 void runtime_lock(Lock*)
470 __asm__(GOSYM_PREFIX "runtime.lock");
471 void runtime_unlock(Lock*)
472 __asm__(GOSYM_PREFIX "runtime.unlock");
475 * sleep and wakeup on one-time events.
476 * before any calls to notesleep or notewakeup,
477 * must call noteclear to initialize the Note.
478 * then, exactly one thread can call notesleep
479 * and exactly one thread can call notewakeup (once).
480 * once notewakeup has been called, the notesleep
481 * will return. future notesleep will return immediately.
482 * subsequent noteclear must be called only after
483 * previous notesleep has returned, e.g. it's disallowed
484 * to call noteclear straight after notewakeup.
486 * notetsleep is like notesleep but wakes up after
487 * a given number of nanoseconds even if the event
488 * has not yet happened. if a goroutine uses notetsleep to
489 * wake up early, it must wait to call noteclear until it
490 * can be sure that no other goroutine is calling
491 * notewakeup.
493 * notesleep/notetsleep are generally called on g0,
494 * notetsleepg is similar to notetsleep but is called on user g.
496 void runtime_noteclear(Note*)
497 __asm__ (GOSYM_PREFIX "runtime.noteclear");
498 void runtime_notesleep(Note*)
499 __asm__ (GOSYM_PREFIX "runtime.notesleep");
500 void runtime_notewakeup(Note*)
501 __asm__ (GOSYM_PREFIX "runtime.notewakeup");
502 bool runtime_notetsleep(Note*, int64) // false - timeout
503 __asm__ (GOSYM_PREFIX "runtime.notetsleep");
504 bool runtime_notetsleepg(Note*, int64) // false - timeout
505 __asm__ (GOSYM_PREFIX "runtime.notetsleepg");
508 * Lock-free stack.
509 * Initialize uint64 head to 0, compare with 0 to test for emptiness.
510 * The stack does not keep pointers to nodes,
511 * so they can be garbage collected if there are no other pointers to nodes.
513 void runtime_lfstackpush(uint64 *head, LFNode *node)
514 __asm__ (GOSYM_PREFIX "runtime.lfstackpush");
515 LFNode* runtime_lfstackpop(uint64 *head);
518 * Parallel for over [0, n).
519 * body() is executed for each iteration.
520 * nthr - total number of worker threads.
521 * if wait=true, threads return from parfor() when all work is done;
522 * otherwise, threads can return while other threads are still finishing processing.
524 ParFor* runtime_parforalloc(uint32 nthrmax);
525 void runtime_parforsetup(ParFor *desc, uint32 nthr, uint32 n, bool wait, const FuncVal *body);
526 void runtime_parfordo(ParFor *desc);
527 void runtime_parforiters(ParFor*, uintptr, uintptr*, uintptr*);
530 * low level C-called
532 #define runtime_mmap mmap
533 #define runtime_munmap munmap
534 #define runtime_madvise madvise
535 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
536 #define runtime_getcallerpc(p) __builtin_return_address(0)
538 #ifdef __rtems__
539 void __wrap_rtems_task_variable_add(void **);
540 #endif
543 * runtime go-called
545 void reflect_call(const struct __go_func_type *, FuncVal *, _Bool, _Bool,
546 void **, void **)
547 __asm__ (GOSYM_PREFIX "reflect.call");
548 #define runtime_panic __go_panic
551 * runtime c-called (but written in Go)
553 void runtime_printany(Eface)
554 __asm__ (GOSYM_PREFIX "runtime.Printany");
555 void runtime_newTypeAssertionError(const String*, const String*, const String*, const String*, Eface*)
556 __asm__ (GOSYM_PREFIX "runtime.NewTypeAssertionError");
557 void runtime_newErrorCString(const char*, Eface*)
558 __asm__ (GOSYM_PREFIX "runtime.NewErrorCString");
561 * wrapped for go users
563 void runtime_semacquire(uint32 volatile *, bool)
564 __asm__ (GOSYM_PREFIX "runtime.semacquire");
565 void runtime_semrelease(uint32 volatile *)
566 __asm__ (GOSYM_PREFIX "runtime.semrelease");
567 int32 runtime_gomaxprocsfunc(int32 n);
568 void runtime_procyield(uint32)
569 __asm__(GOSYM_PREFIX "runtime.procyield");
570 void runtime_osyield(void)
571 __asm__(GOSYM_PREFIX "runtime.osyield");
572 void runtime_lockOSThread(void);
573 void runtime_unlockOSThread(void);
574 bool runtime_lockedOSThread(void);
576 bool runtime_showframe(String, bool);
577 void runtime_printcreatedby(G*);
579 uintptr runtime_memlimit(void);
581 #define ISNAN(f) __builtin_isnan(f)
583 enum
585 UseSpanType = 1,
588 #define runtime_setitimer setitimer
590 void runtime_check(void)
591 __asm__ (GOSYM_PREFIX "runtime.check");
593 // A list of global variables that the garbage collector must scan.
594 struct root_list {
595 struct root_list *next;
596 struct root {
597 void *decl;
598 size_t size;
599 } roots[];
602 void __go_register_gc_roots(struct root_list*);
604 // Size of stack space allocated using Go's allocator.
605 // This will be 0 when using split stacks, as in that case
606 // the stacks are allocated by the splitstack library.
607 extern uintptr runtime_stacks_sys;
609 struct backtrace_state;
610 extern struct backtrace_state *__go_get_backtrace_state(void);
611 extern _Bool __go_file_line(uintptr, int, String*, String*, intgo *);
612 extern void runtime_main(void*);
613 extern uint32 runtime_in_callers;
615 int32 getproccount(void);
617 #define PREFETCH(p) __builtin_prefetch(p)
619 bool runtime_gcwaiting(void);
620 void runtime_badsignal(int);
621 Defer* runtime_newdefer(void);
622 void runtime_freedefer(Defer*);
624 struct time_now_ret
626 int64_t sec;
627 int32_t nsec;
630 struct time_now_ret now() __asm__ (GOSYM_PREFIX "time.now")
631 __attribute__ ((no_split_stack));
633 extern void _cgo_wait_runtime_init_done (void);
634 extern void _cgo_notify_runtime_init_done (void);
635 extern _Bool runtime_iscgo;
636 extern _Bool runtime_cgoHasExtraM;
637 extern Hchan *runtime_main_init_done;
638 extern uintptr __go_end __attribute__ ((weak));