File: TimestampConverter.h

package info (click to toggle)
nvidia-cuda-toolkit 12.4.1-3
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 18,505,836 kB
  • sloc: ansic: 203,477; cpp: 64,769; python: 34,699; javascript: 22,006; xml: 13,410; makefile: 3,085; sh: 2,343; perl: 352
file content (105 lines) | stat: -rw-r--r-- 4,397 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (c) 2018-2019, NVIDIA CORPORATION.  All rights reserved.
 *
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <stdint.h>

struct TimestampConverter
{
    int64_t dstAtSyncPoint;
    int64_t srcAtSyncPoint;
    double scale;

    int64_t operator()(int64_t srcTimestamp) const
    {
        auto srcDelta = srcTimestamp - srcAtSyncPoint;
        auto dstDelta = static_cast<int64_t>(scale * static_cast<double>(srcDelta));
        return dstDelta + dstAtSyncPoint;
    }
};

inline TimestampConverter CreateTimestampConverter(
    int64_t srcStart,
    int64_t srcEnd,
    int64_t dstStart,
    int64_t dstEnd)
{
    auto dstDelta = dstEnd - dstStart;
    auto srcDelta = srcEnd - srcStart;
    double scale = (srcDelta == 0.0) ? 0.0 :
        (static_cast<double>(dstDelta) / static_cast<double>(srcDelta));

    // Any sync point can be used for conversions.  Since we are subtracting the
    // sync point from each timestamp before scaling it, the 53-bit mantissa of
    // double makes our scaling precision about 1 nanosecond per week of distance
    // from the sync point (assuming 1 GHz clocks).  So, accuracy is best near
    // the sync point, samples one week later could be off by 1ns, samples two
    // weeks later by 2ns, etc.  For short captures the choice of sync point is
    // irrelevant, but in a snapshot-based tool where the region of interest is
    // nearer to the end, we should prioritize accuracy at the end highest, so we
    // select the end of capture as our sync point.

    return {dstEnd, srcEnd, scale};
}

// Helper functions for common timestamp-conversion cases:
// - Converting CPU ticks to nanoseconds
// - Creating a new converter that is the reverse of an existing one
// - Creating a new converter that is the composition two existing ones

inline TimestampConverter CpuToNsTimestampConverter(
    int64_t cpuTimestampTicksPerSecond,
    int64_t cpuTimestampStart)
{
    // Source is CPU timestamps, destination is nanoseconds since cpuTimestampStart
    double nsPerCpuTimestampTick =
        static_cast<double>(1'000'000'000) / static_cast<double>(cpuTimestampTicksPerSecond);
    return {0, cpuTimestampStart, nsPerCpuTimestampTick};
}

inline TimestampConverter ReverseConverter(
    TimestampConverter const& in)
{
    // Swap dst & src offsets, and invert scale
    return {in.srcAtSyncPoint, in.dstAtSyncPoint, 1.0 / in.scale};
}

inline TimestampConverter ComposeConverters(
    TimestampConverter const& ab,
    TimestampConverter const& bc)
{
    // ConvAtoB(a) = (a - abSrc) * abScale + abDst
    // ConvBtoC(b) = (b - bcSrc) * bcScale + bcDst
    // So, replacing b in ConvBtoC with ConvAtoB(a), we define ConvAtoC(a):
    //     = (((a - abSrc) * abScale + abDst) - bcSrc) * bcScale + bcDst
    //     = ((a - abSrc) * abScale + (abDst - bcSrc)) * bcScale + bcDst
    //     = (a - abSrc) * abScale * bcScale + (abDst - bcSrc) * bcScale + bcDst
    // ...which fits the form (a - acSrc) * acScale + acDst, using:
    //     acScale = abScale * bcScale
    //     acSrc = abSrc
    //     acDst = bcDst + (abDst - bcSrc) * bcScale
    int64_t acSrc = ab.srcAtSyncPoint;
    int64_t acDst = bc.dstAtSyncPoint +
        static_cast<int64_t>(static_cast<double>(ab.dstAtSyncPoint - bc.srcAtSyncPoint) * bc.scale);
    return {acDst, acSrc, ab.scale * bc.scale};
}