2021-01-23 14:49:46 +08:00
|
|
|
package statistic
|
2019-10-27 21:44:07 +08:00
|
|
|
|
|
|
|
import (
|
2023-04-07 23:53:46 +08:00
|
|
|
"os"
|
2019-10-27 21:44:07 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
2020-10-29 17:51:14 +08:00
|
|
|
|
2023-04-07 23:53:46 +08:00
|
|
|
"github.com/shirou/gopsutil/v3/process"
|
2020-10-29 17:51:14 +08:00
|
|
|
"go.uber.org/atomic"
|
2019-10-27 21:44:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var DefaultManager *Manager
|
|
|
|
|
|
|
|
func init() {
|
2020-10-29 17:51:14 +08:00
|
|
|
DefaultManager = &Manager{
|
|
|
|
uploadTemp: atomic.NewInt64(0),
|
|
|
|
downloadTemp: atomic.NewInt64(0),
|
|
|
|
uploadBlip: atomic.NewInt64(0),
|
|
|
|
downloadBlip: atomic.NewInt64(0),
|
|
|
|
uploadTotal: atomic.NewInt64(0),
|
|
|
|
downloadTotal: atomic.NewInt64(0),
|
2023-04-07 23:53:46 +08:00
|
|
|
pid: os.Getpid(),
|
2020-10-29 17:51:14 +08:00
|
|
|
}
|
2020-09-03 10:30:18 +08:00
|
|
|
|
|
|
|
go DefaultManager.handle()
|
2019-10-27 21:44:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type Manager struct {
|
|
|
|
connections sync.Map
|
2020-10-29 17:51:14 +08:00
|
|
|
uploadTemp *atomic.Int64
|
|
|
|
downloadTemp *atomic.Int64
|
|
|
|
uploadBlip *atomic.Int64
|
|
|
|
downloadBlip *atomic.Int64
|
|
|
|
uploadTotal *atomic.Int64
|
|
|
|
downloadTotal *atomic.Int64
|
2023-04-07 23:53:46 +08:00
|
|
|
pid int
|
2019-10-27 21:44:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Join(c tracker) {
|
|
|
|
m.connections.Store(c.ID(), c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Leave(c tracker) {
|
|
|
|
m.connections.Delete(c.ID())
|
|
|
|
}
|
|
|
|
|
2020-09-02 16:34:12 +08:00
|
|
|
func (m *Manager) PushUploaded(size int64) {
|
2020-10-29 17:51:14 +08:00
|
|
|
m.uploadTemp.Add(size)
|
|
|
|
m.uploadTotal.Add(size)
|
2019-10-27 21:44:07 +08:00
|
|
|
}
|
|
|
|
|
2020-09-02 16:34:12 +08:00
|
|
|
func (m *Manager) PushDownloaded(size int64) {
|
2020-10-29 17:51:14 +08:00
|
|
|
m.downloadTemp.Add(size)
|
|
|
|
m.downloadTotal.Add(size)
|
2019-10-27 21:44:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Now() (up int64, down int64) {
|
2020-10-29 17:51:14 +08:00
|
|
|
return m.uploadBlip.Load(), m.downloadBlip.Load()
|
2019-10-27 21:44:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Snapshot() *Snapshot {
|
|
|
|
connections := []tracker{}
|
2022-03-16 12:10:13 +08:00
|
|
|
m.connections.Range(func(key, value any) bool {
|
2019-10-27 21:44:07 +08:00
|
|
|
connections = append(connections, value.(tracker))
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2023-04-07 22:55:01 +08:00
|
|
|
getMem := func() uint64 {
|
2023-04-07 23:53:46 +08:00
|
|
|
p, err := process.NewProcess(int32(m.pid))
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
stat, err := p.MemoryInfo()
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return stat.RSS
|
2023-04-07 22:55:01 +08:00
|
|
|
}
|
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
return &Snapshot{
|
2020-10-29 17:51:14 +08:00
|
|
|
UploadTotal: m.uploadTotal.Load(),
|
|
|
|
DownloadTotal: m.downloadTotal.Load(),
|
2019-10-27 21:44:07 +08:00
|
|
|
Connections: connections,
|
2023-04-07 22:55:01 +08:00
|
|
|
Memory: getMem(),
|
2019-10-27 21:44:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 00:20:10 +08:00
|
|
|
func (m *Manager) ResetStatistic() {
|
2020-10-29 17:51:14 +08:00
|
|
|
m.uploadTemp.Store(0)
|
|
|
|
m.uploadBlip.Store(0)
|
|
|
|
m.uploadTotal.Store(0)
|
|
|
|
m.downloadTemp.Store(0)
|
|
|
|
m.downloadBlip.Store(0)
|
|
|
|
m.downloadTotal.Store(0)
|
2020-01-11 00:20:10 +08:00
|
|
|
}
|
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
func (m *Manager) handle() {
|
|
|
|
ticker := time.NewTicker(time.Second)
|
2020-09-02 16:34:12 +08:00
|
|
|
|
2020-09-03 10:30:18 +08:00
|
|
|
for range ticker.C {
|
2020-10-29 17:51:14 +08:00
|
|
|
m.uploadBlip.Store(m.uploadTemp.Load())
|
|
|
|
m.uploadTemp.Store(0)
|
|
|
|
m.downloadBlip.Store(m.downloadTemp.Load())
|
|
|
|
m.downloadTemp.Store(0)
|
2019-10-27 21:44:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Snapshot struct {
|
|
|
|
DownloadTotal int64 `json:"downloadTotal"`
|
|
|
|
UploadTotal int64 `json:"uploadTotal"`
|
|
|
|
Connections []tracker `json:"connections"`
|
2023-04-07 22:55:01 +08:00
|
|
|
Memory uint64 `json:"memory"`
|
2019-10-27 21:44:07 +08:00
|
|
|
}
|