Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
gostnops
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
gostnops
Commits
fce9b2f2
Commit
fce9b2f2
authored
Dec 09, 2020
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add cpu/memory
parent
fe7d5168
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
20 deletions
+50
-20
go.mod
go.mod
+0
-1
sys.go
runtime/sys.go
+29
-7
sys_test.go
runtime/sys_test.go
+21
-12
No files found.
go.mod
View file @
fce9b2f2
...
...
@@ -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
...
...
runtime/sys.go
View file @
fce9b2f2
...
...
@@ -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
()))
...
...
runtime/sys_test.go
View file @
fce9b2f2
/*
* 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
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment