Switch to CLOCK_MONOTONIC_COARSE 23/279123/3
authorKonrad Lipinski <k.lipinski2@samsung.com>
Tue, 2 Aug 2022 07:35:50 +0000 (09:35 +0200)
committerKonrad Lipinski <k.lipinski2@samsung.com>
Tue, 2 Aug 2022 10:54:56 +0000 (12:54 +0200)
All uses of clock_gettime() are fine with coarse granularity. Renamed
monotonicNow() to monotonicCoarseNow() to reflect that.

Change-Id: Id60e79ca28a888ad98907184b7c11dd9d0b4aeee

src/common/include/utils.h
src/common/utils.cpp
src/server/main/socket-manager.cpp
test/test_misc.cpp

index b499888..8ee30be 100644 (file)
@@ -50,7 +50,7 @@ namespace SecurityManager {
  */
 int try_catch(const std::function<int()>& func);
 
-time_t monotonicNow();
+time_t monotonicCoarseNow();
 
 // Used for measuring function/method/scope execution time
 class ScopedTimeStamper {
index f30f8a2..071919f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (c) 2019-2022 Samsung Electronics Co., Ltd. All rights reserved.
  *
  * This file is licensed under the terms of MIT License or the Apache License
  * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
@@ -71,9 +71,9 @@ int try_catch(const std::function<int()>& func)
     return SECURITY_MANAGER_ERROR_UNKNOWN;
 }
 
-time_t monotonicNow() {
+time_t monotonicCoarseNow() {
     struct timespec now;
-    if (clock_gettime(CLOCK_MONOTONIC_RAW, &now) == -1) {
+    if (clock_gettime(CLOCK_MONOTONIC_COARSE, &now)) {
         int err = errno;
         LogError("Can't access monotonic clock, error: " <<  GetErrnoString(err));
         return 0;
index c762341..97b50d4 100644 (file)
@@ -75,7 +75,7 @@ void SocketManager::CreateDefaultReadSocketDescription(int sock)
 
     auto &desc = m_socketDescriptionVector[sock];
     desc.isOpen = true;
-    desc.timeout = monotonicNow() + SOCKET_TIMEOUT;
+    desc.timeout = monotonicCoarseNow() + SOCKET_TIMEOUT;
     desc.buffer.InitForInput();
 
     if (false == desc.isTimeout) {
@@ -172,7 +172,7 @@ bool SocketManager::GotSigTerm() const {
 void SocketManager::ReadyForRead(int sock) {
     auto &desc = m_socketDescriptionVector[sock];
     auto &buffer = desc.buffer;
-    desc.timeout = monotonicNow() + SOCKET_TIMEOUT;
+    desc.timeout = monotonicCoarseNow() + SOCKET_TIMEOUT;
 
     ssize_t size = read(sock, buffer.Ptr(), buffer.InputSize());
 
@@ -242,7 +242,7 @@ void SocketManager::ReadyForWrite(int sock) {
         return; // We do not want to propagate error to next layer
     }
 
-    desc.timeout = monotonicNow() + SOCKET_TIMEOUT;
+    desc.timeout = monotonicCoarseNow() + SOCKET_TIMEOUT;
     if (buffer.OutputDone(result))
         CloseSocket(sock);
 }
@@ -285,7 +285,7 @@ void SocketManager::MainLoop() {
             LogDebug("No usable timeout found.");
             ptrTimeout = NULL; // select will wait without timeout
         } else {
-            time_t currentTime = monotonicNow();
+            time_t currentTime = monotonicCoarseNow();
             auto &pqTimeout = m_timeoutQueue.top();
 
             // 0 means that select won't block and socket will be closed ;-)
index 80716a0..ee99bbc 100644 (file)
@@ -118,7 +118,7 @@ POSITIVE_TEST_CASE(T287_groups_loading)
 POSITIVE_TEST_CASE(T288_monotonic_clock)
 {
     time_t t;
-    BOOST_REQUIRE_NO_THROW(t = monotonicNow());
+    BOOST_REQUIRE_NO_THROW(t = monotonicCoarseNow());
     BOOST_REQUIRE_MESSAGE(t > 0, "Invalid monotonic time value");
 }