NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
os_utils.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024-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 
26 #ifndef APPLICATIONS_STORAGE_STORAGE_COMMON_OS_UTILS_HPP_
27 #define APPLICATIONS_STORAGE_STORAGE_COMMON_OS_UTILS_HPP_
28 
29 #include <cstdint>
30 #include <functional>
31 #include <string>
32 #include <thread>
33 
34 #ifdef __linux__
35 #include <endian.h>
36 #else // ifdef __linux__
37 #error UNSUPPORTED OS
38 // FUTURE: Define windows suitable impls for these
39 #define htobe16
40 #define htobe32
41 #define htobe64
42 #define betoh16
43 #define betoh32
44 #define betoh64
45 #endif // ifdef __linux__
46 
47 namespace storage {
48 
49 /*
50  * Set the affinity of a given thread to a specified core
51  *
52  * @throws std::runtime_error: if the operation fails
53  *
54  * @thread [in]: Thread to set affinity for
55  * @cpu_core_idx [in]: Core affinity to set
56  */
57 void set_thread_affinity(std::thread &thread, uint32_t cpu_core_idx);
58 
59 /*
60  * Convenience wrapper for strerror_r to make it easier to use
61  *
62  * @err [in]: posix status code
63  * @return: String description of the error
64  */
65 std::string strerror_r(int err) noexcept;
66 
67 /*
68  * Get the system page size
69  *
70  * @throws std::runtime_error: if the operation fails
71  *
72  * @return: Page size (in bytes)
73  */
74 uint32_t get_system_page_size(void);
75 
76 /*
77  * Install a handler for user abort (control + C)
78  */
79 void install_ctrl_c_handler(std::function<void(void)> callback);
80 
81 /*
82  * Uninstall a handler for user abort (control + C)
83  */
85 
86 /*
87  * Allocate aligned memory
88  */
89 void *aligned_alloc(size_t alignment, size_t size);
90 
91 /*
92  * Free aligned memory
93  */
94 void aligned_free(void *memory);
95 
96 } // namespace storage
97 
98 #endif /* APPLICATIONS_STORAGE_STORAGE_COMMON_OS_UTILS_HPP_ */
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