diff mbox series

Add tests for VK_EXT_calibrated_timestamps [v2]

Message ID 20181015211515.31787-1-keithp@keithp.com (mailing list archive)
State New, archived
Headers show
Series Add tests for VK_EXT_calibrated_timestamps [v2] | expand

Commit Message

Keith Packard Oct. 15, 2018, 9:15 p.m. UTC
Five tests:

 1) Check for non-null function pointers
 2) Check for in-range time domains
 3) Check monotonic domains for correct values
 4) Check correlation between monotonic and device domains
 5) Check to make sure times in device domain match queue times

Signed-off-by: Keith Packard <keithp@keithp.com>
---
 Makefile.am                            |   1 +
 src/tests/func/calibrated-timestamps.c | 442 +++++++++++++++++++++++++
 2 files changed, 443 insertions(+)
 create mode 100644 src/tests/func/calibrated-timestamps.c

Comments

Jason Ekstrand Oct. 15, 2018, 9:19 p.m. UTC | #1
We're using MRs for crucible.  Please create one and make sure you check
the "Allow commits from members who can merge to the target branch" so it
can be rebased through the UI by someone other than yourself.

--Jason

On Mon, Oct 15, 2018 at 4:15 PM Keith Packard <keithp@keithp.com> wrote:

> Five tests:
>
>  1) Check for non-null function pointers
>  2) Check for in-range time domains
>  3) Check monotonic domains for correct values
>  4) Check correlation between monotonic and device domains
>  5) Check to make sure times in device domain match queue times
>
> Signed-off-by: Keith Packard <keithp@keithp.com>
> ---
>  Makefile.am                            |   1 +
>  src/tests/func/calibrated-timestamps.c | 442 +++++++++++++++++++++++++
>  2 files changed, 443 insertions(+)
>  create mode 100644 src/tests/func/calibrated-timestamps.c
>
> diff --git a/Makefile.am b/Makefile.am
> index 0ca35bd..ba98c60 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -113,6 +113,7 @@ bin_crucible_SOURCES = \
>         src/tests/stress/lots-of-surface-state.c \
>         src/tests/stress/buffer_limit.c \
>         src/tests/self/concurrent-output.c \
> +       src/tests/func/calibrated-timestamps.c \
>         src/util/cru_cleanup.c \
>         src/util/cru_format.c \
>         src/util/cru_image.c \
> diff --git a/src/tests/func/calibrated-timestamps.c
> b/src/tests/func/calibrated-timestamps.c
> new file mode 100644
> index 0000000..a98150b
> --- /dev/null
> +++ b/src/tests/func/calibrated-timestamps.c
> @@ -0,0 +1,442 @@
> +/*
> + * Copyright © 2018 Keith Packard <keithp@keithp.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation, either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful, but
> + * WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * General Public License for more details.
> + */
> +
> +#include "tapi/t.h"
> +#include <time.h>
> +#include <math.h>
> +#include <stdio.h>
> +
> +#define GET_DEVICE_FUNCTION_PTR(name) \
> +    PFN_vk##name name = (PFN_vk##name)vkGetDeviceProcAddr(t_device,
> "vk"#name)
> +
> +#define GET_INSTANCE_FUNCTION_PTR(name) \
> +    PFN_vk##name name = (PFN_vk##name)vkGetInstanceProcAddr(t_instance,
> "vk"#name)
> +
> +/* Test 1: Make sure the function pointers promised by the extension
> + * are valid
> + */
> +static void
> +test_funcs(void)
> +{
> +    t_require_ext("VK_EXT_calibrated_timestamps");
> +
> +
> GET_INSTANCE_FUNCTION_PTR(GetPhysicalDeviceCalibrateableTimeDomainsEXT);
> +    GET_DEVICE_FUNCTION_PTR(GetCalibratedTimestampsEXT);
> +
> +    t_assert(GetPhysicalDeviceCalibrateableTimeDomainsEXT != NULL);
> +    t_assert(GetCalibratedTimestampsEXT != NULL);
> +}
> +
> +test_define {
> +    .name = "func.calibrated-timestamps.funcs",
> +    .start = test_funcs,
> +    .no_image = true,
> +};
> +
> +/* Test 2: Make sure all of the domains offered by the driver are in range
> + */
> +static void
> +test_domains(void)
> +{
> +    t_require_ext("VK_EXT_calibrated_timestamps");
> +
> +
> GET_INSTANCE_FUNCTION_PTR(GetPhysicalDeviceCalibrateableTimeDomainsEXT);
> +    GET_DEVICE_FUNCTION_PTR(GetCalibratedTimestampsEXT);
> +
> +    t_assert(GetPhysicalDeviceCalibrateableTimeDomainsEXT != NULL);
> +    t_assert(GetCalibratedTimestampsEXT != NULL);
> +
> +    VkResult result;
> +
> +    uint32_t timeDomainCount;
> +    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(
> +        t_physical_dev,
> +        &timeDomainCount,
> +        NULL);
> +    t_assert(result == VK_SUCCESS);
> +    t_assert(timeDomainCount > 0);
> +
> +    VkTimeDomainEXT *timeDomains = calloc(timeDomainCount, sizeof
> (VkTimeDomainEXT));
> +    t_assert(timeDomains != NULL);
> +
> +    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(
> +        t_physical_dev,
> +        &timeDomainCount,
> +        timeDomains);
> +
> +    t_assert(result == VK_SUCCESS);
> +
> +    /* Make sure all reported domains are valid */
> +    for (uint32_t d = 0; d < timeDomainCount; d++) {
> +        t_assert(VK_TIME_DOMAIN_BEGIN_RANGE_EXT <= timeDomains[d] &&
> +                 timeDomains[d] <= VK_TIME_DOMAIN_END_RANGE_EXT);
> +    }
> +}
> +
> +test_define {
> +    .name = "func.calibrated-timestamps.domains",
> +    .start = test_domains,
> +    .no_image = true,
> +};
> +
> +static uint64_t
> +crucible_clock_gettime(VkTimeDomainEXT domain)
> +{
> +    struct timespec current;
> +    int ret;
> +    clockid_t clock_id;
> +
> +    switch (domain) {
> +    case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT:
> +        clock_id = CLOCK_MONOTONIC;
> +        break;
> +    case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT:
> +        clock_id = CLOCK_MONOTONIC_RAW;
> +        break;
> +    default:
> +        t_assert(0);
> +        return 0;
> +    }
> +
> +    ret = clock_gettime(clock_id, &current);
> +    t_assert (ret >= 0);
> +    if (ret < 0)
> +        return 0;
> +
> +    return (uint64_t) current.tv_sec * 1000000000ULL + current.tv_nsec;
> +}
> +
> +/* Test 3: Make sure any monotonic domains return accurate data
> + */
> +static void
> +test_monotonic(void)
> +{
> +    t_require_ext("VK_EXT_calibrated_timestamps");
> +
> +
> GET_INSTANCE_FUNCTION_PTR(GetPhysicalDeviceCalibrateableTimeDomainsEXT);
> +    GET_DEVICE_FUNCTION_PTR(GetCalibratedTimestampsEXT);
> +
> +    t_assert(GetPhysicalDeviceCalibrateableTimeDomainsEXT != NULL);
> +    t_assert(GetCalibratedTimestampsEXT != NULL);
> +
> +    VkResult result;
> +
> +    uint32_t timeDomainCount;
> +    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(
> +        t_physical_dev,
> +        &timeDomainCount,
> +        NULL);
> +    t_assert(result == VK_SUCCESS);
> +    t_assert(timeDomainCount > 0);
> +
> +    VkTimeDomainEXT *timeDomains = calloc(timeDomainCount, sizeof
> (VkTimeDomainEXT));
> +    t_assert(timeDomains != NULL);
> +
> +    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(
> +        t_physical_dev,
> +        &timeDomainCount,
> +        timeDomains);
> +
> +    t_assert(result == VK_SUCCESS);
> +
> +    /* Test all monotonic clocks */
> +
> +    VkCalibratedTimestampInfoEXT        *timestampInfo =
> +        calloc(timeDomainCount, sizeof (VkCalibratedTimestampInfoEXT));
> +    t_assert(timestampInfo != NULL);
> +
> +    uint64_t                            *timestamps =
> +        calloc(timeDomainCount, sizeof (uint64_t));
> +    t_assert(timestamps != NULL);
> +
> +    uint64_t                            maxDeviation;
> +
> +    for (uint32_t i = 0; i < 10; i++) {
> +        for (uint32_t d = 0; d < timeDomainCount; d++) {
> +            uint64_t        before, after;
> +
> +            switch (timeDomains[d]) {
> +            case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT:
> +            case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT:
> +                timestampInfo[0] = (VkCalibratedTimestampInfoEXT) {
> +                    .sType =
> VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,
> +                    .pNext = NULL,
> +                    .timeDomain = timeDomains[d]
> +                };
> +                before = crucible_clock_gettime(timeDomains[d]);
> +                result = GetCalibratedTimestampsEXT(
> +                    t_device,
> +                    1,
> +                    timestampInfo,
> +                    timestamps,
> +                    &maxDeviation
> +                    );
> +                t_assert(result == VK_SUCCESS);
> +                after = crucible_clock_gettime(timeDomains[d]);
> +                t_assert(before <= timestamps[0]);
> +                t_assert(timestamps[0] <= after);
> +                break;
> +            default:
> +                break;
> +            }
> +        }
> +
> +        struct timespec req = {
> +            .tv_sec = 0,
> +            .tv_nsec = 100000000UL
> +        };
> +        nanosleep(&req, NULL);
> +    }
> +}
> +
> +test_define {
> +    .name = "func.calibrated-timestamps.monotonic",
> +    .start = test_monotonic,
> +    .no_image = true,
> +};
> +
> +
> +static uint64_t
> +device_time_to_ns(uint64_t device_time)
> +{
> +    float timestamp_period = t_physical_dev_props->limits.timestampPeriod;
> +    long double dt_ld = (long double) device_time;
> +    long double tp_ld = (long double) timestamp_period;
> +    long double ns = dt_ld * tp_ld;
> +
> +    while (ns >= UINT64_MAX)
> +        ns -= UINT64_MAX;
> +
> +    return (uint64_t) floorl(ns + 0.5);
> +}
> +
> +/* Test 4: Make sure the Device domain doesn't drift relative to a
> + * monotonic domain
> + */
> +static void
> +test_device(void)
> +{
> +    t_require_ext("VK_EXT_calibrated_timestamps");
> +
> +
> GET_INSTANCE_FUNCTION_PTR(GetPhysicalDeviceCalibrateableTimeDomainsEXT);
> +    GET_DEVICE_FUNCTION_PTR(GetCalibratedTimestampsEXT);
> +
> +    t_assert(GetPhysicalDeviceCalibrateableTimeDomainsEXT != NULL);
> +    t_assert(GetCalibratedTimestampsEXT != NULL);
> +
> +    VkResult result;
> +
> +    uint32_t timeDomainCount;
> +    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(
> +        t_physical_dev,
> +        &timeDomainCount,
> +        NULL);
> +    t_assert(result == VK_SUCCESS);
> +    t_assert(timeDomainCount > 0);
> +
> +    VkTimeDomainEXT *timeDomains = calloc(timeDomainCount, sizeof
> (VkTimeDomainEXT));
> +    t_assert(timeDomains != NULL);
> +
> +    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(
> +        t_physical_dev,
> +        &timeDomainCount,
> +        timeDomains);
> +
> +    t_assert(result == VK_SUCCESS);
> +
> +    /* Test device clock */
> +
> +
> +    /* Pick a monotonic domain to test against. Prefer MONOTONIC_RAW */
> +    VkTimeDomainEXT     monotonicDomain = VK_TIME_DOMAIN_MAX_ENUM_EXT;
> +    bool                foundDeviceDomain = false;
> +
> +    for (uint32_t d = 0; d < timeDomainCount; d++) {
> +        switch (timeDomains[d]) {
> +        case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT:
> +            if (monotonicDomain == VK_TIME_DOMAIN_MAX_ENUM_EXT)
> +                monotonicDomain = timeDomains[d];
> +            break;
> +        case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT:
> +            monotonicDomain = timeDomains[d];
> +            break;
> +        case VK_TIME_DOMAIN_DEVICE_EXT:
> +            foundDeviceDomain = true;
> +            break;
> +        default:
> +            break;
> +        }
> +    }
> +
> +    t_assert(foundDeviceDomain);
> +    t_assert(monotonicDomain != VK_TIME_DOMAIN_MAX_ENUM_EXT);
> +
> +    VkCalibratedTimestampInfoEXT        timestampInfo[2] = {
> +        {
> +            .sType = VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,
> +            .pNext = NULL,
> +            .timeDomain = VK_TIME_DOMAIN_DEVICE_EXT
> +        },
> +        {
> +            .sType = VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,
> +            .pNext = NULL,
> +            .timeDomain = monotonicDomain,
> +        }
> +    };
> +    uint64_t                            timestamps_start[2];
> +    uint64_t                            device_time_start;
> +    uint64_t                            timestamps[2];
> +    uint64_t                            device_time;
> +    uint64_t                            max_deviation_start;
> +    uint64_t                            max_deviation;
> +
> +    result = GetCalibratedTimestampsEXT(
> +        t_device,
> +        2,
> +        timestampInfo,
> +        timestamps_start,
> +        &max_deviation_start
> +        );
> +    t_assert(result == VK_SUCCESS);
> +
> +    device_time_start = device_time_to_ns(timestamps_start[0]);
> +
> +    /* Make sure device time doesn't drift relative to monotonic time by
> more than
> +     * that promised by the driver
> +     */
> +    for (uint32_t i = 0; i < 10; i++) {
> +        result = GetCalibratedTimestampsEXT(
> +            t_device,
> +            2,
> +            timestampInfo,
> +            timestamps,
> +            &max_deviation
> +            );
> +
> +        t_assert(result == VK_SUCCESS);
> +
> +        device_time = device_time_to_ns(timestamps[0]);
> +
> +        uint64_t        device_delta = device_time - device_time_start;
> +        uint64_t        mono_delta = timestamps[1] - timestamps_start[1];
> +
> +        uint64_t        difference = device_delta > mono_delta ?
> device_delta - mono_delta : mono_delta - device_delta;
> +
> +        t_assert (difference <= max_deviation_start + max_deviation);
> +
> +        struct timespec req = {
> +            .tv_sec = 0,
> +            .tv_nsec = 100000000UL
> +        };
> +        nanosleep(&req, NULL);
> +    }
> +}
> +
> +test_define {
> +    .name = "func.calibrated-timestamps.device",
> +    .start = test_device,
> +    .no_image = true,
> +};
> +
> +static uint64_t
> +get_timestamps(void)
> +{
> +    VkQueryPool pool = qoCreateQueryPool(t_device,
> +                                         .queryType =
> VK_QUERY_TYPE_TIMESTAMP,
> +                                         .queryCount = 1);
> +
> +    VkCommandBuffer cmdBuffer = qoAllocateCommandBuffer(t_device,
> t_cmd_pool);
> +    qoBeginCommandBuffer(cmdBuffer);
> +    vkCmdWriteTimestamp(cmdBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
> pool, 0);
> +    qoEndCommandBuffer(cmdBuffer);
> +
> +    qoQueueSubmit(t_queue, 1, &cmdBuffer, VK_NULL_HANDLE);
> +
> +    qoQueueWaitIdle(t_queue);
> +
> +    uint64_t results[1];
> +    vkGetQueryPoolResults(t_device, pool, 0, 1,
> +                          sizeof(results), results, sizeof (results[0]),
> +                          VK_QUERY_RESULT_64_BIT);
> +    return results[0];
> +}
> +
> +static void
> +test_command(void)
> +{
> +    t_require_ext("VK_EXT_calibrated_timestamps");
> +
> +
> GET_INSTANCE_FUNCTION_PTR(GetPhysicalDeviceCalibrateableTimeDomainsEXT);
> +    GET_DEVICE_FUNCTION_PTR(GetCalibratedTimestampsEXT);
> +
> +    t_assert(GetPhysicalDeviceCalibrateableTimeDomainsEXT != NULL);
> +    t_assert(GetCalibratedTimestampsEXT != NULL);
> +
> +    VkResult result;
> +
> +    VkCalibratedTimestampInfoEXT        timestampInfo[1] = {
> +        {
> +            .sType = VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,
> +            .pNext = NULL,
> +            .timeDomain = VK_TIME_DOMAIN_DEVICE_EXT
> +        },
> +    };
> +
> +    for (uint32_t t = 0; t < 10; t++) {
> +        uint64_t    device_time_before;
> +        uint64_t    device_time_after;
> +        uint64_t    max_deviation;
> +
> +        result = GetCalibratedTimestampsEXT(
> +            t_device,
> +            1,
> +            timestampInfo,
> +            &device_time_before,
> +            &max_deviation);
> +        t_assert (result == VK_SUCCESS);
> +
> +        uint64_t    queue_time;
> +
> +        queue_time = get_timestamps();
> +
> +        result = GetCalibratedTimestampsEXT(
> +            t_device,
> +            1,
> +            timestampInfo,
> +            &device_time_after,
> +            &max_deviation);
> +        t_assert (result == VK_SUCCESS);
> +
> +        printf("before %lu queue %lu (%ld) after %lu (%ld)\n",
> +               device_time_to_ns(device_time_before),
> +               device_time_to_ns(queue_time),
> +               (int64_t) (device_time_to_ns(queue_time) -
> device_time_to_ns(device_time_before)),
> +               device_time_to_ns(device_time_after),
> +               (int64_t) (device_time_to_ns(device_time_after) -
> device_time_to_ns(device_time_before)));
> +
> +        t_assert(device_time_before <= queue_time);
> +        t_assert(queue_time <= device_time_after);
> +        struct timespec req = {
> +            .tv_sec = 0,
> +            .tv_nsec = 100000000UL
> +        };
> +        nanosleep(&req, NULL);
> +    }
> +}
> +
> +test_define {
> +    .name = "func.calibrated-timestamps.command",
> +    .start = test_command,
> +    .no_image = true,
> +};
> --
> 2.19.1
>
> _______________________________________________
> dri-devel mailing list
> dri-devel@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/dri-devel
>
<div dir="ltr"><div dir="ltr">We&#39;re using MRs for crucible.  Please create one and make sure you check the &quot;Allow commits from members who can merge to the target branch&quot; so it can be rebased through the UI by someone other than yourself.</div><div dir="ltr"><br></div><div>--Jason<br></div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Oct 15, 2018 at 4:15 PM Keith Packard &lt;<a href="mailto:keithp@keithp.com">keithp@keithp.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Five tests:<br>
<br>
 1) Check for non-null function pointers<br>
 2) Check for in-range time domains<br>
 3) Check monotonic domains for correct values<br>
 4) Check correlation between monotonic and device domains<br>
 5) Check to make sure times in device domain match queue times<br>
