File: test_timekeeping.py

package info (click to toggle)
drgn 0.0.33-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,892 kB
  • sloc: python: 59,081; ansic: 51,400; awk: 423; makefile: 339; sh: 113
file content (128 lines) | stat: -rw-r--r-- 4,506 bytes parent folder | download
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# SPDX-License-Identifier: LGPL-2.1-or-later

from pathlib import Path
import time

from drgn import cast
from drgn.helpers.linux.timekeeping import (
    ktime_get_boottime_seconds,
    ktime_get_clocktai_seconds,
    ktime_get_coarse_boottime_ns,
    ktime_get_coarse_clocktai_ns,
    ktime_get_coarse_ns,
    ktime_get_coarse_real_ns,
    ktime_get_real_seconds,
    ktime_get_seconds,
    uptime,
    uptime_pretty,
)
from tests.linux_kernel import LinuxKernelTestCase, skip_unless_have_test_kmod

# These aren't available in the time module as of Python 3.14.
CLOCK_REALTIME_COARSE = getattr(time, "CLOCK_REALTIME_COARSE", 5)
CLOCK_MONOTONIC_COARSE = getattr(time, "CLOCK_MONOTONIC_COARSE", 6)


class TestTimekeeping(LinuxKernelTestCase):
    def assert_in_range(self, a, b, c):
        self.assertTrue(a <= b <= c, f"{b} is not in range [{a}, {c}]")

    def test_ktime_get_seconds(self):
        t1 = int(time.clock_gettime(CLOCK_MONOTONIC_COARSE))
        t2 = ktime_get_seconds(self.prog)
        t3 = int(time.clock_gettime(CLOCK_MONOTONIC_COARSE))

        self.assert_in_range(t1, t2.value_(), t3)
        self.assertIdentical(t2, cast("time64_t", t2))

    def test_ktime_get_coarse_ns(self):
        t1 = time.clock_gettime_ns(CLOCK_MONOTONIC_COARSE)
        t2 = ktime_get_coarse_ns(self.prog)
        t3 = time.clock_gettime_ns(CLOCK_MONOTONIC_COARSE)

        self.assert_in_range(t1, t2.value_(), t3)
        self.assertIdentical(t2, cast("u64", t2))

    def test_ktime_get_real_seconds(self):
        t1 = int(time.clock_gettime(CLOCK_REALTIME_COARSE))
        t2 = ktime_get_real_seconds(self.prog)
        t3 = int(time.clock_gettime(CLOCK_REALTIME_COARSE))

        self.assert_in_range(t1, t2.value_(), t3)
        self.assertIdentical(t2, cast("time64_t", t2))

    def test_ktime_get_coarse_real_ns(self):
        t1 = time.clock_gettime_ns(CLOCK_REALTIME_COARSE)
        t2 = ktime_get_coarse_real_ns(self.prog)
        t3 = time.clock_gettime_ns(CLOCK_REALTIME_COARSE)

        self.assert_in_range(t1, t2.value_(), t3)
        self.assertIdentical(t2, cast("u64", t2))

    @skip_unless_have_test_kmod
    def test_ktime_get_boottime_seconds(self):
        # There is no CLOCK_BOOTTIME_COARSE, so the test module exposes it in
        # sysfs.
        path = Path("/sys/kernel/drgn_test/boottime_seconds")

        t1 = int(path.read_text())
        t2 = ktime_get_boottime_seconds(self.prog)
        t3 = int(path.read_text())

        self.assert_in_range(t1, t2.value_(), t3)
        self.assertIdentical(t2, cast("time64_t", t2))

    @skip_unless_have_test_kmod
    def test_ktime_get_coarse_boottime_ns(self):
        # There is no CLOCK_BOOTTIME_COARSE, so the test module exposes it in
        # sysfs.
        path = Path("/sys/kernel/drgn_test/coarse_boottime_ns")

        t1 = int(path.read_text())
        t2 = ktime_get_coarse_boottime_ns(self.prog)
        t3 = int(path.read_text())

        self.assert_in_range(t1, t2.value_(), t3)
        self.assertIdentical(t2, cast("u64", t2))

    @skip_unless_have_test_kmod
    def test_ktime_get_clocktai_seconds(self):
        # There is no CLOCK_TAI_COARSE, so the test module exposes it in sysfs.
        path = Path("/sys/kernel/drgn_test/clocktai_seconds")

        t1 = int(path.read_text())
        t2 = ktime_get_clocktai_seconds(self.prog)
        t3 = int(path.read_text())

        self.assert_in_range(t1, t2.value_(), t3)
        self.assertIdentical(t2, cast("time64_t", t2))

    @skip_unless_have_test_kmod
    def test_ktime_get_coarse_clocktai_ns(self):
        # There is no CLOCK_TAI_COARSE, so the test module exposes it in sysfs.
        path = Path("/sys/kernel/drgn_test/coarse_clocktai_ns")

        t1 = int(path.read_text())
        t2 = ktime_get_coarse_clocktai_ns(self.prog)
        t3 = int(path.read_text())

        self.assert_in_range(t1, t2.value_(), t3)
        self.assertIdentical(t2, cast("u64", t2))

    @skip_unless_have_test_kmod
    def test_uptime(self):
        # There is no CLOCK_BOOTTIME_COARSE, so the test module exposes it in
        # sysfs.
        path = Path("/sys/kernel/drgn_test/coarse_boottime_ns")

        t1 = int(path.read_text())
        t2 = uptime(self.prog)
        t3 = int(path.read_text())

        self.assert_in_range(t1 / 1e9, t2, t3 / 1e9)
        self.assertIsInstance(t2, float)

    def test_uptime_pretty(self):
        # Just test that it succeeds.
        uptime_pretty(self.prog)