自动更新管控端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

62 lines
1.3 KiB

package core
import (
"encoding/json"
"fmt"
"net/http"
"net/rpc/jsonrpc"
"runtime"
)
type Dtm struct {
TotalAlloc string `json:"total_alloc"`
Alloc string `json:"alloc"`
}
// 获取内存信息
func Dtmem(w http.ResponseWriter, r *http.Request) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// 测试JSONRPC
service := "192.168.66.16:9098"
client, err := jsonrpc.Dial("tcp", service)
if err != nil {
fmt.Fprintf(w, "jsonrpc dial faild %v", err)
return
}
// 调用远程方法
var reply int
err = client.Call("Service.GetFilePath", "hello", &reply)
if err != nil {
fmt.Fprintf(w, "jsonrpc call faild %v", err)
return
}
// 打印返回值
fmt.Fprintf(w, "jsonrpc call success, reply: %d", reply)
dtm := Dtm{
TotalAlloc: fmt.Sprintf("%d", m.TotalAlloc),
Alloc: fmt.Sprintf("%v MB", m.Alloc/1024),
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(dtm)
//
// jsonobj, err := json.Marshal(m)
// if err != nil {
// fmt.Fprintf(w, "json marshal faild %v", err)
// return
// }
// fmt.Fprintf(w, "%s", jsonobj)
// usage:=m.TotalAlloc/1024/1024
// free:=m.Free/1024/1024
// //
// fmt.Fprintf("总分配内存: %v MB\n", m.TotalAlloc/1024/1024)
// fmt.Fprintf("已分配内存: %v MB\n", m.Alloc/1024/1024)
}