Expose resource usage in process state

This commit is contained in:
Ingo Oppermann
2023-04-25 16:20:59 +02:00
parent 3a1825cf61
commit b70382c40b
7 changed files with 208 additions and 15 deletions

View File

@@ -3384,6 +3384,10 @@ const docTemplate = `{
},
"type": {
"type": "string"
},
"updated_at": {
"type": "integer",
"format": "int64"
}
}
},
@@ -3564,6 +3568,9 @@ const docTemplate = `{
},
"progress": {
"$ref": "#/definitions/api.Progress"
},
"resources": {
"$ref": "#/definitions/api.ProcessUsage"
}
}
},
@@ -3604,6 +3611,9 @@ const docTemplate = `{
},
"progress": {
"$ref": "#/definitions/api.Progress"
},
"resources": {
"$ref": "#/definitions/api.ProcessUsage"
}
}
},
@@ -3639,6 +3649,7 @@ const docTemplate = `{
}
},
"cpu_usage": {
"description": "deprecated, use Resources.Memory.Current",
"type": "number"
},
"exec": {
@@ -3648,6 +3659,7 @@ const docTemplate = `{
"type": "string"
},
"memory_bytes": {
"description": "deprecated, use Resources.CPU.Current",
"type": "integer",
"format": "uint64"
},
@@ -3661,12 +3673,63 @@ const docTemplate = `{
"type": "integer",
"format": "int64"
},
"resources": {
"$ref": "#/definitions/api.ProcessUsage"
},
"runtime_seconds": {
"type": "integer",
"format": "int64"
}
}
},
"api.ProcessUsage": {
"type": "object",
"properties": {
"cpu_usage": {
"$ref": "#/definitions/api.ProcessUsageCPU"
},
"memory_bytes": {
"$ref": "#/definitions/api.ProcessUsageMemory"
}
}
},
"api.ProcessUsageCPU": {
"type": "object",
"properties": {
"avg": {
"type": "number"
},
"cur": {
"type": "number"
},
"limit": {
"type": "number"
},
"max": {
"type": "number"
}
}
},
"api.ProcessUsageMemory": {
"type": "object",
"properties": {
"avg": {
"type": "number"
},
"cur": {
"type": "integer",
"format": "uint64"
},
"limit": {
"type": "integer",
"format": "uint64"
},
"max": {
"type": "integer",
"format": "uint64"
}
}
},
"api.Progress": {
"type": "object",
"properties": {

View File

@@ -3377,6 +3377,10 @@
},
"type": {
"type": "string"
},
"updated_at": {
"type": "integer",
"format": "int64"
}
}
},
@@ -3557,6 +3561,9 @@
},
"progress": {
"$ref": "#/definitions/api.Progress"
},
"resources": {
"$ref": "#/definitions/api.ProcessUsage"
}
}
},
@@ -3597,6 +3604,9 @@
},
"progress": {
"$ref": "#/definitions/api.Progress"
},
"resources": {
"$ref": "#/definitions/api.ProcessUsage"
}
}
},
@@ -3632,6 +3642,7 @@
}
},
"cpu_usage": {
"description": "deprecated, use Resources.Memory.Current",
"type": "number"
},
"exec": {
@@ -3641,6 +3652,7 @@
"type": "string"
},
"memory_bytes": {
"description": "deprecated, use Resources.CPU.Current",
"type": "integer",
"format": "uint64"
},
@@ -3654,12 +3666,63 @@
"type": "integer",
"format": "int64"
},
"resources": {
"$ref": "#/definitions/api.ProcessUsage"
},
"runtime_seconds": {
"type": "integer",
"format": "int64"
}
}
},
"api.ProcessUsage": {
"type": "object",
"properties": {
"cpu_usage": {
"$ref": "#/definitions/api.ProcessUsageCPU"
},
"memory_bytes": {
"$ref": "#/definitions/api.ProcessUsageMemory"
}
}
},
"api.ProcessUsageCPU": {
"type": "object",
"properties": {
"avg": {
"type": "number"
},
"cur": {
"type": "number"
},
"limit": {
"type": "number"
},
"max": {
"type": "number"
}
}
},
"api.ProcessUsageMemory": {
"type": "object",
"properties": {
"avg": {
"type": "number"
},
"cur": {
"type": "integer",
"format": "uint64"
},
"limit": {
"type": "integer",
"format": "uint64"
},
"max": {
"type": "integer",
"format": "uint64"
}
}
},
"api.Progress": {
"type": "object",
"properties": {

View File

@@ -755,6 +755,9 @@ definitions:
$ref: '#/definitions/api.ProcessState'
type:
type: string
updated_at:
format: int64
type: integer
type: object
api.ProcessConfig:
properties:
@@ -877,6 +880,8 @@ definitions:
type: array
progress:
$ref: '#/definitions/api.Progress'
resources:
$ref: '#/definitions/api.ProcessUsage'
type: object
api.ProcessReportEntry:
properties:
@@ -904,6 +909,8 @@ definitions:
type: array
progress:
$ref: '#/definitions/api.Progress'
resources:
$ref: '#/definitions/api.ProcessUsage'
type: object
api.ProcessReportSearchResult:
properties:
@@ -927,12 +934,14 @@ definitions:
type: string
type: array
cpu_usage:
description: deprecated, use Resources.Memory.Current
type: number
exec:
type: string
last_logline:
type: string
memory_bytes:
description: deprecated, use Resources.CPU.Current
format: uint64
type: integer
order:
@@ -942,10 +951,44 @@ definitions:
reconnect_seconds:
format: int64
type: integer
resources:
$ref: '#/definitions/api.ProcessUsage'
runtime_seconds:
format: int64
type: integer
type: object
api.ProcessUsage:
properties:
cpu_usage:
$ref: '#/definitions/api.ProcessUsageCPU'
memory_bytes:
$ref: '#/definitions/api.ProcessUsageMemory'
type: object
api.ProcessUsageCPU:
properties:
avg:
type: number
cur:
type: number
limit:
type: number
max:
type: number
type: object
api.ProcessUsageMemory:
properties:
avg:
type: number
cur:
format: uint64
type: integer
limit:
format: uint64
type: integer
max:
format: uint64
type: integer
type: object
api.Progress:
properties:
bitrate_kbit:

View File

@@ -202,15 +202,16 @@ func (cfg *ProcessConfig) Unmarshal(c *app.Config) {
// ProcessState represents the current state of an ffmpeg process
type ProcessState struct {
Order string `json:"order" jsonschema:"enum=start,enum=stop"`
State string `json:"exec" jsonschema:"enum=finished,enum=starting,enum=running,enum=finishing,enum=killed,enum=failed"`
Runtime int64 `json:"runtime_seconds" jsonschema:"minimum=0" format:"int64"`
Reconnect int64 `json:"reconnect_seconds" format:"int64"`
LastLog string `json:"last_logline"`
Progress *Progress `json:"progress"`
Memory uint64 `json:"memory_bytes" format:"uint64"`
CPU json.Number `json:"cpu_usage" swaggertype:"number" jsonschema:"type=number"`
Command []string `json:"command"`
Order string `json:"order" jsonschema:"enum=start,enum=stop"`
State string `json:"exec" jsonschema:"enum=finished,enum=starting,enum=running,enum=finishing,enum=killed,enum=failed"`
Runtime int64 `json:"runtime_seconds" jsonschema:"minimum=0" format:"int64"`
Reconnect int64 `json:"reconnect_seconds" format:"int64"`
LastLog string `json:"last_logline"`
Progress *Progress `json:"progress"`
Memory uint64 `json:"memory_bytes" format:"uint64"` // deprecated, use Resources.CPU.Current
CPU json.Number `json:"cpu_usage" swaggertype:"number" jsonschema:"type=number"` // deprecated, use Resources.Memory.Current
Resources ProcessUsage `json:"resources"`
Command []string `json:"command"`
}
// Unmarshal converts a restreamer ffmpeg process state to a state in API representation
@@ -227,19 +228,35 @@ func (s *ProcessState) Unmarshal(state *app.State) {
s.Progress = &Progress{}
s.Memory = state.Memory
s.CPU = toNumber(state.CPU)
s.Resources.CPU = ProcessUsageCPU{
Current: toNumber(state.Resources.CPU.Current),
Average: toNumber(state.Resources.CPU.Average),
Max: toNumber(state.Resources.CPU.Max),
Limit: toNumber(state.Resources.CPU.Limit),
}
s.Resources.Memory = ProcessUsageMemory{
Current: state.Resources.Memory.Current,
Average: toNumber(state.Resources.Memory.Average),
Max: state.Resources.Memory.Max,
Limit: state.Resources.Memory.Limit,
}
s.Command = state.Command
s.Progress.Unmarshal(&state.Progress)
}
type ProcessUsageCPU struct {
Current json.Number `json:"cur" swaggertype:"number" jsonschema:"type=number"`
Average json.Number `json:"avg" swaggertype:"number" jsonschema:"type=number"`
Max json.Number `json:"max" swaggertype:"number" jsonschema:"type=number"`
Limit json.Number `json:"limit" swaggertype:"number" jsonschema:"type=number"`
}
type ProcessUsageMemory struct {
Current uint64 `json:"cur" format:"uint64"`
Average json.Number `json:"avg" swaggertype:"number" jsonschema:"type=number"`
Max uint64 `json:"max" format:"uint64"`
Limit uint64 `json:"limit" format:"uint64"`
}
type ProcessUsage struct {

View File

@@ -57,10 +57,12 @@ func (report *ProcessReport) Unmarshal(l *app.Log) {
CPU: ProcessUsageCPU{
Average: toNumber(h.Usage.CPU.Average),
Max: toNumber(h.Usage.CPU.Max),
Limit: toNumber(h.Usage.CPU.Limit),
},
Memory: ProcessUsageMemory{
Average: toNumber(h.Usage.Memory.Average),
Max: h.Usage.Memory.Max,
Limit: h.Usage.Memory.Limit,
},
},
}

View File

@@ -160,19 +160,22 @@ type State struct {
Progress Progress // Progress data of the process
Memory uint64 // Current memory consumption in bytes
CPU float64 // Current CPU consumption in percent
Resources ProcessUsage // Current resource usage, include CPU and memory consumption
Command []string // ffmpeg command line parameters
}
type ProcessUsageCPU struct {
Average float64
Max float64
Limit float64
Current float64 // percent 0-100
Average float64 // percent 0-100
Max float64 // percent 0-100
Limit float64 // percent 0-100
}
type ProcessUsageMemory struct {
Average float64
Max uint64
Limit uint64
Current uint64 // bytes
Average float64 // bytes
Max uint64 // bytes
Limit uint64 // bytes
}
type ProcessUsage struct {

View File

@@ -1283,6 +1283,8 @@ func (r *restream) GetProcessState(id string) (*app.State, error) {
state.Time = status.Time.Unix()
state.Memory = status.Memory.Current
state.CPU = status.CPU.Current
state.Resources.CPU = status.CPU
state.Resources.Memory = status.Memory
state.Duration = status.Duration.Round(10 * time.Millisecond).Seconds()
state.Reconnect = -1
state.Command = status.CommandArgs