NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
os_utils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2025 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 
27 
28 #include <csignal>
29 #include <cstdlib>
30 #include <cstring>
31 #include <stdexcept>
32 
33 #include <sched.h>
34 #include <pthread.h>
35 #include <unistd.h>
36 
38 
39 namespace storage {
40 
41 namespace {
42 
43 std::function<void(void)> g_sig_int_handler_callback;
44 
45 void sig_int_handler(int)
46 {
47  if (g_sig_int_handler_callback)
48  g_sig_int_handler_callback();
49 }
50 
51 } // namespace
52 
53 void set_thread_affinity(std::thread &thread, uint32_t cpu_core_idx)
54 {
55  cpu_set_t actual_cpuset;
56  cpu_set_t desired_cpuset;
57  int ret;
58 
59  CPU_ZERO(&actual_cpuset);
60  CPU_ZERO(&desired_cpuset);
61  CPU_SET(cpu_core_idx, &desired_cpuset);
62 
63  ret = pthread_setaffinity_np(thread.native_handle(), sizeof(desired_cpuset), &desired_cpuset);
64  if (ret != 0) {
66  "Unable to set thread affinity to cpu " + std::to_string(cpu_core_idx) +
67  ". Underlying error: " + storage::strerror_r(ret)};
68  }
69 
70  ret = pthread_getaffinity_np(thread.native_handle(), sizeof(actual_cpuset), &actual_cpuset);
71  if (ret != 0) {
73  "Unable to retrieve thread affinity to verify usage of cpu " +
74  std::to_string(cpu_core_idx) +
75  ". Underlying error: " + storage::strerror_r(ret)};
76  }
77 
78  for (size_t ii = 0; ii != CPU_SETSIZE; ++ii) {
79  if (CPU_ISSET(ii, &actual_cpuset) && CPU_ISSET(ii, &desired_cpuset)) {
80  return;
81  }
82  }
83 
85  "Set affinity to cpu " + std::to_string(cpu_core_idx) +
86  ". Failed to apply successfully"};
87 }
88 
89 std::string strerror_r(int err) noexcept
90 {
91  char buffer[128];
92  return ::strerror_r(err, buffer, 128);
93 }
94 
95 uint32_t get_system_page_size(void)
96 {
97  auto const result = sysconf(_SC_PAGESIZE);
98  if (result < 0) {
100  "Failed to get system page size. Error: " + strerror_r(errno)};
101  }
102 
103  return static_cast<uint32_t>(result);
104 }
105 
106 void install_ctrl_c_handler(std::function<void(void)> callback)
107 {
108  g_sig_int_handler_callback = callback;
109  struct sigaction new_sigaction {};
110  new_sigaction.sa_handler = sig_int_handler;
111  new_sigaction.sa_flags = 0;
112 
113  sigemptyset(&new_sigaction.sa_mask);
114 
115  if (sigaction(SIGINT, &new_sigaction, nullptr) != 0) {
117  "failed to set SIGINT signal handler: " + storage::strerror_r(errno)};
118  }
119 }
120 
122 {
123  g_sig_int_handler_callback = {};
124 }
125 
126 void *aligned_alloc(size_t alignment, size_t size)
127 {
128  return ::aligned_alloc(alignment, size);
129 }
130 
131 void aligned_free(void *memory)
132 {
133  free(memory);
134 }
135 
136 } // namespace storage
int32_t result
@ DOCA_ERROR_OPERATING_SYSTEM
Definition: doca_error.h:58
std::string to_string(storage::control::message_type type)
void set_thread_affinity(std::thread &thread, uint32_t cpu_core_idx)
Definition: os_utils.cpp:53
void uninstall_ctrl_c_handler()
Definition: os_utils.cpp:121
std::string strerror_r(int err) noexcept
Definition: os_utils.cpp:89
void install_ctrl_c_handler(std::function< void(void)> callback)
Definition: os_utils.cpp:106
void aligned_free(void *memory)
Definition: os_utils.cpp:131
void * aligned_alloc(size_t alignment, size_t size)
Definition: os_utils.cpp:126
uint32_t get_system_page_size(void)
Definition: os_utils.cpp:95