[virt-tools-list] [vhostmd 5/5] Fix update_interval behaviour

Michael Trapp Michael.Trapp at sap.com
Thu Aug 30 10:11:10 UTC 2018


Vhostmd uses the update_interval value to sleep() between the metrics updates.
But a long runtime of the metrics updates can result in a noticeable longer
time between the updates and a consumer might read 'old' data after waiting
update_interval seconds.
Fix the delay between updates, that
'runtime of metrics update' + 'sleep time' matches the update_interval value.
---
 vhostmd/vhostmd.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/vhostmd/vhostmd.c b/vhostmd/vhostmd.c
index e6a1283..d581f25 100644
--- a/vhostmd/vhostmd.c
+++ b/vhostmd/vhostmd.c
@@ -968,6 +968,9 @@ static int vhostmd_run(int diskfd)
    }
    
    while (!down) {
+      time_t run_time,
+             start_time = time(NULL);
+
       vu_buffer_add(buf, "<metrics>\n", -1);
       if (metrics_host_get(buf))
          vu_log(VHOSTMD_ERR, "Failed to collect host metrics "
@@ -986,7 +989,11 @@ static int vhostmd_run(int diskfd)
 #endif
       if (ids)
           free(ids);
-      sleep(update_period);
+
+      run_time = time(NULL) - start_time;
+      if (run_time < update_period)
+         sleep(update_period - run_time);
+
       vu_buffer_erase(buf);
    }
    vu_buffer_delete(buf);
-- 
2.15.2 (Apple Git-101.1)




More information about the virt-tools-list mailing list