<br>
Signed-off-by: Keith Packard &lt;<a href="mailto:keithp@keithp.com" target="_blank">keithp@keithp.com</a>&gt;<br>
---<br>
 Makefile.am                            |   1 +<br>
 src/tests/func/calibrated-timestamps.c | 442 +++++++++++++++++++++++++<br>
 2 files changed, 443 insertions(+)<br>
 create mode 100644 src/tests/func/calibrated-timestamps.c<br>
<br>
diff --git a/Makefile.am b/Makefile.am<br>
index 0ca35bd..ba98c60 100644<br>
--- a/Makefile.am<br>
+++ b/Makefile.am<br>
@@ -113,6 +113,7 @@ bin_crucible_SOURCES = \<br>
        src/tests/stress/lots-of-surface-state.c \<br>
        src/tests/stress/buffer_limit.c \<br>
        src/tests/self/concurrent-output.c \<br>
+       src/tests/func/calibrated-timestamps.c \<br>
        src/util/cru_cleanup.c \<br>
        src/util/cru_format.c \<br>
        src/util/cru_image.c \<br>
diff --git a/src/tests/func/calibrated-timestamps.c b/src/tests/func/calibrated-timestamps.c<br>
new file mode 100644<br>
index 0000000..a98150b<br>
--- /dev/null<br>
+++ b/src/tests/func/calibrated-timestamps.c<br>
@@ -0,0 +1,442 @@<br>
+/*<br>
+ * Copyright © 2018 Keith Packard &lt;<a href="mailto:keithp@keithp.com" target="_blank">keithp@keithp.com</a>&gt;<br>
+ *<br>
+ * This program is free software; you can redistribute it and/or modify<br>
+ * it under the terms of the GNU General Public License as published by<br>
+ * the Free Software Foundation, either version 2 of the License, or<br>
+ * (at your option) any later version.<br>
+ *<br>
+ * This program is distributed in the hope that it will be useful, but<br>
+ * WITHOUT ANY WARRANTY; without even the implied warranty of<br>
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br>
+ * General Public License for more details.<br>
+ */<br>
+<br>
+#include &quot;tapi/t.h&quot;<br>
+#include &lt;time.h&gt;<br>
+#include &lt;math.h&gt;<br>
+#include &lt;stdio.h&gt;<br>
+<br>
+#define GET_DEVICE_FUNCTION_PTR(name) \<br>
+    PFN_vk##name name = (PFN_vk##name)vkGetDeviceProcAddr(t_device, &quot;vk&quot;#name)<br>
+<br>
+#define GET_INSTANCE_FUNCTION_PTR(name) \<br>
+    PFN_vk##name name = (PFN_vk##name)vkGetInstanceProcAddr(t_instance, &quot;vk&quot;#name)<br>
+<br>
+/* Test 1: Make sure the function pointers promised by the extension<br>
+ * are valid<br>
+ */<br>
+static void<br>
+test_funcs(void)<br>
+{<br>
+    t_require_ext(&quot;VK_EXT_calibrated_timestamps&quot;);<br>
+<br>
+    GET_INSTANCE_FUNCTION_PTR(GetPhysicalDeviceCalibrateableTimeDomainsEXT);<br>
+    GET_DEVICE_FUNCTION_PTR(GetCalibratedTimestampsEXT);<br>
+<br>
+    t_assert(GetPhysicalDeviceCalibrateableTimeDomainsEXT != NULL);<br>
+    t_assert(GetCalibratedTimestampsEXT != NULL);<br>
+}<br>
+<br>
+test_define {<br>
+    .name = &quot;func.calibrated-timestamps.funcs&quot;,<br>
+    .start = test_funcs,<br>
+    .no_image = true,<br>
+};<br>
+<br>
+/* Test 2: Make sure all of the domains offered by the driver are in range<br>
+ */<br>
+static void<br>
+test_domains(void)<br>
+{<br>
+    t_require_ext(&quot;VK_EXT_calibrated_timestamps&quot;);<br>
+<br>
+    GET_INSTANCE_FUNCTION_PTR(GetPhysicalDeviceCalibrateableTimeDomainsEXT);<br>
+    GET_DEVICE_FUNCTION_PTR(GetCalibratedTimestampsEXT);<br>
+<br>
+    t_assert(GetPhysicalDeviceCalibrateableTimeDomainsEXT != NULL);<br>
+    t_assert(GetCalibratedTimestampsEXT != NULL);<br>
+<br>
+    VkResult result;<br>
+<br>
+    uint32_t timeDomainCount;<br>
+    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(<br>
+        t_physical_dev,<br>
+        &amp;timeDomainCount,<br>
+        NULL);<br>
+    t_assert(result == VK_SUCCESS);<br>
+    t_assert(timeDomainCount &gt; 0);<br>
+<br>
+    VkTimeDomainEXT *timeDomains = calloc(timeDomainCount, sizeof (VkTimeDomainEXT));<br>
+    t_assert(timeDomains != NULL);<br>
+<br>
+    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(<br>
+        t_physical_dev,<br>
+        &amp;timeDomainCount,<br>
+        timeDomains);<br>
+<br>
+    t_assert(result == VK_SUCCESS);<br>
+<br>
+    /* Make sure all reported domains are valid */<br>
+    for (uint32_t d = 0; d &lt; timeDomainCount; d++) {<br>
+        t_assert(VK_TIME_DOMAIN_BEGIN_RANGE_EXT &lt;= timeDomains[d] &amp;&amp;<br>
+                 timeDomains[d] &lt;= VK_TIME_DOMAIN_END_RANGE_EXT);<br>
+    }<br>
+}<br>
+<br>
+test_define {<br>
+    .name = &quot;func.calibrated-timestamps.domains&quot;,<br>
+    .start = test_domains,<br>
+    .no_image = true,<br>
+};<br>
+<br>
+static uint64_t<br>
+crucible_clock_gettime(VkTimeDomainEXT domain)<br>
+{<br>
+    struct timespec current;<br>
+    int ret;<br>
+    clockid_t clock_id;<br>
+<br>
+    switch (domain) {<br>
+    case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT:<br>
+        clock_id = CLOCK_MONOTONIC;<br>
+        break;<br>
+    case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT:<br>
+        clock_id = CLOCK_MONOTONIC_RAW;<br>
+        break;<br>
+    default:<br>
+        t_assert(0);<br>
+        return 0;<br>
+    }<br>
+<br>
+    ret = clock_gettime(clock_id, &amp;current);<br>
+    t_assert (ret &gt;= 0);<br>
+    if (ret &lt; 0)<br>
+        return 0;<br>
+<br>
+    return (uint64_t) current.tv_sec * 1000000000ULL + current.tv_nsec;<br>
+}<br>
+<br>
+/* Test 3: Make sure any monotonic domains return accurate data<br>
+ */<br>
+static void<br>
+test_monotonic(void)<br>
+{<br>
+    t_require_ext(&quot;VK_EXT_calibrated_timestamps&quot;);<br>
+<br>
+    GET_INSTANCE_FUNCTION_PTR(GetPhysicalDeviceCalibrateableTimeDomainsEXT);<br>
+    GET_DEVICE_FUNCTION_PTR(GetCalibratedTimestampsEXT);<br>
+<br>
+    t_assert(GetPhysicalDeviceCalibrateableTimeDomainsEXT != NULL);<br>
+    t_assert(GetCalibratedTimestampsEXT != NULL);<br>
+<br>
+    VkResult result;<br>
+<br>
+    uint32_t timeDomainCount;<br>
+    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(<br>
+        t_physical_dev,<br>
+        &amp;timeDomainCount,<br>
+        NULL);<br>
+    t_assert(result == VK_SUCCESS);<br>
+    t_assert(timeDomainCount &gt; 0);<br>
+<br>
+    VkTimeDomainEXT *timeDomains = calloc(timeDomainCount, sizeof (VkTimeDomainEXT));<br>
+    t_assert(timeDomains != NULL);<br>
+<br>
+    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(<br>
+        t_physical_dev,<br>
+        &amp;timeDomainCount,<br>
+        timeDomains);<br>
+<br>
+    t_assert(result == VK_SUCCESS);<br>
+<br>
+    /* Test all monotonic clocks */<br>
+<br>
+    VkCalibratedTimestampInfoEXT        *timestampInfo =<br>
+        calloc(timeDomainCount, sizeof (VkCalibratedTimestampInfoEXT));<br>
+    t_assert(timestampInfo != NULL);<br>
+<br>
+    uint64_t                            *timestamps =<br>
+        calloc(timeDomainCount, sizeof (uint64_t));<br>
+    t_assert(timestamps != NULL);<br>
+<br>
+    uint64_t                            maxDeviation;<br>
+<br>
+    for (uint32_t i = 0; i &lt; 10; i++) {<br>
+        for (uint32_t d = 0; d &lt; timeDomainCount; d++) {<br>
+            uint64_t        before, after;<br>
+<br>
+            switch (timeDomains[d]) {<br>
+            case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT:<br>
+            case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT:<br>
+                timestampInfo[0] = (VkCalibratedTimestampInfoEXT) {<br>
+                    .sType = VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,<br>
+                    .pNext = NULL,<br>
+                    .timeDomain = timeDomains[d]<br>
+                };<br>
+                before = crucible_clock_gettime(timeDomains[d]);<br>
+                result = GetCalibratedTimestampsEXT(<br>
+                    t_device,<br>
+                    1,<br>
+                    timestampInfo,<br>
+                    timestamps,<br>
+                    &amp;maxDeviation<br>
+                    );<br>
+                t_assert(result == VK_SUCCESS);<br>
+                after = crucible_clock_gettime(timeDomains[d]);<br>
+                t_assert(before &lt;= timestamps[0]);<br>
+                t_assert(timestamps[0] &lt;= after);<br>
+                break;<br>
+            default:<br>
+                break;<br>
+            }<br>
+        }<br>
+<br>
+        struct timespec req = {<br>
+            .tv_sec = 0,<br>
+            .tv_nsec = 100000000UL<br>
+        };<br>
+        nanosleep(&amp;req, NULL);<br>
+    }<br>
+}<br>
+<br>
+test_define {<br>
+    .name = &quot;func.calibrated-timestamps.monotonic&quot;,<br>
+    .start = test_monotonic,<br>
+    .no_image = true,<br>
+};<br>
+<br>
+<br>
+static uint64_t<br>
+device_time_to_ns(uint64_t device_time)<br>
+{<br>
+    float timestamp_period = t_physical_dev_props-&gt;limits.timestampPeriod;<br>
+    long double dt_ld = (long double) device_time;<br>
+    long double tp_ld = (long double) timestamp_period;<br>
+    long double ns = dt_ld * tp_ld;<br>
+<br>
+    while (ns &gt;= UINT64_MAX)<br>
+        ns -= UINT64_MAX;<br>
+<br>
+    return (uint64_t) floorl(ns + 0.5);<br>
+}<br>
+<br>
+/* Test 4: Make sure the Device domain doesn&#39;t drift relative to a<br>
+ * monotonic domain<br>
+ */<br>
+static void<br>
+test_device(void)<br>
+{<br>
+    t_require_ext(&quot;VK_EXT_calibrated_timestamps&quot;);<br>
+<br>
+    GET_INSTANCE_FUNCTION_PTR(GetPhysicalDeviceCalibrateableTimeDomainsEXT);<br>
+    GET_DEVICE_FUNCTION_PTR(GetCalibratedTimestampsEXT);<br>
+<br>
+    t_assert(GetPhysicalDeviceCalibrateableTimeDomainsEXT != NULL);<br>
+    t_assert(GetCalibratedTimestampsEXT != NULL);<br>
+<br>
+    VkResult result;<br>
+<br>
+    uint32_t timeDomainCount;<br>
+    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(<br>
+        t_physical_dev,<br>
+        &amp;timeDomainCount,<br>
+        NULL);<br>
+    t_assert(result == VK_SUCCESS);<br>
+    t_assert(timeDomainCount &gt; 0);<br>
+<br>
+    VkTimeDomainEXT *timeDomains = calloc(timeDomainCount, sizeof (VkTimeDomainEXT));<br>
+    t_assert(timeDomains != NULL);<br>
+<br>
+    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(<br>
+        t_physical_dev,<br>
+        &amp;timeDomainCount,<br>
+        timeDomains);<br>
+<br>
+    t_assert(result == VK_SUCCESS);<br>
+<br>
+    /* Test device clock */<br>
+<br>
+<br>
+    /* Pick a monotonic domain to test against. Prefer MONOTONIC_RAW */<br>
+    VkTimeDomainEXT     monotonicDomain = VK_TIME_DOMAIN_MAX_ENUM_EXT;<br>
+    bool                foundDeviceDomain = false;<br>
+<br>
+    for (uint32_t d = 0; d &lt; timeDomainCount; d++) {<br>
+        switch (timeDomains[d]) {<br>
+        case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT:<br>
+            if (monotonicDomain == VK_TIME_DOMAIN_MAX_ENUM_EXT)<br>
+                monotonicDomain = timeDomains[d];<br>
+            break;<br>
+        case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT:<br>
+            monotonicDomain = timeDomains[d];<br>
+            break;<br>
+        case VK_TIME_DOMAIN_DEVICE_EXT:<br>
+            foundDeviceDomain = true;<br>
+            break;<br>
+        default:<br>
+            break;<br>
+        }<br>
+    }<br>
+<br>
+    t_assert(foundDeviceDomain);<br>
+    t_assert(monotonicDomain != VK_TIME_DOMAIN_MAX_ENUM_EXT);<br>
+<br>
+    VkCalibratedTimestampInfoEXT        timestampInfo[2] = {<br>
+        {<br>
+            .sType = VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,<br>
+            .pNext = NULL,<br>
+            .timeDomain = VK_TIME_DOMAIN_DEVICE_EXT<br>
+        },<br>
+        {<br>
+            .sType = VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,<br>
+            .pNext = NULL,<br>
+            .timeDomain = monotonicDomain,<br>
+        }<br>
+    };<br>
+    uint64_t                            timestamps_start[2];<br>
+    uint64_t                            device_time_start;<br>
+    uint64_t                            timestamps[2];<br>
+    uint64_t                            device_time;<br>
+    uint64_t                            max_deviation_start;<br>
+    uint64_t                            max_deviation;<br>
+<br>
+    result = GetCalibratedTimestampsEXT(<br>
+        t_device,<br>
+        2,<br>
+        timestampInfo,<br>
+        timestamps_start,<br>
+        &amp;max_deviation_start<br>
+        );<br>
+    t_assert(result == VK_SUCCESS);<br>
+<br>
+    device_time_start = device_time_to_ns(timestamps_start[0]);<br>
+<br>
+    /* Make sure device time doesn&#39;t drift relative to monotonic time by more than<br>
+     * that promised by the driver<br>
+     */<br>
+    for (uint32_t i = 0; i &lt; 10; i++) {<br>
+        result = GetCalibratedTimestampsEXT(<br>
+            t_device,<br>
+            2,<br>
+            timestampInfo,<br>
+            timestamps,<br>
+            &amp;max_deviation<br>
+            );<br>
+<br>
+        t_assert(result == VK_SUCCESS);<br>
+<br>
+        device_time = device_time_to_ns(timestamps[0]);<br>
+<br>
+        uint64_t        device_delta = device_time - device_time_start;<br>
+        uint64_t        mono_delta = timestamps[1] - timestamps_start[1];<br>
+<br>
+        uint64_t        difference = device_delta &gt; mono_delta ? device_delta - mono_delta : mono_delta - device_delta;<br>
+<br>
+        t_assert (difference &lt;= max_deviation_start + max_deviation);<br>
+<br>
+        struct timespec req = {<br>
+            .tv_sec = 0,<br>
+            .tv_nsec = 100000000UL<br>
+        };<br>
+        nanosleep(&amp;req, NULL);<br>
+    }<br>
+}<br>
+<br>
+test_define {<br>
+    .name = &quot;func.calibrated-timestamps.device&quot;,<br>
+    .start = test_device,<br>
+    .no_image = true,<br>
+};<br>
+<br>
+static uint64_t<br>
+get_timestamps(void)<br>
+{<br>
+    VkQueryPool pool = qoCreateQueryPool(t_device,<br>
+                                         .queryType = VK_QUERY_TYPE_TIMESTAMP,<br>
+                                         .queryCount = 1);<br>
+<br>
+    VkCommandBuffer cmdBuffer = qoAllocateCommandBuffer(t_device, t_cmd_pool);<br>
+    qoBeginCommandBuffer(cmdBuffer);<br>
+    vkCmdWriteTimestamp(cmdBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, pool, 0);<br>
+    qoEndCommandBuffer(cmdBuffer);<br>
+<br>
+    qoQueueSubmit(t_queue, 1, &amp;cmdBuffer, VK_NULL_HANDLE);<br>
+<br>
+    qoQueueWaitIdle(t_queue);<br>
+<br>
+    uint64_t results[1];<br>
+    vkGetQueryPoolResults(t_device, pool, 0, 1,<br>
+                          sizeof(results), results, sizeof (results[0]),<br>
+                          VK_QUERY_RESULT_64_BIT);<br>
+    return results[0];<br>
+}<br>
+<br>
+static void<br>
+test_command(void)<br>
+{<br>
+    t_require_ext(&quot;VK_EXT_calibrated_timestamps&quot;);<br>
+<br>
+    GET_INSTANCE_FUNCTION_PTR(GetPhysicalDeviceCalibrateableTimeDomainsEXT);<br>
+    GET_DEVICE_FUNCTION_PTR(GetCalibratedTimestampsEXT);<br>
+<br>
+    t_assert(GetPhysicalDeviceCalibrateableTimeDomainsEXT != NULL);<br>
+    t_assert(GetCalibratedTimestampsEXT != NULL);<br>
+<br>
+    VkResult result;<br>
+<br>
+    VkCalibratedTimestampInfoEXT        timestampInfo[1] = {<br>
+        {<br>
+            .sType = VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,<br>
+            .pNext = NULL,<br>
+            .timeDomain = VK_TIME_DOMAIN_DEVICE_EXT<br>
+        },<br>
+    };<br>
+<br>
+    for (uint32_t t = 0; t &lt; 10; t++) {<br>
+        uint64_t    device_time_before;<br>
+        uint64_t    device_time_after;<br>
+        uint64_t    max_deviation;<br>
+<br>
+        result = GetCalibratedTimestampsEXT(<br>
+            t_device,<br>
+            1,<br>
+            timestampInfo,<br>
+            &amp;device_time_before,<br>
+            &amp;max_deviation);<br>
+        t_assert (result == VK_SUCCESS);<br>
+<br>
+        uint64_t    queue_time;<br>
+<br>
+        queue_time = get_timestamps();<br>
+<br>
+        result = GetCalibratedTimestampsEXT(<br>
+            t_device,<br>
+            1,<br>
+            timestampInfo,<br>
+            &amp;device_time_after,<br>
+            &amp;max_deviation);<br>
+        t_assert (result == VK_SUCCESS);<br>
+<br>
+        printf(&quot;before %lu queue %lu (%ld) after %lu (%ld)\n&quot;,<br>
+               device_time_to_ns(device_time_before),<br>
+               device_time_to_ns(queue_time),<br>
+               (int64_t) (device_time_to_ns(queue_time) - device_time_to_ns(device_time_before)),<br>
+               device_time_to_ns(device_time_after),<br>
+               (int64_t) (device_time_to_ns(device_time_after) - device_time_to_ns(device_time_before)));<br>
+<br>
+        t_assert(device_time_before &lt;= queue_time);<br>
+        t_assert(queue_time &lt;= device_time_after);<br>
+        struct timespec req = {<br>
+            .tv_sec = 0,<br>
+            .tv_nsec = 100000000UL<br>
+        };<br>
+        nanosleep(&amp;req, NULL);<br>
+    }<br>
+}<br>
+<br>
+test_define {<br>
+    .name = &quot;func.calibrated-timestamps.command&quot;,<br>
+    .start = test_command,<br>
+    .no_image = true,<br>
+};<br>
-- <br>
2.19.1<br>
<br>
_______________________________________________<br>
dri-devel mailing list<br>
<a href="mailto:dri-devel@lists.freedesktop.org" target="_blank">dri-devel@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/dri-devel" rel="noreferrer" target="_blank">https://lists.freedesktop.org/mailman/listinfo/dri-devel</a><br>
</blockquote></div>
Keith Packard Oct. 15, 2018, 9:23 p.m. UTC | #2
Jason Ekstrand <jason@jlekstrand.net> writes:

