~noskcaj/ubuntu/vivid/gnome-system-monitor/titlebars

« back to all changes in this revision

Viewing changes to .pc/01-fix_ftbfs_hurd.patch/src/procproperties.cpp

  • Committer: Package Import Robot
  • Author(s): Andreas Henriksson
  • Date: 2014-02-17 22:12:59 UTC
  • mfrom: (1.4.12) (15 sid)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: package-import@ubuntu.com-20140217221259-khdbenb3wt1b81zw
Tags: 3.11.90-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 
2
/* Process properties dialog
 
3
 * Copyright (C) 2010 Krishnan Parthasarathi <krishnan.parthasarathi@gmail.com>
 
4
 *                    Robert Ancell <robert.ancell@canonical.com>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Library General Public
 
17
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 
18
 *
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
 
 
23
#include <glib/gi18n.h>
 
24
#include <glibtop/procmem.h>
 
25
#include <glibtop/procmap.h>
 
26
#include <glibtop/procstate.h>
 
27
#if defined (__linux__)
 
28
#include <asm/param.h>
 
29
#elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
 
30
#include <sys/param.h>
 
31
#include <sys/sysctl.h>
 
32
#endif
 
33
 
 
34
#include "application.h"
 
35
#include "procproperties.h"
 
36
#include "proctable.h"
 
37
#include "util.h"
 
38
 
 
39
enum
 
40
{
 
41
    COL_PROP = 0,
 
42
    COL_VAL,
 
43
    NUM_COLS,
 
44
};
 
45
 
 
46
typedef struct _proc_arg {
 
47
    const gchar *prop;
 
48
    gchar *val;
 
49
} proc_arg;
 
50
 
 
51
static gchar*
 
52
format_memsize(guint64 size)
 
53
{
 
54
    if (size == 0)
 
55
        return g_strdup(_("N/A"));
 
56
    else
 
57
        return g_format_size_full(size, G_FORMAT_SIZE_IEC_UNITS);
 
58
}
 
59
 
 
60
static void
 
61
fill_proc_properties (GtkWidget *tree, ProcInfo *info)
 
62
{
 
63
    guint i;
 
64
    GtkListStore *store;
 
65
    
 
66
    if (!info)
 
67
        return;
 
68
 
 
69
    get_process_memory_writable (info);
 
70
 
 
71
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
 
72
    struct clockinfo cinf;
 
73
    size_t size = sizeof (cinf);
 
74
    int HZ;
 
75
    int mib[] = { CTL_KERN, KERN_CLOCKRATE };
 
76
 
 
77
    if (sysctl (mib, G_N_ELEMENTS (mib), &cinf, &size, NULL, 0) == -1)
 
78
        HZ = 100;
 
79
    else
 
80
        HZ = (cinf.stathz ? cinf.stathz : cinf.hz);
 
81
#endif
 
82
    proc_arg proc_props[] = {
 
83
        { N_("Process Name"), g_strdup_printf("%s", info->name)},
 
84
        { N_("User"), g_strdup_printf("%s (%d)", info->user.c_str(), info->uid)},
 
85
        { N_("Status"), g_strdup(format_process_state(info->status))},
 
86
        { N_("Memory"), format_memsize(info->mem)},
 
87
        { N_("Virtual Memory"), format_memsize(info->vmsize)},
 
88
        { N_("Resident Memory"), format_memsize(info->memres)},
 
89
        { N_("Writable Memory"), format_memsize(info->memwritable)},
 
90
        { N_("Shared Memory"), format_memsize(info->memshared)},
 
91
#ifdef HAVE_WNCK
 
92
        { N_("X Server Memory"), format_memsize(info->memxserver)},
 
93
#endif
 
94
        { N_("CPU"), g_strdup_printf("%d%%", info->pcpu)},
 
95
        { N_("CPU Time"), g_strdup_printf(ngettext("%lld second", "%lld seconds", info->cpu_time/HZ), (unsigned long long)info->cpu_time/HZ) },
 
96
        { N_("Started"), g_strdup_printf("%s", ctime((const time_t*)(&info->start_time)))},
 
97
        { N_("Nice"), g_strdup_printf("%d", info->nice)},
 
98
        { N_("Priority"), g_strdup_printf("%s", procman::get_nice_level(info->nice)) },
 
99
        { N_("ID"), g_strdup_printf("%d", info->pid)},
 
100
        { N_("Security Context"), info->security_context?g_strdup_printf("%s", info->security_context):g_strdup(_("N/A"))},
 
101
        { N_("Command Line"), g_strdup_printf("%s", info->arguments)},
 
102
        { N_("Waiting Channel"), g_strdup_printf("%s", info->wchan)},
 
103
        { N_("Control Group"), info->cgroup_name?g_strdup_printf("%s", info->cgroup_name):g_strdup(_("N/A"))},
 
104
        { NULL, NULL}
 
105
    };
 
106
 
 
107
    store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(tree)));
 
108
    for (i = 0; proc_props[i].prop; i++) {
 
109
        GtkTreeIter iter;
 
110
 
 
111
        if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL(store), &iter, NULL, i)) {
 
112
            gtk_list_store_append(store, &iter);
 
113
            gtk_list_store_set(store, &iter, COL_PROP, gettext(proc_props[i].prop), -1);
 
114
        }
 
115
 
 
116
        gtk_list_store_set(store, &iter, COL_VAL, g_strstrip(proc_props[i].val), -1);
 
117
        g_free(proc_props[i].val);
 
118
    }
 
119
}
 
120
 
 
121
static void
 
122
update_procproperties_dialog (GtkWidget *tree)
 
123
{
 
124
    ProcInfo *info;
 
125
 
 
126
    pid_t pid = GPOINTER_TO_UINT(static_cast<pid_t*>(g_object_get_data (G_OBJECT (tree), "selected_info")));
 
127
    info = ProcInfo::find(pid);
 
128
 
 
129
    fill_proc_properties(tree, info);
 
130
}
 
131
 
 
132
static void
 
133
close_procprop_dialog (GtkDialog *dialog, gint id, gpointer data)
 
134
{
 
135
    GtkWidget *tree = static_cast<GtkWidget*>(data);
 
136
    guint timer;
 
137
 
 
138
    timer = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (tree), "timer"));
 
139
    g_source_remove (timer);
 
140
 
 
141
    gtk_widget_destroy (GTK_WIDGET (dialog));
 
142
}
 
143
 
 
144
static GtkWidget *
 
145
create_procproperties_tree (GsmApplication *app, ProcInfo *info)
 
146
{
 
147
    GtkWidget *tree;
 
148
    GtkListStore *model;
 
149
    GtkTreeViewColumn *column;
 
150
    GtkCellRenderer *cell;
 
151
    gint i;
 
152
 
 
153
    model = gtk_list_store_new (NUM_COLS,
 
154
                                G_TYPE_STRING,  /* Property */
 
