GCC Code Coverage Report


Directory: ./
File: tests/unit/05_api/api_talp_nvtx_00.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 2 2 100.0%
Functions: 1 1 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2026 Barcelona Supercomputing Center */
3 /* */
4 /* This file is part of the DLB library. */
5 /* */
6 /* DLB is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU Lesser General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* DLB is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU Lesser General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU Lesser General Public License */
17 /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */
18 /*********************************************************************************/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #ifdef HAVE_NVTX3_NVTOOLSEXT_H
25
26 #include "unique_shmem.h"
27
28 #include "apis/dlb.h"
29 #include "apis/dlb_talp.h"
30 #include "talp/regions.h"
31
32 #include <nvtx3/nvToolsExt.h>
33
34 #include <assert.h>
35 #include <pthread.h>
36 #include <string.h>
37
38 /* The test is statically linked with DLB, so NVTX attaches to it through the
39 * static injection symbol. Nothing else references DLB's NVTX object, so
40 * reference its entry point to make sure the linker pulls it from the archive. */
41 int InitializeInjectionNvtx2(NvtxGetExportTableFunc_t getExportTable);
42 static void (*volatile force_nvtx_injection_link)(void) =
43 (void (*)(void))InitializeInjectionNvtx2;
44
45 static nvtxEventAttributes_t registered_attr(nvtxStringHandle_t string) {
46 nvtxEventAttributes_t attr = {0};
47 attr.version = NVTX_VERSION;
48 attr.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE;
49 attr.messageType = NVTX_MESSAGE_TYPE_REGISTERED;
50 attr.message.registered = string;
51 return attr;
52 }
53
54 static nvtxEventAttributes_t ascii_attr(const char *message) {
55 nvtxEventAttributes_t attr = {0};
56 attr.version = NVTX_VERSION;
57 attr.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE;
58 attr.messageType = NVTX_MESSAGE_TYPE_ASCII;
59 attr.message.ascii = message;
60 return attr;
61 }
62
63 static void* unknown_thread(void *arg) {
64 /* Unknown threads cannot start regions, but push/pop must be balanced */
65 assert( nvtxRangePushA("thread") == 0 );
66 assert( nvtxRangePop() == 0 );
67 return NULL;
68 }
69
70 int main(int argc, char *argv[]) {
71
72 /* NVTX ranges as TALP regions */
73 {
74 char options[64] = "--talp --shm-key=";
75 strcat(options, SHMEM_KEY);
76 assert( DLB_Init(0, NULL, options) == DLB_SUCCESS );
77
78 /* Pop without push */
79 assert( nvtxRangePop() < 0 );
80
81 /* Simple range */
82 assert( nvtxRangePushA("a") == 0 );
83 dlb_monitor_t *region_a = DLB_MonitoringRegionRegister("a");
84 assert( region_a != NULL );
85 assert( region_is_started(region_a) );
86 assert( nvtxRangePop() == 0 );
87 assert( !region_is_started(region_a) );
88 assert( region_a->num_measurements == 1 );
89
90 /* Recursive range with the same name: only the outermost one counts */
91 assert( nvtxRangePushA("a") == 0 );
92 assert( nvtxRangePushA("a") == 1 );
93 assert( nvtxRangePop() == 1 );
94 assert( region_is_started(region_a) );
95 assert( nvtxRangePop() == 0 );
96 assert( !region_is_started(region_a) );
97 assert( region_a->num_measurements == 2 );
98
99 /* nvtxRangePushEx */
100 nvtxEventAttributes_t attr = ascii_attr("b");
101 assert( nvtxRangePushEx(&attr) == 0 );
102 assert( nvtxRangePop() == 0 );
103 dlb_monitor_t *region_b = DLB_MonitoringRegionRegister("b");
104 assert( region_b->num_measurements == 1 );
105
106 /* Domains: region names are prefixed with the domain name */
107 nvtxDomainHandle_t domain = nvtxDomainCreateA("lib");
108 assert( domain != NULL );
109 nvtxStringHandle_t string = nvtxDomainRegisterStringA(domain, "k");
110 assert( string != NULL );
111
112 /* Pop on a domain without any range */
113 assert( nvtxDomainRangePop(domain) < 0 );
114
115 /* Domains and the default domain have independent stacks */
116 assert( nvtxRangePushA("outer") == 0 );
117 attr = registered_attr(string);
118 assert( nvtxDomainRangePushEx(domain, &attr) == 0 );
119 attr = ascii_attr("m");
120 assert( nvtxDomainRangePushEx(domain, &attr) == 1 );
121 dlb_monitor_t *region_outer = DLB_MonitoringRegionRegister("outer");
122 dlb_monitor_t *region_lib_k = DLB_MonitoringRegionRegister("lib/k");
123 dlb_monitor_t *region_lib_m = DLB_MonitoringRegionRegister("lib/m");
124 assert( region_is_started(region_outer) );
125 assert( region_is_started(region_lib_k) );
126 assert( region_is_started(region_lib_m) );
127 assert( nvtxRangePop() == 0 );
128 assert( !region_is_started(region_outer) );
129 assert( region_is_started(region_lib_k) );
130 assert( nvtxDomainRangePop(domain) == 1 );
131 assert( !region_is_started(region_lib_m) );
132 assert( nvtxDomainRangePop(domain) == 0 );
133 assert( !region_is_started(region_lib_k) );
134 assert( nvtxDomainRangePop(domain) < 0 );
135 assert( region_outer->num_measurements == 1 );
136 assert( region_lib_k->num_measurements == 1 );
137 assert( region_lib_m->num_measurements == 1 );
138 nvtxDomainDestroy(domain);
139
140 /* Destroy a domain that never pushed any range */
141 nvtxDomainHandle_t unused_domain = nvtxDomainCreateA("unused");
142 assert( unused_domain != NULL );
143 nvtxDomainDestroy(unused_domain);
144
145 /* Deep nesting, beyond the initial stack capacity */
146 enum { DEPTH = 100 };
147 for (int i = 0; i < DEPTH; ++i) {
148 assert( nvtxRangePushA("deep") == i );
149 }
150 for (int i = DEPTH - 1; i >= 0; --i) {
151 assert( nvtxRangePop() == i );
152 }
153 assert( DLB_MonitoringRegionRegister("deep")->num_measurements == 1 );
154
155 /* Long recursion of the same range, collapsed into a single entry */
156 enum { RECURSION = 1000 };
157 dlb_monitor_t *region_rec = DLB_MonitoringRegionRegister("rec");
158 for (int i = 0; i < RECURSION; ++i) {
159 assert( nvtxRangePushA("rec") == i );
160 }
161 for (int i = RECURSION - 1; i >= 0; --i) {
162 assert( region_is_started(region_rec) );
163 assert( nvtxRangePop() == i );
164 }
165 assert( !region_is_started(region_rec) );
166 assert( region_rec->num_measurements == 1 );
167
168 /* Interleaved recursion: the inner range must not close the outer one */
169 dlb_monitor_t *region_x = DLB_MonitoringRegionRegister("x");
170 dlb_monitor_t *region_y = DLB_MonitoringRegionRegister("y");
171 assert( nvtxRangePushA("x") == 0 );
172 assert( nvtxRangePushA("y") == 1 );
173 assert( nvtxRangePushA("x") == 2 );
174 assert( nvtxRangePop() == 2 );
175 assert( region_is_started(region_x) );
176 assert( region_is_started(region_y) );
177 assert( nvtxRangePop() == 1 );
178 assert( region_is_started(region_x) );
179 assert( !region_is_started(region_y) );
180 assert( nvtxRangePop() == 0 );
181 assert( !region_is_started(region_x) );
182 assert( region_x->num_measurements == 1 );
183 assert( region_y->num_measurements == 1 );
184
185 /* Unicode messages are not supported, but are still balanced */
186 attr = (nvtxEventAttributes_t) {
187 .version = NVTX_VERSION,
188 .size = NVTX_EVENT_ATTRIB_STRUCT_SIZE,
189 .messageType = NVTX_MESSAGE_TYPE_UNICODE,
190 .message.unicode = L"unicode",
191 };
192 assert( nvtxRangePushEx(&attr) == 0 );
193 assert( nvtxRangePop() == 0 );
194
195 /* Unknown threads */
196 pthread_t thread;
197 pthread_create(&thread, NULL, unknown_thread, NULL);
198 pthread_join(thread, NULL);
199 dlb_monitor_t *region_thread = DLB_MonitoringRegionRegister("thread");
200 assert( region_thread->num_measurements == 0 );
201
202 assert( DLB_Finalize() == DLB_SUCCESS );
203 }
204
205 /* DLB initialized without TALP: ranges are ignored but still balanced */
206 {
207 char options[64] = "--shm-key=";
208 strcat(options, SHMEM_KEY);
209 assert( DLB_Init(0, NULL, options) == DLB_SUCCESS );
210
211 assert( nvtxRangePushA("a") == 0 );
212 assert( nvtxRangePop() == 0 );
213 assert( DLB_MonitoringRegionRegister("a") == NULL );
214
215 assert( DLB_Finalize() == DLB_SUCCESS );
216 }
217
218 /* TALP initialized again */
219 {
220 char options[64] = "--talp --shm-key=";
221 strcat(options, SHMEM_KEY);
222 assert( DLB_Init(0, NULL, options) == DLB_SUCCESS );
223
224 assert( nvtxRangePushA("a") == 0 );
225 assert( nvtxRangePop() == 0 );
226 dlb_monitor_t *region_a = DLB_MonitoringRegionRegister("a");
227 assert( region_a != NULL );
228 assert( region_a->num_measurements == 1 );
229
230 assert( DLB_Finalize() == DLB_SUCCESS );
231 }
232
233 /* DLB not initialized */
234 {
235 assert( nvtxRangePushA("a") == 0 );
236 assert( nvtxRangePop() == 0 );
237 }
238
239 return 0;
240 }
241
242 #else
243
244 1 int main(int argc, char *argv[]) {
245 1 return 77;
246 }
247
248 #endif /* HAVE_NVTX3_NVTOOLSEXT_H */
249