Skip to content

Commit

Permalink
[202411_RC] [Mellanox] do not use system clock to avoid issues caused…
Browse files Browse the repository at this point in the history
… by system time change (Junchao-sonic-net#238)
  • Loading branch information
dprital committed Jan 15, 2025
1 parent 0315efd commit 081203a
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 27 deletions.
10 changes: 5 additions & 5 deletions platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES
# Copyright (c) 2019-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Copyright (c) 2019-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -447,7 +447,7 @@ def get_change_event_for_module_host_management_mode(self, timeout):
timeout = 1000.0 if timeout >= 1000 else float(timeout)
port_dict = {}
error_dict = {}
begin = time.time()
begin = time.monotonic()
wait_ready_task = sfp.SFP.get_wait_ready_task()

while True:
Expand Down Expand Up @@ -524,7 +524,7 @@ def get_change_event_for_module_host_management_mode(self, timeout):
}
else:
if not wait_forever:
elapse = time.time() - begin
elapse = time.monotonic() - begin
if elapse * 1000 >= timeout:
return True, {'sfp': {}}

Expand Down Expand Up @@ -569,7 +569,7 @@ def get_change_event_legacy(self, timeout):
timeout = 1000.0 if timeout >= 1000 else float(timeout)
port_dict = {}
error_dict = {}
begin = time.time()
begin = time.monotonic()

while True:
fds_events = self.poll_obj.poll(timeout)
Expand Down Expand Up @@ -619,7 +619,7 @@ def get_change_event_legacy(self, timeout):
}
else:
if not wait_forever:
elapse = time.time() - begin
elapse = time.monotonic() - begin
if elapse * 1000 >= timeout:
return True, {'sfp': {}}

Expand Down
10 changes: 5 additions & 5 deletions platform/mellanox/mlnx-platform-api/sonic_platform/dpuctlplat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES
# Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -206,8 +206,8 @@ def wait_for_pci(self):
return True
poll_obj = poll()
poll_obj.register(dir_fd, POLLIN)
start = time.time()
while (time.time() - start) < WAIT_FOR_PCI_DEV:
start = time.monotonic()
while (time.monotonic() - start) < WAIT_FOR_PCI_DEV:
events = poll_obj.poll(WAIT_FOR_PCI_DEV * 1000)
if events:
if os.path.exists(os.path.dirname(self.get_pci_dev_path())):
Expand Down Expand Up @@ -473,9 +473,9 @@ def boot_prog_context(self):
@contextmanager
def time_check_context(self, msg):
if self.verbosity:
start_time = time.time()
start_time = time.monotonic()
yield
end_time = time.time()
end_time = time.monotonic()
self.log_info(f"Total time taken = {end_time - start_time} for {msg}")
return
yield
6 changes: 3 additions & 3 deletions platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES
# Copyright (c) 2019-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Copyright (c) 2019-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -1477,14 +1477,14 @@ def initialize_sfp_modules(cls, sfp_list):
# Resetting SFP requires a reloading of module firmware, it takes up to 3 seconds
# according to standard
max_wait_time = 3.5
begin = time.time()
begin = time.monotonic()
while True:
ready_sfp_set = wait_ready_task.get_ready_set()
for sfp_index in ready_sfp_set:
s = sfp_list[sfp_index]
logger.log_debug(f'SFP {sfp_index} is recovered from resetting state')
s.on_event(EVENT_RESET_DONE)
elapse = time.time() - begin
elapse = time.monotonic() - begin
if elapse < max_wait_time:
time.sleep(0.5)
else:
Expand Down
6 changes: 3 additions & 3 deletions platform/mellanox/mlnx-platform-api/sonic_platform/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES
# Copyright (c) 2020-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Copyright (c) 2020-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -340,7 +340,7 @@ def schedule(self, interval, cb, repeat=True, run_now=True):
self.add_timer_event(timer_event, run_now)

def add_timer_event(self, timer_event, run_now=True):
timestamp = time.time()
timestamp = time.monotonic()
if not run_now:
timestamp += timer_event.interval