155
                                G_TYPE_STRING   /* Value */
 
156
        );
 
157
 
 
158
    tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
 
159
    gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (tree), TRUE);
 
160
    g_object_unref (G_OBJECT (model));
 
161
 
 
162
    for (i = 0; i < NUM_COLS; i++) {
 
163
        cell = gtk_cell_renderer_text_new ();
 
164
 
 
165
        column = gtk_tree_view_column_new_with_attributes (NULL,
 
166
                                                           cell,
 
167
                                                           "text", i,
 
168
                                                           NULL);
 
169
        gtk_tree_view_column_set_resizable (column, TRUE);
 
170
        gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
 
171
    }
 
172
 
 
173
    gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(tree), FALSE);
 
174
    fill_proc_properties(tree, info);
 
175
 
 
176
    return tree;
 
177
}
 
178
 
 
179
static gboolean
 
180
procprop_timer (gpointer data)
 
181
{
 
182
    GtkWidget *tree = static_cast<GtkWidget*>(data);
 
183
    GtkTreeModel *model;
 
184
 
 
185
    model = gtk_tree_view_get_model (GTK_TREE_VIEW (tree));
 
186
    g_assert(model);
 
187
 
 
188
    update_procproperties_dialog (tree);
 
189
 
 
190
    return TRUE;
 
191
}
 
