summaryrefslogtreecommitdiffstats
path: root/src/application.h
blob: 455a699b60c77a2ba7961f97ae87fe54365745cf (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#ifndef _GSM_APPLICATION_H_
#define _GSM_APPLICATION_H_

#include <gtkmm.h>
#include <glibtop/cpu.h>
#include <handy.h>

#include <algorithm>

struct LoadGraph;

#include "smooth_refresh.h"
#include "prettytable.h"
#include "legacy/treeview.h"
#include "util.h"

static const unsigned MIN_UPDATE_INTERVAL =   1 * 1000;
static const unsigned MAX_UPDATE_INTERVAL = 100 * 1000;


enum ProcmanTab
{
    PROCMAN_TAB_PROCESSES,
    PROCMAN_TAB_RESOURCES,
    PROCMAN_TAB_DISKS
};


struct ProcConfig
  : private procman::NonCopyable
{
    ProcConfig()
      : update_interval(0),
	graph_update_interval(0),
	disks_update_interval(0),
	graph_data_points(0),
	mem_color(),
	swap_color(),
	net_in_color(),
	net_out_color(),
	bg_color(),
	frame_color(),
	num_cpus(0),
	solaris_mode(false),
	process_memory_in_iec(true),
	logarithmic_scale(false),
	draw_stacked(false),
	draw_smooth(true),
	resources_memory_in_iec(true),
	network_in_bits(false),
	network_total_unit(false),
	network_total_in_bits(false)
    {
      std::fill(&this->cpu_color[0], &this->cpu_color[GLIBTOP_NCPU], GdkRGBA());
    }

    int             update_interval;
    int             graph_update_interval;
    int             disks_update_interval;
    int             graph_data_points;
    GdkRGBA         cpu_color[GLIBTOP_NCPU];
    GdkRGBA         mem_color;
    GdkRGBA         swap_color;
    GdkRGBA         net_in_color;
    GdkRGBA         net_out_color;
    GdkRGBA         bg_color;
    GdkRGBA         frame_color;
    gint            num_cpus;
    bool solaris_mode;
    bool process_memory_in_iec;
    bool logarithmic_scale;
    bool draw_stacked;
    bool draw_smooth;
    bool resources_memory_in_iec;
    bool network_in_bits;
    bool network_total_unit;
    bool network_total_in_bits;
};



struct MutableProcInfo
  : private procman::NonCopyable
{

MutableProcInfo()
  : vmsize(0UL),
    memres(0UL),
    memshared(0UL),
    memwritable(0UL),
    mem(0UL),
#ifdef HAVE_WNCK
    memxserver(0UL),
#endif
    start_time(0UL),
    cpu_time(0ULL),
    disk_read_bytes_total(0ULL),
    disk_write_bytes_total(0ULL),
    disk_read_bytes_current(0ULL),
    disk_write_bytes_current(0ULL),
    status(0U),
    pcpu(0),
    nice(0)
    {
    }

    std::string user;

    std::string wchan;

    // all these members are filled with libgtop which uses
    // guint64 (to have fixed size data) but we don't need more
    // than an unsigned long (even for 32bit apps on a 64bit
    // kernel) as these data are amounts, not offsets.
    gulong vmsize;
    gulong memres;
    gulong memshared;
    gulong memwritable;
    gulong mem;

#ifdef HAVE_WNCK
    // wnck gives an unsigned long
    gulong memxserver;
#endif

    gulong start_time;
    guint64 cpu_time;
    guint64 disk_read_bytes_total;
    guint64 disk_write_bytes_total;
    guint64 disk_read_bytes_current;
    guint64 disk_write_bytes_current;
    guint status;
    gdouble pcpu;
    gint nice;
    std::string cgroup_name;

    std::string unit;
    std::string session;
    std::string seat;

    std::string owner;
};


class ProcInfo
: public MutableProcInfo
{
  public:
    ProcInfo& operator=(const ProcInfo&) = delete;
    ProcInfo(const ProcInfo&) = delete;
    ProcInfo(pid_t pid);
    // adds one more ref to icon
    void set_icon(Glib::RefPtr<Gdk::Pixbuf> icon);
    void set_user(guint uid);
    std::string lookup_user(guint uid);

    GtkTreeIter     node;
    Glib::RefPtr<Gdk::Pixbuf> pixbuf;
    std::string     tooltip;
    std::string     name;
    std::string     arguments;

    std::string     security_context;

    const pid_t     pid;
    pid_t           ppid;
    guint           uid;
};

class ProcList {
    // TODO: use a set instead
    // sorted by pid. The map has a nice property : it is sorted
    // by pid so this helps a lot when looking for the parent node
    // as ppid is nearly always < pid.
    typedef std::map<pid_t, ProcInfo> List;
    List data;
    std::mutex data_lock;
public:
    std::map<pid_t, unsigned long> cpu_times;
    typedef List::iterator Iterator;
    Iterator begin() { return std::begin(data); }
    Iterator end() { return std::end(data); }
    Iterator erase(Iterator it) {
        std::lock_guard<std::mutex> lg(data_lock);
        return data.erase(it);
    }
    ProcInfo* add(pid_t pid) { return &data.emplace(std::piecewise_construct, std::forward_as_tuple(pid), std::forward_as_tuple(pid)).first->second; }
    void clear() { return data.clear(); }

    ProcInfo* find(pid_t pid);
};

class GsmApplication : public Gtk::Application, private procman::NonCopyable

{
private:
    void load_settings();
    void load_resources();

    void on_preferences_activate(const Glib::VariantBase&);
    void on_lsof_activate(const Glib::VariantBase&);
    void on_help_activate(const Glib::VariantBase&);
    void on_quit_activate(const Glib::VariantBase&);
protected:
    GsmApplication();
public:
    static Glib::RefPtr<GsmApplication> get ();

    void save_config();
    void shutdown();

    ProcList         processes;
    GsmTreeView      *tree;
    GtkRevealer      *proc_actionbar_revealer;
    GtkMenu          *popup_menu;
    GsmTreeView      *disk_list;
    GtkStack         *stack;
    GtkButton        *refresh_button;
    GtkMenuButton    *process_menu_button;
    GtkMenuButton    *window_menu_button;
    GtkButton        *end_process_button;
    GtkButton        *search_button;
    GtkSearchEntry   *search_entry;
    GtkSearchBar     *search_bar;
    ProcConfig        config;
    LoadGraph        *cpu_graph;
    LoadGraph        *mem_graph;
    LoadGraph        *net_graph;
    gint              cpu_label_fixed_width;
    gint              net_label_fixed_width;
    GtkTreeSelection *selection;
    guint             timeout;
    guint             disk_timeout;

    GtkTreePath      *top_of_tree;
    gdouble          last_vscroll_max;
    gdouble          last_vscroll_value;

    PrettyTable      *pretty_table;

    Glib::RefPtr<Gio::Settings> settings;
    HdyApplicationWindow *main_window;

    unsigned         frequency;

    SmoothRefresh    *smooth_refresh;

    guint64           cpu_total_time;
    guint64           cpu_total_time_last;

protected:
    virtual void on_activate();
    virtual int on_command_line(const Glib::RefPtr<Gio::ApplicationCommandLine>& command_line);
    virtual void on_startup();
};

#endif /* _GSM_APPLICATION_H_ */