2019-08-17 20:19:56 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2020-07-17 02:56:53 +00:00
|
|
|
"sync"
|
2020-11-07 05:53:00 +00:00
|
|
|
"sync/atomic"
|
2021-01-10 01:22:39 +00:00
|
|
|
|
|
|
|
"github.com/pterodactyl/wings/environment"
|
|
|
|
"github.com/pterodactyl/wings/system"
|
2019-08-17 20:19:56 +00:00
|
|
|
)
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
// ResourceUsage defines the current resource usage for a given server instance. If a server is offline you
|
2019-08-17 20:19:56 +00:00
|
|
|
// should obviously expect memory and CPU usage to be 0. However, disk will always be returned
|
|
|
|
// since that is not dependent on the server being running to collect that data.
|
|
|
|
type ResourceUsage struct {
|
2020-07-19 23:27:55 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
|
2020-08-19 04:38:42 +00:00
|
|
|
// Embed the current environment stats into this server specific resource usage struct.
|
|
|
|
environment.Stats
|
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
// The current server status.
|
2020-12-26 01:04:18 +00:00
|
|
|
State *system.AtomicString `json:"state"`
|
2020-07-17 02:56:53 +00:00
|
|
|
|
2020-10-11 22:02:37 +00:00
|
|
|
// The current disk space being used by the server. This value is not guaranteed to be accurate
|
|
|
|
// at all times. It is "manually" set whenever server.Proc() is called. This is kind of just a
|
|
|
|
// hacky solution for now to avoid passing events all over the place.
|
2019-08-17 23:10:48 +00:00
|
|
|
Disk int64 `json:"disk_bytes"`
|
2019-08-17 20:19:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
// Proc returns the current resource usage stats for the server instance. This returns
|
2020-12-17 06:03:35 +00:00
|
|
|
// a copy of the tracked resources, so making any changes to the response will not
|
|
|
|
// have the desired outcome for you most likely.
|
|
|
|
func (s *Server) Proc() ResourceUsage {
|
|
|
|
s.resources.mu.Lock()
|
|
|
|
defer s.resources.mu.Unlock()
|
2020-11-07 05:53:00 +00:00
|
|
|
// Store the updated disk usage when requesting process usage.
|
|
|
|
atomic.StoreInt64(&s.resources.Disk, s.Filesystem().CachedUsage())
|
2020-12-17 06:03:35 +00:00
|
|
|
//goland:noinspection GoVetCopyLock
|
|
|
|
return s.resources
|
|
|
|
}
|
2020-10-11 22:02:37 +00:00
|
|
|
|
2022-01-30 16:55:59 +00:00
|
|
|
// UpdateStats updates the current stats for the server's resource usage.
|
|
|
|
func (ru *ResourceUsage) UpdateStats(stats environment.Stats) {
|
|
|
|
ru.mu.Lock()
|
|
|
|
ru.Stats = stats
|
|
|
|
ru.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
// Reset resets the usages values to zero, used when a server is stopped to ensure we don't hold
|
2020-12-17 06:03:35 +00:00
|
|
|
// onto any values incorrectly.
|
|
|
|
func (ru *ResourceUsage) Reset() {
|
|
|
|
ru.mu.Lock()
|
|
|
|
defer ru.mu.Unlock()
|
2021-08-02 21:07:00 +00:00
|
|
|
|
2020-12-17 06:03:35 +00:00
|
|
|
ru.Memory = 0
|
|
|
|
ru.CpuAbsolute = 0
|
2021-10-03 19:59:03 +00:00
|
|
|
ru.Uptime = 0
|
2020-12-17 06:03:35 +00:00
|
|
|
ru.Network.TxBytes = 0
|
|
|
|
ru.Network.RxBytes = 0
|
2020-07-19 23:27:55 +00:00
|
|
|
}
|