2021-01-23 06:49:46 +00:00
|
|
|
package statistic
|
2019-10-27 13:44:07 +00:00
|
|
|
|
|
|
|
import (
|
2023-04-07 15:53:46 +00:00
|
|
|
"os"
|
2019-10-27 13:44:07 +00:00
|
|
|
"time"
|
2020-10-29 09:51:14 +00:00
|
|
|
|
2023-04-22 07:37:57 +00:00
|
|
|
"github.com/Dreamacro/clash/common/atomic"
|
|
|
|
|
2023-09-02 08:54:35 +00:00
|
|
|
"github.com/puzpuzpuz/xsync/v2"
|
2023-04-07 15:53:46 +00:00
|
|
|
"github.com/shirou/gopsutil/v3/process"
|
2019-10-27 13:44:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var DefaultManager *Manager
|
|
|
|
|
|
|
|
func init() {
|
2020-10-29 09:51:14 +00:00
|
|
|
DefaultManager = &Manager{
|
2023-09-02 08:54:35 +00:00
|
|
|
connections: xsync.NewMapOf[Tracker](),
|
2020-10-29 09:51:14 +00:00
|
|
|
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-08 01:56:02 +00:00
|
|
|
process: &process.Process{Pid: int32(os.Getpid())},
|
2020-10-29 09:51:14 +00:00
|
|
|
}
|
2020-09-03 02:30:18 +00:00
|
|
|
|
|
|
|
go DefaultManager.handle()
|
2019-10-27 13:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Manager struct {
|
2023-09-02 08:54:35 +00:00
|
|
|
connections *xsync.MapOf[string, Tracker]
|
2020-10-29 09:51:14 +00:00
|
|
|
uploadTemp *atomic.Int64
|
|
|
|
downloadTemp *atomic.Int64
|
|
|
|
uploadBlip *atomic.Int64
|
|
|
|
downloadBlip *atomic.Int64
|
|
|
|
uploadTotal *atomic.Int64
|
|
|
|
downloadTotal *atomic.Int64
|
2023-04-08 01:56:02 +00:00
|
|
|
process *process.Process
|
2023-04-07 16:55:25 +00:00
|
|
|
memory uint64
|
2019-10-27 13:44:07 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 09:46:14 +00:00
|
|
|
func (m *Manager) Join(c Tracker) {
|
2019-10-27 13:44:07 +00:00
|
|
|
m.connections.Store(c.ID(), c)
|
|
|
|
}
|
|
|
|
|
2023-06-26 09:46:14 +00:00
|
|
|
func (m *Manager) Leave(c Tracker) {
|
2019-10-27 13:44:07 +00:00
|
|
|
m.connections.Delete(c.ID())
|
|
|
|
}
|
|
|
|
|
2023-06-26 10:25:36 +00:00
|
|
|
func (m *Manager) Get(id string) (c Tracker) {
|
|
|
|
if value, ok := m.connections.Load(id); ok {
|
2023-09-02 08:54:35 +00:00
|
|
|
c = value
|
2023-06-26 10:25:36 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Range(f func(c Tracker) bool) {
|
2023-09-02 08:54:35 +00:00
|
|
|
m.connections.Range(func(key string, value Tracker) bool {
|
|
|
|
return f(value)
|
2023-06-26 10:25:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-02 08:34:12 +00:00
|
|
|
func (m *Manager) PushUploaded(size int64) {
|
2020-10-29 09:51:14 +00:00
|
|
|
m.uploadTemp.Add(size)
|
|
|
|
m.uploadTotal.Add(size)
|
2019-10-27 13:44:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 08:34:12 +00:00
|
|
|
func (m *Manager) PushDownloaded(size int64) {
|
2020-10-29 09:51:14 +00:00
|
|
|
m.downloadTemp.Add(size)
|
|
|
|
m.downloadTotal.Add(size)
|
2019-10-27 13:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Now() (up int64, down int64) {
|
2020-10-29 09:51:14 +00:00
|
|
|
return m.uploadBlip.Load(), m.downloadBlip.Load()
|
2019-10-27 13:44:07 +00:00
|
|
|
}
|
|
|
|
|
2023-04-07 16:55:25 +00:00
|
|
|
func (m *Manager) Memory() uint64 {
|
2023-04-27 06:55:53 +00:00
|
|
|
m.updateMemory()
|
2023-04-07 16:55:25 +00:00
|
|
|
return m.memory
|
|
|
|
}
|
|
|
|
|
2019-10-27 13:44:07 +00:00
|
|
|
func (m *Manager) Snapshot() *Snapshot {
|
2023-06-26 10:13:17 +00:00
|
|
|
var connections []*TrackerInfo
|
2023-06-26 10:25:36 +00:00
|
|
|
m.Range(func(c Tracker) bool {
|
|
|
|
connections = append(connections, c.Info())
|
2019-10-27 13:44:07 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
return &Snapshot{
|
2020-10-29 09:51:14 +00:00
|
|
|
UploadTotal: m.uploadTotal.Load(),
|
|
|
|
DownloadTotal: m.downloadTotal.Load(),
|
2019-10-27 13:44:07 +00:00
|
|
|
Connections: connections,
|
2023-04-07 16:55:25 +00:00
|
|
|
Memory: m.memory,
|
2019-10-27 13:44:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 06:55:53 +00:00
|
|
|
func (m *Manager) updateMemory() {
|
|
|
|
stat, err := m.process.MemoryInfo()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.memory = stat.RSS
|
|
|
|
}
|
|
|
|
|
2020-01-10 16:20:10 +00:00
|
|
|
func (m *Manager) ResetStatistic() {
|
2020-10-29 09:51:14 +00: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-10 16:20:10 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 13:44:07 +00:00
|
|
|
func (m *Manager) handle() {
|
|
|
|
ticker := time.NewTicker(time.Second)
|
2020-09-02 08:34:12 +00:00
|
|
|
|
2020-09-03 02:30:18 +00:00
|
|
|
for range ticker.C {
|
2020-10-29 09:51:14 +00:00
|
|
|
m.uploadBlip.Store(m.uploadTemp.Load())
|
|
|
|
m.uploadTemp.Store(0)
|
|
|
|
m.downloadBlip.Store(m.downloadTemp.Load())
|
|
|
|
m.downloadTemp.Store(0)
|
2019-10-27 13:44:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Snapshot struct {
|
2023-06-26 10:13:17 +00:00
|
|
|
DownloadTotal int64 `json:"downloadTotal"`
|
|
|
|
UploadTotal int64 `json:"uploadTotal"`
|
|
|
|
Connections []*TrackerInfo `json:"connections"`
|
2023-06-26 10:25:36 +00:00
|
|
|
Memory uint64 `json:"memory"`
|
2019-10-27 13:44:07 +00:00
|
|
|
}
|