GCC Code Coverage Report


Directory: ./
File: tests/unit/04_core/talp_02.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 103 104 99.0%
Functions: 4 4 100.0%
Branches: 29 58 50.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 #include "unique_shmem.h"
21
22 #include "LB_core/DLB_kernel.h"
23 #include "LB_core/node_barrier.h"
24 #include "LB_core/spd.h"
25 #include "LB_core/thread_ctx.h"
26 #include "LB_comm/shmem_procinfo.h"
27 #include "apis/dlb_talp.h"
28 #include "apis/dlb_errors.h"
29 #include "support/atomic.h"
30 #include "support/mask_utils.h"
31 #include "support/mytime.h"
32 #include "talp/regions.h"
33 #include "talp/sample.h"
34 #include "talp/talp.h"
35 #include "talp/talp_mpi.h"
36 #include "talp/talp_types.h"
37
38 #include <float.h>
39 #include <pthread.h>
40 #include <sched.h>
41 #include <stdint.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <assert.h>
45
46 /* Test TALP node metrics */
47
48
49 1 static int cmp_node_metrics(const dlb_node_metrics_t *node_metrics1,
50 const dlb_node_metrics_t *node_metrics2) {
51
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (node_metrics1->node_id == node_metrics2->node_id
52
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 && node_metrics1->processes_per_node == node_metrics2->processes_per_node
53
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 && node_metrics1->total_useful_time == node_metrics2->total_useful_time
54
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 && node_metrics1->total_mpi_time == node_metrics2->total_mpi_time
55
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 && node_metrics1->max_useful_time == node_metrics2->max_useful_time
56
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 && node_metrics1->max_mpi_time == node_metrics2->max_mpi_time
57
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 && node_metrics1->load_balance - node_metrics2->load_balance < FLT_EPSILON
58
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 && node_metrics1->parallel_efficiency - node_metrics2->parallel_efficiency < FLT_EPSILON
59 ) {
60 1 return 0;
61 }
62 ✗ return 1;
63 }
64
65 1 static void print_node_metrics(const dlb_node_metrics_t *node_metrics) {
66 1 printf( "Node id: %d\n"
67 "Processes per node: %d\n"
68 "Total useful time: %"PRId64"\n"
69 "Total MPI time: %"PRId64"\n"
70 "Max useful time: %"PRId64"\n"
71 "Max MPI time: %"PRId64"\n"
72 "Load Balance: %f\n"
73 "Parallel Eff.: %f\n",
74 1 node_metrics->node_id,
75 1 node_metrics->processes_per_node,
76 1 node_metrics->total_useful_time,
77 1 node_metrics->total_mpi_time,
78 1 node_metrics->max_useful_time,
79 1 node_metrics->max_mpi_time,
80 1 node_metrics->load_balance,
81 1 node_metrics->parallel_efficiency
82 );
83 1 }
84
85 1 static void* observer_func(void *arg) {
86 1 subprocess_descriptor_t *spd = arg;
87 1 spd_enter_dlb(spd);
88
89 /* Set up observer flag */
90 1 thread_ctx_set_observer(true);
91
92 /* Get global region */
93 1 const dlb_monitor_t *global_monitor = region_get_global(spd);
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( global_monitor != NULL );
95 1 int num_measurements = global_monitor->num_measurements;
96
97 /* An observer may call MPI functions without affecting TALP */
98 1 sync_call_flags_t blocking = { .is_blocking = true, .is_collective = true };
99 1 talp_into_sync_call(spd, blocking);
100 1 talp_out_of_sync_call(spd, blocking);
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( num_measurements == global_monitor->num_measurements );
102
103 /* An observer may not start/stop/update regions */
104
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( region_start(spd, DLB_GLOBAL_REGION) == DLB_ERR_PERM );
105
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( region_stop(spd, DLB_GLOBAL_REGION) == DLB_ERR_PERM );
106
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( talp_aggregate_samples_to_regions(spd->talp_info) == DLB_ERR_PERM );
107
108 /* Get MPI metrics */
109 dlb_node_metrics_t node_metrics;
110
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( talp_collect_pop_node_metrics(spd, DLB_GLOBAL_REGION, &node_metrics) == DLB_SUCCESS );
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( node_metrics.processes_per_node == 1 );
112
113 1 return NULL;
114 }
115
116
117 1 int main(int argc, char *argv[]) {
118 // This test needs at least room for 2 CPUs
119 enum { SYS_SIZE = 2 };
120 1 mu_init();
121 1 mu_testing_set_sys_size(SYS_SIZE);
122
123 1 char options[64] = "--talp --shm-key=";
124 1 strcat(options, SHMEM_KEY);
125
126 /* This spd needs static storage to spport talp atexit dtor */
127 static subprocess_descriptor_t spd = {0};
128
129 /* Single thread */
130 {
131 1 printf("pid: %d\n", getpid());
132 /* Init spd */
133 1 spd = (const subprocess_descriptor_t){.id = 111};
134 1 spd_enter_dlb(&spd);
135 1 options_init(&spd.options, options);
136 1 mu_parse_mask("0", &spd.process_mask);
137
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_procinfo__init(spd.id, /* preinit_pid */ 0, &spd.process_mask,
138 NULL, spd.options.shm_key, spd.options.shm_size_multiplier) == DLB_SUCCESS );
139 1 shmem_barrier__init(spd.options.shm_key, spd.options.shm_size_multiplier);
140 1 node_barrier_init(&spd);
141 1 thread_ctx_set_main(THREAD_MAIN_SEQUENTIAL);
142 1 talp_init(&spd);
143 1 talp_info_t *talp_info = spd.talp_info;
144 1 dlb_monitor_t *global_monitor = talp_info->monitor;
145
146 /* Start global monitoring region */
147 1 talp_mpi_init(&spd);
148
149 /* MPI call */
150 1 sync_call_flags_t no_blocking = { .is_blocking = false, .is_collective = false };
151 1 talp_into_sync_call(&spd, no_blocking);
152 1 talp_out_of_sync_call(&spd, no_blocking);
153
154 /* Stop global monitoring region so that we can collect its metrics twice
155 * with the same values */
156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( region_stop(&spd, global_monitor) == DLB_SUCCESS );
157
158 /* Get MPI metrics */
159 dlb_node_metrics_t node_metrics1;
160 dlb_node_metrics_t node_metrics2;
161
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( talp_collect_pop_node_metrics(&spd, DLB_GLOBAL_REGION, &node_metrics1)
162 == DLB_SUCCESS );
163
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( talp_collect_pop_node_metrics(&spd, global_monitor, &node_metrics2)
164 == DLB_SUCCESS );
165
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( !region_is_started(global_monitor) );
166
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( cmp_node_metrics(&node_metrics1, &node_metrics2) == 0 );
167 1 print_node_metrics(&node_metrics1);
168
169 /* User defined region */
170 dlb_node_metrics_t node_metrics3;
171 1 dlb_monitor_t *monitor = region_register(&spd, NULL);
172
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( region_start(&spd, monitor) == DLB_SUCCESS );
173
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( talp_collect_pop_node_metrics(&spd, monitor, &node_metrics3) == DLB_SUCCESS );
174
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( region_is_started(monitor) );
175
176 /* Finalize */
177 1 talp_mpi_finalize(&spd);
178 1 talp_finalize(&spd);
179 1 node_barrier_finalize(&spd);
180 1 shmem_barrier__finalize(spd.options.shm_key, spd.options.shm_size_multiplier);
181
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_procinfo__finalize(spd.id, /* return_stolen */ false,
182 spd.options.shm_key, spd.options.shm_size_multiplier) == DLB_SUCCESS );
183 }
184
185 /* Single thread + observer thread */
186 {
187 1 spd = (const subprocess_descriptor_t){.id = 111};
188 1 spd_enter_dlb(&spd);
189 1 options_init(&spd.options, options);
190 1 mu_parse_mask("0", &spd.process_mask);
191
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_procinfo__init(spd.id, /* preinit_pid */ 0, &spd.process_mask,
192 NULL, spd.options.shm_key, spd.options.shm_size_multiplier) == DLB_SUCCESS );
193 1 shmem_barrier__init(spd.options.shm_key, spd.options.shm_size_multiplier);
194 1 node_barrier_init(&spd);
195 1 talp_init(&spd);
196 1 talp_info_t *talp_info = spd.talp_info;
197 1 talp_info->flags.external_profiler = true;
198
199 /* Start global monitoring region */
200 1 talp_mpi_init(&spd);
201
202 /* MPI call */
203 1 sync_call_flags_t no_blocking = { .is_blocking = false, .is_collective = false };
204 1 talp_into_sync_call(&spd, no_blocking);
205 1 talp_out_of_sync_call(&spd, no_blocking);
206
207 /* Observer threads cannot force-update regions, do it before */
208 1 talp_sample_update(talp_info);
209
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( talp_aggregate_samples_to_regions(talp_info) == DLB_SUCCESS );
210
211 /* Observer thread is created here */
212 pthread_t observer_pthread;
213 1 pthread_create(&observer_pthread, NULL, observer_func, &spd);
214 1 pthread_join(observer_pthread, NULL);
215
216 /* MPI call (force update) */
217 1 sync_call_flags_t blocking = { .is_blocking = false, .is_collective = false };
218 1 talp_into_sync_call(&spd, blocking);
219 1 talp_out_of_sync_call(&spd, blocking);
220
221 /* Upon gathering samples, only one thread sample has been reduced */
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( talp_info->sample_registry.num_samples == 1 );
223
224 /* Finalize */
225 1 talp_mpi_finalize(&spd);
226 1 talp_finalize(&spd);
227 1 node_barrier_finalize(&spd);
228 1 shmem_barrier__finalize(spd.options.shm_key, spd.options.shm_size_multiplier);
229
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_procinfo__finalize(spd.id, /* return_stolen */ false,
230 spd.options.shm_key, spd.options.shm_size_multiplier) == DLB_SUCCESS );
231 }
232
233 1 mu_finalize();
234
235 1 return 0;
236 }
237