summaryrefslogtreecommitdiff
path: root/virtManager/lib/statsmanager.py
blob: ece130ab2fe90b47bdbb4b1cbc8427c5e5dd6a31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# Copyright (C) 2018 Red Hat, Inc.
#
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.

import re
import time

import libvirt

from virtinst import log

from ..baseclass import vmmGObject


class _VMStatsRecord(object):
    """
    Tracks a set of VM stats for a single timestamp
    """
    def __init__(self, timestamp,
                 cpuTime, cpuTimeAbs,
                 cpuHostPercent, cpuGuestPercent,
                 curmem, currMemPercent,
                 diskRdBytes, diskWrBytes,
                 netRxBytes, netTxBytes):
        self.timestamp = timestamp
        self.cpuTime = cpuTime
        self.cpuTimeAbs = cpuTimeAbs
        self.cpuHostPercent = cpuHostPercent
        self.cpuGuestPercent = cpuGuestPercent
        self.curmem = curmem
        self.currMemPercent = currMemPercent
        self.diskRdKiB = diskRdBytes // 1024
        self.diskWrKiB = diskWrBytes // 1024
        self.netRxKiB = netRxBytes // 1024
        self.netTxKiB = netTxBytes // 1024

        # These are set in _VMStatsList.append_stats
        self.diskRdRate = None
        self.diskWrRate = None
        self.netRxRate = None
        self.netTxRate = None


class _VMStatsList(vmmGObject):
    """
    Tracks a list of VMStatsRecords for a single VM
    """
    def __init__(self):
        vmmGObject.__init__(self)
        self._stats = []

        self.diskRdMaxRate = 10.0
        self.diskWrMaxRate = 10.0
        self.netRxMaxRate = 10.0
        self.netTxMaxRate = 10.0

        self.mem_stats_period_is_set = False
        self.stats_disk_skip = []
        self.stats_net_skip = []

    def _cleanup(self):
        pass

    def append_stats(self, newstats):
        expected = self.config.get_stats_history_length()
        current = len(self._stats)
        if current > expected:  # pragma: no cover
            del self._stats[expected:current]

        def _calculate_rate(record_name):
            ret = 0.0
            if self._stats:
                oldstats = self._stats[0]
                ratediff = (getattr(newstats, record_name) -
                            getattr(oldstats, record_name))
                timediff = newstats.timestamp - oldstats.timestamp
                ret = float(ratediff) / float(timediff)
            return max(ret, 0.0)

        newstats.diskRdRate = _calculate_rate("diskRdKiB")
        newstats.diskWrRate = _calculate_rate("diskWrKiB")
        newstats.netRxRate = _calculate_rate("netRxKiB")
        newstats.netTxRate = _calculate_rate("netTxKiB")

        self.diskRdMaxRate = max(newstats.diskRdRate, self.diskRdMaxRate)
        self.diskWrMaxRate = max(newstats.diskWrRate, self.diskWrMaxRate)
        self.netRxMaxRate = max(newstats.netRxRate, self.netRxMaxRate)
        self.netTxMaxRate = max(newstats.netTxRate, self.netTxMaxRate)

        self._stats.insert(0, newstats)

    def get_record(self, record_name):
        if not self._stats:
            return 0
        return getattr(self._stats[0], record_name)

    def get_vector(self, record_name, limit, ceil=100.0):
        vector = []
        statslen = self.config.get_stats_history_length() + 1
        if limit is not None:
            statslen = min(statslen, limit)

        for i in range(statslen):
            if i < len(self._stats):
                vector.append(getattr(self._stats[i], record_name) / ceil)
            else:
                vector.append(0)
        return vector

    def get_in_out_vector(self, name1, name2, limit, ceil):
        return (self.get_vector(name1, limit, ceil=ceil),
                self.get_vector(name2, limit, ceil=ceil))


