mirror of
https://github.com/GNS3/gns3-server.git
synced 2024-11-16 16:54:51 +02:00
Implement a minimum interval between cpu_percent() calls. Fixes #1738
This commit is contained in:
parent
4ab100b716
commit
10bb259248
@ -25,6 +25,7 @@ import tempfile
|
|||||||
from gns3server.web.route import Route
|
from gns3server.web.route import Route
|
||||||
from gns3server.compute.project_manager import ProjectManager
|
from gns3server.compute.project_manager import ProjectManager
|
||||||
from gns3server.compute import MODULES
|
from gns3server.compute import MODULES
|
||||||
|
from gns3server.utils.cpu_percent import CpuPercent
|
||||||
|
|
||||||
from gns3server.schemas.project import (
|
from gns3server.schemas.project import (
|
||||||
PROJECT_OBJECT_SCHEMA,
|
PROJECT_OBJECT_SCHEMA,
|
||||||
@ -208,7 +209,7 @@ class ProjectHandler:
|
|||||||
"""
|
"""
|
||||||
stats = {}
|
stats = {}
|
||||||
# Non blocking call in order to get cpu usage. First call will return 0
|
# Non blocking call in order to get cpu usage. First call will return 0
|
||||||
stats["cpu_usage_percent"] = psutil.cpu_percent(interval=None)
|
stats["cpu_usage_percent"] = CpuPercent.get(interval=None)
|
||||||
stats["memory_usage_percent"] = psutil.virtual_memory().percent
|
stats["memory_usage_percent"] = psutil.virtual_memory().percent
|
||||||
return {"action": "ping", "event": stats}
|
return {"action": "ping", "event": stats}
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@ from gns3server.config import Config
|
|||||||
from gns3server.schemas.version import VERSION_SCHEMA
|
from gns3server.schemas.version import VERSION_SCHEMA
|
||||||
from gns3server.schemas.server_statistics import SERVER_STATISTICS_SCHEMA
|
from gns3server.schemas.server_statistics import SERVER_STATISTICS_SCHEMA
|
||||||
from gns3server.compute.port_manager import PortManager
|
from gns3server.compute.port_manager import PortManager
|
||||||
|
from gns3server.utils.cpu_percent import CpuPercent
|
||||||
from gns3server.version import __version__
|
from gns3server.version import __version__
|
||||||
from aiohttp.web import HTTPConflict
|
from aiohttp.web import HTTPConflict
|
||||||
from psutil._common import bytes2human
|
from psutil._common import bytes2human
|
||||||
@ -57,7 +58,7 @@ class ServerHandler:
|
|||||||
swap_total = psutil.swap_memory().total
|
swap_total = psutil.swap_memory().total
|
||||||
swap_free = psutil.swap_memory().free
|
swap_free = psutil.swap_memory().free
|
||||||
swap_used = psutil.swap_memory().used
|
swap_used = psutil.swap_memory().used
|
||||||
cpu_percent = int(psutil.cpu_percent())
|
cpu_percent = int(CpuPercent.get())
|
||||||
load_average_percent = [int(x / psutil.cpu_count() * 100) for x in psutil.getloadavg()]
|
load_average_percent = [int(x / psutil.cpu_count() * 100) for x in psutil.getloadavg()]
|
||||||
memory_percent = int(psutil.virtual_memory().percent)
|
memory_percent = int(psutil.virtual_memory().percent)
|
||||||
swap_percent = int(psutil.swap_memory().percent)
|
swap_percent = int(psutil.swap_memory().percent)
|
||||||
|
@ -19,6 +19,8 @@ import asyncio
|
|||||||
import json
|
import json
|
||||||
import psutil
|
import psutil
|
||||||
|
|
||||||
|
from gns3server.utils.cpu_percent import CpuPercent
|
||||||
|
|
||||||
|
|
||||||
class NotificationQueue(asyncio.Queue):
|
class NotificationQueue(asyncio.Queue):
|
||||||
"""
|
"""
|
||||||
@ -51,7 +53,7 @@ class NotificationQueue(asyncio.Queue):
|
|||||||
"""
|
"""
|
||||||
msg = {}
|
msg = {}
|
||||||
# Non blocking call in order to get cpu usage. First call will return 0
|
# Non blocking call in order to get cpu usage. First call will return 0
|
||||||
msg["cpu_usage_percent"] = psutil.cpu_percent(interval=None)
|
msg["cpu_usage_percent"] = CpuPercent.get(interval=None)
|
||||||
msg["memory_usage_percent"] = psutil.virtual_memory().percent
|
msg["memory_usage_percent"] = psutil.virtual_memory().percent
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
|
48
gns3server/utils/cpu_percent.py
Normal file
48
gns3server/utils/cpu_percent.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# Copyright (C) 2020 GNS3 Technologies Inc.
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import psutil
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
class CpuPercent:
|
||||||
|
"""
|
||||||
|
Ensures a minumum interval between two cpu_percent() calls
|
||||||
|
"""
|
||||||
|
|
||||||
|
_last_measurement = None # time of last measurement
|
||||||
|
_last_cpu_percent = 0.0 # last cpu_percent
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get(cls, interval=None):
|
||||||
|
"""
|
||||||
|
Get CPU utilization as a percentage
|
||||||
|
|
||||||
|
:returns: float
|
||||||
|
"""
|
||||||
|
|
||||||
|
if interval:
|
||||||
|
cls._last_cpu_percent = psutil.cpu_percent(interval=interval)
|
||||||
|
cls._last_measurement = time.monotonic()
|
||||||
|
else:
|
||||||
|
cur_time = time.monotonic()
|
||||||
|
if cls._last_measurement is None or \
|
||||||
|
(cur_time - cls._last_measurement) >= 1.9:
|
||||||
|
cls._last_cpu_percent = psutil.cpu_percent(interval=None)
|
||||||
|
cls._last_measurement = cur_time
|
||||||
|
|
||||||
|
return cls._last_cpu_percent
|
Loading…
Reference in New Issue
Block a user