Merge pull request #1186 from tahoe-lafs/3883.drop-cpuusagemonitor-foolscap-dep

Drop the Foolscap dependency in allmydata.stats

Fixes: ticket:3883
This commit is contained in:
Jean-Paul Calderone 2022-03-24 08:09:42 -04:00 committed by GitHub
commit f48055179f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 28 deletions

0
newsfragments/3883.minor Normal file
View File

View File

@ -1,54 +1,39 @@
"""
Ported to Python 3.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from future.utils import PY2
if PY2:
from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401
from time import clock as process_time
else:
from time import process_time
from collections import deque
from time import process_time
import time
from typing import Deque, Tuple
from twisted.application import service
from twisted.application.internet import TimerService
from zope.interface import implementer
from foolscap.api import eventually
from allmydata.util import log, dictutil
from allmydata.interfaces import IStatsProducer
@implementer(IStatsProducer)
class CPUUsageMonitor(service.MultiService):
HISTORY_LENGTH = 15
POLL_INTERVAL = 60 # type: float
HISTORY_LENGTH: int = 15
POLL_INTERVAL: float = 60
initial_cpu: float = 0.0
def __init__(self):
service.MultiService.__init__(self)
# we don't use process_time() here, because the constructor is run by
# the twistd parent process (as it loads the .tac file), whereas the
# rest of the program will be run by the child process, after twistd
# forks. Instead, set self.initial_cpu as soon as the reactor starts
# up.
self.initial_cpu = 0.0 # just in case
eventually(self._set_initial_cpu)
self.samples = []
self.samples: Deque[Tuple[float, float]] = deque([], self.HISTORY_LENGTH + 1)
# we provide 1min, 5min, and 15min moving averages
TimerService(self.POLL_INTERVAL, self.check).setServiceParent(self)
def _set_initial_cpu(self):
def startService(self):
self.initial_cpu = process_time()
return super().startService()
def check(self):
now_wall = time.time()
now_cpu = process_time()
self.samples.append( (now_wall, now_cpu) )
while len(self.samples) > self.HISTORY_LENGTH+1:
self.samples.pop(0)
def _average_N_minutes(self, size):
if len(self.samples) < size+1:

View File

@ -17,7 +17,7 @@ from allmydata.util import pollmixin
import allmydata.test.common_util as testutil
class FasterMonitor(CPUUsageMonitor):
POLL_INTERVAL = 0.1
POLL_INTERVAL = 0.01
class CPUUsage(unittest.TestCase, pollmixin.PollMixin, testutil.StallMixin):
@ -36,9 +36,9 @@ class CPUUsage(unittest.TestCase, pollmixin.PollMixin, testutil.StallMixin):
def _poller():
return bool(len(m.samples) == m.HISTORY_LENGTH+1)
d = self.poll(_poller)
# pause one more second, to make sure that the history-trimming code
# is exercised
d.addCallback(self.stall, 1.0)
# pause a couple more intervals, to make sure that the history-trimming
# code is exercised
d.addCallback(self.stall, FasterMonitor.POLL_INTERVAL * 2)
def _check(res):
s = m.get_stats()
self.failUnless("cpu_monitor.1min_avg" in s)