__init__.py 3.03 KB
Newer Older
Mario Hock's avatar
Mario Hock committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import psutil
import time


## XXX for interactive debugging only
def RELOAD():
    print ("import importlib")
    print ("importlib.reload(cpunetlog)")


MEASUREMENT_INTERVAL = 0.2

class Measurement:
    last_measurement = None

    @staticmethod
    def get_time():
        """ Unified/comparable clock access """
        return time.time()

24
25
26
    @classmethod
    def get_time_since_last_measurement(cls):
        """ Time since last measurement in seconds (float) """
Mario Hock's avatar
Mario Hock committed
27
28
        assert( Measurement.last_measurement )
        return Measurement.get_time() - Measurement.last_measurement
29

Mario Hock's avatar
Mario Hock committed
30
31
32
    @staticmethod
    def update_last_measurement(t):
        Measurement.last_measurement = t
Mario Hock's avatar
Mario Hock committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

    ## Exceptions
    class TaintedResultsException(Exception):
        pass


    ## ***
    def __init__(self):
        ## sanity check
        if ( self.last_measurement ):
            if ( self.get_time() - self.last_measurement < 0.09 ):
                raise self.TaintedResultsException

        ## timing
        t = self.get_time()

        ## * measurements *
Mario Hock's avatar
Mario Hock committed
50
51
52
53
        self.cpu_util = psutil.cpu_percent(interval=0, percpu=True)
        self.cpu_times = psutil.cpu_times_percent(interval=0, percpu=True)
        self.memory = psutil.virtual_memory()
        self.net_io = psutil.net_io_counters(pernic=True)
Mario Hock's avatar
Mario Hock committed
54
55
56

        ## timing
        t2 = time.time()
Mario Hock's avatar
Mario Hock committed
57
        assert( t2-t < 0.01 )   ## XXX should be fast!!
Mario Hock's avatar
Mario Hock committed
58
59
60
61
62
63
64
65
66
67
68
69
70

        ## store timespan for these statistics (if reasonable)
        if ( self.last_measurement ):
            self.timespan = t - self.last_measurement
            self.valid = True
        else:
            self.timespan = None
            self.valid = False

        ## update class variable
        self.update_last_measurement(t)

    def __str__(self):
Mario Hock's avatar
Mario Hock committed
71
72
73
74
75
76
        ## •‣∘⁕∗◘☉☀★◾☞☛⦿
        return "◘ Timespan: " + str(self.timespan) +              \
                "\n◘ CPU utilization: " + str(self.cpu_util) +    \
                "\n◘ CPU times: " + str(self.cpu_times) +         \
                "\n◘ RAM: " + str(self.memory) +                  \
                "\n◘ NET: " + str(self.net_io)
Mario Hock's avatar
Mario Hock committed
77
78


79
## TODO: debug and extend
Mario Hock's avatar
Mario Hock committed
80
81
def display(measurement):
    nic = "eth0"
Mario Hock's avatar
Mario Hock committed
82

Mario Hock's avatar
Mario Hock committed
83
84
85
86
87
    print( ", ".join([str(x) + "%" for x in measurement.cpu_util]) )
    print( "[" + nic + "] sent: " + str(measurement.net_io[nic].bytes_sent) +
          ", received: " + str(measurement.net_io[nic].bytes_sent) )

    print
Mario Hock's avatar
Mario Hock committed
88

89
## Take a first (invalid) reading to initialize the system.
Mario Hock's avatar
Mario Hock committed
90
91
init_measure = Measurement()

92
93
94
95
96
97
98
99
100
## Takes a reading and ensures that (at least) |interval| seconds are covered.
#    NOTE: does not handle race conditions (well)
def take_reading(interval = MEASUREMENT_INTERVAL):
    t = interval - Measurement.get_time_since_last_measurement()

    ## sleep if necessary
    if ( t > 0 ):
        time.sleep(t)

Mario Hock's avatar
Mario Hock committed
101
    m = Measurement()
102
103
104
105
106
107
108
109
110
    assert(m.timespan >= interval)

    return m


## XXX TESTING
def test_loop():
    for i in range(10):
        m = take_reading(0.5)
Mario Hock's avatar
Mario Hock committed
111
        display( m )
112
113
114


## XXX TESTING
Mario Hock's avatar
Mario Hock committed
115
test_loop()
Mario Hock's avatar
Mario Hock committed
116
117