]> source.dussan.org Git - tigervnc.git/commitdiff
Corrected algorithm of adjusting polling intervals, to make sudden
authorConstantin Kaplinsky <const@tightvnc.com>
Thu, 16 Feb 2006 13:01:22 +0000 (13:01 +0000)
committerConstantin Kaplinsky <const@tightvnc.com>
Thu, 16 Feb 2006 13:01:22 +0000 (13:01 +0000)
changes more graceful.

git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@491 3789f03b-4d11-0410-bbf8-ca57d06f2519

x0vncserver/x0vncserver.cxx

index 7b8c66098c3d5100a1ab513950618bcc20ff5aed..af63ad00e0ab82a460489bccb8c7d6ad861f7ee9 100644 (file)
@@ -332,19 +332,37 @@ static void adjustPollingCycle(int *cycle, CPUMonitor *mon)
 {
   int coeff = mon->check();
   if (coeff < 90 || coeff > 110) {
-#ifdef DEBUG
-    int oldPollingCycle = *cycle;
-#endif
-    *cycle = (*cycle * 100 + coeff/2) / coeff;
-    if (*cycle < (int)pollingCycle) {
-      *cycle = (int)pollingCycle;
-    } else if (*cycle > (int)pollingCycle * 32) {
-      *cycle = (int)pollingCycle * 32;
+    int oldValue = *cycle;
+    int newValue = (oldValue * 100 + coeff/2) / coeff;
+
+    // Correct sudden changes.
+    if (newValue < oldValue / 2) {
+      newValue = oldValue / 2;
+    } else if (newValue > oldValue * 2) {
+      newValue = oldValue * 2;
+    }
+
+    // Compute upper limit.
+    int upperLimit = (int)pollingCycle * 32;
+    if (upperLimit < 100) {
+      upperLimit = 100;
+    } else if (upperLimit > 1000) {
+      upperLimit = 1000;
     }
+
+    // Limit lower and upper bounds.
+    if (newValue < (int)pollingCycle) {
+      newValue = (int)pollingCycle;
+    } else if (newValue > upperLimit) {
+      newValue = upperLimit;
+    }
+
+    if (newValue != oldValue) {
+      *cycle = newValue;
 #ifdef DEBUG
-    if (*cycle != oldPollingCycle)
-      fprintf(stderr, "\t[new cycle %dms]\n", *cycle);
+      fprintf(stderr, "\t[new cycle %dms]\n", newValue);
 #endif
+    }
   }
 }