Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2006 Constantin Kaplinsky. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | // |
| 20 | // PollingScheduler class. It is used for deciding when to start new |
| 21 | // polling pass, and how much time it is ok to sleep before starting. |
| 22 | // PollingScheduler is given a desired polling interval, but it can |
| 23 | // add time between polling passes if needed for satisfying processor |
| 24 | // usage limitation. |
| 25 | // |
| 26 | |
| 27 | #ifndef __POLLINGSCHEDULER_H__ |
| 28 | #define __POLLINGSCHEDULER_H__ |
| 29 | |
| 30 | #include <x0vncserver/TimeMillis.h> |
| 31 | |
| 32 | class PollingScheduler { |
| 33 | |
| 34 | public: |
| 35 | |
| 36 | PollingScheduler(int interval, int maxload = 50); |
| 37 | |
| 38 | // Set polling parameters. |
| 39 | void setParameters(int interval, int maxload = 50); |
| 40 | |
| 41 | // Reset the object into the initial state (no polling performed). |
| 42 | void reset(); |
| 43 | |
Constantin Kaplinsky | 813dbb4 | 2006-12-05 08:03:18 +0000 | [diff] [blame^] | 44 | // Check if the object is active (not in the initial state). |
| 45 | bool isRunning(); |
| 46 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 47 | // Tell the scheduler that new polling pass is just being started. |
| 48 | void newPass(); |
| 49 | |
| 50 | // Inform the scheduler about times when we sleep. |
| 51 | void sleepStarted(); |
| 52 | void sleepFinished(); |
| 53 | |
| 54 | // This function estimates time remaining before new polling pass. |
| 55 | int millisRemaining() const; |
| 56 | |
| 57 | // This function tells if it's ok to start polling pass right now. |
| 58 | bool goodTimeToPoll() const; |
| 59 | |
| 60 | protected: |
| 61 | |
| 62 | // Parameters. |
| 63 | int m_interval; |
| 64 | int m_maxload; |
| 65 | |
| 66 | // This boolean flag is true when we do not poll the screen. |
| 67 | bool m_initialState; |
| 68 | |
| 69 | // Time stamp saved on starting current polling pass. |
| 70 | TimeMillis m_passStarted; |
| 71 | |
| 72 | // Desired duration of current polling pass. |
| 73 | int m_ratedDuration; |
| 74 | |
| 75 | // These are for measuring sleep time in current pass. |
| 76 | TimeMillis m_sleepStarted; |
| 77 | bool m_sleeping; |
| 78 | int m_sleptThisPass; |
| 79 | |
| 80 | // Ring buffer for tracking past timing errors. |
| 81 | int m_errors[8]; |
| 82 | int m_errorSum; |
| 83 | int m_errorAbsSum; |
| 84 | |
| 85 | // Ring buffer for tracking total pass durations (work + sleep). |
| 86 | int m_durations[8]; |
| 87 | int m_durationSum; |
| 88 | |
| 89 | // Ring buffer for tracking past sleep times. |
| 90 | int m_slept[8]; |
| 91 | int m_sleptSum; |
| 92 | |
| 93 | // Indexer for all ring buffers. |
| 94 | int m_idx; |
| 95 | |
| 96 | // Pass counter. |
| 97 | int m_count; |
| 98 | }; |
| 99 | |
| 100 | #endif // __POLLINGSCHEDULER_H__ |
| 101 | |