192
 
 
193
static void
 
194
create_single_procproperties_dialog (GtkTreeModel *model, GtkTreePath *path,
 
195
                                     GtkTreeIter *iter, gpointer data)
 
196
{
 
197
    GsmApplication *app = static_cast<GsmApplication *>(data);
 
198
 
 
199
    GtkWidget *procpropdialog;
 
200
    GtkWidget *dialog_vbox, *vbox;
 
201
    GtkWidget *cmd_hbox;
 
202
    GtkWidget *label;
 
203
    GtkWidget *scrolled;
 
204
    GtkWidget *tree;
 
205
    ProcInfo *info;
 
206
    guint timer;
 
207
 
 
208
    gtk_tree_model_get (model, iter, COL_POINTER, &info, -1);
 
209
 
 
210
    if (!info)
 
211
        return;
 
212
 
 
213
    procpropdialog = gtk_dialog_new_with_buttons (_("Process Properties"), NULL,
 
214
                                                  GTK_DIALOG_DESTROY_WITH_PARENT,
 
215
                                                  _("_Close"), GTK_RESPONSE_CLOSE,
 
216
                                                  NULL);
 
217
    gtk_window_set_resizable (GTK_WINDOW (procpropdialog), TRUE);
 
218
    gtk_window_set_default_size (GTK_WINDOW (procpropdialog), 575, 400);
 
219
    gtk_container_set_border_width (GTK_CONTAINER (procpropdialog), 5);
 
220
 
 
221
    vbox = gtk_dialog_get_content_area (GTK_DIALOG (procpropdialog));
 
222
    gtk_box_set_spacing (GTK_BOX (vbox), 2);
 
223
    gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
 
224
 
 
225
    dialog_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
 
226
    gtk_container_set_border_width (GTK_CONTAINER (dialog_vbox), 5);
 
227
    gtk_box_pack_start (GTK_BOX (vbox), dialog_vbox, TRUE, TRUE, 0);
 
228
 
 
229
    cmd_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
 
230
    gtk_box_pack_start (GTK_BOX (dialog_vbox), cmd_hbox, FALSE, FALSE, 0);
 
231
 
 
232
    label = procman_make_label_for_mmaps_or_ofiles (
 
233
        _("Properties of process \"%s\" (PID %u):"),
 
234
        info->name,
 
235
        info->pid);
 
236
 
 
237
    gtk_box_pack_start (GTK_BOX (cmd_hbox),label, FALSE, FALSE, 0);
 
238
 
 
239
    scrolled = gtk_scrolled_window_new (NULL, NULL);
 
240
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
 
241
                                    GTK_POLICY_AUTOMATIC,
 
242
                                    GTK_POLICY_AUTOMATIC);
 
243
    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled),
 
244
                                         GTK_SHADOW_IN);
 
245
 
 
246
    tree = create_procproperties_tree (app, info);
 
247
    gtk_container_add (GTK_CONTAINER (scrolled), tree);
 
248
    g_object_set_data (G_OBJECT (tree), "selected_info", GUINT_TO_POINTER (info->pid));
 
249
 
 
250
    gtk_box_pack_start (GTK_BOX (dialog_vbox), scrolled, TRUE, TRUE, 0);
 
251
    gtk_widget_show_all (scrolled);
 
252
 
 
253
    g_signal_connect (G_OBJECT (procpropdialog), "response",
 
254
                      G_CALLBACK (close_procprop_dialog), tree);
 
255
 
 
256
    gtk_widget_show_all (procpropdialog);
 
257
 
 
258
    timer = g_timeout_add_seconds (5, procprop_timer, tree);
 
259
    g_object_set_data (G_OBJECT (tree), "timer", GUINT_TO_POINTER (timer));
 
260
 
 
261
    update_procproperties_dialog (tree);
 
262
}
 
263
 
 
264
void
 
265
create_procproperties_dialog (GsmApplication *app)
 
266
{
 
267
    gtk_tree_selection_selected_foreach (app->selection, create_single_procproperties_dialog,
 
268
                                         app);
 
269
}