summaryrefslogtreecommitdiff
path: root/tests/kmstest.py
blob: 4bd4e0ad27a6cd9d42b9c18034dc04efe29dfe06 (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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#!/usr/bin/python3
# SPDX-License-Identifier: GPL-2.0-or-later
# SPDX-FileCopyrightText: 2017-2019 Renesas Electronics Corporation

import collections.abc
import errno
import fcntl
import os
import pykms
import selectors
import sys
import time

class Timer(object):
    def __init__(self, timeout, callback):
        self.timeout = time.clock_gettime(time.CLOCK_MONOTONIC) + timeout
        self.callback = callback


class EventLoop(selectors.DefaultSelector):
    def __init__(self):
        super().__init__()
        self.__timers = []

    def add_timer(self, timeout, callback):
        self.__timers.append(Timer(timeout, callback))
        self.__timers.sort(key=lambda timer: timer.timeout)

    def fire_timers(self):
        clk = time.clock_gettime(time.CLOCK_MONOTONIC)
        while len(self.__timers) > 0:
            timer = self.__timers[0]
            if timer.timeout > clk:
                break

            del self.__timers[0]
            timer.callback()

    def next_timeout(self):
        clk = time.clock_gettime(time.CLOCK_MONOTONIC)
        if len(self.__timers) == 0 or self.__timers[0].timeout < clk:
            return None

        return self.__timers[0].timeout - clk

    def run(self, duration=0):
        if duration:
            self.add_timer(duration, self.stop)

        timeout = self.next_timeout()

        self._stop = False
        while not self._stop:
            for key, events in self.select(timeout):
                key.data(key.fileobj, events)
            self.fire_timers()

        self.__timers = []

    def stop(self):
        self._stop = True


class KernelLogMessage(object):
    def __init__(self, msg):
        pos = msg.find(';')
        header = msg[:pos]
        msg = msg[pos+1:]

        facility, sequence, timestamp, *other = header.split(',')
        self.facility = int(facility)
        self.sequence = int(sequence)
        self.timestamp = int(timestamp) / 1000000.

        msg = msg.split('\n')
        self.msg = msg[0]
        self.tags = {}

        try:
            tags = msg[1:-1]
            for tag in tags:
                tag = tag.strip().split('=')
                self.tags[tag[0]] = tag[1]
        except:
            pass


class KernelLogReader(object):
    def __init__(self):
        self.kmsg = os.open('/dev/kmsg', 0)
        flags = fcntl.fcntl(self.kmsg, fcntl.F_GETFL)
        fcntl.fcntl(self.kmsg, fcntl.F_SETFL, flags | os.O_NONBLOCK)
        os.lseek(self.kmsg, 0, os.SEEK_END)

    def __del__(self):
        os.close(self.kmsg)

    def read(self):
        msgs = []
        while True:
            try:
                msg = os.read(self.kmsg, 8191)
                msg = msg.decode('utf-8')
            except OSError as e:
                if e.errno == errno.EAGAIN:
                    break
                else:
                    raise e
            msgs.append(KernelLogMessage(msg))

        return msgs


class Logger(object):
    def __init__(self, name):
        self.logfile = open(f'{name}.log', 'w')
        self._kmsg = KernelLogReader()

    def __del__(self):
        self.close()

    def close(self):
        if self.logfile:
            # Capture the last kernel messages.
            self.event()
            self.logfile.close()
            self.logfile = None

    def event(self):
        kmsgs = self._kmsg.read()
        for msg in kmsgs:
            self.logfile.write(f'K [{msg.timestamp:6f} {msg.msg}\n')
        self.logfile.flush()

    @property
    def fd(self):
        return self._kmsg.kmsg

    def flush(self):
        self.logfile.flush()
        os.fsync(self.logfile)

    def log(self, msg):
        # Start by processing the kernel log as there might not be any event
        # loop running.
        self.event()

        now = time.clock_gettime(time.CLOCK_MONOTONIC)
        self.logfile.write(f'U [{now:6f}] {msg}\n')
        self.logfile.flush()


class CRC(object):
    def __init__(self, crc):
        if crc.startswith('XXXXXXXXXX'):
            self.frame = None
        else:
            self.frame = int(crc[:10], 16)

        crc = crc[11:].strip('\n\0').split(' ')
        self.crcs = [int(c, 16) for c in crc]


class CRCReader(object):

    MAX_CRC_ENTRIES = 10
    MAX_LINE_LEN  = 10 + 11 * MAX_CRC_ENTRIES + 1

    def __init__(self, crtc):
        self.pipe = crtc.idx
        self.ctrl = -1
        self.dir = -1
        self.data = -1

        # Hardcode the device minor to 0 as the KMSTest constructor opens the
        # default card object.
        self.dir = os.open(f'/sys/kernel/debug/dri/0/crtc-{self.pipe}/crc', 0)
        self.ctrl = os.open('control', os.O_WRONLY, dir_fd = self.dir)

    def __del__(self):
        self.stop()
        if self.ctrl != -1:
            os.close(self.ctrl)
        if self.dir != -1:
            os.close(self.dir)

    def start(self, source):
        os.write(self.ctrl, source.encode('ascii'))
        self.data = os.open('data', os.O_RDONLY, dir_fd = self.dir)

    def stop(self):
        if self.data != -1:
            os.close(self.data)
            self.data = -1

    def read(self, num_entries=1):
        crcs = []
        while len(crcs) < num_entries:
            try:
                crc = os.read(self.data, CRCReader.MAX_LINE_LEN)
                crc = crc.decode('ascii')
            except OSError as e:
                if e.errno == errno.EAGAIN:
                    break
                else:
                    raise e
            crcs.append(CRC(crc))

        return crcs


class Dist(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __repr__(self):
        return f'({self.x},{self.y})'


class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __repr__(self):
        return f'({self.x},{self.y})'

    def move(self, distance):
        self.x += distance.x
        self.y += distance.y


class Size(object):
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def __repr__(self):
        return f'{self.width}x{self.height}'


class Rect(object):
    def __init__(self, left, top, width, height):
        self.left = left
        self.top = top
        self.width = width
        self.height = height

    def __repr__(self):
        return f'({self.left},{self.top})/{self.width}x{self.height}'

    def isEmpty(self):
        """Check if the rectangle has a zero width or height"""
        return self.width == 0 or self.height == 0


class AtomicRequest(pykms.AtomicReq):
    """pymkms.AtomicReq wrapper to track state changes"""
    def __init__(self, test):
        super().__init__(test.card)
        self.__test = test
        self.__props = {}

    def __format_props(self, obj, props):
        out = {}
        for k, v in props.items():
            if isinstance(v, str):
                if v.endswith('%'):
                    prop = obj.get_prop(k)
                    if not prop:
                        raise RuntimeError(f'Property {k} not supported by object {obj}')
                    if prop.type not in (pykms.PropertyType.Range, pykms.PropertyType.SignedRange):
                        raise RuntimeError(f'Unsupported property type {prop.type} for value {v}')

                    min, max = prop.values
                    v = min + int((max - min) * int(v[:-1]) / 100)
                elif v.isnumeric():
                    v = int(v)
                else:
                    prop = obj.get_prop(k)
                    if not prop:
                        raise RuntimeError(f'Property {k} not supported by object {obj}')
                    if prop.type != pykms.PropertyType.Enum:
                        raise RuntimeError(f'Unsupported property type {prop.type} for value {v}')
                    for value, name in prop.enums.items():
                        if name == v:
                            v = value
                            break
                    else:
                        raise RuntimeError(f'Enum value with name "{v}" not found in property {k}')

            if not isinstance(v, int):
                raise RuntimeError(f'Unsupported value type {type(v)} for property {k}')

            # Convert negative values to a 64-bit unsigned integer as required
            # by the bindings for AtomicRequest::add().
            out[k] = v & ((1 << 64) - 1)
        return out

    def add(self, obj, *kwargs):
        if obj.id not in self.__props:
            self.__props[obj.id] = {}
        obj_props = self.__props[obj.id]

        if len(kwargs) == 1 and isinstance(kwargs[0], collections.abc.Mapping):
            props = self.__format_props(obj, kwargs[0])
        elif len(kwargs) == 2:
            props = self.__format_props(obj, { kwargs[0]: kwargs[1] })

        obj_props.update(props)

        super().add(obj, props)

    def commit(self, data=0, allow_modeset=False):
        ret = super().commit(data, allow_modeset)
        if ret == 0:
            self.__test._props.update(self.__props)
        return ret

    def commit_sync(self, allow_modeset=False):
        ret = super().commit_sync(allow_modeset)
        if ret == 0:
            self.__test._props.update(self.__props)
        return ret

    def __repr__(self):
        return repr(self.__props)


class KMSTest(object):
    def __init__(self, use_default_key_handler=False):
        if not getattr(self, 'main', None):
            raise RuntimeError('Test class must implement main method')

        self.card = pykms.Card()
        if not self.card.has_atomic:
            raise RuntimeError("Device doesn't support the atomic API")

        self._props = {}

        logname = self.__class__.__name__
        self.logger = Logger(logname)

        self.loop = EventLoop()
        self.loop.register(self.logger.fd, selectors.EVENT_READ, self.__read_logger)
        self.loop.register(self.card.fd, selectors.EVENT_READ, self.__read_event)
        if use_default_key_handler:
            self.loop.register(sys.stdin, selectors.EVENT_READ, self.__read_key)

    def __enter__(self):
        return self

    def __exit__(self, *err):
        self.card = None
        self.loop.close()
        self.logger.close()

    def __del__(self):
        self.card = None
        self.loop.close()
        self.logger.close()

    def atomic_crtc_disable(self, crtc, sync=True):
        req = AtomicRequest(self)
        req.add(crtc, {'ACTIVE': 0, 'MODE_ID': 0})
        for connector in self.card.connectors:
            if connector.id in self._props:
                props = self._props[connector.id]
                try:
                    if props['CRTC_ID'] == crtc.id:
                        req.add(connector, 'CRTC_ID', 0)
                except KeyError:
                    pass
        for plane in self.card.planes:
            if plane.id in self._props:
                props = self._props[plane.id]
                try:
                    if props['CRTC_ID'] == crtc.id:
                        req.add(plane, {'CRTC_ID': 0, 'FB_ID': 0})
                except KeyError:
                    pass
        if sync:
            return req.commit_sync(True)
        else:
            return req.commit(0, True)

    def atomic_crtc_mode_set(self, crtc, connector, mode, fb=None, sync=False):
        """Perform a mode set on the given connector and CRTC. The framebuffer,
        if present, will be output on the primary plane. Otherwise no plane is
        configured for the CRTC."""

        # Mode blobs are reference-counted, make sure the blob stays valid until
        # the commit completes.
        mode_blob = mode.to_blob(self.card)

        req = AtomicRequest(self)
        req.add(connector, 'CRTC_ID', crtc.id)
        req.add(crtc, { 'ACTIVE': 1, 'MODE_ID': mode_blob.id })
        if fb:
            req.add(crtc.primary_plane, {
                        'FB_ID': fb.id,
                        'CRTC_ID': crtc.id,
                        'SRC_X': 0,
                        'SRC_Y': 0,
                        'SRC_W': int(fb.width * 65536),
                        'SRC_H': int(fb.height * 65536),
                        'CRTC_X': 0,
                        'CRTC_Y': 0,
                        'CRTC_W': fb.width,
                        'CRTC_H': fb.height,
            })
        if sync:
            return req.commit_sync(True)
        else:
            return req.commit(0, True)

    def atomic_plane_set(self, plane, crtc, source, destination, fb, alpha=None,
                         zpos=None, blendmode=None, sync=False):
        req = AtomicRequest(self)
        req.add(plane, {
                    'FB_ID': fb.id,
                    'CRTC_ID': crtc.id,
                    'SRC_X': int(source.left * 65536),
                    'SRC_Y': int(source.top * 65536),
                    'SRC_W': int(source.width * 65536),
                    'SRC_H': int(source.height * 65536),
                    'CRTC_X': destination.left,
                    'CRTC_Y': destination.top,
                    'CRTC_W': destination.width,
                    'CRTC_H': destination.height,
        })
        if alpha is not None:
            req.add(plane, 'alpha', alpha)
        if zpos is not None:
            req.add(plane, 'zpos', zpos)
        if blendmode is not None:
            req.add(plane, 'pixel blend mode', blendmode)
        if sync:
            return req.commit_sync()
        else:
            return req.commit(0)

    def atomic_plane_disable(self, plane, sync=True):
        req = AtomicRequest(self)
        req.add(plane, { 'FB_ID': 0, 'CRTC_ID': 0 })

        if sync:
            return req.commit_sync()
        else:
            return req.commit(0)

    def atomic_planes_disable(self, sync=True):
        req = AtomicRequest(self)
        for plane in self.card.planes:
            req.add(plane, { 'FB_ID': 0, 'CRTC_ID': 0 })

        if sync:
            return req.commit_sync()
        else:
            return req.commit(0)

    def output_connectors(self):
        for connector in self.card.connectors:
            if connector.fullname.startswith('writeback-'):
                continue
            yield connector

    def __handle_page_flip(self, frame, time):
        self.flips += 1
        try:
            # The handle_page_flip() method is optional, ignore attribute errors
            self.handle_page_flip(frame, time)
        except AttributeError:
            pass

    def __read_event(self, fileobj, events):
        for event in self.card.read_events():
            if event.type == pykms.DrmEventType.FLIP_COMPLETE:
                self.__handle_page_flip(event.seq, event.time)

    def __read_logger(self, fileobj, events):
        self.logger.event()

    def __read_key(self, fileobj, events):
        sys.stdin.readline()
        self.loop.stop()

    def execute(self):
        """Execute the test by running the main function."""
        self.main()

    def flush_events(self):
        """Discard all pending DRM events."""

        # Temporarily switch to non-blocking I/O to read events, as there might
        # be no event pending.
        flags = fcntl.fcntl(self.card.fd, fcntl.F_GETFL)
        fcntl.fcntl(self.card.fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)

        # read_events() is a generator so we have to go through all events
        # explicitly. Ignore -EAGAIN errors, they're expected in non-blocking
        # I/O mode.
        try:
            for event in self.card.read_events():
                pass
        except OSError as e:
            if e.errno != errno.EAGAIN:
                raise e

        fcntl.fcntl(self.card.fd, fcntl.F_SETFL, flags)

    def run(self, duration):
        """Run the event loop for the given duration (in seconds)."""
        self.flips = 0
        self.loop.run(duration)

    def start(self, name):
        """Start a test."""
        self.test_name = name
        self.logger.log(f'Testing {name}')
        sys.stdout.write(f'Testing {name}: ')
        sys.stdout.flush()

    def progress(self, current, maximum):
        sys.stdout.write(f'\rTesting {self.test_name}: {current}/{maximum}')
        sys.stdout.flush()

    def fail(self, reason):
        """Complete a test with failure."""
        self.logger.log(f'Test failed. Reason: {reason}')
        self.logger.flush()
        sys.stdout.write(f'\rTesting {self.test_name}: FAIL\n')
        sys.stdout.flush()

    def skip(self, reason):
        """Complete a test with skip."""
        self.logger.log(f'Test skipped. Reason: {reason}')
        self.logger.flush()
        sys.stdout.write('SKIP\n')
        sys.stdout.flush()

    def success(self):
        """Complete a test with success."""
        self.logger.log('Test completed successfully')
        self.logger.flush()
        sys.stdout.write(f'\rTesting {self.test_name}: SUCCESS\n')
        sys.stdout.flush()


if __name__ == '__main__':
    import importlib
    import inspect
    import os

    files = []
    for path in os.scandir():
        if path.is_file() and path.name.startswith('kms-test-') and path.name.endswith('.py'):
            files.append(path.name)

    files.sort()
    for file in files:
        print(f'- {file}')
        module = importlib.import_module(file[:-3])
        tests = []
        for name in dir(module):
            obj = getattr(module, name)
            if not isinstance(obj, type):
                continue

            if 'KMSTest' in [cls.__name__ for cls in inspect.getmro(obj)]:
                tests.append(obj)

        for test in tests:
            # Use a context manager to ensure proper cleanup after each test,
            # otherwise state from one test may leak to the other based on when
            # objects end up being deleted.
            with test() as test:
                test.execute()