Debian Codegraph @

src/libsysprof/sysprof-time-span.h

raw
1 /* sysprof-time-span.h
2 *
3 * Copyright 2023 Christian Hergert <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #pragma once
22
23 #include <glib-object.h>
24
25 #include <sysprof-capture.h>
26
27 G_BEGIN_DECLS
28
29 #define SYSPROF_TYPE_TIME_SPAN (sysprof_time_span_get_type())
30
31 typedef struct _SysprofTimeSpan
32 {
33 gint64 begin_nsec;
34 gint64 end_nsec;
35 } SysprofTimeSpan;
36
37 SYSPROF_AVAILABLE_IN_ALL
38 GType sysprof_time_span_get_type (void) G_GNUC_CONST;
39 SYSPROF_AVAILABLE_IN_ALL
40 SysprofTimeSpan *sysprof_time_span_copy (const SysprofTimeSpan *self);
41 SYSPROF_AVAILABLE_IN_ALL
42 void sysprof_time_span_free (SysprofTimeSpan *self);
43
44 #ifndef __GI_SCANNER__
45 static inline gint64
46 sysprof_time_span_duration (SysprofTimeSpan time_span)
47 {
48 return time_span.end_nsec - time_span.begin_nsec;
49 }
50
51 static inline SysprofTimeSpan
52 sysprof_time_span_relative_to (SysprofTimeSpan time_span,
53 gint64 point)
54 {
55 return (SysprofTimeSpan) {
56 time_span.begin_nsec - point,
57 time_span.end_nsec - point
58 };
59 }
60
61 static inline void
62 sysprof_time_span_normalize (SysprofTimeSpan time_span,
63 SysprofTimeSpan allowed,
64 float values[SYSPROF_RESTRICT 2])
65 {
66 double duration = allowed.end_nsec - allowed.begin_nsec;
67
68 time_span = sysprof_time_span_relative_to (time_span, allowed.begin_nsec);
69
70 values[0] = time_span.begin_nsec / duration;
71 values[1] = time_span.end_nsec / duration;
72 }
73
74 static inline SysprofTimeSpan
75 sysprof_time_span_order (SysprofTimeSpan time_span)
76 {
77 if (time_span.begin_nsec > time_span.end_nsec)
78 return (SysprofTimeSpan) { time_span.end_nsec, time_span.begin_nsec };
79
80 return time_span;
81 }
82
83 static inline gboolean
84 sysprof_time_span_clamp (SysprofTimeSpan *time_span,
85 SysprofTimeSpan allowed)
86 {
87 if (time_span->end_nsec <= allowed.begin_nsec ||
88 time_span->begin_nsec >= allowed.end_nsec)
89 {
90 time_span->begin_nsec = time_span->end_nsec = 0;
91 return FALSE;
92 }
93
94 if (time_span->begin_nsec < allowed.begin_nsec)
95 time_span->begin_nsec = allowed.begin_nsec;
96
97 if (time_span->end_nsec > allowed.end_nsec)
98 time_span->end_nsec = allowed.end_nsec;
99
100 return TRUE;
101 }
102
103 static inline char *
104 sysprof_time_offset_to_string (gint64 o)
105 {
106 char str[32];
107
108 if (o == 0)
109 g_snprintf (str, sizeof str, "%.3lfs", .0);
110 else if (o < 1000000)
111 g_snprintf (str, sizeof str, "%.3lfμs", o/1000.);
112 else if (o < (gint64)SYSPROF_NSEC_PER_SEC)
113 g_snprintf (str, sizeof str, "%.3lfms", o/1000000.);
114 else
115 g_snprintf (str, sizeof str, "%.3lfs", o/(double)SYSPROF_NSEC_PER_SEC);
116
117 return g_strdup (str);
118 }
119
120 static inline char *
121 sysprof_time_span_to_string (const SysprofTimeSpan *span)
122 {
123 g_autofree char *begin = sysprof_time_offset_to_string (span->begin_nsec);
124 g_autofree char *end = NULL;
125
126 if (span->end_nsec == span->begin_nsec)
127 return g_steal_pointer (&begin);
128
129 end = sysprof_time_offset_to_string (span->end_nsec - span->begin_nsec);
130
131 return g_strdup_printf ("%s (%s)", begin, end);
132 }
133
134 static inline gboolean
135 sysprof_time_span_equal (const SysprofTimeSpan *a,
136 const SysprofTimeSpan *b)
137 {
138 if (a == b)
139 return TRUE;
140
141 if (a == NULL || b == NULL)
142 return FALSE;
143
144 return memcmp (a, b, sizeof *a) == 0;
145 }
146 #endif
147
148 G_END_DECLS