NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
rmax_set_affinity_sample.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-2024 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 "rmax_common.h"
27 
28 DOCA_LOG_REGISTER(RMAX_SET_AFFINITY);
29 
30 /*
31  * Sets the CPU affinity, through DOCA Rivermax API
32  *
33  * @core [in]: CPU core number.
34  * @return: DOCA_SUCCESS on success and DOCA error otherwise
35  */
36 doca_error_t set_affinity(unsigned core)
37 {
38  doca_error_t result, cleanup_result;
39  struct doca_rmax_cpu_affinity *mask;
40 
41  /* create CPU affinity structure */
42  result = doca_rmax_cpu_affinity_create(&mask);
43  if (result != DOCA_SUCCESS) {
44  DOCA_LOG_ERR("Failed to create CPU affinity: %s", doca_error_get_descr(result));
45  return result;
46  }
47  /* set affinity mask to selected CPU */
48  result = doca_rmax_cpu_affinity_set(mask, core);
49  if (result != DOCA_SUCCESS) {
50  DOCA_LOG_ERR("Failed to set CPU in affinity mask: %s", doca_error_get_descr(result));
51  goto cleanup;
52  }
53  /* apply CPU affinity mask */
54  result = doca_rmax_set_cpu_affinity_mask(mask);
55  if (result != DOCA_SUCCESS) {
56  DOCA_LOG_ERR("Failed to set CPU affinity: %s", doca_error_get_descr(result));
57  goto cleanup;
58  }
59 
60 cleanup:
61  /* destroy CPU affinity structure */
62  cleanup_result = doca_rmax_cpu_affinity_destroy(mask);
63  if (cleanup_result != DOCA_SUCCESS) {
64  DOCA_LOG_ERR("Failed to destroy CPU affinity: %s", doca_error_get_descr(cleanup_result));
65  }
66  return result;
67 }
68 
69 /*
70  * Sets the CPU affinity, through DOCA Rivermax API (main sample logic)
71  *
72  * @core [in]: CPU core number.
73  * @return: DOCA_SUCCESS on success and DOCA error otherwise
74  */
76 {
78 
79  DOCA_LOG_INFO("Setting internal thread CPU affinity to CPU %u", core);
80  result = set_affinity(core);
81  if (result != DOCA_SUCCESS) {
82  DOCA_LOG_ERR("Error setting CPU affinity: %s", doca_error_get_descr(result));
83  return result;
84  }
85 
86  /* library initialization */
87  result = doca_rmax_init();
88  if (result != DOCA_SUCCESS) {
89  DOCA_LOG_ERR("Failed to initialize DOCA Rivermax: %s", doca_error_get_descr(result));
90  return result;
91  }
92 
93  /* application code here */
94 
95  /* deinitialization */
96  result = doca_rmax_release();
97  if (result != DOCA_SUCCESS) {
98  DOCA_LOG_ERR("Failed to deinitialize DOCA Rivermax: %s", doca_error_get_descr(result));
99  return result;
100  }
101 
102  return result;
103 }
int32_t result
static void cleanup(struct cache_invalidate_sample_state *state)
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_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_LOG_REGISTER(RMAX_SET_AFFINITY)
doca_error_t set_affinity(unsigned core)
doca_error_t set_affinity_sample(unsigned core)