NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
pe_event_sample.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <sys/epoll.h>
31 #include <sys/eventfd.h>
32 #include <unistd.h>
33 
34 #include <doca_mmap.h>
35 #include <doca_buf.h>
36 #include <doca_buf_inventory.h>
37 #include <doca_ctx.h>
38 #include <doca_dma.h>
39 #include <doca_types.h>
40 #include <doca_log.h>
41 #include <doca_pe.h>
42 
43 #include <samples/common.h>
44 #include "pe_common.h"
45 
46 DOCA_LOG_REGISTER(PE_EVENT::SAMPLE);
47 
63 #define EXIT_ON_FAILURE(_expression_) \
64  { \
65  doca_error_t _status_ = _expression_; \
66 \
67  if (_status_ != DOCA_SUCCESS) { \
68  DOCA_LOG_ERR("%s failed with status %s", __func__, doca_error_get_descr(_status_)); \
69  return _status_; \
70  } \
71  }
72 
73 #define NUM_TASKS (16)
74 #define DMA_BUFFER_SIZE (1024)
75 #define BUFFER_SIZE (DMA_BUFFER_SIZE * 2 * NUM_TASKS)
76 #define BUF_INVENTORY_SIZE (NUM_TASKS * 2)
77 
83 
84  struct doca_dma *dma;
85  struct doca_ctx *dma_ctx;
86  struct doca_dma_task_memcpy *tasks[NUM_TASKS];
87  int epoll_fd;
88 };
89 
90 /*
91  * DMA Memcpy task completed callback
92  *
93  * @dma_task [in]: Completed task
94  * @task_user_data [in]: doca_data from the task
95  * @ctx_user_data [in]: doca_data from the context
96  */
97 static void dma_memcpy_completed_callback(struct doca_dma_task_memcpy *dma_task,
98  union doca_data task_user_data,
99  union doca_data ctx_user_data)
100 {
101  uint8_t expected_value = (uint8_t)task_user_data.u64;
102  struct pe_event_sample_state *state = (struct pe_event_sample_state *)ctx_user_data.ptr;
103 
104  state->base.num_completed_tasks++;
105 
110  (void)process_completed_dma_memcpy_task(dma_task, expected_value);
111 
112  /* The task is no longer required, therefore it can be freed */
113  (void)dma_task_free(dma_task);
114 }
115 
116 /*
117  * Memcpy task error callback
118  *
119  * @dma_task [in]: failed task
120  * @task_user_data [in]: doca_data from the task
121  * @ctx_user_data [in]: doca_data from the context
122  */
123 static void dma_memcpy_error_callback(struct doca_dma_task_memcpy *dma_task,
124  union doca_data task_user_data,
125  union doca_data ctx_user_data)
126 {
127  struct pe_event_sample_state *state = (struct pe_event_sample_state *)ctx_user_data.ptr;
128  struct doca_task *task = doca_dma_task_memcpy_as_task(dma_task);
129 
130  (void)task_user_data;
131 
132  /* This sample defines that a task is completed even if it is completed with error */
133  state->base.num_completed_tasks++;
134 
135  DOCA_LOG_ERR("Task failed with status %s", doca_error_get_descr(doca_task_get_status(task)));
136 
137  /* The task is no longer required, therefore it can be freed */
138  (void)dma_task_free(dma_task);
139 }
140 
150 {
152  struct epoll_event events_in = {.events = EPOLLIN, .data.fd = 0};
153 
154  DOCA_LOG_INFO("Registering PE event");
155 
156  /* This section prepares an epoll that the sample can wait on to be notified that a task is completed */
157  state->epoll_fd = epoll_create1(0);
158  if (state->epoll_fd == -1) {
159  DOCA_LOG_ERR("Failed to create epoll_fd, error=%d", errno);
161  }
162 
163  /* doca_event_handle_t is a file descriptor that can be added to an epoll */
164  EXIT_ON_FAILURE(doca_pe_get_notification_handle(state->base.pe, &event_handle));
165 
166  if (epoll_ctl(state->epoll_fd, EPOLL_CTL_ADD, event_handle, &events_in) != 0) {
167  DOCA_LOG_ERR("Failed to register epoll, error=%d", errno);
169  }
170 
171  return DOCA_SUCCESS;
172 }
173 
181 {
182  union doca_data ctx_user_data = {0};
183 
184  DOCA_LOG_INFO("Creating DMA");
185 
186  EXIT_ON_FAILURE(doca_dma_create(state->base.device, &state->dma));
187  state->dma_ctx = doca_dma_as_ctx(state->dma);
188 
189  /* A context can only be connected to one PE (PE can run multiple contexts) */
191 
197  ctx_user_data.ptr = state;
198  EXIT_ON_FAILURE(doca_ctx_set_user_data(state->dma_ctx, ctx_user_data));
199 
203  NUM_TASKS));
204 
205  return DOCA_SUCCESS;
206 }
207 
216 {
217  static const int no_timeout = -1;
218  struct epoll_event ep_event = {0};
219  int epoll_status = 0;
220 
221  DOCA_LOG_INFO("Running until all tasks are complete");
222 
227  do {
236  while (doca_pe_progress(state->base.pe) != 0) {
237  if (state->base.num_completed_tasks == NUM_TASKS) {
238  DOCA_LOG_INFO("All tasks completed");
239  return DOCA_SUCCESS;
240  }
241  }
242 
248 
249  epoll_status = epoll_wait(state->epoll_fd, &ep_event, 1, no_timeout);
250  if (epoll_status == -1) {
251  DOCA_LOG_ERR("Failed waiting for event, error=%d", errno);
253  }
254 
255  /* handle parameter is not used in Linux */
257  } while (1);
258 }
259 
268 void cleanup(struct pe_event_sample_state *state)
269 {
270  /* A context must be stopped before it is destroyed */
271  if (state->dma_ctx != NULL)
272  (void)doca_ctx_stop(state->dma_ctx);
273 
274  /* All contexts must be destroyed before PE is destroyed. Context destroy disconnects it from the PE */
275  if (state->dma != NULL)
276  (void)doca_dma_destroy(state->dma);
277 
278  pe_sample_base_cleanup(&state->base);
279 }
280 
290 {
291  memset(state, 0, sizeof(*state));
292 
293  state->base.buffer_size = BUFFER_SIZE;
295 
297  EXIT_ON_FAILURE(open_device(&state->base));
298  EXIT_ON_FAILURE(create_mmap(&state->base));
300  EXIT_ON_FAILURE(create_pe(&state->base));
302  EXIT_ON_FAILURE(create_dma(state));
307 
308  return DOCA_SUCCESS;
309 }
310 
317 {
318  struct pe_event_sample_state state;
319  doca_error_t status = run(&state);
320 
321  cleanup(&state);
322 
323  return status;
324 }
#define NULL
Definition: __stddef_null.h:26
static doca_error_t allocate_buffer(struct cache_invalidate_sample_state *state)
static doca_error_t create_buf_inventory(struct cache_invalidate_sample_state *state)
static doca_error_t create_pe(struct cache_invalidate_sample_state *state)
static doca_error_t create_mmap(struct doca_dev *doca_device, unsigned int mmap_permissions, void *memrange_addr, size_t memrange_len, struct doca_mmap **mmap, doca_dpa_dev_mmap_t *dpa_mmap_handle)
DOCA_STABLE doca_error_t doca_ctx_start(struct doca_ctx *ctx)
Finalizes all configurations, and starts the DOCA CTX.
DOCA_STABLE doca_error_t doca_ctx_set_user_data(struct doca_ctx *ctx, union doca_data user_data)
set user data to context
DOCA_STABLE doca_error_t doca_ctx_stop(struct doca_ctx *ctx)
Stops the context allowing reconfiguration.
DOCA_STABLE struct doca_task * doca_dma_task_memcpy_as_task(struct doca_dma_task_memcpy *task)
This method converts a memcpy task to doca_task.
DOCA_STABLE struct doca_ctx * doca_dma_as_ctx(struct doca_dma *dma)
DOCA_STABLE doca_error_t doca_dma_task_memcpy_set_conf(struct doca_dma *dma, doca_dma_task_memcpy_completion_cb_t task_completion_cb, doca_dma_task_memcpy_completion_cb_t task_error_cb, uint32_t num_memcpy_tasks)
This method sets the DMA memcpy tasks configuration.
DOCA_STABLE doca_error_t doca_dma_create(struct doca_dev *dev, struct doca_dma **dma)
DOCA_STABLE doca_error_t doca_dma_destroy(struct doca_dma *dma)
enum doca_error doca_error_t
DOCA API return codes.
DOCA_STABLE const char * doca_error_get_descr(doca_error_t error)
Returns the description string of an error code.
@ DOCA_ERROR_OPERATING_SYSTEM
Definition: doca_error.h:58
@ DOCA_SUCCESS
Definition: doca_error.h:38
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
Definition: doca_log.h:466
#define DOCA_LOG_INFO(format,...)
Generates an INFO application log message.
Definition: doca_log.h:486
DOCA_STABLE doca_error_t doca_pe_request_notification(struct doca_pe *pe)
Arm the progress engine to wait for completion.
DOCA_STABLE doca_error_t doca_task_get_status(const struct doca_task *task)
Get task status.
DOCA_STABLE doca_error_t doca_pe_clear_notification(struct doca_pe *pe, doca_notification_handle_t handle)
Clear triggered completions after wait.
DOCA_STABLE doca_error_t doca_pe_connect_ctx(struct doca_pe *pe, struct doca_ctx *ctx)
This method connects a context to a progress engine.
DOCA_STABLE uint8_t doca_pe_progress(struct doca_pe *pe)
Run the progress engine.
void * doca_event_handle_t
Definition: doca_types.h:42
#define doca_event_invalid_handle
Definition: doca_types.h:43
doca_dev * open_device(std::string const &identifier)
Definition: doca_utils.cpp:43
void pe_sample_base_cleanup(struct pe_sample_state_base *state)
Definition: pe_common.c:311
doca_error_t dma_task_free(struct doca_dma_task_memcpy *dma_task)
Definition: pe_common.c:108
doca_error_t submit_dma_tasks(uint32_t num_tasks, struct doca_dma_task_memcpy **tasks)
Definition: pe_common.c:211
doca_error_t allocate_dma_tasks(struct pe_sample_state_base *state, struct doca_dma *dma, uint32_t num_tasks, size_t dma_buffer_size, struct doca_dma_task_memcpy **tasks)
Definition: pe_common.c:155
doca_error_t process_completed_dma_memcpy_task(struct doca_dma_task_memcpy *dma_task, uint8_t expected_value)
Definition: pe_common.c:66
doca_error_t create_dma(struct pe_event_sample_state *state)
#define EXIT_ON_FAILURE(_expression_)
doca_error_t run_pe_event_sample(void)
doca_error_t run(struct pe_event_sample_state *state)
#define BUF_INVENTORY_SIZE
void cleanup(struct pe_event_sample_state *state)
#define BUFFER_SIZE
doca_error_t register_pe_event(struct pe_event_sample_state *state)
static void dma_memcpy_completed_callback(struct doca_dma_task_memcpy *dma_task, union doca_data task_user_data, union doca_data ctx_user_data)
#define NUM_TASKS
static void dma_memcpy_error_callback(struct doca_dma_task_memcpy *dma_task, union doca_data task_user_data, union doca_data ctx_user_data)
DOCA_LOG_REGISTER(PE_EVENT::SAMPLE)
doca_error_t run_for_completion(struct pe_event_sample_state *state)
#define DMA_BUFFER_SIZE
struct doca_dma * dma
struct pe_sample_state_base base
struct doca_ctx * dma_ctx
struct doca_dma_task_memcpy * tasks[NUM_TASKS]
struct doca_dev * device
Definition: pe_common.h:39
size_t buf_inventory_size
Definition: pe_common.h:45
struct doca_pe * pe
Definition: pe_common.h:42
uint32_t num_completed_tasks
Definition: pe_common.h:56
Convenience type for representing opaque data.
Definition: doca_types.h:56
uint64_t u64
Definition: doca_types.h:58
void * ptr
Definition: doca_types.h:57