Skip to content

Commit

Permalink
librdmacm/cmtime: Use atomic for 'use_qpn' and 'disc_events'
Browse files Browse the repository at this point in the history
Once multi-thread support is added to cmtime, the use_qpn
and disc_events variables will be access by multiple threads.
Use an atomic variable instead of volatiles.

Signed-off-by: Sean Hefty <[email protected]>
  • Loading branch information
Sean Hefty committed Apr 23, 2024
1 parent fa89ab6 commit 78a4f91
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions librdmacm/examples/cmtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static char *src_addr;
static int timeout = 2000;
static int retries = 2;
static uint32_t base_qpn = 1000;
static uint32_t use_qpn;
static _Atomic(uint32_t) cur_qpn;
static uint32_t mimic_qp_delay;
static bool mimic;

Expand Down Expand Up @@ -123,7 +123,7 @@ static int node_index;
static uint64_t times[STEP_CNT][2];
static int connections;
static int num_threads = 1;
static volatile int disc_events;
static _Atomic(int) disc_events;

static _Atomic(int) completed[STEP_CNT];

Expand Down Expand Up @@ -222,7 +222,7 @@ static void create_qp(struct work_item *item)
attr.cap.max_inline_data = 0;

start_perf(n, STEP_CREATE_QP);
if (!use_qpn) {
if (atomic_load(&cur_qpn) == 0) {
n->qp = ibv_create_qp(pd, &attr);
if (!n->qp) {
perror("ibv_create_qp");
Expand Down Expand Up @@ -282,7 +282,7 @@ static void init_conn_param(struct node *n, struct rdma_conn_param *param)
param->retry_count = 0;
param->rnr_retry_count = 0;
param->srq = 0;
param->qp_num = n->qp ? n->qp->qp_num : use_qpn++;
param->qp_num = n->qp ? n->qp->qp_num : atomic_fetch_add(&cur_qpn, 1);
}

static void connect_qp(struct node *n)
Expand Down Expand Up @@ -382,7 +382,7 @@ static void server_disconnect(struct work_item *item)
rdma_disconnect(n->id);
end_perf(n, STEP_DISCONNECT);

if (disc_events >= connections)
if (atomic_load(&disc_events) >= connections)
end_time(STEP_DISCONNECT);
atomic_fetch_add(&completed[STEP_DISCONNECT], 1);
}
Expand Down Expand Up @@ -453,11 +453,10 @@ static void cma_handler(struct rdma_cm_id *id, struct rdma_cm_event *event)
atomic_fetch_add(&completed[STEP_DISCONNECT], 1);
} else {
*/
if (disc_events == 0) {
if (atomic_fetch_add(&disc_events, 1) == 0) {
printf("\tDisconnecting\n");
start_time(STEP_DISCONNECT);
}
disc_events++;
wq_insert(&wq, &n->work, server_disconnect);
}
break;
Expand Down Expand Up @@ -566,7 +565,7 @@ static void reset_test(int iter)
int i;

node_index = 0;
disc_events = 0;
atomic_store(&disc_events, 0);
connections = iter;

memset(times, 0, sizeof times);
Expand Down Expand Up @@ -706,13 +705,13 @@ static void run_client(int iter)
} else {
printf("Connect (%d) simulated QPs test (delay %d us)\n",
iter, mimic_qp_delay);
use_qpn = base_qpn;
atomic_store(&cur_qpn, base_qpn);
}
client_connect(iter);
show_perf(iter);

printf("Connect (%d) test - no QPs\n", iter);
use_qpn = base_qpn;
atomic_store(&cur_qpn, base_qpn);
mimic_qp_delay = 0;
client_connect(iter);
show_perf(iter);
Expand Down Expand Up @@ -741,13 +740,13 @@ static void run_server(int iter)
} else {
printf("Accept (%d) simulated QPs test (delay %d us)\n",
iter, mimic_qp_delay);
use_qpn = base_qpn;
atomic_store(&cur_qpn, base_qpn);
}
server_connect(iter);
show_perf(iter);

printf("Accept (%d) test - no QPs\n", iter);
use_qpn = base_qpn;
atomic_store(&cur_qpn, base_qpn);
mimic_qp_delay = 0;
server_connect(iter);
show_perf(iter);
Expand Down

0 comments on commit 78a4f91

Please sign in to comment.