forked from petermattis/goid
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Cholerae Hu <[email protected]>
- Loading branch information
1 parent
ac9ff60
commit c7f10e1
Showing
4 changed files
with
102 additions
and
37 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 was deleted.
Oops, something went wrong.
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,31 @@ | ||
// Copyright 2016 Peter Mattis. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. See the AUTHORS file | ||
// for names of contributors. | ||
|
||
// Assembly to mimic runtime.getg. | ||
|
||
// +build arm64 | ||
// +build gc,go1.5 | ||
|
||
#include "go_asm.h" | ||
#include "textflag.h" | ||
|
||
// func getPid() int64 | ||
TEXT ·getPid(SB),NOSPLIT,$0-8 | ||
MOVD g, R14 | ||
MOVD g_m(R14), R13 | ||
MOVD m_p(R13), R14 | ||
MOVW p_id(R14), R13 | ||
MOVD R13, 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,70 @@ | ||
// +build gc,go1.5 | ||
|
||
package goid | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"unsafe" | ||
) | ||
|
||
var _ = unsafe.Sizeof(0) | ||
|
||
//go:linkname procPin runtime.procPin | ||
//go:nosplit | ||
func procPin() int | ||
|
||
//go:linkname procUnpin runtime.procUnpin | ||
//go:nosplit | ||
func procUnpin() | ||
|
||
func getTID() int { | ||
tid := procPin() | ||
procUnpin() | ||
return tid | ||
} | ||
|
||
func TestParallelGetPid(t *testing.T) { | ||
ch := make(chan *string, 100) | ||
for i := 0; i < cap(ch); i++ { | ||
go func(i int) { | ||
id := GetPid() | ||
expected := getTID() | ||
if id == expected { | ||
ch <- nil | ||
return | ||
} | ||
s := fmt.Sprintf("Expected %d, but got %d", expected, id) | ||
ch <- &s | ||
}(i) | ||
} | ||
|
||
for i := 0; i < cap(ch); i++ { | ||
val := <-ch | ||
if val != nil { | ||
t.Fatal(*val) | ||
} | ||
} | ||
} | ||
|
||
func TestGetPid(t *testing.T) { | ||
p1 := GetPid() | ||
p2 := getTID() | ||
if p1 != p2 { | ||
t.Fatalf("The result of GetPid %d procPin %d are not equal!", p1, p2) | ||
} | ||
} | ||
|
||
func BenchmarkGetPid(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
GetPid() | ||
} | ||
} | ||
|
||
func BenchmarkParallelGetPid(b *testing.B) { | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
GetPid() | ||
} | ||
}) | ||
} |