summaryrefslogtreecommitdiff
path: root/src/procmap-store.c
blob: 182af959936e49739c91277335609acfccc037a8 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
   This file is part of odin, a memory profiler with fragmentation analysis.

   Copyright (C) 2007 Chris Wilson <chris@chris-wilson.co.uk>

   odin is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 3 of the
   License, or (at your option) any later version.

   odin is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with odin. If not, see <http://www.gnu.org/licenses/>/

   The GNU General Public License is contained in the file COPYING.
*/

#include <gtk/gtk.h>
#include <glibtop.h>
#include <glibtop/procmap.h>

#include "odin.h"
#include "procmap.h"

struct _procmap_store {
    GObject object;

    GPid pid;
    glibtop_proc_map procmap;
    glibtop_map_entry *maps;

    GCompareFunc sort_func;
};

typedef struct _procmap_store_class {
    GObjectClass parent_class;
} ProcmapStoreClass;

static void
procmap_store_tree_model_init (GtkTreeModelIface *iface);

static GType
procmap_store_get_type (void);

G_DEFINE_TYPE_WITH_CODE (ProcmapStore, procmap_store, G_TYPE_OBJECT,
	G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
	                       procmap_store_tree_model_init))

static void
procmap_store_finalize (GObject *obj)
{
    ProcmapStore *self = (ProcmapStore *) obj;

    g_free (self->maps);

    G_OBJECT_CLASS (procmap_store_parent_class)->finalize (obj);
}

static void
procmap_store_class_init (ProcmapStoreClass *klass)
{
    GObjectClass *object_class = (GObjectClass *) klass;

    object_class->finalize = procmap_store_finalize;
}


/* GtkTreeModelIface */

static GtkTreeModelFlags
procmap_store_get_flags (GtkTreeModel *tree_model)
{
    return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
}

static gint
procmap_store_get_n_columns (GtkTreeModel *tree_model)
{
    return PROCMAP_N_COLUMNS;
}

static GType
procmap_store_get_column_type (GtkTreeModel *tree_model,
				  gint          index)
{
    switch (index) {
    case PROCMAP_FILENAME: return G_TYPE_STRING;
    case PROCMAP_MODE: return G_TYPE_STRING;

    case PROCMAP_START: return G_TYPE_ULONG;
    case PROCMAP_END: return G_TYPE_ULONG;
    case PROCMAP_OFFSET: return G_TYPE_ULONG;

    case PROCMAP_SIZE: return G_TYPE_ULONG;
    case PROCMAP_RSS: return G_TYPE_ULONG;

    case PROCMAP_PRIVATE_CLEAN: return G_TYPE_ULONG;
    case PROCMAP_PRIVATE_DIRTY: return G_TYPE_ULONG;
    default: return G_TYPE_INVALID;
    }
}

static gboolean
procmap_store_get_iter (GtkTreeModel *tree_model,
			   GtkTreeIter  *iter,
			   GtkTreePath  *path)
{
    ProcmapStore *self = (ProcmapStore *) tree_model;
    guint n;

    n = gtk_tree_path_get_depth (path);
    g_return_val_if_fail (n == 1, FALSE);

    n = gtk_tree_path_get_indices (path)[0];
    if (n >= self->procmap.number)
	return FALSE;

    iter->user_data = &self->maps[n];
    iter->user_data2 = GUINT_TO_POINTER (n);
    iter->stamp = 1;
    return TRUE;
}

static GtkTreePath *
procmap_store_get_path (GtkTreeModel *tree_model,
			GtkTreeIter  *iter)
{
    GtkTreePath *path = gtk_tree_path_new ();
    gtk_tree_path_append_index (path, GPOINTER_TO_UINT (iter->user_data2));
    return path;
}