class vmmStatsManager(vmmGObject):
    """
    Class for polling statistics
    """
    def __init__(self):
        vmmGObject.__init__(self)
        self._vm_stats = {}
        self._latest_all_stats = {}

        self._all_stats_supported = True
        self._net_stats_supported = True
        self._disk_stats_supported = True
        self._disk_stats_lxc_supported = True
        self._mem_stats_supported = True

    def _cleanup(self):
        for statslist in self._vm_stats.values():
            statslist.cleanup()
        self._latest_all_stats = None


    ######################
    # CPU stats handling #
    ######################

    def _old_cpu_stats_helper(self, vm):
        info = vm.get_backend().info()
        state = info[0]
        guestcpus = info[3]
        cpuTimeAbs = info[4]
        return state, guestcpus, cpuTimeAbs

    def _sample_cpu_stats(self, vm, allstats):
        timestamp = time.time()
        if (not vm.is_active() or
            not self.config.get_stats_enable_cpu_poll()):
            return 0, 0, 0, 0, timestamp

        cpuTime = 0
        cpuHostPercent = 0
        cpuGuestPercent = 0
        prevTimestamp = self.get_vm_statslist(vm).get_record("timestamp")
        prevCpuTime = self.get_vm_statslist(vm).get_record("cpuTimeAbs")

        if allstats:
            state = allstats.get("state.state", 0)
            guestcpus = allstats.get("vcpu.current", 0)
            cpuTimeAbs = allstats.get("cpu.time", 0)
            timestamp = allstats.get("virt-manager.timestamp")
        else:
            state, guestcpus, cpuTimeAbs = self._old_cpu_stats_helper(vm)

        is_offline = (state in [libvirt.VIR_DOMAIN_SHUTOFF,
                                libvirt.VIR_DOMAIN_CRASHED])
        if is_offline:
            guestcpus = 0
            cpuTimeAbs = 0

        cpuTime = cpuTimeAbs - prevCpuTime
        if not is_offline:
            hostcpus = vm.conn.host_active_processor_count()

            pcentbase = (
                    ((cpuTime) * 100.0) /
                    ((timestamp - prevTimestamp) * 1000.0 * 1000.0 * 1000.0))
            cpuHostPercent = pcentbase / hostcpus
            # Under RHEL-5.9 using a XEN HV guestcpus can be 0 during shutdown
            # so play safe and check it.
            cpuGuestPercent = guestcpus > 0 and pcentbase / guestcpus or 0

        cpuHostPercent = max(0.0, min(100.0, cpuHostPercent))
        cpuGuestPercent = max(0.0, min(100.0, cpuGuestPercent))

        return cpuTime, cpuTimeAbs, cpuHostPercent, cpuGuestPercent, timestamp


    ######################
    # net stats handling #
    ######################

    def _old_net_stats_helper(self, vm, dev):
        statslist = self.get_vm_statslist(vm)
        try:
            io = vm.get_backend().interfaceStats(dev)
            if io:
                rx = io[0]
                tx = io[4]
                return rx, tx
        except libvirt.libvirtError as err:  # pragma: no cover
            if vm.conn.support.is_error_nosupport(err):
                log.debug("conn does not support interfaceStats")
                self._net_stats_supported = False
                return 0, 0

            log.debug("Error in interfaceStats for '%s' dev '%s': %s",
                          vm.get_name(), dev, err)
            if vm.is_active():
                log.debug("Adding %s to skip list", dev)
                statslist.stats_net_skip.append(dev)
            else:
                log.debug("Aren't running, don't add to skiplist")

        return 0, 0  # pragma: no cover

    def _sample_net_stats(self, vm, allstats):
        rx = 0
        tx = 0
        statslist = self.get_vm_statslist(vm)
        if (not self._net_stats_supported or
            not vm.is_active() or
            not self.config.get_stats_enable_net_poll()):
            statslist.stats_net_skip = []
            return rx, tx

        if allstats:
            for key in allstats.keys():  # pragma: no cover
                if re.match(r"net.[0-9]+.rx.bytes", key):
                    rx += allstats[key]
                if re.match(r"net.[0-9]+.tx.bytes", key):
                    tx += allstats[key]
            return rx, tx

        for iface in vm.get_interface_devices_norefresh():
            dev = iface.target_dev
            if not dev:
                continue  # pragma: no cover
            if dev in statslist.stats_net_skip:
                continue  # pragma: no cover

            devrx, devtx = self._old_net_stats_helper(vm, dev)
            rx += devrx
            tx += devtx

        return rx, tx


    #######################
    # disk stats handling #
    #######################

    def _old_disk_stats_helper(self, vm, dev):
        statslist = self.get_vm_statslist(vm)
        try:
            io = vm.get_backend().blockStats(dev)
            if io:
                rd = io[1]
                wr = io[3]
                return rd, wr
        except libvirt.libvirtError as err:  # pragma: no cover
            if vm.conn.support.is_error_nosupport(err):
                log.debug("conn does not support blockStats")
                self._disk_stats_supported = False
                return 0, 0

            log.debug("Error in blockStats for '%s' dev '%s': %s",
                          vm.get_name(), dev, err)
            if vm.is_active():
                log.debug("Adding %s to skip list", dev)
                statslist.stats_disk_skip.append(dev)
            else:
                log.debug("Aren't running, don't add to skiplist")

        return 0, 0  # pragma: no cover

    def _sample_disk_stats(self, vm, allstats):
        rd = 0
        wr = 0
        statslist = self.get_vm_statslist(vm)
        if (not self._disk_stats_supported or
            not vm.is_active() or
            not self.config.get_stats_enable_disk_poll()):
            statslist.stats_disk_skip = []
            return rd, wr

        if allstats:
            for key in allstats.keys():
                if re.match(r"block.[0-9]+.rd.bytes", key):
                    rd += allstats[key]
                if re.match(r"block.[0-9]+.wr.bytes", key):
                    wr += allstats[key]
            return rd, wr

        # LXC has a special blockStats method
        if vm.conn.is_lxc() and self._disk_stats_lxc_supported:
            try:
                io = vm.get_backend().blockStats('')
                if io:
                    rd = io[1]
                    wr = io[3]
                    return rd, wr
            except libvirt.libvirtError as e:  # pragma: no cover
                log.debug("LXC style disk stats not supported: %s", e)
                self._disk_stats_lxc_supported = False

        for disk in vm.get_disk_devices_norefresh():
            dev = disk.target
            if not dev:
                continue  # pragma: no cover
            if dev in statslist.stats_disk_skip:
                continue  # pragma: no cover

            diskrd, diskwr = self._old_disk_stats_helper(vm, dev)
            rd += diskrd
            wr += diskwr

        return rd, wr


    #########################
    # memory stats handling #
    #########################

    def _set_mem_stats_period(self, vm):
        # QEMU requires to explicitly enable memory stats polling per VM
        # if we want fine grained memory stats
        if not vm.conn.support.conn_mem_stats_period():
            return

        # Only works for virtio balloon
        if not any([b for b in vm.get_xmlobj().devices.memballoon if
                    b.model == "virtio"]):
            return  # pragma: no cover

        try:
            secs = 5
            vm.get_backend().setMemoryStatsPeriod(secs,
                libvirt.VIR_DOMAIN_AFFECT_LIVE)
        except Exception as e:  # pragma: no cover
            log.debug("Error setting memstats period: %s", e)

    def _old_mem_stats_helper(self, vm):
        totalmem = 1
        curmem = 0
        try:
            stats = vm.get_backend().memoryStats()
            totalmem = stats.get("actual", 1)
            curmem = max(0, totalmem - stats.get("unused", totalmem))
        except libvirt.libvirtError as err:  # pragma: no cover
            if vm.conn.support.is_error_nosupport(err):
                log.debug("conn does not support memoryStats")
                self._mem_stats_supported = False
            else:
                log.debug("Error reading mem stats: %s", err)

        return totalmem, curmem

    def _sample_mem_stats(self, vm, allstats):
        statslist = self.get_vm_statslist(vm)
        if (not self._mem_stats_supported or
            not vm.is_active() or
            not self.config.get_stats_enable_memory_poll()):
            statslist.mem_stats_period_is_set = False
            return 0, 0

        if statslist.mem_stats_period_is_set is False:
            self._set_mem_stats_period(vm)
            statslist.mem_stats_period_is_set = True

        if allstats:
            totalmem = allstats.get("balloon.current", 1)
            curmem = max(0,
                    totalmem - allstats.get("balloon.unused", totalmem))
        else:
            totalmem, curmem = self._old_mem_stats_helper(vm)

        currMemPercent = (curmem / float(totalmem)) * 100
        currMemPercent = max(0.0, min(currMemPercent, 100.0))

        return currMemPercent, curmem


    ####################
    # alltats handling #
    ####################

    def _get_all_stats(self, conn):
        # test conn supports allstats as of 2021, but for test coverage
        # purposes lets still use the old stats code for the test driver
        if not self._all_stats_supported or conn.is_test():
            return {}

        statflags = 0
        if self.config.get_stats_enable_cpu_poll():
            statflags |= libvirt.VIR_DOMAIN_STATS_STATE
            statflags |= libvirt.VIR_DOMAIN_STATS_CPU_TOTAL
            statflags |= libvirt.VIR_DOMAIN_STATS_VCPU
        if self.config.get_stats_enable_memory_poll():
            statflags |= libvirt.VIR_DOMAIN_STATS_BALLOON
        if self.config.get_stats_enable_disk_poll():
            statflags |= libvirt.VIR_DOMAIN_STATS_BLOCK
        if self.config.get_stats_enable_net_poll():
            statflags |= libvirt.VIR_DOMAIN_STATS_INTERFACE
        if statflags == 0:
            return {}  # pragma: no cover

        ret = {}
        try:
            timestamp = time.time()
            rawallstats = conn.get_backend().getAllDomainStats(statflags, 0)

            # Reformat the output to be a bit more friendly
            for dom, domallstats in rawallstats:
                domallstats["virt-manager.timestamp"] = timestamp
                ret[dom.UUIDString()] = domallstats
        except libvirt.libvirtError as err:
            if conn.support.is_error_nosupport(err):
                log.debug("conn does not support getAllDomainStats()")
                self._all_stats_supported = False
            else:  # pragma: no cover
                log.debug("Error call getAllDomainStats(): %s", err)
        return ret


    ##############
    # Public API #
    ##############

    def refresh_vm_stats(self, vm):
        domallstats = self._latest_all_stats.get(vm.get_uuid(), None)

        (cpuTime, cpuTimeAbs, cpuHostPercent, cpuGuestPercent, timestamp) = \
                self._sample_cpu_stats(vm, domallstats)
        currMemPercent, curmem = self._sample_mem_stats(vm, domallstats)
        diskRdBytes, diskWrBytes = self._sample_disk_stats(vm, domallstats)
        netRxBytes, netTxBytes = self._sample_net_stats(vm, domallstats)

        newstats = _VMStatsRecord(
                timestamp, cpuTime, cpuTimeAbs,
                cpuHostPercent, cpuGuestPercent,
                curmem, currMemPercent,
                diskRdBytes, diskWrBytes,
                netRxBytes, netTxBytes)
        self.get_vm_statslist(vm).append_stats(newstats)

    def cache_all_stats(self, conn):
        self._latest_all_stats = self._get_all_stats(conn)

    def get_vm_statslist(self, vm):
        if vm.get_name() not in self._vm_stats:
            self._vm_stats[vm.get_name()] = _VMStatsList()
        return self._vm_stats[vm.get_name()]