Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
gpuhw
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
Odysseus
gpuhw
Commits
68789801
Commit
68789801
authored
Mar 20, 2024
by
Ubuntu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add usage
parent
c995df4a
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
160 additions
and
2 deletions
+160
-2
block.go
block.go
+36
-0
cpu.go
cpu.go
+60
-0
go.mod
go.mod
+4
-1
go.sum
go.sum
+2
-0
main.go
main.go
+26
-1
mem.go
mem.go
+32
-0
No files found.
block.go
View file @
68789801
...
...
@@ -6,11 +6,15 @@
package
main
//https://github.com/ricochet2200/go-disk-usage.git
import
(
"fmt"
"github.com/jaypipes/ghw"
"github.com/pkg/errors"
"github.com/ricochet2200/go-disk-usage/du"
)
// showBlock show block storage information for the host system.
...
...
@@ -41,3 +45,35 @@ func getBlock() ([]DeviceInfo, error) {
return
res
,
nil
}
func
getBlockUsage
()
([]
DeviceInfo
,
error
)
{
//block := ghw.Block()
block
,
err
:=
ghw
.
Block
()
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"error getting block device info"
)
}
res
:=
make
([]
DeviceInfo
,
0
,
len
(
block
.
Disks
))
usage
:=
du
.
NewDiskUsage
(
"/"
)
fmt
.
Println
(
"Usage:"
,
usage
.
Usage
()
*
100
,
"%"
)
// for i, disk := range block.Disks {
// if disk.Model == "unknown" {
// continue
// }
e
:=
DeviceInfo
{
Type
:
"volumePath /"
,
//Model: ,
Param
:
"disk usage"
,
Power
:
uint64
(
usage
.
Usage
()
*
100
),
}
res
=
append
(
res
,
e
)
// }
return
res
,
nil
}
cpu.go
View file @
68789801
...
...
@@ -2,6 +2,8 @@ package main
import
(
"fmt"
"syscall"
"time"
"github.com/jaypipes/ghw"
"github.com/pkg/errors"
...
...
@@ -39,3 +41,61 @@ func getCPU() ([]DeviceInfo, error) {
return
res
,
nil
}
func
getCPUUsage
()
([]
DeviceInfo
,
error
)
{
cpu
,
err
:=
ghw
.
CPU
()
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"error getting CPU info"
)
}
res
:=
make
([]
DeviceInfo
,
0
,
len
(
cpu
.
Processors
))
// for i, proc := range cpu.Processors {
// fmt.Printf(" %v\n", proc)
// e := DeviceInfo{
// Type: fmt.Sprintf("proc-%d", i),
// Model: proc.Model,
// Param: "proc.NumThreads",
// Power: uint64(proc.NumThreads),
// }
// res = append(res, e)
// }
var
rusage
syscall
.
Rusage
pid
:=
syscall
.
Getpid
()
if
err
:=
syscall
.
Getrusage
(
pid
,
&
rusage
);
err
!=
nil
{
return
nil
,
err
}
utime
:=
rusage
.
Utime
.
Nano
()
stime
:=
rusage
.
Stime
.
Nano
()
total
:=
utime
+
stime
fmt
.
Printf
(
"CPU usage: %.2f%%
\n
"
,
float64
(
total
)
/
float64
(
time
.
Second
)
*
100
)
e
:=
DeviceInfo
{
Type
:
"cpu"
,
//Model: proc.Model,
Param
:
"proc.NumThreads"
,
Power
:
uint64
(
float64
(
total
)
/
float64
(
time
.
Second
)
*
100
),
}
res
=
append
(
res
,
e
)
return
res
,
nil
}
// var rusage syscall.Rusage
// pid := syscall.Getpid()
// for {
// err := syscall.Getrusage(pid, &rusage)
// if err != nil {
// panic(err)
// }
// utime := rusage.Utime.Nano()
// stime := rusage.Stime.Nano()
// total := utime + stime
// fmt.Printf("CPU usage: %.2f%%\n", float64(total)/float64(time.Second)*100)
// time.Sleep(time.Second)
// }
go.mod
View file @
68789801
module gpuhw
go 1.20
go 1.21
toolchain go1.22.1
require github.com/jaypipes/ghw v0.12.0
...
...
@@ -20,6 +22,7 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/ricochet2200/go-disk-usage/du v0.0.0-20210707232629-ac9918953285 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.52.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
...
...
go.sum
View file @
68789801
...
...
@@ -36,6 +36,8 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/ricochet2200/go-disk-usage/du v0.0.0-20210707232629-ac9918953285 h1:d54EL9l+XteliUfUCGsEwwuk65dmmxX85VXF+9T6+50=
github.com/ricochet2200/go-disk-usage/du v0.0.0-20210707232629-ac9918953285/go.mod h1:fxIDly1xtudczrZeOOlfaUvd2OPb2qZAPuWdU2BsBTk=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.52.0 h1:wqBQpxH71XW0e2g+Og4dzQM8pk34aFYlA1Ga8db7gU0=
...
...
main.go
View file @
68789801
...
...
@@ -50,7 +50,7 @@ func main() {
//return c.SendString("Hello, World 👋!")
})
app
.
Get
(
"/hw/us
ed
"
,
func
(
c
fiber
.
Ctx
)
error
{
app
.
Get
(
"/hw/us
age
"
,
func
(
c
fiber
.
Ctx
)
error
{
res
:=
make
([]
DeviceInfo
,
0
)
...
...
@@ -62,6 +62,31 @@ func main() {
res
=
append
(
res
,
gpu
...
)
cpu
,
err
:=
getCPUUsage
()
if
err
!=
nil
{
c
.
SendString
(
"getCPUUsage err: "
+
err
.
Error
())
}
res
=
append
(
res
,
cpu
...
)
mem
,
err
:=
getMemoryUsage
()
if
err
!=
nil
{
c
.
SendString
(
"getMemoryUsage err: "
+
err
.
Error
())
}
res
=
append
(
res
,
mem
...
)
block
,
err
:=
getBlockUsage
()
if
err
!=
nil
{
c
.
SendString
(
"getBlockUsage err: "
+
err
.
Error
())
}
res
=
append
(
res
,
block
...
)
return
c
.
JSON
(
res
)
//res := make([]DeviceInfo, 0)
...
...
mem.go
View file @
68789801
...
...
@@ -5,6 +5,8 @@ import (
"github.com/jaypipes/ghw"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
// showMemory show memory information for the host system.
...
...
@@ -30,3 +32,33 @@ func getMemory() ([]DeviceInfo, error) {
return
res
,
nil
}
func
getMemoryUsage
()
([]
DeviceInfo
,
error
)
{
mem
,
err
:=
ghw
.
Memory
()
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"error getting memory info"
)
}
fmt
.
Println
(
mem
.
Area
.
TotalPhysicalBytes
,
mem
.
Area
.
TotalUsableBytes
)
usable
:=
decimal
.
NewFromInt
(
mem
.
Area
.
TotalUsableBytes
)
total
:=
decimal
.
NewFromInt
(
mem
.
Area
.
TotalPhysicalBytes
)
mem
.
TotalUsableBytes
e
:=
DeviceInfo
{
Type
:
fmt
.
Sprintf
(
"mem"
),
//Model: disk.Model,
Param
:
"mem usage"
,
Power
:
uint64
((
1
-
usable
.
Div
(
total
)
.
int64
())
*
100
),
}
res
:=
make
([]
DeviceInfo
,
0
,
1
)
res
=
append
(
res
,
e
)
return
res
,
nil
}
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