From 6f27a2b3ff85b2f5bd7d98fc43254408a8168592 Mon Sep 17 00:00:00 2001 From: AlexBlago Date: Fri, 2 Aug 2024 18:14:38 -0700 Subject: [PATCH] user: common: Rework msleep routine This introduces a "mft_msleep/mft_usleep"-api intended to suspend execution of the current thread for specified time. Rationale for the change: Legacy code relies heavily on "msleep"-macro which is implemented as a wrapper around "usleep"-api. Apart from that, there are a number of places across the codebase where "usleep" is used directly. However "usleep" has been declared as obsolete since POSIX.1-2001 suggesting "nanosleep"-api instead. In POSIX.1-2008 it is removed. Newly added implementation separates C and C++ cases. For C++ it utilizes "std::this_thread::sleep_for"-api which is part of the standard starting C++11. For C implementation is based on "nanosleep"-api (https://man7.org/linux/man-pages/man2/nanosleep.2.html) Separation is done in order to keep implementation in the header relying on compiler ability to inline it where it is possible (api is time sensitive so that extra function calls better to be avoided). --- common/BUILD | 82 ++++++++++++++ common/Makefile.am | 16 ++- common/compatibility.h | 10 +- common/tools_time.c | 34 ++++++ common/tools_time.cpp | 34 ++++++ common/tools_time.h | 116 ++++++++++++++++++++ common/tools_time_c_test.c | 53 +++++++++ common/tools_time_c_test.cpp | 63 +++++++++++ common/tools_time_c_test.h | 47 ++++++++ common/tools_time_test.cpp | 58 ++++++++++ fw_comps_mgr/fw_comps_mgr.cpp | 5 +- fw_comps_mgr/fw_comps_mgr_dma_access.cpp | 5 +- mflash/mflash.c | 5 +- mflash/mflash_gw.c | 7 +- mflash/mflash_new_gw.c | 7 +- mlxfwops/lib/fs3_ops.cpp | 7 +- mlxfwops/lib/fs4_ops.cpp | 7 +- mlxfwops/lib/security_version_gw.cpp | 5 +- mlxlink/modules/mlxlink_amBER_collector.cpp | 4 +- mlxlink/modules/mlxlink_commander.cpp | 3 +- mlxlink/modules/mlxlink_eye_opener.cpp | 3 +- mlxreg/mlxreg_lib/mlxreg_lib.cpp | 5 +- mtcr_freebsd/mtcr_ul.c | 11 +- mtcr_ul/mtcr_tools_cif.c | 7 +- mtcr_ul/mtcr_ul_com.c | 13 +-- mtcr_ul/mtcr_ul_icmd_cif.c | 12 +- tools_res_mgmt/tools_res_mgmt.c | 11 +- 27 files changed, 562 insertions(+), 68 deletions(-) create mode 100644 common/tools_time.c create mode 100644 common/tools_time.cpp create mode 100644 common/tools_time.h create mode 100644 common/tools_time_c_test.c create mode 100644 common/tools_time_c_test.cpp create mode 100644 common/tools_time_c_test.h create mode 100644 common/tools_time_test.cpp diff --git a/common/BUILD b/common/BUILD index 9f6a2321d..a32f30ea3 100644 --- a/common/BUILD +++ b/common/BUILD @@ -1,3 +1,33 @@ +# Copyright (c) 2023-2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. +# +# This software is available to you under a choice of one of two +# licenses. You may choose to be licensed under the terms of the GNU +# General Public License (GPL) Version 2, available from the file +# COPYING in the main directory of this source tree, or the +# OpenIB.org BSD license below: +# +# Redistribution and use in source and binary forms, with or +# without modification, are permitted provided that the following +# conditions are met: +# +# - Redistributions of source code must retain the above +# copyright notice, this list of conditions and the following +# disclaimer. +# +# - Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test") package( @@ -280,3 +310,55 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) + +cc_library( + name = "tools-time", + srcs = [ + "tools_time.c", + "tools_time.cpp", + ], + hdrs = [ + "tools_time.h", + ], + copts = [ + "-Wall", + "-Wextra", + "-Wpedantic", + ], +) + +cc_test( + name = "tools-time-test", + size = "small", + srcs = [ + "tools_time_test.cpp", + ], + copts = [ + "-Wall", + "-Wextra", + "-Wpedantic", + ], + deps = [ + ":tools-time", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "tools-time-c-test", + size = "small", + srcs = [ + "tools_time_c_test.c", + "tools_time_c_test.cpp", + "tools_time_c_test.h", + ], + copts = [ + "-Wall", + "-Wextra", + "-Wpedantic", + ], + deps = [ + ":tools-time", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/common/Makefile.am b/common/Makefile.am index abc6ac20b..7a78c7629 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -1,4 +1,4 @@ -# Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. +# Copyright (c) 2023-2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. # # This software is available to you under a choice of one of two # licenses. You may choose to be licensed under the terms of the GNU @@ -30,12 +30,20 @@ # Makefile.am -- Process this file with automake to produce Makefile.in -noinst_HEADERS = compatibility.h bit_slice.h tools_utils.h tools_utils.h tools_version.h tools_filesystem.h tools_regex.h tools_algorithm.h - noinst_LTLIBRARIES = libcommon.la + libcommon_la_SOURCES = \ + bit_slice.h \ + compatibility.h \ + tools_algorithm.h \ tools_filesystem.cpp \ - tools_regex.cpp + tools_filesystem.h \ + tools_regex.cpp \ + tools_regex.h \ + tools_time.h \ + tools_utils.h \ + tools_utils.h \ + tools_version.h commonincludedir = $(includedir)/mstflint/common/ commoninclude_HEADERS = compatibility.h diff --git a/common/compatibility.h b/common/compatibility.h index c1836a7b3..3c998a658 100644 --- a/common/compatibility.h +++ b/common/compatibility.h @@ -1,6 +1,6 @@ /* * Copyright (C) Jan 2006 Mellanox Technologies Ltd. All rights reserved. - * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -340,7 +340,6 @@ inline typedef struct stat Stat; -#include #include #endif @@ -402,13 +401,6 @@ typedef uint8_t u_int8_t; #endif -/* define msleep(x) - sleeps for x milliseconds */ -#if defined(_WIN32) || defined(_WIN64) || defined(__MINGW32__) || defined(__MINGW64__) -#define msleep(x) Sleep(x) -#else -#define msleep(x) usleep(((unsigned long)x) * 1000) -#endif - // Convert BYTES - DWORDS with MEMCPY BE #define BYTES_TO_DWORD_BE(dw_dest, byte_src) \ do \ diff --git a/common/tools_time.c b/common/tools_time.c new file mode 100644 index 000000000..6c2a74f22 --- /dev/null +++ b/common/tools_time.c @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. + * + * This software is available to you under a choice of one of two + * licenses. You may choose to be licensed under the terms of the GNU + * General Public License (GPL) Version 2, available from the file + * COPYING in the main directory of this source tree, or the + * OpenIB.org BSD license below: + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +// purpose of this file is to verify compilation of tools time library against C code +#include "tools_time.h" diff --git a/common/tools_time.cpp b/common/tools_time.cpp new file mode 100644 index 000000000..bb06766e4 --- /dev/null +++ b/common/tools_time.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. + * + * This software is available to you under a choice of one of two + * licenses. You may choose to be licensed under the terms of the GNU + * General Public License (GPL) Version 2, available from the file + * COPYING in the main directory of this source tree, or the + * OpenIB.org BSD license below: + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +// purpose of this file is to verify compilation of tools time library against C++ code +#include "tools_time.h" diff --git a/common/tools_time.h b/common/tools_time.h new file mode 100644 index 000000000..46030e401 --- /dev/null +++ b/common/tools_time.h @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. + * + * This software is available to you under a choice of one of two + * licenses. You may choose to be licensed under the terms of the GNU + * General Public License (GPL) Version 2, available from the file + * COPYING in the main directory of this source tree, or the + * OpenIB.org BSD license below: + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#pragma once + +#if __cplusplus +#include +#include +#else +#include +#include +#endif + +#if __cplusplus +namespace nbu +{ +namespace mft +{ +namespace common +{ +/** + * @brief Pauses the execution of the current thread for at least the specified number of milliseconds. + * @param msecs The number of milliseconds to sleep. + * + * The execution of the current thread is stopped until at least msecs milliseconds has + # passed from now. Other threads continue their execution. + */ +inline void mft_msleep(uint32_t msecs) +{ + ::std::this_thread::sleep_for(std::chrono::milliseconds(msecs)); +} + +/** + * @brief Pauses the execution of the current thread for at least the specified number of microseconds. + * @param msecs The number of microseconds to sleep. + * + * The execution of the current thread is stopped until at least msecs microseconds has + # passed from now. Other threads continue their execution. + */ +inline void mft_usleep(uint64_t usecs) +{ + ::std::this_thread::sleep_for(std::chrono::microseconds(usecs)); +} + +} // namespace common +} // namespace mft +} // namespace nbu + +#define msleep(x) nbu::mft::common::mft_msleep(x) + +#else // C { + +/** + * @brief Pauses the execution of the current thread for at least the specified number of milliseconds. + * @param msecs The number of milliseconds for which to pause the execution. + * @return 0 on success, -1 otherwise. If interrupted by a signal, `errno` is set to `EINTR`. + * + * The execution of the current thread is stopped until at least msecs milliseconds has passed from now. + * Other threads continue their execution. There no support for Windows platforms. + */ +static inline int mft_msleep(uint32_t msecs) +{ + struct timespec req; + req.tv_sec = msecs / 1000; + req.tv_nsec = ((long int)msecs % 1000) * 1000000; + return nanosleep(&req, NULL); +} + +/** + * @brief Pauses the execution of the current thread for at least the specified number of microseconds. + * @param msecs The number of microseconds for which to pause the execution. + * @return 0 on success, -1 otherwise. If interrupted by a signal, `errno` is set to `EINTR`. + * + * The execution of the current thread is stopped until at least msecs microseconds has passed from now. + * Other threads continue their execution. There no support for Windows platforms. + */ +static inline int mft_usleep(uint64_t usecs) +{ + struct timespec req; + req.tv_sec = usecs / 1000000; + req.tv_nsec = ((long int)usecs % 1000000) * 1000; + return nanosleep(&req, NULL); +} + +#define msleep(x) mft_msleep(x) + +#endif // } C diff --git a/common/tools_time_c_test.c b/common/tools_time_c_test.c new file mode 100644 index 000000000..d69a0d22b --- /dev/null +++ b/common/tools_time_c_test.c @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. + * + * This software is available to you under a choice of one of two + * licenses. You may choose to be licensed under the terms of the GNU + * General Public License (GPL) Version 2, available from the file + * COPYING in the main directory of this source tree, or the + * OpenIB.org BSD license below: + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * @file tools_time_c_test.c + * @brief Provides integration between the googleest framework and the C implementation of the tools time library. + * + * This source file contains helper functions that bridge calls from C++ unit tests + * to the C implementation of the tools time library. + */ + +#include "tools_time_c_test.h" + +#include "tools_time.h" + +int mft_msleep_cwrapper(uint32_t msecs) +{ + return mft_msleep(msecs); +} + +int mft_usleep_cwrapper(uint64_t usecs) +{ + return mft_usleep(usecs); +} diff --git a/common/tools_time_c_test.cpp b/common/tools_time_c_test.cpp new file mode 100644 index 000000000..625113bd3 --- /dev/null +++ b/common/tools_time_c_test.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. + * + * This software is available to you under a choice of one of two + * licenses. You may choose to be licensed under the terms of the GNU + * General Public License (GPL) Version 2, available from the file + * COPYING in the main directory of this source tree, or the + * OpenIB.org BSD license below: + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +extern "C" +{ +#include "tools_time_c_test.h" +} + +#include + +// These tests verify that the thread suspension API effectively pauses the current thread as intended. +// Tests may occasionally produce unreliable results depending on the system's conditions and load. +// We're not assessing clock accuracy because the API implementation simply wraps standard library routines. + +TEST(mft_msleep, PoundOfSleep) +{ + auto start = std::chrono::steady_clock::now(); + mft_msleep_cwrapper(454); + auto end = std::chrono::steady_clock::now(); + std::chrono::duration elapsed = end - start; + ASSERT_GT(elapsed.count(), 0.454); +} + +TEST(mft_usleep, PoundOfSleep) +{ + auto start = std::chrono::steady_clock::now(); + mft_usleep_cwrapper(453592); + auto end = std::chrono::steady_clock::now(); + std::chrono::duration elapsed = end - start; + ASSERT_GT(elapsed.count(), 0.453592); +} diff --git a/common/tools_time_c_test.h b/common/tools_time_c_test.h new file mode 100644 index 000000000..bd6bcd2b6 --- /dev/null +++ b/common/tools_time_c_test.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. + * + * This software is available to you under a choice of one of two + * licenses. You may choose to be licensed under the terms of the GNU + * General Public License (GPL) Version 2, available from the file + * COPYING in the main directory of this source tree, or the + * OpenIB.org BSD license below: + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#pragma once + +/** + * @file tools_time_c_test.h + * @brief Helper for integrating the googletest framework with the C implementation of the tools time library. + * + * This header file provides the necessary declarations to connect the googletest framework with + * the implementation of the tools time library in C. + */ + +#include + +int mft_msleep_cwrapper(uint32_t msecs); + +int mft_usleep_cwrapper(uint64_t usecs); diff --git a/common/tools_time_test.cpp b/common/tools_time_test.cpp new file mode 100644 index 000000000..dc1c9fc6e --- /dev/null +++ b/common/tools_time_test.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. + * + * This software is available to you under a choice of one of two + * licenses. You may choose to be licensed under the terms of the GNU + * General Public License (GPL) Version 2, available from the file + * COPYING in the main directory of this source tree, or the + * OpenIB.org BSD license below: + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +#include "tools_time.h" + +// These tests verify that the thread suspension API effectively pauses the current thread as intended. +// Tests may occasionally produce unreliable results depending on the system's conditions and load. +// We're not assessing clock accuracy because the API implementation simply wraps standard library routines. + +TEST(mft_msleep, PoundOfSleep) +{ + auto start = std::chrono::steady_clock::now(); + nbu::mft::common::mft_msleep(454); + auto end = std::chrono::steady_clock::now(); + std::chrono::duration elapsed = end - start; + ASSERT_GT(elapsed.count(), 0.454); +} + +TEST(mft_usleep, PoundOfSleep) +{ + auto start = std::chrono::steady_clock::now(); + nbu::mft::common::mft_usleep(453592); + auto end = std::chrono::steady_clock::now(); + std::chrono::duration elapsed = end - start; + ASSERT_GT(elapsed.count(), 0.453592); +} diff --git a/fw_comps_mgr/fw_comps_mgr.cpp b/fw_comps_mgr/fw_comps_mgr.cpp index b5491a611..b8536e965 100644 --- a/fw_comps_mgr/fw_comps_mgr.cpp +++ b/fw_comps_mgr/fw_comps_mgr.cpp @@ -1,6 +1,6 @@ /* * Copyright (C) Jan 2013 Mellanox Technologies Ltd. All rights reserved. - * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -40,7 +40,8 @@ #include "fw_comps_mgr.h" #include "fw_comps_mgr_abstract_access.h" #include "fw_comps_mgr_dma_access.h" -#include "bit_slice.h" +#include "common/bit_slice.h" +#include "common/tools_time.h" #include #include diff --git a/fw_comps_mgr/fw_comps_mgr_dma_access.cpp b/fw_comps_mgr/fw_comps_mgr_dma_access.cpp index 95fa0f5c1..77dd76575 100644 --- a/fw_comps_mgr/fw_comps_mgr_dma_access.cpp +++ b/fw_comps_mgr/fw_comps_mgr_dma_access.cpp @@ -1,6 +1,6 @@ /* * Copyright (C) Jan 2013 Mellanox Technologies Ltd. All rights reserved. - * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -40,7 +40,8 @@ #include #include "fw_comps_mgr_dma_access.h" -#include "bit_slice.h" +#include "common/bit_slice.h" +#include "common/tools_time.h" #ifndef UEFI_BUILD #include diff --git a/mflash/mflash.c b/mflash/mflash.c index 87c82a3d2..5d0727a8b 100644 --- a/mflash/mflash.c +++ b/mflash/mflash.c @@ -1,6 +1,6 @@ /* * Copyright (C) Jan 2013 Mellanox Technologies Ltd. All rights reserved. - * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -43,7 +43,8 @@ #include #include -#include +#include "common/bit_slice.h" +#include "common/tools_time.h" #include #include #include diff --git a/mflash/mflash_gw.c b/mflash/mflash_gw.c index 039f1b122..429f7c321 100644 --- a/mflash/mflash_gw.c +++ b/mflash/mflash_gw.c @@ -1,6 +1,6 @@ /* * Copyright (C) Jan 2020 Mellanox Technologies Ltd. All rights reserved. - * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -38,6 +38,7 @@ * Created on: Jul 8, 2020 * Author: edwardg */ +#include "common/tools_time.h" #include "mflash_pack_layer.h" #include "mflash_dev_capability.h" #include "mflash_access_layer.h" @@ -69,7 +70,7 @@ static int st_spi_wait_wip(mflash* mfl, u_int32_t init_delay_us, u_int32_t retry u_int8_t status = 0; u_int32_t i = 0; - usleep(init_delay_us); + mft_usleep(init_delay_us); for (i = 0; i < num_of_retries; ++i) { @@ -79,7 +80,7 @@ static int st_spi_wait_wip(mflash* mfl, u_int32_t init_delay_us, u_int32_t retry { return MFE_OK; } - usleep(retry_delay_us); + mft_usleep(retry_delay_us); if (mfl->cputUtilizationApplied) { if ((i % mfl->cpuPercent) == 0) diff --git a/mflash/mflash_new_gw.c b/mflash/mflash_new_gw.c index 03b903669..d7c7a06f6 100644 --- a/mflash/mflash_new_gw.c +++ b/mflash/mflash_new_gw.c @@ -1,6 +1,6 @@ /* * Copyright (C) Jan 2020 Mellanox Technologies Ltd. All rights reserved. - * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -38,6 +38,7 @@ * Created on: Jul 8, 2020 * Author: edwardg */ +#include "common/tools_time.h" #include "mflash_pack_layer.h" #include "mflash_dev_capability.h" #include "mflash_access_layer.h" @@ -92,7 +93,7 @@ static int st_spi_wait_wip(mflash* mfl, u_int32_t init_delay_us, u_int32_t retry u_int8_t status = 0; u_int32_t i = 0; - usleep(init_delay_us); + mft_usleep(init_delay_us); for (i = 0; i < num_of_retries; ++i) { @@ -102,7 +103,7 @@ static int st_spi_wait_wip(mflash* mfl, u_int32_t init_delay_us, u_int32_t retry { return MFE_OK; } - usleep(retry_delay_us); + mft_usleep(retry_delay_us); if (mfl->cputUtilizationApplied) { if ((i % mfl->cpuPercent) == 0) diff --git a/mlxfwops/lib/fs3_ops.cpp b/mlxfwops/lib/fs3_ops.cpp index a47902e9c..02fecf2d9 100644 --- a/mlxfwops/lib/fs3_ops.cpp +++ b/mlxfwops/lib/fs3_ops.cpp @@ -1,6 +1,6 @@ /* * Copyright (C) Jan 2013 Mellanox Technologies Ltd. All rights reserved. - * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -36,8 +36,9 @@ #include #include -#include -#include +#include "common/tools_utils.h" +#include "common/tools_time.h" +#include "common/bit_slice.h" #include #include diff --git a/mlxfwops/lib/fs4_ops.cpp b/mlxfwops/lib/fs4_ops.cpp index beb981208..f802ad49e 100644 --- a/mlxfwops/lib/fs4_ops.cpp +++ b/mlxfwops/lib/fs4_ops.cpp @@ -1,6 +1,6 @@ /* * Copyright (C) Jan 2013 Mellanox Technologies Ltd. All rights reserved. - * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -36,8 +36,9 @@ #include #include -#include -#include +#include "common/tools_utils.h" +#include "common/bit_slice.h" +#include "common/tools_time.h" #include #include #include diff --git a/mlxfwops/lib/security_version_gw.cpp b/mlxfwops/lib/security_version_gw.cpp index d2ea12093..cd2904e16 100644 --- a/mlxfwops/lib/security_version_gw.cpp +++ b/mlxfwops/lib/security_version_gw.cpp @@ -1,6 +1,6 @@ /* * Copyright (C) Jan 2013 Mellanox Technologies Ltd. All rights reserved. - * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -32,6 +32,7 @@ */ #include "security_version_gw.h" +#include "tools_time.h" bool SecurityVersionGW::isAccessibleInLiveFish() { @@ -214,4 +215,4 @@ u_int32_t SecurityVersionGW::countSetBits(u_int32_t num) num >>= 1; } return count; -} \ No newline at end of file +} diff --git a/mlxlink/modules/mlxlink_amBER_collector.cpp b/mlxlink/modules/mlxlink_amBER_collector.cpp index 3ee00ae69..1b3ec313d 100644 --- a/mlxlink/modules/mlxlink_amBER_collector.cpp +++ b/mlxlink/modules/mlxlink_amBER_collector.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2020-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -31,7 +31,7 @@ */ #include "mlxlink_amBER_collector.h" -#include +#include MlxlinkAmBerCollector::MlxlinkAmBerCollector(Json::Value& jsonRoot) : _jsonRoot(jsonRoot) { diff --git a/mlxlink/modules/mlxlink_commander.cpp b/mlxlink/modules/mlxlink_commander.cpp index 4e84df4db..bf039f5ba 100644 --- a/mlxlink/modules/mlxlink_commander.cpp +++ b/mlxlink/modules/mlxlink_commander.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2019-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -33,6 +33,7 @@ */ #include "mlxlink_commander.h" +#include "common/tools_time.h" using namespace mlxreg; diff --git a/mlxlink/modules/mlxlink_eye_opener.cpp b/mlxlink/modules/mlxlink_eye_opener.cpp index c16e05171..6347be7bf 100644 --- a/mlxlink/modules/mlxlink_eye_opener.cpp +++ b/mlxlink/modules/mlxlink_eye_opener.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2020-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -31,6 +31,7 @@ */ #include "mlxlink_eye_opener.h" +#include "common/tools_time.h" MlxlinkEyeOpener::MlxlinkEyeOpener(Json::Value& jsonRoot) : _jsonRoot(jsonRoot) { diff --git a/mlxreg/mlxreg_lib/mlxreg_lib.cpp b/mlxreg/mlxreg_lib/mlxreg_lib.cpp index 51c8c24f0..ed0cf1bae 100644 --- a/mlxreg/mlxreg_lib/mlxreg_lib.cpp +++ b/mlxreg/mlxreg_lib/mlxreg_lib.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2019-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -39,7 +39,8 @@ #include #endif #include -#include +#include "common/tools_time.h" +#include "common/tools_utils.h" #include #define REG_ACCESS_UNION_NODE "access_reg_summary" diff --git a/mtcr_freebsd/mtcr_ul.c b/mtcr_freebsd/mtcr_ul.c index f55dd5063..34a83a892 100644 --- a/mtcr_freebsd/mtcr_ul.c +++ b/mtcr_freebsd/mtcr_ul.c @@ -1,6 +1,6 @@ /* * Copyright (C) Jan 2013 Mellanox Technologies Ltd. All rights reserved. - * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -47,8 +47,9 @@ #include #include "mtcr.h" -#include -#include +#include "common/compatibility.h" +#include "common/bit_slice.h" +#include "common/tools_time.h" #include #include "tools_dev_types.h" @@ -115,7 +116,7 @@ static int _flock_int(int fdlock, int operation) { break; // BAD! lock/free failed } - usleep(10); + mft_usleep(10); cnt++; } while (cnt < FREEBSD_MAX_RETRY_CNT); perror("failed to perform lock operation."); @@ -3062,4 +3063,4 @@ int is_zombiefish_device(mfile* mf) } return (gis != gis_operational); -} \ No newline at end of file +} diff --git a/mtcr_ul/mtcr_tools_cif.c b/mtcr_ul/mtcr_tools_cif.c index 3cf29114f..b99834799 100644 --- a/mtcr_ul/mtcr_tools_cif.c +++ b/mtcr_ul/mtcr_tools_cif.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2013-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -35,8 +35,9 @@ #include #include -#include -#include +#include "common/bit_slice.h" +#include "common/tools_utils.h" +#include "common/tools_time.h" #include "mtcr_tools_cif.h" diff --git a/mtcr_ul/mtcr_ul_com.c b/mtcr_ul/mtcr_ul_com.c index 1ea309541..00eac394a 100644 --- a/mtcr_ul/mtcr_ul_com.c +++ b/mtcr_ul/mtcr_ul_com.c @@ -1,6 +1,5 @@ /* - * - * Copyright (c) 2013-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2013-2024 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -29,9 +28,6 @@ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. - * - * mtcr_ul.c - Mellanox Hardware Access implementation - * */ /* use memory mapped /dev/mem for access */ @@ -93,7 +89,8 @@ #include #endif -#include +#include "common/bit_slice.h" +#include "common/tools_time.h" #include "tools_utils.h" #include "mtcr_ul_com.h" #include "mtcr_int_defs.h" @@ -160,7 +157,7 @@ static int _flock_int(int fdlock, int operation) break; /* BAD! lock/free failed */ } if ((cnt & 0xf) == 0) { /* sleep every 16 retries */ - usleep(1); + mft_usleep(1); } cnt++; } while (cnt < MAX_RETRY_CNT); @@ -3777,4 +3774,4 @@ int is_zombiefish_device(mfile* mf) } return (gis != gis_operational); -} \ No newline at end of file +} diff --git a/mtcr_ul/mtcr_ul_icmd_cif.c b/mtcr_ul/mtcr_ul_icmd_cif.c index 8e56da0fe..fe49a3dbd 100644 --- a/mtcr_ul/mtcr_ul_icmd_cif.c +++ b/mtcr_ul/mtcr_ul_icmd_cif.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2013-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -38,8 +38,8 @@ #if !defined(_MSC_VER) #include #endif -#include -//#include +#include "common/bit_slice.h" +#include "common/tools_time.h" #include "mtcr_icmd_cif.h" #include "packets_common.h" #ifndef __FreeBSD__ @@ -521,11 +521,7 @@ static int set_and_poll_on_busy_bit(mfile* mf, int enhanced, int busy_bit_offset } else { -#ifdef _MSC_VER - msleep(1); -#else - usleep(1); -#endif + mft_usleep(1); } } diff --git a/tools_res_mgmt/tools_res_mgmt.c b/tools_res_mgmt/tools_res_mgmt.c index 71d79fd28..eb5ce9b57 100644 --- a/tools_res_mgmt/tools_res_mgmt.c +++ b/tools_res_mgmt/tools_res_mgmt.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * Copyright (c) 2013-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU @@ -34,10 +34,11 @@ #include #include -#include -#include -#include -#include +#include "common/tools_utils.h" +#include "common/bit_slice.h" +#include "common/compatibility.h" +#include "common/tools_time.h" +#include "dev_mgt/tools_dev_types.h" #if !defined(__FreeBSD__) && !defined(UEFI_BUILD) #include