Commit fe7d5168 authored by AlexStocks's avatar AlexStocks

Add: ps resource

parent a125b16f
......@@ -8,6 +8,7 @@ require (
github.com/k0kubun/pp v3.0.1+incompatible
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/mattn/go-isatty v0.0.12
github.com/pbnjay/memory v0.0.0-20201129165224-b12e5d931931
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
......
......@@ -14,6 +14,8 @@ github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3
github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pbnjay/memory v0.0.0-20201129165224-b12e5d931931 h1:EeWknjeRU+R3O4ghG7XZCpgSfJNStZyEP8aWyQwJM8s=
github.com/pbnjay/memory v0.0.0-20201129165224-b12e5d931931/go.mod h1:RMU2gJXhratVxBDTFeOdNhd540tG57lt9FIUV0YLvIQ=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
......
......@@ -18,6 +18,7 @@
package gxruntime
import (
"github.com/pbnjay/memory"
"io/ioutil"
"os"
"runtime"
......@@ -39,6 +40,16 @@ const (
cgroupMemLimitPath = "/sys/fs/cgroup/memory/memory.limit_in_bytes"
)
// GetCPUNum gets current os's cpu number
func GetCPUNum() int {
return runtime.NumCPU()
}
// GetMemoryLimit gets current os's memory size in bytes
func GetMemoryLimit() int {
return int(memory.TotalMemory())
}
// GetThreadNum gets current process's thread number
func GetThreadNum() int {
return pprof.Lookup("threadcreate").Count()
......
/*
* 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 (
"os"
"testing"
"time"
)
func TestProcessSysStat(t *testing.T) {
// exists returns whether the given file or directory exists
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil { return true, nil }
if os.IsNotExist(err) { return false, nil }
return false, err
}
func t1(t *testing.T) {
t.Logf("current os cpu number %d, memory limit %d bytes", GetCPUNum(), GetMemoryLimit())
t.Logf("current prcess thread number %d", GetThreadNum())
go func() {
time.Sleep(10e9)
......@@ -40,16 +34,18 @@ func TestProcessSysStat(t *testing.T) {
t.Logf("process cpu stat %v", cpu)
size := 100 * 1024 * 1024
bytes := make([]byte, size)
_ = bytes[:size-1]
arr := make([]byte, size)
for idx, _ := range arr {
arr[idx] = byte(idx / 255)
}
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)
//}
//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 {
......@@ -57,4 +53,19 @@ func TestProcessSysStat(t *testing.T) {
}
t.Logf("process memory usage percent %v", memoryUsage)
if ok, _ := exists(cgroupMemLimitPath); ok {
memoryLimit, err := GetCgroupMemoryLimit()
if err != nil {
t.Errorf("GetCgroupMemoryLimit() = error %+v", err)
}
t.Logf("CGroupMemoryLimit() = %d", memoryLimit)
memoryPercent, err := GetCgroupProcessMemoryPercent()
if err != nil {
t.Errorf("GetCgroupProcessMemoryPercent(ps:%d) = error %+v", CurrentPID, err)
}
t.Logf("GetCgroupProcessMemoryPercent(ps:%d) = %+v", CurrentPID, memoryPercent)
}
}
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