| 1 | #ifndef _GSM_SMOOTH_REFRESH_H |
| 2 | #define _GSM_SMOOTH_REFRESH_H |
| 3 | |
| 4 | #include <giomm.h> |
| 5 | #include <glibmm.h> |
| 6 | #include <string> |
| 7 | |
| 8 | #include "util.h" |
| 9 | |
| 10 | using std::string; |
| 11 | |
| 12 | |
| 13 | |
| 14 | class SmoothRefresh |
| 15 | : private procman::NonCopyable |
| 16 | { |
| 17 | public: |
| 18 | |
| 19 | /* |
| 20 | smooth_refresh_new |
| 21 | |
| 22 | @config_interval : pointer to config_interval so we can observe |
| 23 | config_interval changes. |
| 24 | |
| 25 | @return : initialized SmoothRefresh |
| 26 | */ |
| 27 | SmoothRefresh(Glib::RefPtr<Gio::Settings> a_settings); |
| 28 | |
| 29 | ~SmoothRefresh(); |
| 30 | |
| 31 | /* |
| 32 | smooth_refresh_reset |
| 33 | |
| 34 | Resets state and re-read config_interval |
| 35 | */ |
| 36 | void reset (); |
| 37 | |
| 38 | /* |
| 39 | smooth_refresh_get |
| 40 | |
| 41 | Computes the new refresh_interval so that CPU usage is lower than |
| 42 | SMOOTH_REFRESH_PCPU. |
| 43 | |
| 44 | @new_interval : where the new refresh_interval is stored. |
| 45 | |
| 46 | @return : TRUE is refresh_interval has changed. The new refresh_interval |
| 47 | is stored in @new_interval. Else FALSE; |
| 48 | */ |
| 49 | bool get (guint &new_interval); |
| 50 | |
| 51 | |
| 52 | static const string KEY; |
| 53 | |
| 54 | private: |
| 55 | |
| 56 | unsigned get_own_cpu_usage (); |
| 57 | |
| 58 | void load_settings_value (Glib::ustring key); |
| 59 | |
| 60 | /* |
| 61 | fuzzy logic: |
| 62 | - decrease refresh interval only if current CPU% and last CPU% |
| 63 | are higher than PCPU_LO |
| 64 | - increase refresh interval only if current CPU% and last CPU% |
| 65 | are higher than PCPU_HI |
| 66 | |
| 67 | */ |
| 68 | |
| 69 | enum |
| 70 | { |
| 71 | PCPU_HI = 22, |
| 72 | PCPU_LO = 18 |
| 73 | }; |
| 74 | |
| 75 | /* |
| 76 | -self : procman's PID (so we call getpid() only once) |
| 77 | |
| 78 | -interval : current refresh interval |
| 79 | |
| 80 | -config_interval : pointer to the configuration refresh interval. |
| 81 | Used to watch configuration changes |
| 82 | |
| 83 | -interval >= -config_interval |
| 84 | |
| 85 | -last_pcpu : to avoid spikes, the last CPU%. See PCPU_{LO,HI} |
| 86 | |
| 87 | -last_total_time: |
| 88 | -last_cpu_time: Save last cpu and process times to compute CPU% |
| 89 | */ |
| 90 | |
| 91 | Glib::RefPtr<Gio::Settings> settings; |
| 92 | bool active; |
| 93 | sigc::connection connection; |
| 94 | guint interval; |
| 95 | unsigned last_pcpu; |
| 96 | guint64 last_total_time; |
| 97 | guint64 last_cpu_time; |
| 98 | }; |
| 99 | |
| 100 | |
| 101 | #endif /* _GSM_SMOOTH_REFRESH_H */ |