| 1 | #include <config.h> |
| 2 | |
| 3 | #include <glib.h> |
| 4 | |
| 5 | #include <glibtop.h> |
| 6 | #include <glibtop/proctime.h> |
| 7 | #include <glibtop/cpu.h> |
| 8 | |
| 9 | #include "smooth_refresh.h" |
| 10 | #include "application.h" |
| 11 | #include "settings-keys.h" |
| 12 | #include "update_interval.h" |
| 13 | #include "util.h" |
| 14 | |
| 15 | |
| 16 | const string SmoothRefresh::KEY (GSM_SETTING_SMOOTH_REFRESH); |
| 17 | |
| 18 | |
| 19 | |
| 20 | unsigned |
| 21 | SmoothRefresh::get_own_cpu_usage () |
| 22 | { |
| 23 | glibtop_cpu cpu; |
| 24 | glibtop_proc_time proctime; |
| 25 | guint64 elapsed; |
| 26 | unsigned usage = PCPU_LO; |
| 27 | |
| 28 | glibtop_get_cpu (&cpu); |
| 29 | elapsed = cpu.total - this->last_total_time; |
| 30 | |
| 31 | if (elapsed) // avoid division by 0 |
| 32 | { |
| 33 | glibtop_get_proc_time (&proctime, getpid ()); |
| 34 | usage = (proctime.rtime - this->last_cpu_time) * 100 / elapsed; |
| 35 | this->last_cpu_time = proctime.rtime; |
| 36 | } |
| 37 | |
| 38 | usage = CLAMP (usage, 0, 100); |
| 39 | |
| 40 | this->last_total_time = cpu.total; |
| 41 | |
| 42 | return usage; |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | SmoothRefresh::load_settings_value (Glib::ustring key) |
| 47 | { |
| 48 | this->active = this->settings->get_boolean (key); |
| 49 | |
| 50 | if (this->active) |
| 51 | procman_debug ("smooth_refresh is enabled"); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | SmoothRefresh::SmoothRefresh(Glib::RefPtr<Gio::Settings> a_settings) |
| 56 | : |
| 57 | settings (a_settings), |
| 58 | active (false), |
| 59 | interval (0), |
| 60 | last_pcpu (0), |
| 61 | last_total_time (0ULL), |
| 62 | last_cpu_time (0ULL) |
| 63 | { |
| 64 | this->connection = a_settings->signal_changed ("smooth_refresh").connect ([this](const Glib::ustring&key) { |
| 65 | this->load_settings_value (key); |
| 66 | }); |
| 67 | |
| 68 | this->reset (); |
| 69 | this->load_settings_value (KEY); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | |
| 74 | void |
| 75 | SmoothRefresh::reset () |
| 76 | { |
| 77 | glibtop_cpu cpu; |
| 78 | glibtop_proc_time proctime; |
| 79 | |
| 80 | glibtop_get_cpu (&cpu); |
| 81 | glibtop_get_proc_time (&proctime, getpid ()); |
| 82 | |
| 83 | this->interval = GsmApplication::get ().config.update_interval; |
| 84 | this->last_pcpu = PCPU_LO; |
| 85 | this->last_total_time = cpu.total; |
| 86 | this->last_cpu_time = proctime.rtime; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | |
| 91 | SmoothRefresh::~SmoothRefresh() |
| 92 | { |
| 93 | if (this->connection) |
| 94 | this->connection.disconnect (); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | bool |
| 100 | SmoothRefresh::get (guint &new_interval) |
| 101 | { |
| 102 | const unsigned config_interval = GsmApplication::get ().config.update_interval; |
| 103 | |
| 104 | g_assert (this->interval >= config_interval); |
| 105 | |
| 106 | if (not this->active) |
| 107 | return false; |
| 108 | |
| 109 | |
| 110 | const unsigned pcpu = this->get_own_cpu_usage (); |
| 111 | |
| 112 | /* |
| 113 | invariant: MAX_UPDATE_INTERVAL >= interval >= config_interval >= MIN_UPDATE_INTERVAL |
| 114 | |
| 115 | i see 3 cases: |
| 116 | |
| 117 | a) interval is too big (CPU usage < 10%) |
| 118 | -> increase interval |
| 119 | |
| 120 | b) interval is too small (CPU usage > 10%) AND interval != config_interval |
| 121 | > |
| 122 | -> decrease interval |
| 123 | |
| 124 | c) interval is config_interval (start or interval is perfect) |
| 125 | |
| 126 | */ |
| 127 | |
| 128 | if (pcpu > PCPU_HI && this->last_pcpu > PCPU_HI) |
| 129 | new_interval = this->interval * 11 / 10; |
| 130 | else if (this->interval != config_interval && pcpu < PCPU_LO && this->last_pcpu < PCPU_LO) |
| 131 | new_interval = this->interval * 9 / 10; |
| 132 | else |
| 133 | new_interval = this->interval; |
| 134 | |
| 135 | new_interval = CLAMP (new_interval, config_interval, config_interval * 2); |
| 136 | new_interval = CLAMP (new_interval, MIN_UPDATE_INTERVAL, MAX_UPDATE_INTERVAL); |
| 137 | |
| 138 | bool changed = this->interval != new_interval; |
| 139 | |
| 140 | if (changed) |
| 141 | this->interval = new_interval; |
| 142 | |
| 143 | this->last_pcpu = pcpu; |
| 144 | |
| 145 | |
| 146 | if (changed) |
| 147 | procman_debug ("CPU usage is %3u%%, changed refresh_interval to %u (config %u)", |
| 148 | this->last_pcpu, |
| 149 | this->interval, |
| 150 | config_interval); |
| 151 | |
| 152 | g_assert (this->interval == new_interval); |
| 153 | g_assert (this->interval >= config_interval); |
| 154 | |
| 155 | return changed; |
| 156 | } |