Expand All @@ -356,7 +356,7 @@ def stop(self):

def run(self):
while not self._stop_event.is_set():
now = time.time()
now = time.monotonic()
item = self._timestamp_queue.get()
self._min_timestamp = item[0]
if self._min_timestamp > now:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#
# Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES
# Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -65,7 +66,7 @@ def schedule_wait(self, sfp_index):
is_empty = len(self._wait_dict) == 0

# The item will be expired in 3 seconds
self._wait_dict[sfp_index] = time.time() + self.WAIT_TIME
self._wait_dict[sfp_index] = time.monotonic() + self.WAIT_TIME

if is_empty:
logger.log_debug('An item arrives, wake up WaitSfpReadyTask')
Expand Down Expand Up @@ -120,7 +121,7 @@ def run(self):
self.event.wait()
self.event.clear()

now = time.time()
now = time.monotonic()
with self.lock:
logger.log_debug(f'Processing wait SFP dict: {self._wait_dict}, now={now}')
for sfp_index, expire_time in self._wait_dict.items():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#
# Copyright (c) 2019-2024 NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES
# Copyright (c) 2019-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -229,7 +230,7 @@ def arm(self, seconds):
# Save the watchdog arm timestamp
# requiered for get_remaining_time()
os.makedirs('/tmp/nvidia', exist_ok=True)
utils.write_file(self.TIMESTAMP_FILE, str(time.time()))
utils.write_file(self.TIMESTAMP_FILE, str(time.monotonic()))

return ret

Expand All @@ -244,7 +245,7 @@ def get_remaining_time(self):

if self.is_armed():
arm_timestamp = utils.read_float_from_file(self.TIMESTAMP_FILE)
timeleft = int(self.timeout - (time.time() - arm_timestamp))
timeleft = int(self.timeout - (time.monotonic() - arm_timestamp))

return timeleft

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#
# Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES
# Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -34,7 +35,7 @@
class TestChangeEvent:
@mock.patch('sonic_platform.sfp.SFP.get_fd_for_polling_legacy')
@mock.patch('select.poll')
@mock.patch('time.time')
@mock.patch('time.monotonic')
@mock.patch('sonic_platform.device_data.DeviceDataManager.is_module_host_management_mode', mock.MagicMock(return_value=False))
@mock.patch('sonic_platform.device_data.DeviceDataManager.get_sfp_count', mock.MagicMock(return_value=1))
@mock.patch('sonic_platform.chassis.extract_RJ45_ports_index', mock.MagicMock(return_value=[]))
Expand Down Expand Up @@ -87,7 +88,7 @@ def test_get_change_event_legacy(self, mock_status, mock_time, mock_create_poll,
@mock.patch('sonic_platform.wait_sfp_ready_task.WaitSfpReadyTask.get_ready_set')
@mock.patch('sonic_platform.sfp.SFP.get_fd')
@mock.patch('select.poll')
@mock.patch('time.time')
@mock.patch('time.monotonic')
@mock.patch('sonic_platform.device_data.DeviceDataManager.is_module_host_management_mode', mock.MagicMock(return_value=True))
@mock.patch('sonic_platform.device_data.DeviceDataManager.get_sfp_count', mock.MagicMock(return_value=1))
@mock.patch('sonic_platform.chassis.extract_RJ45_ports_index', mock.MagicMock(return_value=[]))
Expand Down
4 changes: 2 additions & 2 deletions platform/mellanox/mlnx-platform-api/tests/test_dpuctlplat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES
# Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -461,7 +461,7 @@ def mock_time_diff():
mock_time_diff.counter += 1
return mock_time_diff.counter * timeout_val
mock_time_diff.counter = 0
with patch("time.time", wraps=mock_time_diff):
with patch("time.monotonic", wraps=mock_time_diff):
# PCI Device is not recognized
assert not dpuctl_obj.wait_for_pci()
pci_parent_path = os.path.dirname(pci_dev_path)
Expand Down

0 comments on commit 081203a

Please sign in to comment.