2020-06-10 13:49:16 +00:00
|
|
|
import EmberObject, { get, computed } from '@ember/object';
|
2018-08-30 00:17:18 +00:00
|
|
|
import { alias } from '@ember/object/computed';
|
|
|
|
import RollingArray from 'nomad-ui/utils/classes/rolling-array';
|
|
|
|
import AbstractStatsTracker from 'nomad-ui/utils/classes/abstract-stats-tracker';
|
2020-06-10 13:49:16 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2018-08-30 00:17:18 +00:00
|
|
|
|
|
|
|
const percent = (numerator, denominator) => {
|
|
|
|
if (!numerator || !denominator) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return numerator / denominator;
|
|
|
|
};
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
const empty = (ts) => ({ timestamp: ts, used: null, percent: null });
|
2018-09-17 23:59:09 +00:00
|
|
|
|
2021-03-16 02:43:32 +00:00
|
|
|
// Tasks are sorted by their lifecycle phase in this order:
|
|
|
|
const sortMap = [
|
|
|
|
'main',
|
|
|
|
'prestart-sidecar',
|
|
|
|
'poststart-sidecar',
|
|
|
|
'prestart-ephemeral',
|
|
|
|
'poststart-ephemeral',
|
|
|
|
'poststop',
|
|
|
|
].reduce((map, phase, index) => {
|
|
|
|
map[phase] = index;
|
|
|
|
return map;
|
|
|
|
}, {});
|
|
|
|
|
2021-03-24 22:10:46 +00:00
|
|
|
const taskPrioritySort = (a, b) => sortMap[a.lifecycleName] - sortMap[b.lifecycleName];
|
2021-03-16 02:43:32 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@classic
|
|
|
|
class AllocationStatsTracker extends EmberObject.extend(AbstractStatsTracker) {
|
2018-08-30 00:17:18 +00:00
|
|
|
// Set via the stats computed property macro
|
2020-06-10 13:49:16 +00:00
|
|
|
allocation = null;
|
2018-08-30 00:17:18 +00:00
|
|
|
|
2021-02-17 21:01:44 +00:00
|
|
|
@computed('allocation.id')
|
2020-06-10 13:49:16 +00:00
|
|
|
get url() {
|
2018-08-30 00:17:18 +00:00
|
|
|
return `/v1/client/allocation/${this.get('allocation.id')}/stats`;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-30 00:17:18 +00:00
|
|
|
|
|
|
|
append(frame) {
|
2018-09-14 17:21:01 +00:00
|
|
|
const timestamp = new Date(Math.floor(frame.Timestamp / 1000000));
|
|
|
|
|
2018-08-30 00:17:18 +00:00
|
|
|
const cpuUsed = Math.floor(frame.ResourceUsage.CpuStats.TotalTicks) || 0;
|
2019-03-26 07:46:44 +00:00
|
|
|
this.cpu.pushObject({
|
2018-09-14 17:21:01 +00:00
|
|
|
timestamp,
|
2018-08-30 00:17:18 +00:00
|
|
|
used: cpuUsed,
|
2019-03-26 07:46:44 +00:00
|
|
|
percent: percent(cpuUsed, this.reservedCPU),
|
2018-08-30 00:17:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const memoryUsed = frame.ResourceUsage.MemoryStats.RSS;
|
2019-03-26 07:46:44 +00:00
|
|
|
this.memory.pushObject({
|
2018-09-14 17:21:01 +00:00
|
|
|
timestamp,
|
2018-08-30 00:17:18 +00:00
|
|
|
used: memoryUsed,
|
2019-03-26 07:46:44 +00:00
|
|
|
percent: percent(memoryUsed / 1024 / 1024, this.reservedMemory),
|
2018-08-30 00:17:18 +00:00
|
|
|
});
|
|
|
|
|
2021-03-16 02:43:32 +00:00
|
|
|
let aggregateCpu = 0;
|
|
|
|
let aggregateMemory = 0;
|
|
|
|
for (var stats of this.tasks) {
|
|
|
|
const taskFrame = frame.Tasks[stats.task];
|
2018-08-30 00:17:18 +00:00
|
|
|
|
2021-03-16 02:43:32 +00:00
|
|
|
// If the task is not present in the frame data (because it hasn't started or
|
|
|
|
// it has already stopped), just keep going.
|
|
|
|
if (!taskFrame) continue;
|
2018-08-30 00:17:18 +00:00
|
|
|
|
2018-09-14 17:21:01 +00:00
|
|
|
const frameTimestamp = new Date(Math.floor(taskFrame.Timestamp / 1000000));
|
|
|
|
|
2018-08-30 00:17:18 +00:00
|
|
|
const taskCpuUsed = Math.floor(taskFrame.ResourceUsage.CpuStats.TotalTicks) || 0;
|
2021-03-16 02:43:32 +00:00
|
|
|
const percentCpuTotal = percent(taskCpuUsed, this.reservedCPU);
|
2018-09-14 17:21:01 +00:00
|
|
|
stats.cpu.pushObject({
|
|
|
|
timestamp: frameTimestamp,
|
2018-08-30 00:17:18 +00:00
|
|
|
used: taskCpuUsed,
|
|
|
|
percent: percent(taskCpuUsed, stats.reservedCPU),
|
2021-03-16 02:43:32 +00:00
|
|
|
percentTotal: percentCpuTotal,
|
|
|
|
percentStack: percentCpuTotal + aggregateCpu,
|
2018-08-30 00:17:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const taskMemoryUsed = taskFrame.ResourceUsage.MemoryStats.RSS;
|
2021-03-16 02:43:32 +00:00
|
|
|
const percentMemoryTotal = percent(taskMemoryUsed / 1024 / 1024, this.reservedMemory);
|
2018-09-14 17:21:01 +00:00
|
|
|
stats.memory.pushObject({
|
|
|
|
timestamp: frameTimestamp,
|
2018-08-30 00:17:18 +00:00
|
|
|
used: taskMemoryUsed,
|
|
|
|
percent: percent(taskMemoryUsed / 1024 / 1024, stats.reservedMemory),
|
2021-03-16 02:43:32 +00:00
|
|
|
percentTotal: percentMemoryTotal,
|
|
|
|
percentStack: percentMemoryTotal + aggregateMemory,
|
2018-08-30 00:17:18 +00:00
|
|
|
});
|
2021-03-16 02:43:32 +00:00
|
|
|
|
|
|
|
aggregateCpu += percentCpuTotal;
|
|
|
|
aggregateMemory += percentMemoryTotal;
|
2018-08-30 00:17:18 +00:00
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-30 00:17:18 +00:00
|
|
|
|
2018-09-17 23:59:09 +00:00
|
|
|
pause() {
|
|
|
|
const ts = new Date();
|
2019-03-26 07:46:44 +00:00
|
|
|
this.memory.pushObject(empty(ts));
|
|
|
|
this.cpu.pushObject(empty(ts));
|
2021-12-28 14:45:20 +00:00
|
|
|
this.tasks.forEach((task) => {
|
2018-09-17 23:59:09 +00:00
|
|
|
task.memory.pushObject(empty(ts));
|
|
|
|
task.cpu.pushObject(empty(ts));
|
|
|
|
});
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-09-17 23:59:09 +00:00
|
|
|
|
2018-08-30 00:17:18 +00:00
|
|
|
// Static figures, denominators for stats
|
2020-06-10 13:49:16 +00:00
|
|
|
@alias('allocation.taskGroup.reservedCPU') reservedCPU;
|
|
|
|
@alias('allocation.taskGroup.reservedMemory') reservedMemory;
|
2018-08-30 00:17:18 +00:00
|
|
|
|
|
|
|
// Dynamic figures, collected over time
|
|
|
|
// []{ timestamp: Date, used: Number, percent: Number }
|
2021-02-17 21:01:44 +00:00
|
|
|
@computed('allocation', 'bufferSize')
|
2020-06-10 13:49:16 +00:00
|
|
|
get cpu() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return RollingArray(this.bufferSize);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 21:01:44 +00:00
|
|
|
@computed('allocation', 'bufferSize')
|
2020-06-10 13:49:16 +00:00
|
|
|
get memory() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return RollingArray(this.bufferSize);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-30 00:17:18 +00:00
|
|
|
|
2021-02-17 21:01:44 +00:00
|
|
|
@computed('allocation.taskGroup.tasks', 'bufferSize')
|
2020-06-10 13:49:16 +00:00
|
|
|
get tasks() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const bufferSize = this.bufferSize;
|
2018-09-19 23:33:51 +00:00
|
|
|
const tasks = this.get('allocation.taskGroup.tasks') || [];
|
2021-03-16 02:43:32 +00:00
|
|
|
return tasks
|
2021-03-24 22:10:46 +00:00
|
|
|
.slice()
|
|
|
|
.sort(taskPrioritySort)
|
2021-12-28 14:45:20 +00:00
|
|
|
.map((task) => ({
|
2021-03-16 02:43:32 +00:00
|
|
|
task: get(task, 'name'),
|
|
|
|
|
|
|
|
// Static figures, denominators for stats
|
|
|
|
reservedCPU: get(task, 'reservedCPU'),
|
|
|
|
reservedMemory: get(task, 'reservedMemory'),
|
|
|
|
|
|
|
|
// Dynamic figures, collected over time
|
|
|
|
// []{ timestamp: Date, used: Number, percent: Number }
|
|
|
|
cpu: RollingArray(bufferSize),
|
|
|
|
memory: RollingArray(bufferSize),
|
2021-03-24 22:10:46 +00:00
|
|
|
}));
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-30 00:17:18 +00:00
|
|
|
|
|
|
|
export default AllocationStatsTracker;
|
|
|
|
|
|
|
|
export function stats(allocationProp, fetch) {
|
2021-12-28 14:45:20 +00:00
|
|
|
return computed(allocationProp, function () {
|
2018-08-30 00:17:18 +00:00
|
|
|
return AllocationStatsTracker.create({
|
|
|
|
fetch: fetch.call(this),
|
|
|
|
allocation: this.get(allocationProp),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|