Skip to content

Commit

Permalink
pid: refactor and support arm64
Browse files Browse the repository at this point in the history
Signed-off-by: Cholerae Hu <[email protected]>
  • Loading branch information
choleraehyq committed May 8, 2020
1 parent ac9ff60 commit c7f10e1
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 37 deletions.
22 changes: 1 addition & 21 deletions pid_go1.5_amd64.go → pid_go1.5.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,15 @@
// permissions and limitations under the License. See the AUTHORS file
// for names of contributors.

// +build amd64 amd64p32
// +build amd64 amd64p32 arm64
// +build gc,go1.5

package goid

import (
"unsafe"
)

var _ = unsafe.Sizeof(0)

//go:nosplit
func getPid() uintptr

//go:nosplit
func GetPid() int {
return int(getPid())
}

//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
}
16 changes: 0 additions & 16 deletions pid_go1.5_amd64_test.go

This file was deleted.

31 changes: 31 additions & 0 deletions pid_go1.5_arm64.s
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
70 changes: 70 additions & 0 deletions pid_go1.5_test.go
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()
}
})
}

0 comments on commit c7f10e1

Please sign in to comment.