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.
74 lines
1.4 KiB
74 lines
1.4 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"`
|
|
}
|
|
|
|
// for rpc
|
|
type Args struct {
|
|
FilePath string
|
|
}
|
|
|
|
// 返回的参数
|
|
type Reply struct {
|
|
FilePath string `json:"file_path"`
|
|
}
|
|
|
|
// 获取内存信息
|
|
func Dtmem(w http.ResponseWriter, r *http.Request) {
|
|
var m runtime.MemStats
|
|
runtime.ReadMemStats(&m)
|
|
|
|
// 测试JSONRPC
|
|
service := "192.168.66.212:9098"
|
|
client, err := jsonrpc.Dial("tcp", service)
|
|
if err != nil {
|
|
fmt.Fprintf(w, "jsonrpc dial faild %v", err)
|
|
return
|
|
}
|
|
// 调用远程方法
|
|
var reply string
|
|
var args = Args{"."}
|
|
err = client.Call("FileRpcService.GetFilePath", args, &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)
|
|
}
|
|
|