diff --git a/app/stats/command/command.go b/app/stats/command/command.go index db375a8fd..68bc8e462 100644 --- a/app/stats/command/command.go +++ b/app/stats/command/command.go @@ -6,6 +6,8 @@ package command import ( "context" + "runtime" + "time" grpc "google.golang.org/grpc" @@ -18,11 +20,15 @@ import ( // statsServer is an implementation of StatsService. type statsServer struct { - stats feature_stats.Manager + stats feature_stats.Manager + startTime time.Time } func NewStatsServer(manager feature_stats.Manager) StatsServiceServer { - return &statsServer{stats: manager} + return &statsServer{ + stats: manager, + startTime: time.Now(), + } } func (s *statsServer) GetStats(ctx context.Context, request *GetStatsRequest) (*GetStatsResponse, error) { @@ -76,6 +82,28 @@ func (s *statsServer) QueryStats(ctx context.Context, request *QueryStatsRequest return response, nil } +func (s *statsServer) GetSysStats(ctx context.Context, request *SysStatsRequest) (*SysStatsResponse, error) { + var rtm runtime.MemStats + runtime.ReadMemStats(&rtm) + + uptime := time.Since(s.startTime) + + response := &SysStatsResponse{ + Uptime: uint32(uptime.Seconds()), + NumGoroutine: uint32(runtime.NumGoroutine()), + Alloc: rtm.Alloc, + TotalAlloc: rtm.TotalAlloc, + Sys: rtm.Sys, + Mallocs: rtm.Mallocs, + Frees: rtm.Frees, + LiveObjects: rtm.Mallocs - rtm.Frees, + NumGC: rtm.NumGC, + PauseTotalNs: rtm.PauseTotalNs, + } + + return response, nil +} + type service struct { statsManager feature_stats.Manager } diff --git a/app/stats/command/command.pb.go b/app/stats/command/command.pb.go index b563adcb7..4382a2745 100644 --- a/app/stats/command/command.pb.go +++ b/app/stats/command/command.pb.go @@ -1,3 +1,6 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// source: v2ray.com/core/app/stats/command/command.proto + package command import ( @@ -5,6 +8,8 @@ import ( fmt "fmt" proto "github.com/golang/protobuf/proto" grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" math "math" ) @@ -240,6 +245,148 @@ func (m *QueryStatsResponse) GetStat() []*Stat { return nil } +type SysStatsRequest struct { + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *SysStatsRequest) Reset() { *m = SysStatsRequest{} } +func (m *SysStatsRequest) String() string { return proto.CompactTextString(m) } +func (*SysStatsRequest) ProtoMessage() {} +func (*SysStatsRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_c902411c4948f26b, []int{5} +} + +func (m *SysStatsRequest) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_SysStatsRequest.Unmarshal(m, b) +} +func (m *SysStatsRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_SysStatsRequest.Marshal(b, m, deterministic) +} +func (m *SysStatsRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_SysStatsRequest.Merge(m, src) +} +func (m *SysStatsRequest) XXX_Size() int { + return xxx_messageInfo_SysStatsRequest.Size(m) +} +func (m *SysStatsRequest) XXX_DiscardUnknown() { + xxx_messageInfo_SysStatsRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_SysStatsRequest proto.InternalMessageInfo + +type SysStatsResponse struct { + NumGoroutine uint32 `protobuf:"varint,1,opt,name=NumGoroutine,proto3" json:"NumGoroutine,omitempty"` + NumGC uint32 `protobuf:"varint,2,opt,name=NumGC,proto3" json:"NumGC,omitempty"` + Alloc uint64 `protobuf:"varint,3,opt,name=Alloc,proto3" json:"Alloc,omitempty"` + TotalAlloc uint64 `protobuf:"varint,4,opt,name=TotalAlloc,proto3" json:"TotalAlloc,omitempty"` + Sys uint64 `protobuf:"varint,5,opt,name=Sys,proto3" json:"Sys,omitempty"` + Mallocs uint64 `protobuf:"varint,6,opt,name=Mallocs,proto3" json:"Mallocs,omitempty"` + Frees uint64 `protobuf:"varint,7,opt,name=Frees,proto3" json:"Frees,omitempty"` + LiveObjects uint64 `protobuf:"varint,8,opt,name=LiveObjects,proto3" json:"LiveObjects,omitempty"` + PauseTotalNs uint64 `protobuf:"varint,9,opt,name=PauseTotalNs,proto3" json:"PauseTotalNs,omitempty"` + Uptime uint32 `protobuf:"varint,10,opt,name=Uptime,proto3" json:"Uptime,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *SysStatsResponse) Reset() { *m = SysStatsResponse{} } +func (m *SysStatsResponse) String() string { return proto.CompactTextString(m) } +func (*SysStatsResponse) ProtoMessage() {} +func (*SysStatsResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_c902411c4948f26b, []int{6} +} + +func (m *SysStatsResponse) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_SysStatsResponse.Unmarshal(m, b) +} +func (m *SysStatsResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_SysStatsResponse.Marshal(b, m, deterministic) +} +func (m *SysStatsResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_SysStatsResponse.Merge(m, src) +} +func (m *SysStatsResponse) XXX_Size() int { + return xxx_messageInfo_SysStatsResponse.Size(m) +} +func (m *SysStatsResponse) XXX_DiscardUnknown() { + xxx_messageInfo_SysStatsResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_SysStatsResponse proto.InternalMessageInfo + +func (m *SysStatsResponse) GetNumGoroutine() uint32 { + if m != nil { + return m.NumGoroutine + } + return 0 +} + +func (m *SysStatsResponse) GetNumGC() uint32 { + if m != nil { + return m.NumGC + } + return 0 +} + +func (m *SysStatsResponse) GetAlloc() uint64 { + if m != nil { + return m.Alloc + } + return 0 +} + +func (m *SysStatsResponse) GetTotalAlloc() uint64 { + if m != nil { + return m.TotalAlloc + } + return 0 +} + +func (m *SysStatsResponse) GetSys() uint64 { + if m != nil { + return m.Sys + } + return 0 +} + +func (m *SysStatsResponse) GetMallocs() uint64 { + if m != nil { + return m.Mallocs + } + return 0 +} + +func (m *SysStatsResponse) GetFrees() uint64 { + if m != nil { + return m.Frees + } + return 0 +} + +func (m *SysStatsResponse) GetLiveObjects() uint64 { + if m != nil { + return m.LiveObjects + } + return 0 +} + +func (m *SysStatsResponse) GetPauseTotalNs() uint64 { + if m != nil { + return m.PauseTotalNs + } + return 0 +} + +func (m *SysStatsResponse) GetUptime() uint32 { + if m != nil { + return m.Uptime + } + return 0 +} + type Config struct { XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` @@ -250,7 +397,7 @@ func (m *Config) Reset() { *m = Config{} } func (m *Config) String() string { return proto.CompactTextString(m) } func (*Config) ProtoMessage() {} func (*Config) Descriptor() ([]byte, []int) { - return fileDescriptor_c902411c4948f26b, []int{5} + return fileDescriptor_c902411c4948f26b, []int{7} } func (m *Config) XXX_Unmarshal(b []byte) error { @@ -277,6 +424,8 @@ func init() { proto.RegisterType((*GetStatsResponse)(nil), "v2ray.core.app.stats.command.GetStatsResponse") proto.RegisterType((*QueryStatsRequest)(nil), "v2ray.core.app.stats.command.QueryStatsRequest") proto.RegisterType((*QueryStatsResponse)(nil), "v2ray.core.app.stats.command.QueryStatsResponse") + proto.RegisterType((*SysStatsRequest)(nil), "v2ray.core.app.stats.command.SysStatsRequest") + proto.RegisterType((*SysStatsResponse)(nil), "v2ray.core.app.stats.command.SysStatsResponse") proto.RegisterType((*Config)(nil), "v2ray.core.app.stats.command.Config") } @@ -285,28 +434,38 @@ func init() { } var fileDescriptor_c902411c4948f26b = []byte{ - // 321 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x92, 0xb1, 0x4e, 0xc3, 0x30, - 0x10, 0x86, 0x49, 0x5b, 0xda, 0x72, 0x20, 0x01, 0x16, 0x43, 0x55, 0x75, 0x88, 0x3c, 0x75, 0xc1, - 0xa9, 0x82, 0xc4, 0xc2, 0x04, 0x19, 0x90, 0x50, 0x07, 0x70, 0x25, 0x06, 0x36, 0x13, 0x0e, 0x54, - 0x41, 0x62, 0xd7, 0x76,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 0xf3, 0x65, 0x8c, 0xe4, 0x1d, - 0x86, 0xcd, 0xe4, 0xe4, 0xbc, 0xbd, 0xe0, 0x9f, 0x0b, 0x19, 0xb3, 0x5d, 0xe5, 0x95, 0x7b, 0xba, - 0x47, 0x56, 0x00, 0xeb, 0xa9, 0x48, 0xd0, 0xce, 0x6f, 0x2c, 0x71, 0x3c, 0xdb, 0x1d, 0x68, 0x5a, - 0xde, 0xcc, 0xc1, 0x8f, 0x65, 0xd2, 0x0a, 0xde, 0x7b, 0x4f, 0x83, 0x3a, 0xfc, 0xec, 0x4c, 0x1e, - 0x43, 0x2e, 0x0a, 0x16, 0x95, 0xca, 0x6b, 0xa5, 0xdc, 0x16, 0x0d, 0x8b, 0xaa, 0xe3, 0xe7, 0xbe, - 0x7b, 0xbb, 0x17, 0xdf, 0x01, 0x00, 0x00, 0xff, 0xff, 0xcc, 0x9e, 0xb8, 0xeb, 0xed, 0x02, 0x00, - 0x00, + // 488 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x54, 0x5d, 0x6f, 0xd3, 0x30, + 0x14, 0x5d, 0xdb, 0xac, 0xed, 0x6e, 0x37, 0xad, 0xb3, 0x10, 0xb2, 0xa6, 0x09, 0x45, 0x7e, 0xea, + 0x0b, 0xce, 0x54, 0x24, 0x5e, 0x78, 0x1a, 0x91, 0x98, 0x84, 0xca, 0x18, 0x29, 0xf0, 0xc0, 0x9b, + 0x17, 0x2e, 0x28, 0xd0, 0xc4, 0x9e, 0xed, 0x54, 0xca, 0xdf, 0xe1, 0x91, 0x1f, 0xc5, 0x6f, 0x41, + 0x76, 0x12, 0xfa, 0x31, 0xad, 0x1b, 0x4f, 0xf5, 0x39, 0xf7, 0x9e, 0xdc, 0x73, 0xed, 0xa3, 0x02, + 0x5f, 0x4e, 0xb5, 0xa8, 0x78, 0x2a, 0xf3, 0x28, 0x95, 0x1a, 0x23, 0xa1, 0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3, 0x34, 0x11, 0x15, 0x8f, 0x5d, 0xe7, 0x85, 0x52, 0x3e, + 0xed, 0x86, 0xc7, 0x75, 0xf9, 0xa6, 0xef, 0xff, 0x63, 0x5e, 0xfc, 0x0d, 0x00, 0x00, 0xff, 0xff, + 0x25, 0xa8, 0x5c, 0x1b, 0x95, 0x04, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -323,6 +482,7 @@ const _ = grpc.SupportPackageIsVersion4 type StatsServiceClient interface { GetStats(ctx context.Context, in *GetStatsRequest, opts ...grpc.CallOption) (*GetStatsResponse, error) QueryStats(ctx context.Context, in *QueryStatsRequest, opts ...grpc.CallOption) (*QueryStatsResponse, error) + GetSysStats(ctx context.Context, in *SysStatsRequest, opts ...grpc.CallOption) (*SysStatsResponse, error) } type statsServiceClient struct { @@ -351,10 +511,34 @@ func (c *statsServiceClient) QueryStats(ctx context.Context, in *QueryStatsReque return out, nil } +func (c *statsServiceClient) GetSysStats(ctx context.Context, in *SysStatsRequest, opts ...grpc.CallOption) (*SysStatsResponse, error) { + out := new(SysStatsResponse) + err := c.cc.Invoke(ctx, "/v2ray.core.app.stats.command.StatsService/GetSysStats", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + // StatsServiceServer is the server API for StatsService service. type StatsServiceServer interface { GetStats(context.Context, *GetStatsRequest) (*GetStatsResponse, error) QueryStats(context.Context, *QueryStatsRequest) (*QueryStatsResponse, error) + GetSysStats(context.Context, *SysStatsRequest) (*SysStatsResponse, error) +} + +// UnimplementedStatsServiceServer can be embedded to have forward compatible implementations. +type UnimplementedStatsServiceServer struct { +} + +func (*UnimplementedStatsServiceServer) GetStats(ctx context.Context, req *GetStatsRequest) (*GetStatsResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetStats not implemented") +} +func (*UnimplementedStatsServiceServer) QueryStats(ctx context.Context, req *QueryStatsRequest) (*QueryStatsResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method QueryStats not implemented") +} +func (*UnimplementedStatsServiceServer) GetSysStats(ctx context.Context, req *SysStatsRequest) (*SysStatsResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetSysStats not implemented") } func RegisterStatsServiceServer(s *grpc.Server, srv StatsServiceServer) { @@ -397,6 +581,24 @@ func _StatsService_QueryStats_Handler(srv interface{}, ctx context.Context, dec return interceptor(ctx, in, info, handler) } +func _StatsService_GetSysStats_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(SysStatsRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(StatsServiceServer).GetSysStats(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/v2ray.core.app.stats.command.StatsService/GetSysStats", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(StatsServiceServer).GetSysStats(ctx, req.(*SysStatsRequest)) + } + return interceptor(ctx, in, info, handler) +} + var _StatsService_serviceDesc = grpc.ServiceDesc{ ServiceName: "v2ray.core.app.stats.command.StatsService", HandlerType: (*StatsServiceServer)(nil), @@ -409,6 +611,10 @@ var _StatsService_serviceDesc = grpc.ServiceDesc{ MethodName: "QueryStats", Handler: _StatsService_QueryStats_Handler, }, + { + MethodName: "GetSysStats", + Handler: _StatsService_GetSysStats_Handler, + }, }, Streams: []grpc.StreamDesc{}, Metadata: "v2ray.com/core/app/stats/command/command.proto", diff --git a/app/stats/command/command.proto b/app/stats/command/command.proto index 32512b83a..2bd9cdeca 100644 --- a/app/stats/command/command.proto +++ b/app/stats/command/command.proto @@ -31,9 +31,26 @@ message QueryStatsResponse { repeated Stat stat = 1; } +message SysStatsRequest { +} + +message SysStatsResponse { + uint32 NumGoroutine = 1; + uint32 NumGC = 2; + uint64 Alloc = 3; + uint64 TotalAlloc = 4; + uint64 Sys = 5; + uint64 Mallocs = 6; + uint64 Frees = 7; + uint64 LiveObjects = 8; + uint64 PauseTotalNs = 9; + uint32 Uptime = 10; +} + service StatsService { rpc GetStats(GetStatsRequest) returns (GetStatsResponse) {} rpc QueryStats(QueryStatsRequest) returns (QueryStatsResponse) {} + rpc GetSysStats(SysStatsRequest) returns (SysStatsResponse) {} } message Config {} diff --git a/infra/control/api.go b/infra/control/api.go index 75c4a2457..823d49450 100644 --- a/infra/control/api.go +++ b/infra/control/api.go @@ -6,6 +6,7 @@ import ( "flag" "fmt" "strings" + "time" "github.com/golang/protobuf/proto" "google.golang.org/grpc" @@ -31,10 +32,12 @@ func (c *ApiCommand) Description() Description { "\tLoggerService.RestartLogger", "\tStatsService.GetStats", "\tStatsService.QueryStats", + "API calls in this command have a timeout to the server of 3 seconds.", "Examples:", "v2ctl api --server=127.0.0.1:8080 LoggerService.RestartLogger '' ", "v2ctl api --server=127.0.0.1:8080 StatsService.QueryStats 'pattern: \"\" reset: false'", "v2ctl api --server=127.0.0.1:8080 StatsService.GetStats 'name: \"inbound>>>statin>>>traffic>>>downlink\" reset: false'", + "v2ctl api --server=127.0.0.1:8080 StatsService.GetSysStats ''", }, } } @@ -48,12 +51,6 @@ func (c *ApiCommand) Execute(args []string) error { return err } - conn, err := grpc.Dial(*serverAddrPtr, grpc.WithInsecure(), grpc.WithBlock()) - if err != nil { - return newError("failed to dial ", *serverAddrPtr).Base(err) - } - defer conn.Close() - unnamedArgs := fs.Args() if len(unnamedArgs) < 2 { return newError("service name or request not specified.") @@ -65,7 +62,16 @@ func (c *ApiCommand) Execute(args []string) error { return newError("unknown service: ", service) } - response, err := handler(conn, method, unnamedArgs[1]) + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + conn, err := grpc.DialContext(ctx, *serverAddrPtr, grpc.WithInsecure(), grpc.WithBlock()) + if err != nil { + return newError("failed to dial ", *serverAddrPtr).Base(err) + } + defer conn.Close() + + response, err := handler(ctx, conn, method, unnamedArgs[1]) if err != nil { return newError("failed to call service ", unnamedArgs[0]).Base(err) } @@ -84,14 +90,14 @@ func getServiceMethod(s string) (string, string) { return service, method } -type serviceHandler func(conn *grpc.ClientConn, method string, request string) (string, error) +type serviceHandler func(ctx context.Context, conn *grpc.ClientConn, method string, request string) (string, error) var serivceHandlerMap = map[string]serviceHandler{ "statsservice": callStatsService, "loggerservice": callLogService, } -func callLogService(conn *grpc.ClientConn, method string, request string) (string, error) { +func callLogService(ctx context.Context, conn *grpc.ClientConn, method string, request string) (string, error) { client := logService.NewLoggerServiceClient(conn) switch strings.ToLower(method) { @@ -100,7 +106,7 @@ func callLogService(conn *grpc.ClientConn, method string, request string) (strin if err := proto.UnmarshalText(request, r); err != nil { return "", err } - resp, err := client.RestartLogger(context.Background(), r) + resp, err := client.RestartLogger(ctx, r) if err != nil { return "", err } @@ -110,7 +116,7 @@ func callLogService(conn *grpc.ClientConn, method string, request string) (strin } } -func callStatsService(conn *grpc.ClientConn, method string, request string) (string, error) { +func callStatsService(ctx context.Context, conn *grpc.ClientConn, method string, request string) (string, error) { client := statsService.NewStatsServiceClient(conn) switch strings.ToLower(method) { @@ -119,7 +125,7 @@ func callStatsService(conn *grpc.ClientConn, method string, request string) (str if err := proto.UnmarshalText(request, r); err != nil { return "", err } - resp, err := client.GetStats(context.Background(), r) + resp, err := client.GetStats(ctx, r) if err != nil { return "", err } @@ -129,7 +135,15 @@ func callStatsService(conn *grpc.ClientConn, method string, request string) (str if err := proto.UnmarshalText(request, r); err != nil { return "", err } - resp, err := client.QueryStats(context.Background(), r) + resp, err := client.QueryStats(ctx, r) + if err != nil { + return "", err + } + return proto.MarshalTextString(resp), nil + case "getsysstats": + // SysStatsRequest is an empty message + r := &statsService.SysStatsRequest{} + resp, err := client.GetSysStats(ctx, r) if err != nil { return "", err }