forked from bytedance/gopkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
213 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include "textflag.h" | ||
#include "go_tls.h" | ||
|
||
TEXT ·getg(SB), NOSPLIT, $0-8 | ||
get_tls(CX) | ||
MOVQ g(CX), AX | ||
MOVQ AX, ret+0(FP) | ||
RET |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2014 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
#ifdef GOARCH_arm | ||
#define LR R14 | ||
#endif | ||
|
||
#ifdef GOARCH_amd64 | ||
#define get_tls(r) MOVQ TLS, r | ||
#define g(r) 0(r)(TLS*1) | ||
#endif | ||
|
||
#ifdef GOARCH_amd64p32 | ||
#define get_tls(r) MOVL TLS, r | ||
#define g(r) 0(r)(TLS*1) | ||
#endif | ||
|
||
#ifdef GOARCH_386 | ||
#define get_tls(r) MOVL TLS, r | ||
#define g(r) 0(r)(TLS*1) | ||
#endif |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package runtimex | ||
|
||
import ( | ||
"log" | ||
"unsafe" | ||
|
||
"github.com/modern-go/reflect2" | ||
) | ||
|
||
var ( | ||
// types | ||
gType = reflect2.TypeByName("runtime.g").(reflect2.StructType) | ||
mType = reflect2.TypeByName("runtime.m").(reflect2.StructType) | ||
pType = reflect2.TypeByName("runtime.p").(reflect2.StructType) | ||
// fields | ||
mField = gType.FieldByName("m") | ||
pField = mType.FieldByName("p") | ||
gidField = gType.FieldByName("goid") | ||
gpreemptField = gType.FieldByName("preempt") | ||
midField = mType.FieldByName("id") | ||
mpreemptoffField = mType.FieldByName("preemptoff") | ||
ppreemptField = pType.FieldByName("preempt") | ||
prunqheadField = pType.FieldByName("runqhead") | ||
prunqtailField = pType.FieldByName("runqtail") | ||
//prunqField = pType.FieldByName("runq") | ||
) | ||
|
||
func MPreemptOff() { | ||
g := getg() | ||
m := *((**_m)(unsafe.Pointer(g + mField.Offset()))) | ||
preemptoff := (*string)(unsafe.Pointer((uintptr)(unsafe.Pointer(m)) + mpreemptoffField.Offset())) | ||
if *preemptoff == "" { | ||
*preemptoff = "holding" | ||
} | ||
} | ||
|
||
func MPreemptOn() { | ||
g := getg() | ||
m := *((**_m)(unsafe.Pointer(g + mField.Offset()))) | ||
preemptoff := (*string)(unsafe.Pointer((uintptr)(unsafe.Pointer(m)) + mpreemptoffField.Offset())) | ||
if *preemptoff != "" { | ||
*preemptoff = "" | ||
} | ||
} | ||
|
||
func schedlog() { | ||
g := getg() | ||
m := *((**_m)(unsafe.Pointer(g + mField.Offset()))) | ||
p := *((**_p)(unsafe.Pointer(uintptr(unsafe.Pointer(m)) + pField.Offset()))) | ||
gid := *((*int64)(unsafe.Pointer(g + gidField.Offset()))) | ||
gpreempt := *(*bool)(unsafe.Pointer(g + gpreemptField.Offset())) | ||
mid := *((*int64)(unsafe.Pointer(uintptr(unsafe.Pointer(m)) + midField.Offset()))) | ||
mpreemptoff := *(*string)(unsafe.Pointer((uintptr)(unsafe.Pointer(m)) + mpreemptoffField.Offset())) | ||
pid := p.id | ||
pstatus := p.status | ||
ppreempt := *(*bool)(unsafe.Pointer((uintptr)(unsafe.Pointer(p)) + ppreemptField.Offset())) | ||
prunqhead := *(*uint32)(unsafe.Pointer((uintptr)(unsafe.Pointer(p)) + prunqheadField.Offset())) | ||
prunqtail := *(*uint32)(unsafe.Pointer((uintptr)(unsafe.Pointer(p)) + prunqtailField.Offset())) | ||
//prunq := *(*[256]guintptr)(unsafe.Pointer((uintptr)(unsafe.Pointer(p)) + prunqField.Offset())) | ||
qsize := prunqtail - prunqhead | ||
log.Printf( | ||
"[G] gid=%d,gpreempt=%v | "+ | ||
"[M] mid=%d,mpreemptoff=%s | "+ | ||
"[P] pid=%d,pstatus=%d,ppreempt=%v,qsize=%d", | ||
gid, gpreempt, | ||
mid, mpreemptoff, | ||
pid, pstatus, ppreempt, qsize, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package runtimex | ||
|
||
import ( | ||
"runtime" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
//go:noinline | ||
func testStackFunction(n int) int { | ||
var stack [1024 * 4]int64 | ||
for i := 0; i < len(stack); i++ { | ||
stack[i] = int64(n + i) | ||
} | ||
return int(stack[len(stack)/2]) | ||
} | ||
|
||
func TestCPUBondSingleP(t *testing.T) { | ||
runtime.GOMAXPROCS(4) | ||
const ( | ||
tasks = 8 | ||
interval = 1000000 | ||
round = 1000 | ||
) | ||
begin := time.Now() | ||
var wg sync.WaitGroup | ||
for t := 0; t < tasks; t++ { | ||
wg.Add(1) | ||
go func(n int) { | ||
defer wg.Done() | ||
if n == 0 { | ||
MPreemptOff() | ||
defer MPreemptOn() | ||
} | ||
for i := 0; i < round*interval; i++ { | ||
if i%interval == 0 { | ||
testStackFunction(i) | ||
} | ||
} | ||
}(t) | ||
} | ||
wg.Wait() | ||
cost := time.Now().Sub(begin) | ||
t.Logf("Cost: %vms", cost.Milliseconds()) | ||
} | ||
|
||
func TestIOBondSingleP(t *testing.T) { | ||
runtime.GOMAXPROCS(4) | ||
const ( | ||
tasks = 8 | ||
interval = 1000000 | ||
round = 1000 | ||
) | ||
begin := time.Now() | ||
var wg sync.WaitGroup | ||
for t := 0; t < tasks; t++ { | ||
wg.Add(1) | ||
go func(n int) { | ||
defer wg.Done() | ||
if n == 0 { | ||
MPreemptOff() | ||
defer MPreemptOn() | ||
} | ||
for i := 0; i < round*interval; i++ { | ||
if i%interval == 0 { | ||
testStackFunction(i) | ||
} | ||
if i%interval == interval/2 { | ||
time.Sleep(time.Millisecond) | ||
} | ||
} | ||
}(t) | ||
} | ||
wg.Wait() | ||
cost := time.Now().Sub(begin) | ||
t.Logf("Cost: %vms", cost.Milliseconds()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters