summaryrefslogtreecommitdiff
path: root/src/smooth_refresh.cpp
blob: 677bce566122162aff373be801822791b234e0e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <config.h>

#include <sys/types.h>
#include <unistd.h>

#include <glib.h>
#include <glibtop.h>
#include <glibtop/proctime.h>
#include <glibtop/cpu.h>

#include <algorithm>

#include "smooth_refresh.h"
#include "procman.h"
#include "util.h"


const string SmoothRefresh::KEY("smooth-refresh");


unsigned SmoothRefresh::get_own_cpu_usage()
{
    glibtop_cpu cpu;
    glibtop_proc_time proctime;
    guint64 elapsed;
    unsigned usage = PCPU_LO;

    glibtop_get_cpu (&cpu);
    elapsed = cpu.total - this->last_total_time;

    if (elapsed) { // avoid division by 0
        glibtop_get_proc_time(&proctime, getpid());
        usage = (proctime.rtime - this->last_cpu_time) * 100 / elapsed;
        this->last_cpu_time = proctime.rtime;
    }

    usage = MIN (usage, 100);

    this->last_total_time = cpu.total;

    return usage;
}



void SmoothRefresh::status_changed(GSettings *settings,
                                   const gchar *key,
                                   gpointer user_data)
{
    static_cast<SmoothRefresh*>(user_data)->load_settings_value(key);
}

void SmoothRefresh::load_settings_value(const gchar *key)
{
    this->active = g_settings_get_boolean(settings, key);

    if (this->active)
        procman_debug("smooth_refresh is enabled");
}

SmoothRefresh::SmoothRefresh(GSettings *a_settings)
    :
    settings(a_settings)
{
    this->connection = g_signal_connect(G_OBJECT(settings),
                                        "changed::smooth-refresh",
                                        G_CALLBACK(status_changed),
                                        this);

  this->reset();
  this->load_settings_value(KEY.c_str());
}



void SmoothRefresh::reset()
{
    glibtop_cpu cpu;
    glibtop_proc_time proctime;

    glibtop_get_cpu(&cpu);
    glibtop_get_proc_time(&proctime, getpid());

    this->interval = ProcData::get_instance()->config.update_interval;
    this->last_pcpu = PCPU_LO;
    this->last_total_time = cpu.total;
    this->last_cpu_time = proctime.rtime;
}



SmoothRefresh::~SmoothRefresh()
{
    if (this->connection)
        g_signal_handler_disconnect(G_OBJECT(settings), this->connection);
}



bool
SmoothRefresh::get(guint &new_interval)
{
    const unsigned config_interval = ProcData::get_instance()->config.update_interval;

    g_assert(this->interval >= config_interval);

    if (not this->active)
        return false;


    const unsigned pcpu = this->get_own_cpu_usage();
    /*
      invariant: MAX_UPDATE_INTERVAL >= interval >= config_interval >= MIN_UPDATE_INTERVAL

      i see 3 cases:

      a) interval is too big (CPU usage < 10%)
      -> increase interval

      b) interval is too small (CPU usage > 10%) AND interval != config_interval
      >
      -> decrease interval

      c) interval is config_interval (start or interval is perfect)

    */

    if (pcpu > PCPU_HI && this->last_pcpu > PCPU_HI)
        new_interval = this->interval * 11 / 10;
    else if (this->interval != config_interval && pcpu < PCPU_LO && this->last_pcpu < PCPU_LO)
        new_interval = this->interval * 9 / 10;
    else
        new_interval = this->interval;

    new_interval = CLAMP(new_interval, config_interval, config_interval * 2);
    new_interval = CLAMP(new_interval, MIN_UPDATE_INTERVAL, MAX_UPDATE_INTERVAL);

    bool changed = this->interval != new_interval;

    if (changed)
        this->interval = new_interval;

    this->last_pcpu = pcpu;


    if (changed) {
        procman_debug("CPU usage is %3u%%, changed refresh_interval to %u (config %u)",
                      this->last_pcpu,
                      this->interval,
                      config_interval);
    }

    g_assert(this->interval == new_interval);
    g_assert(this->interval >= config_interval);

    return changed;
}