Commit 41574d3a authored by Your Name's avatar Your Name

fix cpu cores

parent 525e27ce
...@@ -76,7 +76,7 @@ func (c *ProApi) Cpus() (Cpu, error) { ...@@ -76,7 +76,7 @@ func (c *ProApi) Cpus() (Cpu, error) {
Usage: totalUtil, Usage: totalUtil,
Model: modelname, Model: modelname,
Threads: int64(thread) + 1, Threads: int64(thread) + 1,
Cores: int64(core) + 1, Cores: int64(core),
Frequency: strings.TrimSpace(freq), Frequency: strings.TrimSpace(freq),
} }
...@@ -107,9 +107,10 @@ func (c ProApi) CpuItems() (string, int, int, error) { ...@@ -107,9 +107,10 @@ func (c ProApi) CpuItems() (string, int, int, error) {
//res := make([]Cpu, 0, len(vectorVal)) //res := make([]Cpu, 0, len(vectorVal))
model_name := "" model_name := ""
core := 0
thread := 0 thread := 0
countCore := make(map[string]bool)
for k, elem := range vectorVal { for k, elem := range vectorVal {
// int64(elem.Value), nil // int64(elem.Value), nil
...@@ -126,14 +127,16 @@ func (c ProApi) CpuItems() (string, int, int, error) { ...@@ -126,14 +127,16 @@ func (c ProApi) CpuItems() (string, int, int, error) {
if coreStr, ok := elem.Metric["core"]; ok { if coreStr, ok := elem.Metric["core"]; ok {
newCore, err := strconv.Atoi(string(coreStr)) countCore[string(coreStr)] = true
if err != nil {
return "", 0, 0, err // newCore, err := strconv.Atoi(string(coreStr))
} // if err != nil {
// return "", 0, 0, err
// }
// if newCore > core {
// core = newCore
// }
if newCore > core {
core = newCore
}
} else { } else {
continue continue
} }
...@@ -157,7 +160,14 @@ func (c ProApi) CpuItems() (string, int, int, error) { ...@@ -157,7 +160,14 @@ func (c ProApi) CpuItems() (string, int, int, error) {
// Model: model_name, // Model: model_name,
// }) // })
} }
return model_name, core, thread, nil
// c := 0
// for k, _ := range countCore {
// fmt.Println(c, k)
// c = c + 1
// }
return model_name, len(countCore), thread, nil
} }
return "", 0, 0, errors.Errorf("cpu can not find the query %s value", queryParam) return "", 0, 0, errors.Errorf("cpu can not find the query %s value", queryParam)
......
package main package main
import "testing" import (
"encoding/json"
"testing"
)
func TestCpuUtil(t *testing.T) { func TestCpu(t *testing.T) {
cli, err := NewProCli("http://192.168.1.21:9090") // cli, err := NewProCli("http://192.168.1.21:9090")
cli, err := NewProCli("http://47.94.59.74:9000/")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
gpus, err := cli.CpuUtil() gpus, err := cli.Cpus()
if err != nil {
t.Fatal(err)
}
for k, v := range gpus { gpusAsJson, err := json.Marshal(gpus)
t.Log("k", k, "v", v) if err != nil {
t.Fatal(err)
} }
t.Log(string(gpusAsJson))
// for k, v := range gpus {
// t.Log("k", k, "v", v)
// }
} }
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