GCC Code Coverage Report


Directory: ./
File: tests/unit/05_api/api_monitor_01.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 44 44 100.0%
Functions: 3 3 100.0%
Branches: 29 46 63.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 "apis/dlb.h"
23 #include "apis/dlb_talp.h"
24
25 #include <pthread.h>
26 #include <sched.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32
33 /* Test Monitoring Regions registered concurrently from several threads */
34
35 enum { NUM_THREADS = 8 };
36 enum { NUM_ITERATIONS = 200 };
37 enum { NAME_LEN = 64 };
38
39 static pthread_barrier_t barrier;
40 static dlb_monitor_t *registered[NUM_ITERATIONS][NUM_THREADS];
41
42 1600 static void register_region(int iter, int tid) {
43 char name[NAME_LEN];
44 1600 snprintf(name, NAME_LEN, "Region %d", iter);
45 1600 registered[iter][tid] = DLB_MonitoringRegionRegister(name);
46 1600 }
47
48 7 static void* worker(void *arg) {
49 7 int tid = (int)(intptr_t)arg;
50
2/2
✓ Branch 0 taken 1400 times.
✓ Branch 1 taken 7 times.
1407 for (int iter = 0; iter < NUM_ITERATIONS; ++iter) {
51 1400 pthread_barrier_wait(&barrier);
52 1400 register_region(iter, tid);
53 1400 pthread_barrier_wait(&barrier);
54 }
55 7 return NULL;
56 }
57
58 1 int main(int argc, char **argv) {
59 cpu_set_t process_mask;
60 1 CPU_ZERO(&process_mask);
61
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(0, &process_mask);
62
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(1, &process_mask);
63
64 1 char options[64] = "--talp --shm-key=";
65 1 strcat(options, SHMEM_KEY);
66
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Init(0, &process_mask, options) == DLB_SUCCESS );
67
68 1 dlb_monitor_t *outer = DLB_MonitoringRegionRegister("Outer");
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( outer != NULL );
70
71
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_init(&barrier, NULL, NUM_THREADS) == 0 );
72
73 pthread_t threads[NUM_THREADS];
74
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
8 for (int t = 1; t < NUM_THREADS; ++t) {
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 assert( pthread_create(&threads[t], NULL, worker, (void*)(intptr_t)t) == 0 );
76 }
77
78
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1 times.
201 for (int iter = 0; iter < NUM_ITERATIONS; ++iter) {
79 /* All threads register the same name concurrently */
80 200 pthread_barrier_wait(&barrier);
81 200 register_region(iter, 0);
82 200 pthread_barrier_wait(&barrier);
83
84 /* Every thread must have obtained the same monitor */
85 200 dlb_monitor_t *monitor = registered[iter][0];
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 assert( monitor != NULL );
87
2/2
✓ Branch 0 taken 1400 times.
✓ Branch 1 taken 200 times.
1600 for (int t = 1; t < NUM_THREADS; ++t) {
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1400 times.
1400 assert( registered[iter][t] == monitor );
89 }
90
91 /* ... and it must still be the registered one (not freed and replaced) */
92 char name[NAME_LEN];
93 200 snprintf(name, NAME_LEN, "Region %d", iter);
94
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 200 times.
200 assert( DLB_MonitoringRegionRegister(name) == monitor );
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 assert( strcmp(monitor->name, name) == 0 );
96
97 /* Leave it open; starting another region aggregates into all open regions */
98
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 200 times.
200 assert( DLB_MonitoringRegionStart(monitor) == DLB_SUCCESS );
99
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 200 times.
200 assert( DLB_MonitoringRegionStart(outer) == DLB_SUCCESS );
100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 200 times.
200 assert( DLB_MonitoringRegionStop(outer) == DLB_SUCCESS );
101 }
102
103
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
8 for (int t = 1; t < NUM_THREADS; ++t) {
104
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 assert( pthread_join(threads[t], NULL) == 0 );
105 }
106
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_destroy(&barrier) == 0 );
107
108 /* Stop open regions */
109
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1 times.
201 for (int iter = 0; iter < NUM_ITERATIONS; ++iter) {
110
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 200 times.
200 assert( DLB_MonitoringRegionStop(registered[iter][0]) == DLB_SUCCESS );
111 }
112
113
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
114
115 1 return 0;
116 }
117