Skip to content

Commit

Permalink
Do not do a deepcopy
Browse files Browse the repository at this point in the history
  • Loading branch information
sonicaj committed Aug 28, 2024
1 parent 245ba6c commit 1ddcc02
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def list_resources_stats_by_project(project_name: str | None = None) -> dict:
if not project:
continue

blkio_container_stats = stats.get('blkio', {}).get('io_service_bytes_recursive') or {}
blkio_container_stats = stats.get('blkio_stats', {}).get('io_service_bytes_recursive') or {}
project_stats = projects[project]
project_stats['cpu_usage'] += stats.get('cpu_stats', {}).get('cpu_usage', {}).get('total_usage', 0)
project_stats['memory'] += stats.get('memory', {}).get('usage', 0)
project_stats['memory'] += stats.get('memory_stats', {}).get('usage', 0)
for entry in filter(lambda x: x['op'] in ('read', 'write'), blkio_container_stats):
project_stats['blkio'][entry['op']] += entry['value']
for net_name, net_values in stats.get('networks', {}).items():
Expand Down
3 changes: 1 addition & 2 deletions src/middlewared/middlewared/plugins/apps/stats.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import copy
import time

from middlewared.event import EventSource
Expand Down Expand Up @@ -59,7 +58,7 @@ def run_sync(self):
try:
project_stats = list_resources_stats_by_project()
self.send_event(
'ADDED', fields=normalize_projects_stats(copy.deepcopy(project_stats), old_projects_stats, interval)
'ADDED', fields=normalize_projects_stats(project_stats, old_projects_stats, interval)
)
old_projects_stats = project_stats
time.sleep(interval)
Expand Down
32 changes: 18 additions & 14 deletions src/middlewared/middlewared/plugins/apps/stats_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,31 @@
def normalize_projects_stats(all_projects_stats: dict, old_stats: dict, interval: int) -> list[dict]:
normalized_projects_stats = []
for project, data in all_projects_stats.items():
data['app_name'] = get_app_name_from_project_name(project)
normalized_data = {
'app_name': get_app_name_from_project_name(project),
'memory': data['memory'],
'blkio': data['blkio'],
}

# Docker provides CPU usage time in nanoseconds.
# To calculate the CPU usage percentage:
# 1. Calculate the difference in CPU usage (`cpu_delta`) between the current and previous stats.
# 2. Normalize this delta over the given time interval by dividing by (interval * NANO_SECOND).
# 3. Multiply by 100 to convert to percentage.
cpu_delta = data['cpu_usage'] - old_stats[project]['cpu_usage']
data['cpu_usage'] = (cpu_delta / (interval * NANO_SECOND)) * 100
normalized_data['cpu_usage'] = (cpu_delta / (interval * NANO_SECOND)) * 100

networks = []
for net_name, net_data in data['networks'].items():
net_data['interface_name'] = net_name
# calculate networks received/transmitted bytes/s
net_data['rx_bytes'] = int(
(net_data['rx_bytes'] - old_stats[project]['networks'][net_name]['rx_bytes']) / interval
)
net_data['tx_bytes'] = int(
(net_data['tx_bytes'] - old_stats[project]['networks'][net_name]['tx_bytes']) / interval
)
networks.append(net_data)
data['networks'] = networks
normalized_projects_stats.append(data)
for net_name, network_data in data['networks'].items():
networks.append({
'interface_stats': net_name,
'rx_bytes': int(
(network_data['rx_bytes'] - old_stats[project]['networks'][net_name]['rx_bytes']) / interval
),
'tx_bytes': int(
(network_data['tx_bytes'] - old_stats[project]['networks'][net_name]['tx_bytes']) / interval
),
})
normalized_data['networks'] = networks
normalized_projects_stats.append(normalized_data)
return normalized_projects_stats

0 comments on commit 1ddcc02

Please sign in to comment.