SILENT KILLERPanel

Current Path: > > opt > cloudlinux > venv > lib64 > python3.11 > site-packages > lvestats > plugins > generic > > > burster


Operation   : Linux premium131.web-hosting.com 4.18.0-553.44.1.lve.el8.x86_64 #1 SMP Thu Mar 13 14:29:12 UTC 2025 x86_64
Software     : Apache
Server IP    : 162.0.232.56 | Your IP: 216.73.216.111
Domains      : 1034 Domain(s)
Permission   : [ 0755 ]

Files and Folders in: //opt/cloudlinux/venv/lib64/python3.11/site-packages/lvestats/plugins/generic///burster

NameTypeSizeLast ModifiedActions
__pycache__ Directory - -
storage Directory - -
__init__.py File 8734 bytes May 30 2025 10:30:46.
_logs.py File 597 bytes May 30 2025 10:30:46.
adjust.py File 7539 bytes May 30 2025 10:30:46.
common.py File 4668 bytes May 30 2025 10:30:46.
config.py File 8151 bytes May 30 2025 10:30:46.
history.py File 10030 bytes May 30 2025 10:30:46.
lve_sm.py File 18844 bytes May 30 2025 10:30:46.
lves_tracker.py File 9139 bytes May 30 2025 10:30:46.
notify.py File 1679 bytes May 30 2025 10:30:46.
overload.py File 3309 bytes May 30 2025 10:30:46.
profiler.py File 10546 bytes May 30 2025 10:30:46.
utils.py File 1665 bytes May 30 2025 10:30:46.

Reading File: //opt/cloudlinux/venv/lib64/python3.11/site-packages/lvestats/plugins/generic///burster/overload.py

# coding=utf-8
#
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2023 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT
from collections import deque
from typing import NamedTuple, Self, Protocol

from ._logs import logger


class StatsTimes(NamedTuple):
    @classmethod
    def from_proc_stat(cls, line: str) -> Self:
        # NOTE(vlebedev): First element of the line is a name of a CPU core, not time of any kind.
        return cls(*(int(v) for v in line.strip().split()[1:]))

    user: int
    nice: int
    system: int
    idle: int
    iowait: int
    irq: int
    softirq: int
    steal: int
    guest: int
    guest_nice: int


class GetStats(Protocol):
    def __call__(self) -> StatsTimes:
        ...


class OverloadCheckResult(NamedTuple):
    is_overloaded: bool
    server_load: float

    def __bool__(self) -> bool:
        return self.is_overloaded


class OverloadChecker:
    def __init__(
        self,
        idle_time_threshold: float,
        get_stats: GetStats,
        max_samples_number: int,
    ) -> None:
        self._prev_stats = None
        self._idle_time_threshold = idle_time_threshold
        self._weights = _get_weights(num=max_samples_number)
        self._idle_times_history = deque(maxlen=max_samples_number)
        self._get_stats = get_stats
        self._max_samples_number = max_samples_number

    def __call__(self) -> OverloadCheckResult:
        stats = self._get_stats()

        # NOTE(vlebedev): 'delta' represents total time spent by CPU
        #                 on all kinds of activities since the previous measurement.
        delta = stats if self._prev_stats is None else StatsTimes(*(
            new - old
            for (new, old)
            in zip(stats, self._prev_stats)
        ))
        self._prev_stats = stats

        # NOTE(vlebedev): Fraction of total delta time spent by CPU on being 'idle'.
        try:
            idle_time = delta.idle / sum(delta)
        except ZeroDivisionError:
            idle_time = 0.0

        self._idle_times_history.append(idle_time)

        samples_num = len(self._idle_times_history)
        relevant_weights = self._weights[:samples_num]
        weighted_idle_times = [
            weight * idle_time
            for weight, idle_time
            in zip(relevant_weights, self._idle_times_history)
        ]
        weighted_idle_time = sum(weighted_idle_times)
        if samples_num < self._max_samples_number:
            weighted_idle_time /= sum(relevant_weights)

        logger.debug('Current idle time: %02.2f, threshold %s', weighted_idle_time, self._idle_time_threshold)

        return OverloadCheckResult(
            is_overloaded=weighted_idle_time < self._idle_time_threshold,
            server_load=1 - weighted_idle_time,
        )


def _get_weights(num: int = 10, alpha: float = 0.9):
    # NOTE(vlebedev): Generate an exponentially decaying weight list
    weights = [alpha ** i for i in range(num)]
    # NOTE(vlebedev): Normalize the weights so they sum to 1
    total = sum(weights)
    return [weight / total for weight in weights]


def read_times_from_proc() -> StatsTimes:
    with open('/proc/stat', encoding='ascii') as fd:
        return StatsTimes.from_proc_stat(fd.readline())

SILENT KILLER Tool