go-libp2p-resource-manager/trace.go

668 lines
12 KiB
Go
Raw Normal View History

2022-01-15 19:36:28 +08:00
package rcmgr
import (
2022-01-15 23:34:40 +08:00
"compress/gzip"
"context"
"encoding/json"
2022-06-20 15:41:33 +08:00
"fmt"
2022-01-15 23:34:40 +08:00
"io"
"os"
2022-06-20 15:41:33 +08:00
"strings"
2022-01-15 23:34:40 +08:00
"sync"
"time"
2022-01-15 19:36:28 +08:00
"github.com/libp2p/go-libp2p-core/network"
)
2022-01-15 23:34:40 +08:00
type trace struct {
path string
ctx context.Context
cancel func()
closed chan struct{}
mx sync.Mutex
done bool
pend []interface{}
}
func WithTrace(path string) Option {
return func(r *resourceManager) error {
r.trace = &trace{path: path}
return nil
}
}
const (
2022-04-09 03:20:32 +08:00
traceStartEvt = "start"
traceCreateScopeEvt = "create_scope"
traceDestroyScopeEvt = "destroy_scope"
traceReserveMemoryEvt = "reserve_memory"
traceBlockReserveMemoryEvt = "block_reserve_memory"
traceReleaseMemoryEvt = "release_memory"
traceAddStreamEvt = "add_stream"
traceBlockAddStreamEvt = "block_add_stream"
traceRemoveStreamEvt = "remove_stream"
traceAddConnEvt = "add_conn"
traceBlockAddConnEvt = "block_add_conn"
traceRemoveConnEvt = "remove_conn"
2022-01-15 23:34:40 +08:00
)
2022-06-20 15:41:33 +08:00
type scopeClass struct {
name string
}
func (s scopeClass) MarshalJSON() ([]byte, error) {
name := s.name
var span string
if idx := strings.Index(name, "span:"); idx > -1 {
name = name[:idx-1]
span = name[idx+5:]
}
// System and Transient scope
if name == "system" || name == "transient" {
return json.Marshal(struct {
Class string
Span string `json:",omitempty"`
}{
Class: name,
Span: span,
})
}
// Connection scope
if strings.HasPrefix(name, "conn-") {
return json.Marshal(struct {
Class string
Conn string
Span string `json:",omitempty"`
}{
Class: "conn",
Conn: name[5:],
Span: span,
})
}
// Stream scope
if strings.HasPrefix(name, "stream-") {
return json.Marshal(struct {
Class string
Stream string
Span string `json:",omitempty"`
}{
Class: "stream",
Stream: name[7:],
Span: span,
})
}
// Peer scope
if strings.HasPrefix(name, "peer:") {
return json.Marshal(struct {
Class string
Peer string
Span string `json:",omitempty"`
}{
Class: "peer",
Peer: name[5:],
Span: span,
})
}
if strings.HasPrefix(name, "service:") {
if idx := strings.Index(name, "peer:"); idx > 0 { // Peer-Service scope
return json.Marshal(struct {
Class string
Service string
Peer string
Span string `json:",omitempty"`
}{
Class: "service-peer",
Service: name[8 : idx-1],
Peer: name[idx+5:],
Span: span,
})
} else { // Service scope
return json.Marshal(struct {
Class string
Service string
Span string `json:",omitempty"`
}{
Class: "service",
Service: name[8:],
Span: span,
})
}
}
if strings.HasPrefix(name, "protocol:") {
if idx := strings.Index(name, "peer:"); idx > -1 { // Peer-Protocol scope
return json.Marshal(struct {
Class string
Protocol string
Peer string
Span string `json:",omitempty"`
}{
Class: "protocol-peer",
Protocol: name[9 : idx-1],
Peer: name[idx+5:],
Span: span,
})
} else { // Protocol scope
return json.Marshal(struct {
Class string
Protocol string
Span string `json:",omitempty"`
}{
Class: "protocol",
Protocol: name[9:],
Span: span,
})
}
}
return nil, fmt.Errorf("unrecognized scope: %s", name)
}
2022-06-04 21:43:35 +08:00
type TraceEvt struct {
2022-06-01 22:08:25 +08:00
Time string
2022-04-09 03:20:32 +08:00
Type string
2022-01-15 23:34:40 +08:00
2022-06-20 15:41:33 +08:00
Scope *scopeClass `json:",omitempty"`
Name string `json:",omitempty"`
2022-01-15 23:34:40 +08:00
2022-01-15 23:37:12 +08:00
Limit interface{} `json:",omitempty"`
2022-01-15 23:34:40 +08:00
2022-01-15 23:37:12 +08:00
Priority uint8 `json:",omitempty"`
2022-01-15 23:34:40 +08:00
2022-01-15 23:37:12 +08:00
Delta int64 `json:",omitempty"`
DeltaIn int `json:",omitempty"`
DeltaOut int `json:",omitempty"`
2022-01-15 23:34:40 +08:00
2022-01-15 23:37:12 +08:00
Memory int64 `json:",omitempty"`
2022-01-15 23:34:40 +08:00
2022-01-15 23:37:12 +08:00
StreamsIn int `json:",omitempty"`
StreamsOut int `json:",omitempty"`
2022-01-15 23:34:40 +08:00
2022-01-15 23:37:12 +08:00
ConnsIn int `json:",omitempty"`
ConnsOut int `json:",omitempty"`
2022-01-15 23:34:40 +08:00
2022-01-15 23:37:12 +08:00
FD int `json:",omitempty"`
2022-01-15 23:34:40 +08:00
}
2022-06-04 21:43:35 +08:00
func (t *trace) push(evt TraceEvt) {
2022-01-15 23:34:40 +08:00
t.mx.Lock()
defer t.mx.Unlock()
if t.done {
return
}
evt.Time = time.Now().Format(time.RFC3339Nano)
2022-06-20 15:41:33 +08:00
if evt.Name != "" {
evt.Scope = &scopeClass{name: evt.Name}
}
2022-01-15 23:34:40 +08:00
t.pend = append(t.pend, evt)
}
func (t *trace) background(out io.WriteCloser) {
defer close(t.closed)
defer out.Close()
gzOut := gzip.NewWriter(out)
defer gzOut.Close()
jsonOut := json.NewEncoder(gzOut)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
var pend []interface{}
getEvents := func() {
t.mx.Lock()
tmp := t.pend
t.pend = pend[:0]
pend = tmp
t.mx.Unlock()
}
for {
select {
case <-ticker.C:
getEvents()
if len(pend) == 0 {
continue
}
2022-01-15 23:34:40 +08:00
if err := t.writeEvents(pend, jsonOut); err != nil {
log.Warnf("error writing rcmgr trace: %s", err)
t.mx.Lock()
t.done = true
t.mx.Unlock()
return
}
if err := gzOut.Flush(); err != nil {
log.Warnf("error flushing rcmgr trace: %s", err)
t.mx.Lock()
t.done = true
t.mx.Unlock()
return
}
case <-t.ctx.Done():
getEvents()
if len(pend) == 0 {
return
}
2022-01-15 23:34:40 +08:00
if err := t.writeEvents(pend, jsonOut); err != nil {
log.Warnf("error writing rcmgr trace: %s", err)
return
}
if err := gzOut.Flush(); err != nil {
log.Warnf("error flushing rcmgr trace: %s", err)
}
return
}
}
}
func (t *trace) writeEvents(pend []interface{}, jout *json.Encoder) error {
for _, e := range pend {
if err := jout.Encode(e); err != nil {
return err
}
}
return nil
}
2022-01-15 19:36:28 +08:00
func (t *trace) Start(limits Limiter) error {
if t == nil {
return nil
}
2022-01-15 23:34:40 +08:00
t.ctx, t.cancel = context.WithCancel(context.Background())
t.closed = make(chan struct{})
out, err := os.OpenFile(t.path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return nil
}
go t.background(out)
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
Type: traceStartEvt,
Limit: limits,
})
2022-01-15 19:36:28 +08:00
return nil
}
func (t *trace) Close() error {
if t == nil {
return nil
}
2022-01-15 23:34:40 +08:00
t.mx.Lock()
if t.done {
t.mx.Unlock()
return nil
}
t.cancel()
t.done = true
t.mx.Unlock()
<-t.closed
2022-01-15 19:36:28 +08:00
return nil
}
2022-01-15 23:34:40 +08:00
func (t *trace) CreateScope(scope string, limit Limit) {
2022-01-15 19:36:28 +08:00
if t == nil {
return
}
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
2022-01-15 23:34:40 +08:00
Type: traceCreateScopeEvt,
2022-06-20 15:41:33 +08:00
Name: scope,
2022-01-15 23:34:40 +08:00
Limit: limit,
})
2022-01-15 19:36:28 +08:00
}
2022-01-15 23:34:40 +08:00
func (t *trace) DestroyScope(scope string) {
2022-01-15 19:36:28 +08:00
if t == nil {
return
}
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
2022-06-20 15:41:33 +08:00
Type: traceDestroyScopeEvt,
Name: scope,
2022-01-15 23:34:40 +08:00
})
2022-01-15 19:36:28 +08:00
}
func (t *trace) ReserveMemory(scope string, prio uint8, size, mem int64) {
if t == nil {
return
}
if size == 0 {
return
}
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
2022-01-15 23:34:40 +08:00
Type: traceReserveMemoryEvt,
2022-06-20 15:41:33 +08:00
Name: scope,
2022-01-15 23:34:40 +08:00
Priority: prio,
Delta: size,
Memory: mem,
})
2022-01-15 19:36:28 +08:00
}
func (t *trace) BlockReserveMemory(scope string, prio uint8, size, mem int64) {
if t == nil {
return
}
if size == 0 {
return
}
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
2022-01-15 23:34:40 +08:00
Type: traceBlockReserveMemoryEvt,
2022-06-20 15:41:33 +08:00
Name: scope,
2022-01-15 23:34:40 +08:00
Priority: prio,
Delta: size,
Memory: mem,
})
2022-01-15 19:36:28 +08:00
}
func (t *trace) ReleaseMemory(scope string, size, mem int64) {
if t == nil {
return
}
if size == 0 {
return
}
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
2022-01-15 23:34:40 +08:00
Type: traceReleaseMemoryEvt,
2022-06-20 15:41:33 +08:00
Name: scope,
Delta: size,
2022-01-15 23:34:40 +08:00
Memory: mem,
})
2022-01-15 19:36:28 +08:00
}
func (t *trace) AddStream(scope string, dir network.Direction, nstreamsIn, nstreamsOut int) {
if t == nil {
return
}
2022-01-15 23:34:40 +08:00
var deltaIn, deltaOut int
if dir == network.DirInbound {
deltaIn = 1
} else {
deltaOut = 1
}
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
2022-01-15 23:34:40 +08:00
Type: traceAddStreamEvt,
2022-06-20 15:41:33 +08:00
Name: scope,
2022-01-15 23:34:40 +08:00
DeltaIn: deltaIn,
DeltaOut: deltaOut,
StreamsIn: nstreamsIn,
StreamsOut: nstreamsOut,
})
2022-01-15 19:36:28 +08:00
}
func (t *trace) BlockAddStream(scope string, dir network.Direction, nstreamsIn, nstreamsOut int) {
if t == nil {
return
}
2022-01-15 23:34:40 +08:00
var deltaIn, deltaOut int
if dir == network.DirInbound {
deltaIn = 1
} else {
deltaOut = 1
}
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
2022-01-15 23:34:40 +08:00
Type: traceBlockAddStreamEvt,
2022-06-20 15:41:33 +08:00
Name: scope,
2022-01-15 23:34:40 +08:00
DeltaIn: deltaIn,
DeltaOut: deltaOut,
StreamsIn: nstreamsIn,
StreamsOut: nstreamsOut,
})
2022-01-15 19:36:28 +08:00
}
func (t *trace) RemoveStream(scope string, dir network.Direction, nstreamsIn, nstreamsOut int) {
if t == nil {
return
}
2022-01-15 23:34:40 +08:00
var deltaIn, deltaOut int
if dir == network.DirInbound {
deltaIn = -1
} else {
deltaOut = -1
}
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
2022-01-15 23:34:40 +08:00
Type: traceRemoveStreamEvt,
2022-06-20 15:41:33 +08:00
Name: scope,
2022-01-15 23:34:40 +08:00
DeltaIn: deltaIn,
DeltaOut: deltaOut,
StreamsIn: nstreamsIn,
StreamsOut: nstreamsOut,
})
2022-01-15 19:36:28 +08:00
}
2022-01-15 23:34:40 +08:00
func (t *trace) AddStreams(scope string, deltaIn, deltaOut, nstreamsIn, nstreamsOut int) {
2022-01-15 19:36:28 +08:00
if t == nil {
return
}
if deltaIn == 0 && deltaOut == 0 {
return
}
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
2022-01-15 23:34:40 +08:00
Type: traceAddStreamEvt,
2022-06-20 15:41:33 +08:00
Name: scope,
2022-01-15 23:34:40 +08:00
DeltaIn: deltaIn,
DeltaOut: deltaOut,
StreamsIn: nstreamsIn,
StreamsOut: nstreamsOut,
})
2022-01-15 19:36:28 +08:00
}
2022-01-15 23:34:40 +08:00
func (t *trace) BlockAddStreams(scope string, deltaIn, deltaOut, nstreamsIn, nstreamsOut int) {
2022-01-15 19:36:28 +08:00
if t == nil {
return
}
if deltaIn == 0 && deltaOut == 0 {
return
}
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
2022-01-15 23:34:40 +08:00
Type: traceBlockAddStreamEvt,
2022-06-20 15:41:33 +08:00
Name: scope,
2022-01-15 23:34:40 +08:00
DeltaIn: deltaIn,
DeltaOut: deltaOut,
StreamsIn: nstreamsIn,
StreamsOut: nstreamsOut,
})
2022-01-15 19:36:28 +08:00
}
2022-01-15 23:34:40 +08:00
func (t *trace) RemoveStreams(scope string, deltaIn, deltaOut, nstreamsIn, nstreamsOut int) {
2022-01-15 19:36:28 +08:00
if t == nil {
return
}
if deltaIn == 0 && deltaOut == 0 {
return
}
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
2022-01-15 23:34:40 +08:00
Type: traceRemoveStreamEvt,
2022-06-20 15:41:33 +08:00
Name: scope,
2022-01-15 23:34:40 +08:00
DeltaIn: -deltaIn,
DeltaOut: -deltaOut,
StreamsIn: nstreamsIn,
StreamsOut: nstreamsOut,
})
2022-01-15 19:36:28 +08:00
}
func (t *trace) AddConn(scope string, dir network.Direction, usefd bool, nconnsIn, nconnsOut, nfd int) {
if t == nil {
return
}
2022-01-15 23:34:40 +08:00
var deltaIn, deltaOut, deltafd int
if dir == network.DirInbound {
deltaIn = 1
} else {
deltaOut = 1
}
if usefd {
deltafd = 1
}
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
2022-01-15 23:34:40 +08:00
Type: traceAddConnEvt,
2022-06-20 15:41:33 +08:00
Name: scope,
2022-01-15 23:34:40 +08:00
DeltaIn: deltaIn,
DeltaOut: deltaOut,
Delta: int64(deltafd),
ConnsIn: nconnsIn,
ConnsOut: nconnsOut,
FD: nfd,
})
2022-01-15 19:36:28 +08:00
}
func (t *trace) BlockAddConn(scope string, dir network.Direction, usefd bool, nconnsIn, nconnsOut, nfd int) {
if t == nil {
return
}
2022-01-15 23:34:40 +08:00
var deltaIn, deltaOut, deltafd int
if dir == network.DirInbound {
deltaIn = 1
} else {
deltaOut = 1
}
if usefd {
deltafd = 1
}
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
2022-01-15 23:34:40 +08:00
Type: traceBlockAddConnEvt,
2022-06-20 15:41:33 +08:00
Name: scope,
2022-01-15 23:34:40 +08:00
DeltaIn: deltaIn,
DeltaOut: deltaOut,
Delta: int64(deltafd),
ConnsIn: nconnsIn,
ConnsOut: nconnsOut,
FD: nfd,
})
2022-01-15 19:36:28 +08:00
}
func (t *trace) RemoveConn(scope string, dir network.Direction, usefd bool, nconnsIn, nconnsOut, nfd int) {
if t == nil {
return
}
2022-01-15 23:34:40 +08:00
var deltaIn, deltaOut, deltafd int
if dir == network.DirInbound {
deltaIn = -1
} else {
deltaOut = -1
}
if usefd {
deltafd = -1
}
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
2022-01-15 23:34:40 +08:00
Type: traceRemoveConnEvt,
2022-06-20 15:41:33 +08:00
Name: scope,
2022-01-15 23:34:40 +08:00
DeltaIn: deltaIn,
DeltaOut: deltaOut,
Delta: int64(deltafd),
ConnsIn: nconnsIn,
ConnsOut: nconnsOut,
FD: nfd,
})
2022-01-15 19:36:28 +08:00
}
2022-01-15 23:34:40 +08:00
func (t *trace) AddConns(scope string, deltaIn, deltaOut, deltafd, nconnsIn, nconnsOut, nfd int) {
2022-01-15 19:36:28 +08:00
if t == nil {
return
}
if deltaIn == 0 && deltaOut == 0 && deltafd == 0 {
return
}
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
2022-01-15 23:34:40 +08:00
Type: traceAddConnEvt,
2022-06-20 15:41:33 +08:00
Name: scope,
2022-01-15 23:34:40 +08:00
DeltaIn: deltaIn,
DeltaOut: deltaOut,
Delta: int64(deltafd),
ConnsIn: nconnsIn,
ConnsOut: nconnsOut,
FD: nfd,
})
2022-01-15 19:36:28 +08:00
}
2022-01-15 23:34:40 +08:00
func (t *trace) BlockAddConns(scope string, deltaIn, deltaOut, deltafd, nconnsIn, nconnsOut, nfd int) {
2022-01-15 19:36:28 +08:00
if t == nil {
return
}
if deltaIn == 0 && deltaOut == 0 && deltafd == 0 {
return
}
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
2022-01-15 23:34:40 +08:00
Type: traceBlockAddConnEvt,
2022-06-20 15:41:33 +08:00
Name: scope,
2022-01-15 23:34:40 +08:00
DeltaIn: deltaIn,
DeltaOut: deltaOut,
Delta: int64(deltafd),
ConnsIn: nconnsIn,
ConnsOut: nconnsOut,
FD: nfd,
})
2022-01-15 19:36:28 +08:00
}
2022-01-15 23:34:40 +08:00
func (t *trace) RemoveConns(scope string, deltaIn, deltaOut, deltafd, nconnsIn, nconnsOut, nfd int) {
2022-01-15 19:36:28 +08:00
if t == nil {
return
}
if deltaIn == 0 && deltaOut == 0 && deltafd == 0 {
return
}
2022-06-04 21:43:35 +08:00
t.push(TraceEvt{
2022-01-15 23:34:40 +08:00
Type: traceRemoveConnEvt,
2022-06-20 15:41:33 +08:00
Name: scope,
2022-01-15 23:34:40 +08:00
DeltaIn: -deltaIn,
DeltaOut: -deltaOut,
Delta: -int64(deltafd),
ConnsIn: nconnsIn,
ConnsOut: nconnsOut,
FD: nfd,
})
2022-01-15 19:36:28 +08:00
}