Commit c5fc6e3b authored by 李志信's avatar 李志信

fix: fix merge conflict

parents e4aa6a99 a125b16f
...@@ -2,12 +2,15 @@ module github.com/dubbogo/gost ...@@ -2,12 +2,15 @@ module github.com/dubbogo/gost
require ( require (
github.com/buger/jsonparser v1.0.0 github.com/buger/jsonparser v1.0.0
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/davecgh/go-spew v1.1.1 github.com/davecgh/go-spew v1.1.1
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/k0kubun/pp v3.0.1+incompatible github.com/k0kubun/pp v3.0.1+incompatible
github.com/mattn/go-colorable v0.1.7 // indirect github.com/mattn/go-colorable v0.1.7 // indirect
github.com/mattn/go-isatty v0.0.12 github.com/mattn/go-isatty v0.0.12
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/shirou/gopsutil v3.20.11-0.20201116082039-2fb5da2f2449+incompatible
github.com/stretchr/testify v1.6.1 github.com/stretchr/testify v1.6.1
) )
......
github.com/buger/jsonparser v1.0.0 h1:etJTGF5ESxjI0Ic2UaLQs2LQQpa8G9ykQScukbh4L8A= github.com/buger/jsonparser v1.0.0 h1:etJTGF5ESxjI0Ic2UaLQs2LQQpa8G9ykQScukbh4L8A=
github.com/buger/jsonparser v1.0.0/go.mod h1:tgcrVJ81GPSF0mz+0nu1Xaz0fazGPrmmJfJtxjbHhUQ= github.com/buger/jsonparser v1.0.0/go.mod h1:tgcrVJ81GPSF0mz+0nu1Xaz0fazGPrmmJfJtxjbHhUQ=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM= github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k= github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40= github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40=
...@@ -16,6 +20,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= ...@@ -16,6 +20,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shirou/gopsutil v3.20.11-0.20201116082039-2fb5da2f2449+incompatible h1:Wll9sV8SqrD0cSI17l1L1Q2ZcqhhoDb1CUN+6TarZ3I=
github.com/shirou/gopsutil v3.20.11-0.20201116082039-2fb5da2f2449+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package gxruntime
import (
"io/ioutil"
"os"
"runtime"
"runtime/pprof"
"strconv"
"strings"
"time"
)
import (
"github.com/shirou/gopsutil/process"
)
var (
CurrentPID = os.Getpid()
)
const (
cgroupMemLimitPath = "/sys/fs/cgroup/memory/memory.limit_in_bytes"
)
// GetThreadNum gets current process's thread number
func GetThreadNum() int {
return pprof.Lookup("threadcreate").Count()
}
// GetGoroutineNum gets current process's goroutine number
func GetGoroutineNum() int {
return runtime.NumGoroutine()
}
// GetProcessCPUStat gets current process's cpu stat
func GetProcessCPUStat() (float64, error) {
p, err := process.NewProcess(int32(CurrentPID))
if err != nil {
return 0, err
}
cpuPercent, err := p.Percent(time.Second)
if err != nil {
return 0, err
}
// The default percent is if you use one core, then 100%, two core, 200%
// but it's inconvenient to calculate the proper percent
// here we multiply by core number, so we can set a percent bar more intuitively
cpuPercent = cpuPercent / float64(runtime.GOMAXPROCS(-1))
return cpuPercent, nil
}
// GetProcessMemoryStat gets current process's memory usage percent
func GetProcessMemoryPercent() (float32, error) {
p, err := process.NewProcess(int32(CurrentPID))
if err != nil {
return 0, err
}
memPercent, err := p.MemoryPercent()
if err != nil {
return 0, err
}
return memPercent, nil
}
// GetProcessMemoryStat gets current process's memory usage in Byte
func GetProcessMemoryStat() (uint64, error) {
p, err := process.NewProcess(int32(CurrentPID))
if err != nil {
return 0, err
}
memInfo, err := p.MemoryInfo()
return memInfo.RSS, nil
}
// copied from https://github.com/containerd/cgroups/blob/318312a373405e5e91134d8063d04d59768a1bff/utils.go#L251
func parseUint(s string, base, bitSize int) (uint64, error) {
v, err := strconv.ParseUint(s, base, bitSize)
if err != nil {
intValue, intErr := strconv.ParseInt(s, base, bitSize)
// 1. Handle negative values greater than MinInt64 (and)
// 2. Handle negative values lesser than MinInt64
if intErr == nil && intValue < 0 {
return 0, nil
} else if intErr != nil &&
intErr.(*strconv.NumError).Err == strconv.ErrRange &&
intValue < 0 {
return 0, nil
}
return 0, err
}
return v, nil
}
// copied from https://github.com/containerd/cgroups/blob/318312a373405e5e91134d8063d04d59768a1bff/utils.go#L243
func readUint(path string) (uint64, error) {
v, err := ioutil.ReadFile(path)
if err != nil {
return 0, err
}
return parseUint(strings.TrimSpace(string(v)), 10, 64)
}
func GetCgroupMemoryLimit() (uint64, error) {
return readUint(cgroupMemLimitPath)
}
// GetCgroupProcessMemoryPercent gets current process's memory usage percent in cgroup env
func GetCgroupProcessMemoryPercent() (float64, error) {
p, err := process.NewProcess(int32(os.Getpid()))
if err != nil {
return 0, err
}
mem, err := p.MemoryInfo()
if err != nil {
return 0, err
}
memLimit, err := GetCgroupMemoryLimit()
if err != nil {
return 0, err
}
// mem.RSS / cgroup limit in bytes
memPercent := float64(mem.RSS) * 100 / float64(memLimit)
return memPercent, nil
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package gxruntime
import (
"testing"
"time"
)
func TestProcessSysStat(t *testing.T) {
t.Logf("current prcess thread number %d", GetThreadNum())
go func() {
time.Sleep(10e9)
}()
grNum := GetGoroutineNum()
if grNum < 2 {
t.Errorf("current prcess goroutine number %d", grNum)
}
cpu, err := GetProcessCPUStat()
if err != nil {
t.Errorf("GetProcessCPUStat() = error %+v", err)
}
t.Logf("process cpu stat %v", cpu)
size := 100 * 1024 * 1024
bytes := make([]byte, size)
_ = bytes[:size-1]
memoryStat, err := GetProcessMemoryStat()
if err != nil {
t.Errorf("GetProcessMemoryStat() = error %+v", err)
}
t.Logf("process memory usage stat %v", memoryStat)
//if memoryStat <= uint64(size) {
// t.Errorf("memory usage stat %d < %d", memoryStat, size)
//}
memoryUsage, err := GetProcessMemoryPercent()
if err != nil {
t.Errorf("GetProcessMemoryPercent() = error %+v", err)
}
t.Logf("process memory usage percent %v", memoryUsage)
}
...@@ -226,8 +226,8 @@ func (p *TaskPool) Close() { ...@@ -226,8 +226,8 @@ func (p *TaskPool) Close() {
// Task Pool Simple // Task Pool Simple
///////////////////////////////////////// /////////////////////////////////////////
type taskPoolSimple struct { type taskPoolSimple struct {
work chan task work chan task // task channel
sem chan struct{} sem chan struct{} // gr pool size
wg sync.WaitGroup wg sync.WaitGroup
...@@ -238,7 +238,7 @@ type taskPoolSimple struct { ...@@ -238,7 +238,7 @@ type taskPoolSimple struct {
// NewTaskPoolSimple build a simple task pool // NewTaskPoolSimple build a simple task pool
func NewTaskPoolSimple(size int) GenericTaskPool { func NewTaskPoolSimple(size int) GenericTaskPool {
if size < 1 { if size < 1 {
size = runtime.NumCPU() * 100 size = runtime.GOMAXPROCS(-1) * 100
} }
return &taskPoolSimple{ return &taskPoolSimple{
work: make(chan task), work: make(chan task),
...@@ -274,15 +274,19 @@ func (p *taskPoolSimple) AddTaskAlways(t task) { ...@@ -274,15 +274,19 @@ func (p *taskPoolSimple) AddTaskAlways(t task) {
select { select {
case p.work <- t: case p.work <- t:
// exec @t in gr pool
return return
default: default:
} }
select { select {
case p.work <- t: case p.work <- t:
// exec @t in gr pool
case p.sem <- struct{}{}: case p.sem <- struct{}{}:
// add a gr to the gr pool
p.wg.Add(1) p.wg.Add(1)
go p.worker(t) go p.worker(t)
default: default:
// gen a gr temporarily
goSafely(t) goSafely(t)
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment