1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
12 #include <sys/queue.h>
17 #include <rte_keepalive.h>
21 #define MAX_TIMEOUTS 4
22 #define SEM_TIMEOUT_SECS 2
24 static struct rte_keepalive_shm *ka_shm_create(void)
26 int fd = shm_open(RTE_KEEPALIVE_SHM_NAME, O_RDWR, 0666);
27 size_t size = sizeof(struct rte_keepalive_shm);
28 struct rte_keepalive_shm *shm;
31 printf("Failed to open %s as SHM:%s\n",
32 RTE_KEEPALIVE_SHM_NAME,
35 shm = (struct rte_keepalive_shm *) mmap(
36 0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
38 if (shm == MAP_FAILED)
39 printf("Failed to mmap SHM:%s\n", strerror(errno));
44 /* Reset to zero, as it was set to MAP_FAILED aka: (void *)-1 */
51 struct rte_keepalive_shm *shm = ka_shm_create();
52 struct timespec timeout = { .tv_nsec = 0 };
55 uint64_t last_seen_alive_time = 0;
56 uint64_t most_recent_alive_time;
61 printf("Unable to access shared core state\n");
65 most_recent_alive_time = 0;
66 for (idx_core = 0; idx_core < RTE_KEEPALIVE_MAXCORES;
68 if (shm->core_last_seen_times[idx_core] >
69 most_recent_alive_time)
70 most_recent_alive_time =
71 shm->core_last_seen_times[idx_core];
73 timeout.tv_sec = time(NULL) + SEM_TIMEOUT_SECS;
74 if (sem_timedwait(&shm->core_died, &timeout) == -1) {
75 /* Assume no core death signals and no change in any
76 * last-seen times is the keepalive monitor itself
80 last_seen_alive_time = most_recent_alive_time;
81 if (sem_errno == ETIMEDOUT) {
82 if (last_seen_alive_time ==
83 most_recent_alive_time &&
86 printf("No updates. Exiting..\n");
90 printf("sem_timedwait() error (%s)\n",
97 for (idx_core = 0; idx_core < RTE_KEEPALIVE_MAXCORES;
99 if (shm->core_state[idx_core] == RTE_KA_STATE_DEAD)
101 if (cnt_cores == 0) {
102 /* Can happen if core was restarted since Semaphore
103 * was sent, due to agent being offline.
105 printf("Warning: Empty dead core report\n");
109 printf("%i dead cores: ", cnt_cores);
111 idx_core < RTE_KEEPALIVE_MAXCORES;
113 if (shm->core_state[idx_core] == RTE_KA_STATE_DEAD)
114 printf("%d, ", idx_core);
117 if (munmap(shm, sizeof(struct rte_keepalive_shm)) != 0)
118 printf("Warning: munmap() failed\n");