moved cpu usage to a grpc call that the gui will listen to, fixed grpc stream typing to be grpc.Future, fixed pygui issue for start callback when a start fails, but there are no exceptions
This commit is contained in:
parent
3544d00431
commit
fff4bd7963
7 changed files with 85 additions and 47 deletions
|
@ -16,6 +16,7 @@ from core.api.grpc import client
|
|||
from core.api.grpc.common_pb2 import ConfigOption
|
||||
from core.api.grpc.configservices_pb2 import ConfigService, ConfigServiceConfig
|
||||
from core.api.grpc.core_pb2 import (
|
||||
CpuUsageEvent,
|
||||
Event,
|
||||
ExceptionEvent,
|
||||
Hook,
|
||||
|
@ -55,6 +56,7 @@ if TYPE_CHECKING:
|
|||
from core.gui.app import Application
|
||||
|
||||
GUI_SOURCE = "gui"
|
||||
CPU_USAGE_DELAY = 3
|
||||
|
||||
|
||||
class CoreClient:
|
||||
|
@ -92,8 +94,9 @@ class CoreClient:
|
|||
self.hooks: Dict[str, Hook] = {}
|
||||
self.emane_config: Dict[str, ConfigOption] = {}
|
||||
self.mobility_players: Dict[int, MobilityPlayer] = {}
|
||||
self.handling_throughputs: Optional[grpc.Channel] = None
|
||||
self.handling_events: Optional[grpc.Channel] = None
|
||||
self.handling_throughputs: Optional[grpc.Future] = None
|
||||
self.handling_cpu_usage: Optional[grpc.Future] = None
|
||||
self.handling_events: Optional[grpc.Future] = None
|
||||
self.xml_dir: Optional[str] = None
|
||||
self.xml_file: Optional[str] = None
|
||||
|
||||
|
@ -111,6 +114,7 @@ class CoreClient:
|
|||
)
|
||||
if throughputs_enabled:
|
||||
self.enable_throughputs()
|
||||
self.setup_cpu_usage()
|
||||
return self._client
|
||||
|
||||
def reset(self) -> None:
|
||||
|
@ -258,6 +262,20 @@ class CoreClient:
|
|||
self.handling_events.cancel()
|
||||
self.handling_events = None
|
||||
|
||||
def cancel_cpu_usage(self) -> None:
|
||||
if self.handling_cpu_usage:
|
||||
self.handling_cpu_usage.cancel()
|
||||
self.handling_cpu_usage = None
|
||||
|
||||
def setup_cpu_usage(self) -> None:
|
||||
if self.handling_cpu_usage and self.handling_cpu_usage.running():
|
||||
return
|
||||
if self.handling_cpu_usage:
|
||||
self.handling_cpu_usage.cancel()
|
||||
self.handling_cpu_usage = self._client.cpu_usage(
|
||||
CPU_USAGE_DELAY, self.handle_cpu_event
|
||||
)
|
||||
|
||||
def handle_throughputs(self, event: ThroughputsEvent) -> None:
|
||||
if event.session_id != self.session_id:
|
||||
logging.warning(
|
||||
|
@ -269,6 +287,9 @@ class CoreClient:
|
|||
logging.debug("handling throughputs event: %s", event)
|
||||
self.app.after(0, self.app.canvas.set_throughputs, event)
|
||||
|
||||
def handle_cpu_event(self, event: CpuUsageEvent) -> None:
|
||||
self.app.after(0, self.app.statusbar.set_cpu, event.usage)
|
||||
|
||||
def handle_exception_event(self, event: ExceptionEvent) -> None:
|
||||
logging.info("exception event: %s", event)
|
||||
self.app.statusbar.add_alert(event)
|
||||
|
@ -479,6 +500,8 @@ class CoreClient:
|
|||
"""
|
||||
try:
|
||||
self.client.connect()
|
||||
self.setup_cpu_usage()
|
||||
|
||||
# get service information
|
||||
response = self.client.get_services()
|
||||
for service in response.services:
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
"""
|
||||
status bar
|
||||
"""
|
||||
import sched
|
||||
import tkinter as tk
|
||||
from pathlib import Path
|
||||
from threading import Thread
|
||||
from tkinter import ttk
|
||||
from typing import TYPE_CHECKING, List, Optional
|
||||
|
||||
|
@ -16,41 +13,6 @@ if TYPE_CHECKING:
|
|||
from core.gui.app import Application
|
||||
|
||||
|
||||
class CpuUsage:
|
||||
def __init__(self, statusbar: "StatusBar") -> None:
|
||||
self.scheduler: sched.scheduler = sched.scheduler()
|
||||
self.running: bool = False
|
||||
self.thread: Optional[Thread] = None
|
||||
self.prev_idle: int = 0
|
||||
self.prev_total: int = 0
|
||||
self.stat_file: Path = Path("/proc/stat")
|
||||
self.statusbar: "StatusBar" = statusbar
|
||||
|
||||
def start(self) -> None:
|
||||
self.running = True
|
||||
self.thread = Thread(target=self._start, daemon=True)
|
||||
self.thread.start()
|
||||
|
||||
def _start(self):
|
||||
self.scheduler.enter(0, 0, self.run)
|
||||
self.scheduler.run()
|
||||
|
||||
def run(self) -> None:
|
||||
lines = self.stat_file.read_text().splitlines()[0]
|
||||
values = [int(x) for x in lines.split()[1:]]
|
||||
idle = sum(values[3:5])
|
||||
non_idle = sum(values[:3] + values[5:8])
|
||||
total = idle + non_idle
|
||||
total_diff = total - self.prev_total
|
||||
idle_diff = idle - self.prev_idle
|
||||
cpu_percent = (total_diff - idle_diff) / total_diff
|
||||
self.statusbar.after(0, self.statusbar.set_cpu, cpu_percent)
|
||||
self.prev_idle = idle
|
||||
self.prev_total = total
|
||||
if self.running:
|
||||
self.scheduler.enter(3, 0, self.run)
|
||||
|
||||
|
||||
class StatusBar(ttk.Frame):
|
||||
def __init__(self, master: tk.Widget, app: "Application") -> None:
|
||||
super().__init__(master)
|
||||
|
@ -64,8 +26,6 @@ class StatusBar(ttk.Frame):
|
|||
self.running: bool = False
|
||||
self.core_alarms: List[ExceptionEvent] = []
|
||||
self.draw()
|
||||
self.cpu_usage: CpuUsage = CpuUsage(self)
|
||||
self.cpu_usage.start()
|
||||
|
||||
def draw(self) -> None:
|
||||
self.columnconfigure(0, weight=7)
|
||||
|
|
|
@ -305,10 +305,11 @@ class Toolbar(ttk.Frame):
|
|||
self.set_runtime()
|
||||
self.app.core.set_metadata()
|
||||
self.app.core.show_mobility_players()
|
||||
elif response.exceptions:
|
||||
else:
|
||||
enable_buttons(self.design_frame, enabled=True)
|
||||
message = "\n".join(response.exceptions)
|
||||
self.app.show_error("Start Session Error", message)
|
||||
if response.exceptions:
|
||||
message = "\n".join(response.exceptions)
|
||||
self.app.show_error("Start Session Error", message)
|
||||
|
||||
def set_runtime(self) -> None:
|
||||
enable_buttons(self.runtime_frame, enabled=True)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue