<div dir="ltr"><div dir="ltr">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.</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 <<a href="mailto:keithp@keithp.com">keithp@keithp.com</a>> 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 <<a href="mailto:keithp@keithp.com" target="_blank">keithp@keithp.com</a>><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 <<a href="mailto:keithp@keithp.com" target="_blank">keithp@keithp.com</a>><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 "tapi/t.h"<br>
+#include <time.h><br>
+#include <math.h><br>
+#include <stdio.h><br>
+<br>
+#define GET_DEVICE_FUNCTION_PTR(name) \<br>
+    PFN_vk##name name = (PFN_vk##name)vkGetDeviceProcAddr(t_device, "vk"#name)<br>
+<br>
+#define GET_INSTANCE_FUNCTION_PTR(name) \<br>
+    PFN_vk##name name = (PFN_vk##name)vkGetInstanceProcAddr(t_instance, "vk"#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("VK_EXT_calibrated_timestamps");<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 = "func.calibrated-timestamps.funcs",<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("VK_EXT_calibrated_timestamps");<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>
+        &timeDomainCount,<br>
+        NULL);<br>
+    t_assert(result == VK_SUCCESS);<br>
+    t_assert(timeDomainCount > 0);<br>
+<br>
+    VkTimeDomainEXT *timeDomains = calloc(timeDomainCount, sizeof (VkTimeDomainEXT));<br>
+    t_assert(timeDomains != NULL);<br>
+<br>
+    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(<br>
+        t_physical_dev,<br>
+        &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 < timeDomainCount; d++) {<br>
+        t_assert(VK_TIME_DOMAIN_BEGIN_RANGE_EXT <= timeDomains[d] &&<br>
+                 timeDomains[d] <= VK_TIME_DOMAIN_END_RANGE_EXT);<br>
+    }<br>
+}<br>
+<br>
+test_define {<br>
+    .name = "func.calibrated-timestamps.domains",<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, &current);<br>
+    t_assert (ret >= 0);<br>
+    if (ret < 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("VK_EXT_calibrated_timestamps");<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>
+        &timeDomainCount,<br>
+        NULL);<br>
+    t_assert(result == VK_SUCCESS);<br>
+    t_assert(timeDomainCount > 0);<br>
+<br>
+    VkTimeDomainEXT *timeDomains = calloc(timeDomainCount, sizeof (VkTimeDomainEXT));<br>
+    t_assert(timeDomains != NULL);<br>
+<br>
+    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(<br>
+        t_physical_dev,<br>
+        &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 < 10; i++) {<br>
+        for (uint32_t d = 0; d < 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>
+                    &maxDeviation<br>
+                    );<br>
+                t_assert(result == VK_SUCCESS);<br>
+                after = crucible_clock_gettime(timeDomains[d]);<br>
+                t_assert(before <= timestamps[0]);<br>
+                t_assert(timestamps[0] <= 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(&req, NULL);<br>
+    }<br>
+}<br>
+<br>
+test_define {<br>
+    .name = "func.calibrated-timestamps.monotonic",<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->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 >= 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't drift relative to a<br>
+ * monotonic domain<br>
+ */<br>
+static void<br>
+test_device(void)<br>
+{<br>
+    t_require_ext("VK_EXT_calibrated_timestamps");<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>
+        &timeDomainCount,<br>
+        NULL);<br>
+    t_assert(result == VK_SUCCESS);<br>
+    t_assert(timeDomainCount > 0);<br>
+<br>
+    VkTimeDomainEXT *timeDomains = calloc(timeDomainCount, sizeof (VkTimeDomainEXT));<br>
+    t_assert(timeDomains != NULL);<br>
+<br>
+    result = GetPhysicalDeviceCalibrateableTimeDomainsEXT(<br>
+        t_physical_dev,<br>
+        &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 < 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>
+        &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't drift relative to monotonic time by more than<br>
+     * that promised by the driver<br>
+     */<br>
+    for (uint32_t i = 0; i < 10; i++) {<br>
+        result = GetCalibratedTimestampsEXT(<br>
+            t_device,<br>
+            2,<br>
+            timestampInfo,<br>
+            timestamps,<br>
+            &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 > mono_delta ? device_delta - mono_delta : mono_delta - device_delta;<br>
+<br>
+        t_assert (difference <= max_deviation_start + max_deviation);<br>
+<br>
+        struct timespec req = {<br>
+            .tv_sec = 0,<br>
+            .tv_nsec = 100000000UL<br>
+        };<br>
+        nanosleep(&req, NULL);<br>
+    }<br>
+}<br>
+<br>
+test_define {<br>
+    .name = "func.calibrated-timestamps.device",<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, &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("VK_EXT_calibrated_timestamps");<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 < 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>
+            &device_time_before,<br>
+            &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>
+            &device_time_after,<br>
+            &max_deviation);<br>
+        t_assert (result == VK_SUCCESS);<br>
+<br>
+        printf("before %lu queue %lu (%ld) after %lu (%ld)\n",<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 <= queue_time);<br>
+        t_assert(queue_time <= device_time_after);<br>
+        struct timespec req = {<br>
+            .tv_sec = 0,<br>
+            .tv_nsec = 100000000UL<br>
+        };<br>
+        nanosleep(&req, NULL);<br>
+    }<br>
+}<br>
+<br>
+test_define {<br>
+    .name = "func.calibrated-timestamps.command",<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>