#include <string.h>
#include <stdio.h>
#include "cm_setup.h"
#include "cm_error_handler.h"
#define DATA_EVENTS 128
#define NOTIF_EVENTS 4
#define EVENT_GROUP_INCREMENT 1
#define MAX_NBR_OF_CORES 256
typedef struct {
#define MSG_START 1
#define MSG_DATA 2
#define MSG_CHAIN 3
#define MSG_DONE 4
uint64_t msg;
env_time_t start_time;
uint64_t increment;
} event_group_test_t;
typedef struct {
em_eo_t eo;
em_event_group_t event_group;
em_event_group_t chained_event_group;
int event_count;
env_time_t total_time;
uint64_t total_rounds;
} eo_context_t;
EVENT_GROUP_TEST_EO_CONTEXT_SIZE_ERROR);
typedef struct {
uint64_t rcv_ev_cnt;
} core_stat_t;
CORE_STAT_T_SIZE_ERROR);
typedef struct {
em_pool_t pool;
unsigned int core_count;
uint64_t cpu_hz;
} egrp_shm_t;
egroup_start(
void *eo_context, em_eo_t eo,
const em_eo_conf_t *conf);
egroup_stop(void *eo_context, em_eo_t eo);
static void
em_queue_t queue, void *q_ctx);
int main(int argc, char *argv[])
{
return cm_setup(argc, argv);
}
void test_init(const appl_conf_t *appl_conf)
{
(void)appl_conf;
if (core == 0) {
egrp_shm = env_shared_reserve("EGrpSharedMem",
sizeof(egrp_shm_t));
} else {
egrp_shm = env_shared_lookup("EGrpSharedMem");
}
if (egrp_shm == NULL) {
"EventGroupChaining test init failed\n");
} else if (core == 0) {
memset(egrp_shm, 0, sizeof(egrp_shm_t));
}
}
void test_start(const appl_conf_t *appl_conf)
{
em_event_t event;
em_eo_t eo;
em_queue_t queue;
eo_context_t *eo_ctx;
event_group_test_t *egroup_test;
if (appl_conf->num_pools >= 1)
egrp_shm->pool = appl_conf->pools[0];
else
egrp_shm->core_count = appl_conf->core_count;
APPL_PRINT("\n"
"***********************************************************\n"
"EM APPLICATION: '%s' initializing:\n"
" %s: %s() - EM-core:%d\n"
" Application running on %u EM-cores (procs:%u, threads:%u)\n"
"***********************************************************\n"
"\n",
appl_conf->name, NO_PATH(__FILE__), __func__,
em_core_id(),
appl_conf->core_count, appl_conf->num_procs, appl_conf->num_threads,
egrp_shm->pool);
"Undefined application event pool!");
egrp_shm->cpu_hz = env_core_hz();
eo_ctx = &egrp_shm->eo_context;
eo =
em_eo_create(
"evgrp-chaining-test-eo", egroup_start, NULL,
egroup_stop, NULL, egroup_receive, eo_ctx);
test_fatal_if(eo ==
EM_EO_UNDEF,
"EO create failed\n");
eo_ctx->eo = eo;
test_fatal_if(ret !=
EM_OK,
"EO add queue:%" PRI_STAT
".\n"
test_fatal_if(ret !=
EM_OK || eo_start_ret !=
EM_OK,
"EO start failed, EO:%" PRI_EO "\n"
"ret:%" PRI_STAT " EO-start-ret:%" PRI_STAT "",
eo, ret, eo_start_ret);
unsigned int num, i = 1;
em_event_group_t event_group;
APPL_PRINT("%s is using %u event groups:\n",
appl_conf->name, num);
APPL_PRINT(
" evgrp-%d:%" PRI_EGRP "\n", i++, event_group);
}
egrp_shm->pool);
egroup_test->msg = MSG_START;
test_fatal_if(ret !=
EM_OK,
"Send:%" PRI_STAT
" Queue:%" PRI_QUEUE "", ret, queue);
}
void test_stop(const appl_conf_t *appl_conf)
{
em_eo_t eo;
(void)appl_conf;
APPL_PRINT("%s() on EM-core %d\n", __func__, core);
eo = egrp_shm->eo_context.eo;
test_fatal_if(ret !=
EM_OK,
"EO:%" PRI_EO " stop:%" PRI_STAT
"", eo, ret);
test_fatal_if(ret !=
EM_OK,
"EO:%" PRI_EO " delete:%" PRI_STAT
"", eo, ret);
}
void test_term(const appl_conf_t *appl_conf)
{
(void)appl_conf;
APPL_PRINT("%s() on EM-core %d\n", __func__, core);
if (core == 0) {
env_shared_free(egrp_shm);
}
}
egroup_start(
void *eo_context, em_eo_t eo,
const em_eo_conf_t *conf)
{
eo_context_t *eo_ctx = eo_context;
int ready;
(void)conf;
eo_ctx->total_time = ENV_TIME_NULL;
"'event_group' should be ready to be applied");
eo, eo_ctx->event_group);
"'chained_event_group' should be ready to be applied");
APPL_PRINT(
"EO:%" PRI_EO " - chained event group %" PRI_EGRP " created\n",
eo, eo_ctx->chained_event_group);
eo_ctx->event_count = DATA_EVENTS;
}
egroup_stop(void *eo_context, em_eo_t eo)
{
eo_context_t *eo_ctx = eo_context;
em_event_group_t egrp;
int num_notifs;
test_fatal_if(ret !=
EM_OK,
"EO remove queue all:%" PRI_STAT
" EO:%" PRI_EO "",
ret, eo);
egrp = eo_ctx->event_group;
notif_tbl);
if (ret ==
EM_OK && num_notifs > 0)
for (int i = 0; i < num_notifs; i++)
}
test_fatal_if(ret !=
EM_OK,
egrp, ret, eo);
egrp = eo_ctx->chained_event_group;
if (ret ==
EM_OK && num_notifs == 1)
}
test_fatal_if(ret !=
EM_OK,
egrp, ret, eo);
}
static void
em_queue_t queue, void *q_ctx)
{
em_event_t chained_event;
eo_context_t *const eo_ctx = eo_context;
event_group_test_t *egroup_chained;
event_group_test_t *egroup_test;
env_time_t diff_time, start_time, end_time;
uint64_t rcv_ev_cnt;
uint64_t cfg_ev_cnt;
int ready;
(void)type;
(void)q_ctx;
if (unlikely(appl_shm->exit_flag)) {
return;
}
switch (egroup_test->msg) {
case MSG_START:
APPL_PRINT("\n--- Start event group ---\n");
egroup_test->msg = MSG_DONE;
notif_tbl_chained[0].
event = event;
notif_tbl_chained[0].
queue = queue;
for (int i = 0; i < NOTIF_EVENTS; i++) {
chained_event =
em_alloc(
sizeof(event_group_test_t),
egrp_shm->pool);
"Event allocation failed!");
egroup_chained->msg = MSG_CHAIN;
egroup_chained->start_time = env_time_global();
egroup_chained->increment = 0;
notif_tbl[i].
event = chained_event;
notif_tbl[i].
queue = queue;
notif_tbl[i].
egroup = eo_ctx->chained_event_group;
}
NOTIF_EVENTS, 1, notif_tbl_chained);
test_fatal_if(ret !=
EM_OK,
"em_event_group_apply():%" PRI_STAT "", ret);
"chained ev-grp shouldn't be ready yet");
eo_ctx->event_count, NOTIF_EVENTS,
notif_tbl);
test_fatal_if(ret !=
EM_OK,
"em_event_group_apply():%" PRI_STAT "", ret);
"'event_group' should NOT be ready yet");
for (int i = 0; i < eo_ctx->event_count; i++) {
em_event_t ev_data;
event_group_test_t *egrp_test_data;
if (unlikely(appl_shm->exit_flag))
break;
ev_data =
em_alloc(
sizeof(event_group_test_t),
"Event allocation failed!");
egrp_test_data->msg = MSG_DATA;
egrp_test_data->start_time = ENV_TIME_NULL;
egrp_test_data->increment = EVENT_GROUP_INCREMENT;
eo_ctx->event_group);
if (unlikely(ret !=
EM_OK)) {
test_fatal_if(!appl_shm->exit_flag,
"Send grp:%" PRI_STAT "\n"
ret, queue);
}
}
break;
case MSG_DATA:
"'event_group' should not be ready");
"chained ev-grp shouldn't be ready yet");
egrp_shm->core_stats[
em_core_id()].rcv_ev_cnt += 1;
if (egroup_test->increment) {
em_event_group_t event_group;
egroup_test->increment--;
test_fatal_if(ret !=
EM_OK,
"Event group incr:%" PRI_STAT "", ret);
if (unlikely(ret !=
EM_OK)) {
test_fatal_if(!appl_shm->exit_flag,
"Send grp:%" PRI_STAT "\n"
ret, queue);
}
} else {
}
break;
case MSG_CHAIN:
test_fatal_if(ready !=
EM_TRUE,
"event_group should be ready");
"chained ev-grp shouldn't be ready yet");
end_time = env_time_global();
start_time = egroup_test->start_time;
diff_time = env_time_diff(end_time, start_time);
if (eo_ctx->total_rounds == 1) {
eo_ctx->total_time = env_time_sum(eo_ctx->total_time,
diff_time);
eo_ctx->total_time = env_time_sum(eo_ctx->total_time,
diff_time);
} else if (eo_ctx->total_rounds > 1) {
eo_ctx->total_time = env_time_sum(eo_ctx->total_time,
diff_time);
}
eo_ctx->total_rounds++;
rcv_ev_cnt = 0;
for (unsigned int i = 0; i < egrp_shm->core_count; i++)
rcv_ev_cnt += egrp_shm->core_stats[i].rcv_ev_cnt;
cfg_ev_cnt = (DATA_EVENTS * (1 + EVENT_GROUP_INCREMENT));
test_fatal_if(rcv_ev_cnt != cfg_ev_cnt,
"Incorrect nbr of data events before notif:\t"
"%" PRIu64 " != %" PRIu64 "!",
rcv_ev_cnt, cfg_ev_cnt);
APPL_PRINT("Event group notification event received after\t"
"%" PRIu64 " data events.\n"
"Cycles curr:%" PRIu64 ", ave:%" PRIu64 "\n",
rcv_ev_cnt,
env_time_to_cycles(diff_time, egrp_shm->cpu_hz),
env_time_to_cycles(eo_ctx->total_time,
egrp_shm->cpu_hz) /
eo_ctx->total_rounds);
break;
case MSG_DONE:
test_fatal_if(ready !=
EM_TRUE,
"event_group should be ready");
"chained ev-grp should be ready");
APPL_PRINT("--- Chained event group done ---\n");
delay_spin(env_core_hz() / 100);
memset(egrp_shm->core_stats, 0, sizeof(egrp_shm->core_stats));
egroup_test->msg = MSG_START;
if (unlikely(ret !=
EM_OK)) {
test_fatal_if(!appl_shm->exit_flag,
ret, queue);
}
break;
default:
"Bad msg (%" PRIu64 ")!", egroup_test->msg);
};
}