static void
procmap_store_get_value (GtkTreeModel *tree_model,
			 GtkTreeIter  *iter,
			 gint          column,
			 GValue       *value)
{
    glibtop_map_entry *entry = iter->user_data;
    switch (column) {
    case PROCMAP_FILENAME:
	g_value_init (value, G_TYPE_STRING);
	g_value_set_string (value,
		entry->flags & (1L << GLIBTOP_MAP_ENTRY_FILENAME) ?
		entry->filename : "");
	break;
    case PROCMAP_MODE: {
	gchar perm[6];
	perm [0] = entry->perm & GLIBTOP_MAP_PERM_READ    ? 'r' : '-';
	perm [1] = entry->perm & GLIBTOP_MAP_PERM_WRITE   ? 'w' : '-';
	perm [2] = entry->perm & GLIBTOP_MAP_PERM_EXECUTE ? 'x' : '-';
	perm [3] = entry->perm & GLIBTOP_MAP_PERM_SHARED  ? 's' : '-';
	perm [4] = entry->perm & GLIBTOP_MAP_PERM_PRIVATE ? 'p' : '-';
	perm [5] = '\0';
	g_value_init (value, G_TYPE_STRING);
	g_value_set_string (value, perm);
	break;
    }

    case PROCMAP_START:
	g_value_init (value, G_TYPE_ULONG);
	g_value_set_ulong (value, entry->start);
	break;
    case PROCMAP_END:
	g_value_init (value, G_TYPE_ULONG);
	g_value_set_ulong (value, entry->end);
	break;
    case PROCMAP_OFFSET:
	g_value_init (value, G_TYPE_ULONG);
	g_value_set_ulong (value, entry->offset);
	break;

    case PROCMAP_SIZE:
	g_value_init (value, G_TYPE_ULONG);
	g_value_set_ulong (value, entry->size);
	break;
    case PROCMAP_RSS:
	g_value_init (value, G_TYPE_ULONG);
	g_value_set_ulong (value, entry->rss);
	break;

    case PROCMAP_PRIVATE_CLEAN:
	g_value_init (value, G_TYPE_ULONG);
	g_value_set_ulong (value, entry->private_clean);
	break;
    case PROCMAP_PRIVATE_DIRTY:
	g_value_init (value, G_TYPE_ULONG);
	g_value_set_ulong (value, entry->private_dirty);
	break;
    }
}

static gboolean
procmap_store_iter_next (GtkTreeModel  *tree_model,
			 GtkTreeIter   *iter)
{
    ProcmapStore *self = (ProcmapStore *) tree_model;
    guint index = GPOINTER_TO_UINT (iter->user_data2);

    if (++index >= self->procmap.number)
	return FALSE;

    iter->user_data = &self->maps[index];
    iter->user_data2 = GUINT_TO_POINTER (index);
    return TRUE;
}

static gboolean
procmap_store_iter_children (GtkTreeModel *tree_model,
			     GtkTreeIter  *iter,
			     GtkTreeIter  *parent)
{
    return parent == NULL;
}

static gboolean
procmap_store_iter_has_child (GtkTreeModel *tree_model,
			      GtkTreeIter  *iter)
{
    return FALSE;
}

static gint
procmap_store_iter_n_children (GtkTreeModel *tree_model,
			       GtkTreeIter  *iter)
{
    return 0;
}

static gboolean
procmap_store_iter_nth_child (GtkTreeModel *tree_model,
			         GtkTreeIter  *iter,
				 GtkTreeIter  *parent,
				 gint          n)
{
    ProcmapStore *self = (ProcmapStore *) tree_model;

    if (parent != NULL)
	return FALSE;

    if ((guint) n >= self->procmap.number)
	return FALSE;

    iter->stamp = 1;
    iter->user_data = &self->maps[n];
    iter->user_data2 = GUINT_TO_POINTER (n);
    return TRUE;
}

static gboolean
procmap_store_iter_parent (GtkTreeModel *tree_model,
			      GtkTreeIter  *iter,
			      GtkTreeIter  *child)
{
    return FALSE;
}

static void
procmap_store_tree_model_init (GtkTreeModelIface *iface)
{
    iface->get_flags = procmap_store_get_flags;
    iface->get_n_columns = procmap_store_get_n_columns;
    iface->get_column_type = procmap_store_get_column_type;
    iface->get_iter = procmap_store_get_iter;
    iface->get_path = procmap_store_get_path;
    iface->get_value = procmap_store_get_value;
    iface->iter_next = procmap_store_iter_next;
    iface->iter_children = procmap_store_iter_children;
    iface->iter_has_child = procmap_store_iter_has_child;
    iface->iter_n_children = procmap_store_iter_n_children;
    iface->iter_nth_child = procmap_store_iter_nth_child;
    iface->iter_parent = procmap_store_iter_parent;
}

static gint
procmap_store_sort_by_dirty (gconstpointer A, gconstpointer B)
{
    const glibtop_map_entry *a = A, *b = B;
    return b->private_dirty - a->private_dirty;
}

static gint
procmap_store_sort_by_addr (gconstpointer A, gconstpointer B)
{
    const glibtop_map_entry *a = A, *b = B;
    return b->start - a->start;
}

static void
procmap_store_init (ProcmapStore *self)
{
    //self->sort_func = procmap_store_sort_by_dirty;
    self->sort_func = procmap_store_sort_by_addr;
}


void
procmap_store_update (ProcmapStore *self, GPid pid)
{
    g_free (self->maps);

    self->pid = pid;
    self->maps = glibtop_get_proc_map (&self->procmap, (pid_t) pid);

    qsort (self->maps,
	   self->procmap.number, sizeof (glibtop_map_entry),
	   self->sort_func);
}

GtkTreeModel *
procmap_store_new (GPid pid)
{
    ProcmapStore *self;

    self = g_object_new (procmap_store_get_type (), NULL);
    if (pid)
	procmap_store_update (self, pid);

    return (GtkTreeModel *) self;
}