summaryrefslogtreecommitdiff
path: root/src/smooth_refresh.h
blob: 92039e03911c47adaa247d0427ab37d71fec212a (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
#ifndef _PROCMAN_SMOOTH_REFRESH
#define _PROCMAN_SMOOTH_REFRESH

#include <glib.h>
#include <gio/gio.h>
#include <string>

using std::string;



class SmoothRefresh
{
  public:

    /*
      smooth_refresh_new

      @config_interval : pointer to config_interval so we can observe
      config_interval changes.

      @return : initialized SmoothRefresh
    */
    SmoothRefresh(GSettings *a_settings);

    ~SmoothRefresh();

    /*
      smooth_refresh_reset

      Resets state and re-read config_interval
    */
    void reset();

    /*
      smooth_refresh_get

      Computes the new refresh_interval so that CPU usage is lower than
      SMOOTH_REFRESH_PCPU.

      @new_interval : where the new refresh_interval is stored.

      @return : TRUE is refresh_interval has changed. The new refresh_interval
      is stored in @new_interval. Else FALSE;
    */
    bool get(guint &new_interval);


    static const string KEY;
    static const bool KEY_DEFAULT_VALUE;

  private:

    unsigned get_own_cpu_usage();

    static void status_changed(GSettings *settings,
                               const gchar *key,
                               gpointer user_data);

    void load_settings_value(const gchar *key);

    /*
      fuzzy logic:
      - decrease refresh interval only if current CPU% and last CPU%
      are higher than PCPU_LO
      - increase refresh interval only if current CPU% and last CPU%
      are higher than PCPU_HI

    */

    enum
    {
        PCPU_HI = 22,
        PCPU_LO = 18
    };

    /*
      -self : procman's PID (so we call getpid() only once)

      -interval : current refresh interval

      -config_interval : pointer to the configuration refresh interval.
      Used to watch configuration changes

      -interval >= -config_interval

      -last_pcpu : to avoid spikes, the last CPU%. See PCPU_{LO,HI}

      -last_total_time:
      -last_cpu_time: Save last cpu and process times to compute CPU%
    */

    GSettings *settings;
    bool active;
    guint connection;
    guint interval;
    unsigned  last_pcpu;
    guint64 last_total_time;
    guint64 last_cpu_time;
};


#endif /* _PROCMAN_SMOOTH_REFRESH */