[virt-tools-list] [vhostmd virtio PATCH v3 2/6] Fix update_interval behaviour

Michael Trapp Michael.Trapp at sap.com
Mon Nov 12 15:12:37 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 | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/vhostmd/vhostmd.c b/vhostmd/vhostmd.c
index dc80345..e7ec2fc 100644
--- a/vhostmd/vhostmd.c
+++ b/vhostmd/vhostmd.c
@@ -39,6 +39,7 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <sys/stat.h>
+#include <time.h>
 #include <libxml/parser.h>
 #include <libxml/xpath.h>
 
@@ -918,6 +919,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 "
@@ -936,7 +940,11 @@ static int vhostmd_run(int diskfd)
 #endif
       if (ids)
           free(ids);
-      sleep(update_period);
+
+      run_time = time(NULL) - start_time;
+      if ((run_time > 0) && (run_time < update_period))
+         sleep((unsigned int) (update_period - run_time));
+
       vu_buffer_erase(buf);
    }
    vu_buffer_delete(buf);
-- 
2.17.1 (Apple Git-112)




More information about the virt-tools-list mailing list