> We're using MRs for crucible.  Please create one and make sure you check
> the "Allow commits from members who can merge to the target branch" so it
> can be rebased through the UI by someone other than yourself.

OOo. Shiny!
diff mbox series

Patch

diff --git a/Makefile.am b/Makefile.am
index 0ca35bd..ba98c60 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -113,6 +113,7 @@  bin_crucible_SOURCES = \
 	src/tests/stress/lots-of-surface-state.c \
 	src/tests/stress/buffer_limit.c \
 	src/tests/self/concurrent-output.c \
+	src/tests/func/calibrated-timestamps.c \
 	src/util/cru_cleanup.c \
 	src/util/cru_format.c \
 	src/util/cru_image.c \
diff --git a/src/tests/func/calibrated-timestamps.c b/src/tests/func/calibrated-timestamps.c
new file mode 100644
index 0000000..a98150b
--- /dev/null
+++ b/src/tests/func/calibrated-timestamps.c
@@ -0,0 +1,442 @@ 
+/*
+ * Copyright © 2018 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+
+#include "tapi/t.h"
+#include <time.h>
+#include <math.h>
+#include <stdio.h>
+
+#define GET_DEVICE_FUNCTION_PTR(name) \
+    PFN_vk##name name = (PFN_vk##name)vkGetDeviceProcAddr(t_device, "vk"#name)
+
+#define GET_INSTANCE_FUNCTION_PTR(name) \
+    PFN_vk##name name = (PFN_vk##name)vkGetInstanceProcAddr(t_instance, "vk"#name)
+
+/* Test 1: Make sure the function pointers promised by the extension
+ * are valid
+ */
+static void
+test_funcs(void)
+{
+    t_require_ext("VK_EXT_calibrated_timestamps");
+
+    GET_INSTANCE_FUNCTION_PTR(GetPhysicalDeviceCalibrateableTimeDomainsEXT);
+    GET_DEVICE_FUNCTION_PTR(GetCalibratedTimestampsEXT);
+
+    t_assert(GetPhysicalDeviceCalibrateableTimeDomainsEXT != NULL);
+    t_assert(GetCalibratedTimestampsEXT != NULL);
+}
+
+test_define {
+    .name = "func.calibrated-timestamps.funcs",
+    .start = test_funcs,
+    .no_image = true,
+};
+
+/* Test 2: Make sure all of the domains offered by the driver are in range
+ */
+static void
+test_domains(void)
+{
+    t_require_ext("VK_EXT_calibrated_timestamps");
+
+    GET_INSTANCE_FUNCTION_PTR(GetPhysicalDeviceCalibrateableTimeDomainsEXT);
+    GET_DEVICE_FUNCTION_PTR(GetCalibratedTimestampsEXT);
+
+    t_assert(GetPhysicalDeviceCalibrateableTimeDomainsEXT != NULL);
+    t_assert(GetCalibratedTimestampsEXT != NULL);
+
+    VkResult result;
+
+    uint32_t timeDomainCount;
+    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(
+        t_physical_dev,
+        &timeDomainCount,
+        NULL);
+    t_assert(result == VK_SUCCESS);
+    t_assert(timeDomainCount > 0);
+
+    VkTimeDomainEXT *timeDomains = calloc(timeDomainCount, sizeof (VkTimeDomainEXT));
+    t_assert(timeDomains != NULL);
+
+    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(
+        t_physical_dev,
+        &timeDomainCount,
+        timeDomains);
+
+    t_assert(result == VK_SUCCESS);
+
+    /* Make sure all reported domains are valid */
+    for (uint32_t d = 0; d < timeDomainCount; d++) {
+        t_assert(VK_TIME_DOMAIN_BEGIN_RANGE_EXT <= timeDomains[d] &&
+                 timeDomains[d] <= VK_TIME_DOMAIN_END_RANGE_EXT);
+    }
+}
+
+test_define {
+    .name = "func.calibrated-timestamps.domains",
+    .start = test_domains,
+    .no_image = true,
+};
+
+static uint64_t
+crucible_clock_gettime(VkTimeDomainEXT domain)
+{
+    struct timespec current;
+    int ret;
+    clockid_t clock_id;
+
+    switch (domain) {
+    case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT:
+        clock_id = CLOCK_MONOTONIC;
+        break;
+    case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT:
+        clock_id = CLOCK_MONOTONIC_RAW;
+        break;
+    default:
+        t_assert(0);
+        return 0;
+    }
+
+    ret = clock_gettime(clock_id, &current);
+    t_assert (ret >= 0);
+    if (ret < 0)
+        return 0;
+
+    return (uint64_t) current.tv_sec * 1000000000ULL + current.tv_nsec;
+}
+
+/* Test 3: Make sure any monotonic domains return accurate data
+ */
+static void
+test_monotonic(void)
+{
+    t_require_ext("VK_EXT_calibrated_timestamps");
+
+    GET_INSTANCE_FUNCTION_PTR(GetPhysicalDeviceCalibrateableTimeDomainsEXT);
+    GET_DEVICE_FUNCTION_PTR(GetCalibratedTimestampsEXT);
+
+    t_assert(GetPhysicalDeviceCalibrateableTimeDomainsEXT != NULL);
+    t_assert(GetCalibratedTimestampsEXT != NULL);
+
+    VkResult result;
+
+    uint32_t timeDomainCount;
+    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(
+        t_physical_dev,
+        &timeDomainCount,
+        NULL);
+    t_assert(result == VK_SUCCESS);
+    t_assert(timeDomainCount > 0);
+
+    VkTimeDomainEXT *timeDomains = calloc(timeDomainCount, sizeof (VkTimeDomainEXT));
+    t_assert(timeDomains != NULL);
+
+    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(
+        t_physical_dev,
+        &timeDomainCount,
+        timeDomains);
+
+    t_assert(result == VK_SUCCESS);
+
+    /* Test all monotonic clocks */
+
+    VkCalibratedTimestampInfoEXT        *timestampInfo =
+        calloc(timeDomainCount, sizeof (VkCalibratedTimestampInfoEXT));
+    t_assert(timestampInfo != NULL);
+
+    uint64_t                            *timestamps =
+        calloc(timeDomainCount, sizeof (uint64_t));
+    t_assert(timestamps != NULL);
+
+    uint64_t                            maxDeviation;
+
+    for (uint32_t i = 0; i < 10; i++) {
+        for (uint32_t d = 0; d < timeDomainCount; d++) {
+            uint64_t        before, after;
+
+            switch (timeDomains[d]) {
+            case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT:
+            case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT:
+                timestampInfo[0] = (VkCalibratedTimestampInfoEXT) {
+                    .sType = VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,
+                    .pNext = NULL,
+                    .timeDomain = timeDomains[d]
+                };
+                before = crucible_clock_gettime(timeDomains[d]);
+                result = GetCalibratedTimestampsEXT(
+                    t_device,
+                    1,
+                    timestampInfo,
+                    timestamps,
+                    &maxDeviation
+                    );
+                t_assert(result == VK_SUCCESS);
+                after = crucible_clock_gettime(timeDomains[d]);
+                t_assert(before <= timestamps[0]);
+                t_assert(timestamps[0] <= after);
+                break;
+            default:
+                break;
+            }
+        }
+
+        struct timespec req = {
+            .tv_sec = 0,
+            .tv_nsec = 100000000UL
+        };
+        nanosleep(&req, NULL);
+    }
+}
+
+test_define {
+    .name = "func.calibrated-timestamps.monotonic",
+    .start = test_monotonic,
+    .no_image = true,
+};
+
+
+static uint64_t
+device_time_to_ns(uint64_t device_time)
+{
+    float timestamp_period = t_physical_dev_props->limits.timestampPeriod;
+    long double dt_ld = (long double) device_time;
+    long double tp_ld = (long double) timestamp_period;
+    long double ns = dt_ld * tp_ld;
+
+    while (ns >= UINT64_MAX)
+        ns -= UINT64_MAX;
+
+    return (uint64_t) floorl(ns + 0.5);
+}
+
+/* Test 4: Make sure the Device domain doesn't drift relative to a
+ * monotonic domain
+ */
+static void
+test_device(void)
+{
+    t_require_ext("VK_EXT_calibrated_timestamps");
+
+    GET_INSTANCE_FUNCTION_PTR(GetPhysicalDeviceCalibrateableTimeDomainsEXT);
+    GET_DEVICE_FUNCTION_PTR(GetCalibratedTimestampsEXT);
+
+    t_assert(GetPhysicalDeviceCalibrateableTimeDomainsEXT != NULL);
+    t_assert(GetCalibratedTimestampsEXT != NULL);
+
+    VkResult result;
+
+    uint32_t timeDomainCount;
+    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(
+        t_physical_dev,
+        &timeDomainCount,
+        NULL);
+    t_assert(result == VK_SUCCESS);
+    t_assert(timeDomainCount > 0);
+
+    VkTimeDomainEXT *timeDomains = calloc(timeDomainCount, sizeof (VkTimeDomainEXT));
+    t_assert(timeDomains != NULL);
+
+    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(
+        t_physical_dev,
+        &timeDomainCount,
+        timeDomains);
+
+    t_assert(result == VK_SUCCESS);
+
+    /* Test device clock */
+
+
+    /* Pick a monotonic domain to test against. Prefer MONOTONIC_RAW */
+    VkTimeDomainEXT     monotonicDomain = VK_TIME_DOMAIN_MAX_ENUM_EXT;
+    bool                foundDeviceDomain = false;
+
+    for (uint32_t d = 0; d < timeDomainCount; d++) {
+        switch (timeDomains[d]) {
+        case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT:
+            if (monotonicDomain == VK_TIME_DOMAIN_MAX_ENUM_EXT)
+                monotonicDomain = timeDomains[d];
+            break;
+        case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT:
+            monotonicDomain = timeDomains[d];
+            break;
+        case VK_TIME_DOMAIN_DEVICE_EXT:
+            foundDeviceDomain = true;
+            break;
+        default:
+            break;
+        }
+    }
+
+    t_assert(foundDeviceDomain);
+    t_assert(monotonicDomain != VK_TIME_DOMAIN_MAX_ENUM_EXT);
+
+    VkCalibratedTimestampInfoEXT        timestampInfo[2] = {
+        {
+            .sType = VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,
+            .pNext = NULL,
+            .timeDomain = VK_TIME_DOMAIN_DEVICE_EXT
+        },
+        {
+            .sType = VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,
+            .pNext = NULL,
+            .timeDomain = monotonicDomain,
+        }
+    };
+    uint64_t                            timestamps_start[2];
+    uint64_t                            device_time_start;
+    uint64_t                            timestamps[2];
+    uint64_t                            device_time;
+    uint64_t                            max_deviation_start;
+    uint64_t                            max_deviation;
+
+    result = GetCalibratedTimestampsEXT(
+        t_device,
+        2,
+        timestampInfo,
+        timestamps_start,
+        &max_deviation_start
+        );
+    t_assert(result == VK_SUCCESS);
+
+    device_time_start = device_time_to_ns(timestamps_start[0]);
+
+    /* Make sure device time doesn't drift relative to monotonic time by more than
+     * that promised by the driver
+     */
+    for (uint32_t i = 0; i < 10; i++) {
+        result = GetCalibratedTimestampsEXT(
+            t_device,
+            2,
+            timestampInfo,
+            timestamps,
+            &max_deviation
+            );
+
+        t_assert(result == VK_SUCCESS);
+
+        device_time = device_time_to_ns(timestamps[0]);
+
+        uint64_t        device_delta = device_time - device_time_start;
+        uint64_t        mono_delta = timestamps[1] - timestamps_start[1];
+
+        uint64_t        difference = device_delta > mono_delta ? device_delta - mono_delta : mono_delta - device_delta;
+
+        t_assert (difference <= max_deviation_start + max_deviation);
+
+        struct timespec req = {
+            .tv_sec = 0,
+            .tv_nsec = 100000000UL
+        };
+        nanosleep(&req, NULL);
+    }
+}
+
+test_define {
+    .name = "func.calibrated-timestamps.device",
+    .start = test_device,
+    .no_image = true,
+};
+
+static uint64_t
+get_timestamps(void)
+{
+    VkQueryPool pool = qoCreateQueryPool(t_device,
+                                         .queryType = VK_QUERY_TYPE_TIMESTAMP,
+                                         .queryCount = 1);
+
+    VkCommandBuffer cmdBuffer = qoAllocateCommandBuffer(t_device, t_cmd_pool);
+    qoBeginCommandBuffer(cmdBuffer);
+    vkCmdWriteTimestamp(cmdBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, pool, 0);
+    qoEndCommandBuffer(cmdBuffer);
+
+    qoQueueSubmit(t_queue, 1, &cmdBuffer, VK_NULL_HANDLE);
+
+    qoQueueWaitIdle(t_queue);
+
+    uint64_t results[1];
+    vkGetQueryPoolResults(t_device, pool, 0, 1,
+                          sizeof(results), results, sizeof (results[0]),
+                          VK_QUERY_RESULT_64_BIT);
+    return results[0];
+}
+
+static void
+test_command(void)
+{
+    t_require_ext("VK_EXT_calibrated_timestamps");
+
+    GET_INSTANCE_FUNCTION_PTR(GetPhysicalDeviceCalibrateableTimeDomainsEXT);
+    GET_DEVICE_FUNCTION_PTR(GetCalibratedTimestampsEXT);
+
+    t_assert(GetPhysicalDeviceCalibrateableTimeDomainsEXT != NULL);
+    t_assert(GetCalibratedTimestampsEXT != NULL);
+
+    VkResult result;
+
+    VkCalibratedTimestampInfoEXT        timestampInfo[1] = {
+        {
+            .sType = VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,
+            .pNext = NULL,
+            .timeDomain = VK_TIME_DOMAIN_DEVICE_EXT
+        },
+    };
+
+    for (uint32_t t = 0; t < 10; t++) {
+        uint64_t    device_time_before;
+        uint64_t    device_time_after;
+        uint64_t    max_deviation;
+
+        result = GetCalibratedTimestampsEXT(
+            t_device,
+            1,
+            timestampInfo,
+            &device_time_before,
+            &max_deviation);
+        t_assert (result == VK_SUCCESS);
+
+        uint64_t    queue_time;
+
+        queue_time = get_timestamps();
+
+        result = GetCalibratedTimestampsEXT(
+            t_device,
+            1,
+            timestampInfo,
+            &device_time_after,
+            &max_deviation);
+        t_assert (result == VK_SUCCESS);
+
+        printf("before %lu queue %lu (%ld) after %lu (%ld)\n",
+               device_time_to_ns(device_time_before),
+               device_time_to_ns(queue_time),
+               (int64_t) (device_time_to_ns(queue_time) - device_time_to_ns(device_time_before)),
+               device_time_to_ns(device_time_after),
+               (int64_t) (device_time_to_ns(device_time_after) - device_time_to_ns(device_time_before)));
+
+        t_assert(device_time_before <= queue_time);
+        t_assert(queue_time <= device_time_after);
+        struct timespec req = {
+            .tv_sec = 0,
+            .tv_nsec = 100000000UL
+        };
+        nanosleep(&req, NULL);
+    }
+}
+
+test_define {
+    .name = "func.calibrated-timestamps.command",
+    .start = test_command,
+    .no_image = true,
+};