NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
gpunetio_send_wait_time_main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-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 <doca_argp.h>
27 #include <doca_log.h>
28 
29 #include "gpunetio_common.h"
30 
31 DOCA_LOG_REGISTER(GPU_SEND_WAIT_TIME::MAIN);
32 
33 /*
34  * Get GPU PCIe address input.
35  *
36  * @param [in]: Command line parameter
37  * @config [in]: Application configuration structure
38  * @return: DOCA_SUCCESS on success and DOCA_ERROR_INVALID_VALUE otherwise
39  */
40 static doca_error_t gpu_pci_address_callback(void *param, void *config)
41 {
42  struct sample_send_wait_cfg *sample_cfg = (struct sample_send_wait_cfg *)config;
43  char *pci_address = (char *)param;
44  size_t len;
45 
46  len = strnlen(pci_address, DOCA_DEVINFO_PCI_ADDR_SIZE);
48  DOCA_LOG_ERR("PCI address too long. Max %d", DOCA_DEVINFO_PCI_ADDR_SIZE - 1);
50  }
51 
52  strncpy(sample_cfg->gpu_pcie_addr, pci_address, len + 1);
53 
54  return DOCA_SUCCESS;
55 }
56 
57 /*
58  * Get NIC PCIe address input.
59  *
60  * @param [in]: Command line parameter
61  * @config [in]: Application configuration structure
62  * @return: DOCA_SUCCESS on success and DOCA_ERROR_INVALID_VALUE otherwise
63  */
64 static doca_error_t nic_pci_address_callback(void *param, void *config)
65 {
66  struct sample_send_wait_cfg *sample_cfg = (struct sample_send_wait_cfg *)config;
67  char *pci_address = (char *)param;
68  size_t len;
69 
70  len = strnlen(pci_address, DOCA_DEVINFO_PCI_ADDR_SIZE);
72  DOCA_LOG_ERR("PCI address too long. Max %d", DOCA_DEVINFO_PCI_ADDR_SIZE - 1);
74  }
75 
76  strncpy(sample_cfg->nic_pcie_addr, pci_address, len + 1);
77 
78  return DOCA_SUCCESS;
79 }
80 
81 /*
82  * Get time interval input.
83  *
84  * @param [in]: Command line parameter
85  * @config [in]: Application configuration structure
86  * @return: DOCA_SUCCESS on success and DOCA_ERROR_INVALID_VALUE otherwise
87  */
88 static doca_error_t time_interval_callback(void *param, void *config)
89 {
90  struct sample_send_wait_cfg *sample_cfg = (struct sample_send_wait_cfg *)config;
91  uint32_t time_interval_ns = *((uint32_t *)param);
92 
93  if (time_interval_ns == 0) {
94  DOCA_LOG_ERR("Time interval can't be 0");
96  }
97 
98  sample_cfg->time_interval_ns = time_interval_ns;
99 
100  return DOCA_SUCCESS;
101 }
102 
103 /*
104  * Register sample command line parameters.
105  *
106  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
107  */
109 {
111  struct doca_argp_param *gpu_param, *nic_param, *time_param;
112 
113  result = doca_argp_param_create(&gpu_param);
114  if (result != DOCA_SUCCESS) {
115  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
116  return result;
117  }
118 
119  doca_argp_param_set_short_name(gpu_param, "g");
120  doca_argp_param_set_long_name(gpu_param, "gpu");
121  doca_argp_param_set_arguments(gpu_param, "<GPU PCIe address>");
122  doca_argp_param_set_description(gpu_param, "GPU PCIe address to be used by the sample");
126  result = doca_argp_register_param(gpu_param);
127  if (result != DOCA_SUCCESS) {
128  DOCA_LOG_ERR("Failed to register ARGP param: %s", doca_error_get_descr(result));
129  return result;
130  }
131 
132  result = doca_argp_param_create(&nic_param);
133  if (result != DOCA_SUCCESS) {
134  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
135  return result;
136  }
137 
138  doca_argp_param_set_short_name(nic_param, "n");
139  doca_argp_param_set_long_name(nic_param, "nic");
140  doca_argp_param_set_arguments(nic_param, "<NIC PCIe address>");
141  doca_argp_param_set_description(nic_param, "DOCA device PCIe address used by the sample");
145  result = doca_argp_register_param(nic_param);
146  if (result != DOCA_SUCCESS) {
147  DOCA_LOG_ERR("Failed to register ARGP param: %s", doca_error_get_descr(result));
148  return result;
149  }
150 
151  result = doca_argp_param_create(&time_param);
152  if (result != DOCA_SUCCESS) {
153  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
154  return result;
155  }
156 
157  doca_argp_param_set_short_name(time_param, "t");
158  doca_argp_param_set_long_name(time_param, "time");
159  doca_argp_param_set_arguments(time_param, "<Nanoseconds between sends>");
160  doca_argp_param_set_description(time_param, "Time interval in nanoseconds between sends");
163  doca_argp_param_set_mandatory(time_param);
164  result = doca_argp_register_param(time_param);
165  if (result != DOCA_SUCCESS) {
166  DOCA_LOG_ERR("Failed to register ARGP param: %s", doca_error_get_descr(result));
167  return result;
168  }
169 
170  return DOCA_SUCCESS;
171 }
172 
173 /*
174  * Sample main function
175  *
176  * @argc [in]: command line arguments size
177  * @argv [in]: array of command line arguments
178  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
179  */
180 int main(int argc, char **argv)
181 {
183  struct doca_log_backend *sdk_log;
184  struct sample_send_wait_cfg sample_cfg;
185  int exit_status = EXIT_FAILURE;
186  int cuda_id;
187  cudaError_t cuda_ret;
188 
189  /* Register a logger backend */
191  if (result != DOCA_SUCCESS)
192  goto sample_exit;
193 
194  /* Register a logger backend for internal SDK errors and warnings */
195  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
196  if (result != DOCA_SUCCESS)
197  goto sample_exit;
199  if (result != DOCA_SUCCESS)
200  goto sample_exit;
201 
202  DOCA_LOG_INFO("Starting the sample");
203 
204  result = doca_argp_init(NULL, &sample_cfg);
205  if (result != DOCA_SUCCESS) {
206  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
207  goto sample_exit;
208  }
209 
211  if (result != DOCA_SUCCESS) {
212  DOCA_LOG_ERR("Failed to parse application input: %s", doca_error_get_descr(result));
213  goto argp_cleanup;
214  }
215 
216  result = doca_argp_start(argc, argv);
217  if (result != DOCA_SUCCESS) {
218  DOCA_LOG_ERR("Failed to parse application input: %s", doca_error_get_descr(result));
219  goto argp_cleanup;
220  }
221 
222  /* In a multi-GPU system, ensure CUDA refers to the right GPU device */
223  cuda_ret = cudaDeviceGetByPCIBusId(&cuda_id, sample_cfg.gpu_pcie_addr);
224  if (cuda_ret != cudaSuccess) {
225  DOCA_LOG_ERR("Invalid GPU bus id provided %s", sample_cfg.gpu_pcie_addr);
227  }
228  cudaFree(0);
229  cudaSetDevice(cuda_id);
230 
231  DOCA_LOG_INFO("Sample configuration:\n\tGPU %s\n\tNIC %s\n\tTimeout %dns",
232  sample_cfg.gpu_pcie_addr,
233  sample_cfg.nic_pcie_addr,
234  sample_cfg.time_interval_ns);
235 
236  result = gpunetio_send_wait_time(&sample_cfg);
237  if (result != DOCA_SUCCESS) {
238  DOCA_LOG_ERR("gpunetio_send_wait_time() encountered an error: %s", doca_error_get_descr(result));
239  goto argp_cleanup;
240  }
241 
242  exit_status = EXIT_SUCCESS;
243 
244 argp_cleanup:
246 sample_exit:
247  if (exit_status == EXIT_SUCCESS)
248  DOCA_LOG_INFO("Sample finished successfully");
249  else
250  DOCA_LOG_INFO("Sample finished with errors");
251  return exit_status;
252 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
uint64_t len
doca_error_t gpunetio_send_wait_time(struct sample_send_wait_cfg *sample_cfg)
DOCA_LOG_REGISTER(GPU_SEND_WAIT_TIME::MAIN)
static doca_error_t register_sample_params(void)
int main(int argc, char **argv)
static doca_error_t nic_pci_address_callback(void *param, void *config)
static doca_error_t gpu_pci_address_callback(void *param, void *config)
static doca_error_t time_interval_callback(void *param, void *config)
DOCA_EXPERIMENTAL void doca_argp_param_set_description(struct doca_argp_param *param, const char *description)
Set the description of the program param, used during program usage.
DOCA_EXPERIMENTAL void doca_argp_param_set_long_name(struct doca_argp_param *param, const char *name)
Set the long name of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_start(int argc, char **argv)
Parse incoming arguments (cmd line/json).
DOCA_EXPERIMENTAL void doca_argp_param_set_arguments(struct doca_argp_param *param, const char *arguments)
Set the description of the expected arguments of the program param, used during program usage.
DOCA_EXPERIMENTAL doca_error_t doca_argp_init(const char *program_name, void *program_config)
Initialize the parser interface.
DOCA_EXPERIMENTAL void doca_argp_param_set_callback(struct doca_argp_param *param, doca_argp_param_cb_t callback)
Set the callback function of the program param.
DOCA_EXPERIMENTAL void doca_argp_param_set_mandatory(struct doca_argp_param *param)
Mark the program param as mandatory.
DOCA_EXPERIMENTAL doca_error_t doca_argp_param_create(struct doca_argp_param **param)
Create new program param.
DOCA_EXPERIMENTAL void doca_argp_param_set_type(struct doca_argp_param *param, enum doca_argp_type type)
Set the type of the param arguments.
DOCA_EXPERIMENTAL void doca_argp_param_set_short_name(struct doca_argp_param *param, const char *name)
Set the short name of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_destroy(void)
ARG Parser destroy.
DOCA_EXPERIMENTAL doca_error_t doca_argp_register_param(struct doca_argp_param *input_param)
Register a program flag.
@ DOCA_ARGP_TYPE_STRING
Definition: doca_argp.h:56
@ DOCA_ARGP_TYPE_INT
Definition: doca_argp.h:57
#define DOCA_DEVINFO_PCI_ADDR_SIZE
Buffer size to hold PCI BDF format: "XXXX:XX:XX.X". Including a null terminator.
Definition: doca_dev.h:313
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_INVALID_VALUE
Definition: doca_error.h:44
@ DOCA_SUCCESS
Definition: doca_error.h:38
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_standard(void)
Create default, non configurable backend for application messages.
#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_EXPERIMENTAL doca_error_t doca_log_backend_create_with_file_sdk(FILE *fptr, struct doca_log_backend **backend)
Create a logging backend with a FILE* stream for SDK messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_set_sdk_level(struct doca_log_backend *backend, uint32_t level)
Set the log level limit for SDK logging backends.
@ DOCA_LOG_LEVEL_WARNING
Definition: doca_log.h:47
char gpu_pcie_addr[DOCA_DEVINFO_PCI_ADDR_SIZE]
char nic_pcie_addr[DOCA_DEVINFO_PCI_ADDR_SIZE]