logger: handle the (empty) socket epoll instead of sleeping 54/233854/2
authorMichal Bloch <m.bloch@samsung.com>
Thu, 7 May 2020 19:39:21 +0000 (21:39 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Tue, 26 May 2020 12:15:45 +0000 (14:15 +0200)
Change-Id: I666632213a1e99418a35ab5cde0d6ef1904e3650
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/logger/logger.c

index 4efe74e..bd9b80e 100644 (file)
@@ -62,7 +62,8 @@ static const int DEFAULT_EPOLL_TIME_MS = 1000;
 static const int DEFAULT_LAZY_POLLING_TOTAL_MS = 0;
 static const int DEFAULT_LAZY_POLLING_SLEEP_MS = 1000;
 
-static const int MS_TO_US = 1000;
+static const int S_TO_MS = 1000;
+static const int MS_TO_NS = 1000000;
 
 /**
  * @brief Parse permissions
@@ -1972,6 +1973,24 @@ int handle_epoll_events(struct logger *server, struct epoll_metadata *metadata,
        return 0;
 }
 
+int sleep_while_handling_socket(struct logger *server, struct epoll_metadata *metadata, int timeout)
+{
+       struct timespec now;
+
+       do {
+               int r = handle_epoll_events(server, metadata, timeout);
+               if (r < 0)
+                       return r;
+
+               clock_gettime(CLOCK_MONOTONIC, &now);
+               timeout -= (now.tv_sec - server->now.tv_sec) * S_TO_MS + (now.tv_nsec - server->now.tv_nsec) / MS_TO_NS;
+               server->now.tv_sec  = now.tv_sec;
+               server->now.tv_nsec = now.tv_nsec;
+       } while (timeout > 0);
+
+       return 0;
+}
+
 /**
  * @brief Do logging
  * @details The main logging loop
@@ -1999,7 +2018,7 @@ static int do_logger(struct logger* server)
                        use_lazy_polling = (server->lazy_polling_total >= 0);
                }
                if (use_lazy_polling)
-                       usleep(MS_TO_US * server->lazy_polling_sleep);
+                       sleep_while_handling_socket(server, &server->epoll_socket, server->lazy_polling_sleep);
 
                int r = handle_epoll_events(server, &server->epoll_common, server->epolltime * !use_lazy_polling);
                if (r < 0)