The ROme OpTimistic Simulator  3.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
msg_queue_test.c
Go to the documentation of this file.
1 
9 #include <test.h>
10 #include <test_rng.h>
11 
12 #include <datatypes/msg_queue.h>
13 #include <lp/lp.h>
14 
15 #include <memory.h>
16 #include <stdatomic.h>
17 #include <stdint.h>
18 #include <stdlib.h>
19 
20 #define THREAD_CNT 6
21 #define THREAD_REPS 100000
22 
23 uint64_t n_lps_node = 64;
24 static __thread test_rng_state rng_state;
25 static struct lp_ctx lps_m[THREAD_CNT];
26 static atomic_uint msg_missing = THREAD_REPS * THREAD_CNT;
27 static atomic_uint msg_to_free = THREAD_CNT;
28 
29 struct lp_ctx *lps = lps_m;
30 
31 void msg_allocator_free(struct lp_msg *msg)
32 {
33  atomic_fetch_sub_explicit(&msg_to_free, 1U, memory_order_relaxed);
34  free(msg);
35 }
36 
37 static int msg_queue_test_init(void)
38 {
40  return 0;
41 }
42 
43 static int msg_queue_test_fini(void)
44 {
46  return -(atomic_load(&msg_missing) | atomic_load(&msg_to_free));
47 }
48 
49 static int msg_queue_test(void)
50 {
51  int ret = 0;
52  lcg_init(rng_state, (rid + 1) * 1713);
54 
56 
57  unsigned i = THREAD_REPS;
58  while(i--){
59  struct lp_msg *msg = malloc(sizeof(*msg));
60  memset(msg, 0, sizeof(*msg));
61  msg->dest_t = lcg_random(rng_state) * THREAD_REPS;
62  msg->dest = lcg_random(rng_state) * n_lps_node;
63  msg_queue_insert(msg);
64  }
65 
67 
68  struct lp_msg *msg;
69  simtime_t last_time = 0.0;
70 
71  while((msg = msg_queue_extract())){
72  if(msg->dest_t < last_time)
73  --ret;
74  last_time = msg->dest_t;
75  free(msg);
76  atomic_fetch_sub_explicit(&msg_missing, 1U, memory_order_relaxed);
77  }
78 
80 
81  // to test msg cleanup
82  msg = malloc(sizeof(*msg));
83  memset(msg, 0, sizeof(*msg));
84  msg_queue_insert(msg);
85 
87 
89  return ret;
90 }
91 
92 const struct test_config test_config = {
93  .threads_count = THREAD_CNT,
94  .test_init_fnc = msg_queue_test_init,
95  .test_fini_fnc = msg_queue_test_fini,
96  .test_fnc = msg_queue_test
97 };
lcg_random
#define lcg_random(rng_state)
Computes a pseudo random number in the [0, 1] range.
Definition: test_rng.h:49
simtime_t
double simtime_t
The type used to represent logical time in the simulation.
Definition: core.h:62
msg_queue_init
void msg_queue_init(void)
Initializes the message queue for the current thread.
Definition: msg_queue.c:54
msg_queue_extract
struct lp_msg * msg_queue_extract(void)
Extracts the next message from the queue.
Definition: msg_queue.c:98
lp_msg
A model simulation message.
Definition: msg.h:34
msg_queue_global_init
void msg_queue_global_init(void)
Initializes the message queue at the node level.
Definition: msg_queue.c:46
lp_msg::dest
lp_id_t dest
The id of the recipient LP.
Definition: msg.h:36
lp_ctx
A complete LP context.
Definition: lp.h:21
test_config
A complete test configuration.
Definition: test.h:22
lp.h
LP construction functions.
test_thread_barrier
bool test_thread_barrier(void)
Synchronizes threads on a barrier.
Definition: test.c:200
rid
__thread rid_t rid
The identifier of the thread.
Definition: core.c:16
msg_queue.h
Message queue datatype.
test.h
Test framework header.
lcg_init
#define lcg_init(rng_state, initseq)
Initializes the random number generator.
Definition: test_rng.h:30
msg_queue_insert
void msg_queue_insert(struct lp_msg *msg)
Inserts a message in the queue.
Definition: msg_queue.c:168
msg_queue_fini
void msg_queue_fini(void)
Finalizes the message queue for the current thread.
Definition: msg_queue.c:66
test_config::threads_count
unsigned threads_count
test_fnc is executed with that many cores
Definition: test.h:33
test_rng.h
Pseudo random number generator for tests.
msg_queue_global_fini
void msg_queue_global_fini(void)
Finalizes the message queue at the node level.
Definition: msg_queue.c:85
lp_msg::dest_t
simtime_t dest_t
The intended destination logical time of this message.
Definition: msg.h:38
test_rng_state
__uint128_t test_rng_state
The type of this pseudo random number generator state.
Definition: test_rng.h:19
n_lps_node
uint64_t n_lps_node
The total number of LPs hosted in the node.
Definition: msg_queue_test.c:23