Commit fce9b2f2 authored by AlexStocks's avatar AlexStocks

add cpu/memory

parent fe7d5168
......@@ -8,7 +8,6 @@ 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
......
......@@ -18,7 +18,6 @@
package gxruntime
import (
"github.com/pbnjay/memory"
"io/ioutil"
"os"
"runtime"
......@@ -29,6 +28,7 @@ import (
)
import (
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/process"
)
......@@ -46,8 +46,34 @@ func GetCPUNum() int {
}
// GetMemoryLimit gets current os's memory size in bytes
func GetMemoryLimit() int {
return int(memory.TotalMemory())
func GetMemoryStat() (total, used, free uint64, usedPercent float64) {
stat, err := mem.VirtualMemory()
if err != nil {
return 0, 0, 0, 0
}
return stat.Total, stat.Used, stat.Free, stat.UsedPercent
}
// 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 IsCgroup() bool {
ok, _ := exists(cgroupMemLimitPath)
if ok {
return true
}
return false
}
func GetCgroupMemoryLimit() (uint64, error) {
return readUint(cgroupMemLimitPath)
}
// GetThreadNum gets current process's thread number
......@@ -135,10 +161,6 @@ func readUint(path string) (uint64, error) {
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()))
......
/*
* 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"
)
// 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())
func TestSysStat(t *testing.T) {
t.Logf("current os cpu number %d", GetCPUNum())
total, used, free, usedPercent := GetMemoryStat()
t.Logf("memory: limit %d bytes, used %d bytes, free %d bytes, usedPercent %f", total, used, free, usedPercent)
t.Logf("current prcess thread number %d", GetThreadNum())
go func() {
time.Sleep(10e9)
......@@ -53,7 +63,7 @@ func t1(t *testing.T) {
}
t.Logf("process memory usage percent %v", memoryUsage)
if ok, _ := exists(cgroupMemLimitPath); ok {
if IsCgroup() {
memoryLimit, err := GetCgroupMemoryLimit()
if err != nil {
t.Errorf("GetCgroupMemoryLimit() = error %+v", err)
......@@ -65,7 +75,6 @@ func t1(t *testing.T) {
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