add drawing clear upon redraw, fun feature
[lttv.git] / ltt / branches / poly / lttv / modules / gui / controlflow / processlist.c
... / ...
CommitLineData
1/* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Mathieu Desnoyers
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
16 * MA 02111-1307, USA.
17 */
18
19#include <gtk/gtk.h>
20#include <glib.h>
21#include <string.h>
22#include <stdlib.h>
23
24#include "processlist.h"
25#include "drawitem.h"
26
27#define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
28#define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
29
30
31/*****************************************************************************
32 * Methods to synchronize process list *
33 *****************************************************************************/
34
35static guint get_cpu_number_from_name(GQuark name);
36
37/* Enumeration of the columns */
38enum
39{
40 PROCESS_COLUMN,
41 PID_COLUMN,
42 PPID_COLUMN,
43 CPU_COLUMN,
44 BIRTH_S_COLUMN,
45 BIRTH_NS_COLUMN,
46 TRACE_COLUMN,
47 N_COLUMNS
48};
49
50
51gint process_sort_func ( GtkTreeModel *model,
52 GtkTreeIter *it_a,
53 GtkTreeIter *it_b,
54 gpointer user_data)
55{
56 GValue a, b;
57
58 memset(&a, 0, sizeof(GValue));
59 memset(&b, 0, sizeof(GValue));
60
61 /* Order by PID */
62 gtk_tree_model_get_value( model,
63 it_a,
64 PID_COLUMN,
65 &a);
66
67 gtk_tree_model_get_value( model,
68 it_b,
69 PID_COLUMN,
70 &b);
71
72 if(G_VALUE_TYPE(&a) == G_TYPE_UINT
73 && G_VALUE_TYPE(&b) == G_TYPE_UINT )
74 {
75 {
76
77 if(g_value_get_uint(&a) == 0 && g_value_get_uint(&b) == 0) {
78
79 GValue cpua, cpub;
80
81 memset(&cpua, 0, sizeof(GValue));
82 memset(&cpub, 0, sizeof(GValue));
83
84 /* If 0, order by CPU */
85 gtk_tree_model_get_value( model,
86 it_a,
87 CPU_COLUMN,
88 &cpua);
89
90 gtk_tree_model_get_value( model,
91 it_b,
92 CPU_COLUMN,
93 &cpub);
94
95 if(G_VALUE_TYPE(&cpua) == G_TYPE_UINT
96 && G_VALUE_TYPE(&cpub) == G_TYPE_UINT )
97 {
98 if(g_value_get_uint(&cpua) > g_value_get_uint(&cpub))
99 {
100 g_value_unset(&cpua);
101 g_value_unset(&cpub);
102 return 1;
103 }
104 if(g_value_get_uint(&cpua) < g_value_get_uint(&cpub))
105 {
106 g_value_unset(&cpua);
107 g_value_unset(&cpub);
108 return 0;
109 }
110 }
111
112 g_value_unset(&cpua);
113 g_value_unset(&cpub);
114
115 } else { /* if not 0, order by pid */
116
117 if(g_value_get_uint(&a) > g_value_get_uint(&b))
118 {
119 g_value_unset(&a);
120 g_value_unset(&b);
121 return 1;
122 }
123 if(g_value_get_uint(&a) < g_value_get_uint(&b))
124 {
125 g_value_unset(&a);
126 g_value_unset(&b);
127 return 0;
128 }
129 }
130 }
131 }
132
133 g_value_unset(&a);
134 g_value_unset(&b);
135
136
137 /* Order by birth second */
138 gtk_tree_model_get_value( model,
139 it_a,
140 BIRTH_S_COLUMN,
141 &a);
142
143 gtk_tree_model_get_value( model,
144 it_b,
145 BIRTH_S_COLUMN,
146 &b);
147
148
149 if(G_VALUE_TYPE(&a) == G_TYPE_ULONG
150 && G_VALUE_TYPE(&b) == G_TYPE_ULONG )
151 {
152 if(g_value_get_ulong(&a) > g_value_get_ulong(&b))
153 {
154 g_value_unset(&a);
155 g_value_unset(&b);
156 return 1;
157 }
158 if(g_value_get_ulong(&a) < g_value_get_ulong(&b))
159 {
160 g_value_unset(&a);
161 g_value_unset(&b);
162 return 0;
163 }
164
165 }
166
167 g_value_unset(&a);
168 g_value_unset(&b);
169
170 /* Order by birth nanosecond */
171 gtk_tree_model_get_value( model,
172 it_a,
173 BIRTH_NS_COLUMN,
174 &a);
175
176 gtk_tree_model_get_value( model,
177 it_b,
178 BIRTH_NS_COLUMN,
179 &b);
180
181
182 if(G_VALUE_TYPE(&a) == G_TYPE_ULONG
183 && G_VALUE_TYPE(&b) == G_TYPE_ULONG )
184 {
185 if(g_value_get_ulong(&a) > g_value_get_ulong(&b))
186 {
187 g_value_unset(&a);
188 g_value_unset(&b);
189 return 1;
190 }
191 if(g_value_get_ulong(&a) < g_value_get_ulong(&b))
192 {
193 g_value_unset(&a);
194 g_value_unset(&b);
195 return 0;
196 }
197
198 }
199
200 g_value_unset(&a);
201 g_value_unset(&b);
202
203 /* Order by trace_num */
204 gtk_tree_model_get_value( model,
205 it_a,
206 TRACE_COLUMN,
207 &a);
208
209 gtk_tree_model_get_value( model,
210 it_b,
211 TRACE_COLUMN,
212 &b);
213
214 if(G_VALUE_TYPE(&a) == G_TYPE_ULONG
215 && G_VALUE_TYPE(&b) == G_TYPE_ULONG )
216 {
217 if(g_value_get_ulong(&a) > g_value_get_ulong(&b))
218 {
219 g_value_unset(&a);
220 g_value_unset(&b);
221 return 1;
222 }
223 if(g_value_get_ulong(&a) < g_value_get_ulong(&b))
224 {
225 g_value_unset(&a);
226 g_value_unset(&b);
227 return 0;
228 }
229
230 }
231
232 return 0;
233
234}
235
236guint hash_fct(gconstpointer key)
237{
238 return ((ProcessInfo*)key)->pid;
239}
240
241gboolean equ_fct(gconstpointer a, gconstpointer b)
242{
243 const ProcessInfo *pa = (const ProcessInfo*)a;
244 const ProcessInfo *pb = (const ProcessInfo*)b;
245
246 if(pa->pid != pb->pid)
247 return 0;
248
249 if((pa->pid == 0 && (pa->cpu != pb->cpu)))
250 return 0;
251
252 if(pa->birth.tv_sec != pb->birth.tv_sec)
253 return 0;
254
255 if(pa->birth.tv_nsec != pb->birth.tv_nsec)
256 return 0;
257
258 if(pa->trace_num != pb->trace_num)
259 return 0;
260
261 return 1;
262}
263
264void destroy_hash_key(gpointer key);
265
266void destroy_hash_data(gpointer data);
267
268
269
270
271ProcessList *processlist_construct(void)
272{
273 GtkTreeViewColumn *column;
274 GtkCellRenderer *renderer;
275
276 ProcessList* process_list = g_new(ProcessList,1);
277
278 process_list->number_of_process = 0;
279
280 /* Create the Process list */
281 process_list->list_store = gtk_list_store_new ( N_COLUMNS,
282 G_TYPE_STRING,
283 G_TYPE_UINT,
284 G_TYPE_UINT,
285 G_TYPE_UINT,
286 G_TYPE_ULONG,
287 G_TYPE_ULONG,
288 G_TYPE_ULONG);
289
290
291 process_list->process_list_widget =
292 gtk_tree_view_new_with_model
293 (GTK_TREE_MODEL (process_list->list_store));
294 g_object_unref (G_OBJECT (process_list->list_store));
295
296 gtk_tree_sortable_set_sort_func(
297 GTK_TREE_SORTABLE(process_list->list_store),
298 PID_COLUMN,
299 process_sort_func,
300 NULL,
301 NULL);
302
303 gtk_tree_sortable_set_sort_column_id(
304 GTK_TREE_SORTABLE(process_list->list_store),
305 PID_COLUMN,
306 GTK_SORT_ASCENDING);
307
308 process_list->process_hash = g_hash_table_new_full(
309 hash_fct, equ_fct,
310 destroy_hash_key, destroy_hash_data
311 );
312
313
314 gtk_tree_view_set_headers_visible(
315 GTK_TREE_VIEW(process_list->process_list_widget), TRUE);
316
317 /* Create a column, associating the "text" attribute of the
318 * cell_renderer to the first column of the model */
319 /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */
320 renderer = gtk_cell_renderer_text_new ();
321 column = gtk_tree_view_column_new_with_attributes ( "Process",
322 renderer,
323 "text",
324 PROCESS_COLUMN,
325 NULL);
326 gtk_tree_view_column_set_alignment (column, 0.0);
327 gtk_tree_view_column_set_fixed_width (column, 45);
328 gtk_tree_view_append_column (
329 GTK_TREE_VIEW (process_list->process_list_widget), column);
330
331 process_list->button = column->button;
332
333 column = gtk_tree_view_column_new_with_attributes ( "PID",
334 renderer,
335 "text",
336 PID_COLUMN,
337 NULL);
338 gtk_tree_view_append_column (
339 GTK_TREE_VIEW (process_list->process_list_widget), column);
340
341 column = gtk_tree_view_column_new_with_attributes ( "PPID",
342 renderer,
343 "text",
344 PPID_COLUMN,
345 NULL);
346 gtk_tree_view_append_column (
347 GTK_TREE_VIEW (process_list->process_list_widget), column);
348
349 column = gtk_tree_view_column_new_with_attributes ( "CPU",
350 renderer,
351 "text",
352 CPU_COLUMN,
353 NULL);
354 gtk_tree_view_append_column (
355 GTK_TREE_VIEW (process_list->process_list_widget), column);
356
357 column = gtk_tree_view_column_new_with_attributes ( "Birth sec",
358 renderer,
359 "text",
360 BIRTH_S_COLUMN,
361 NULL);
362 gtk_tree_view_append_column (
363 GTK_TREE_VIEW (process_list->process_list_widget), column);
364
365 //gtk_tree_view_column_set_visible(column, 0);
366 //
367 column = gtk_tree_view_column_new_with_attributes ( "Birth nsec",
368 renderer,
369 "text",
370 BIRTH_NS_COLUMN,
371 NULL);
372 gtk_tree_view_append_column (
373 GTK_TREE_VIEW (process_list->process_list_widget), column);
374
375 column = gtk_tree_view_column_new_with_attributes ( "TRACE",
376 renderer,
377 "text",
378 TRACE_COLUMN,
379 NULL);
380 gtk_tree_view_append_column (
381 GTK_TREE_VIEW (process_list->process_list_widget), column);
382
383
384 //gtk_tree_view_column_set_visible(column, 0);
385
386 g_object_set_data_full(
387 G_OBJECT(process_list->process_list_widget),
388 "process_list_Data",
389 process_list,
390 (GDestroyNotify)processlist_destroy);
391
392 return process_list;
393}
394
395void processlist_destroy(ProcessList *process_list)
396{
397 g_debug("processlist_destroy %p", process_list);
398 g_hash_table_destroy(process_list->process_hash);
399 process_list->process_hash = NULL;
400
401 g_free(process_list);
402 g_debug("processlist_destroy end");
403}
404
405static gboolean remove_hash_item(ProcessInfo *process_info,
406 HashedProcessData *hashed_process_data,
407 ProcessList *process_list)
408{
409 GtkTreePath *tree_path;
410 GtkTreeIter iter;
411
412 tree_path = gtk_tree_row_reference_get_path(
413 hashed_process_data->row_ref);
414
415 gtk_tree_model_get_iter (
416 GTK_TREE_MODEL(process_list->list_store),
417 &iter, tree_path);
418
419 gtk_tree_path_free(tree_path);
420
421 gtk_list_store_remove (process_list->list_store, &iter);
422
423 return TRUE; /* remove the element from the hash table */
424}
425
426void processlist_clear(ProcessList *process_list)
427{
428 g_info("processlist_clear %p", process_list);
429
430 g_hash_table_foreach_remove(process_list->process_hash,
431 (GHRFunc)remove_hash_item,
432 (gpointer)process_list);
433 process_list->number_of_process = 0;
434}
435
436
437GtkWidget *processlist_get_widget(ProcessList *process_list)
438{
439 return process_list->process_list_widget;
440}
441
442
443
444gint get_cell_height(GtkTreeView *tree_view)
445{
446 gint height;
447 GtkTreeViewColumn *Column = gtk_tree_view_get_column(tree_view, 0);
448 //GList *Render_List = gtk_tree_view_column_get_cell_renderers(Column);
449 //GtkCellRenderer *Renderer = g_list_first(Render_List)->data;
450
451 //g_list_free(Render_List);
452 gtk_tree_view_column_cell_get_size(Column, NULL, NULL, NULL, NULL, &height);
453 //g_critical("cell 0 height : %u",height);
454
455 return height;
456}
457
458void destroy_hash_key(gpointer key)
459{
460 g_free(key);
461}
462
463void destroy_hash_data(gpointer data)
464{
465 g_free(data);
466}
467
468int processlist_add( ProcessList *process_list,
469 guint pid,
470 guint cpu,
471 guint ppid,
472 LttTime *birth,
473 guint trace_num,
474 const gchar *name,
475 guint *height,
476 HashedProcessData **pm_hashed_process_data)
477{
478 GtkTreeIter iter ;
479 ProcessInfo *Process_Info = g_new(ProcessInfo, 1);
480 HashedProcessData *hashed_process_data = g_new(HashedProcessData, 1);
481 *pm_hashed_process_data = hashed_process_data;
482
483 Process_Info->pid = pid;
484 if(pid == 0)
485 Process_Info->cpu = cpu;
486 else
487 Process_Info->cpu = 0;
488 Process_Info->ppid = ppid;
489 Process_Info->birth = *birth;
490 Process_Info->trace_num = trace_num;
491
492 /* When we create it from before state update, we are sure that the
493 * last event occured before the beginning of the global area.
494 *
495 * If it is created after state update, this value (0) will be
496 * overriden by the new state before anything is drawn.
497 */
498 hashed_process_data->x = 0;
499
500 /* Add a new row to the model */
501 gtk_list_store_append ( process_list->list_store, &iter);
502 //g_critical ( "iter before : %s", gtk_tree_path_to_string (
503 // gtk_tree_model_get_path (
504 // GTK_TREE_MODEL(process_list->list_store),
505 // &iter)));
506 gtk_list_store_set ( process_list->list_store, &iter,
507 PROCESS_COLUMN, name,
508 PID_COLUMN, pid,
509 PPID_COLUMN, ppid,
510 CPU_COLUMN, get_cpu_number_from_name(cpu),
511 BIRTH_S_COLUMN, birth->tv_sec,
512 BIRTH_NS_COLUMN, birth->tv_nsec,
513 TRACE_COLUMN, trace_num,
514 -1);
515 hashed_process_data->row_ref = gtk_tree_row_reference_new (
516 GTK_TREE_MODEL(process_list->list_store),
517 gtk_tree_model_get_path(
518 GTK_TREE_MODEL(process_list->list_store),
519 &iter));
520 g_hash_table_insert(process_list->process_hash,
521 (gpointer)Process_Info,
522 (gpointer)hashed_process_data);
523
524 //g_critical ( "iter after : %s", gtk_tree_path_to_string (
525 // gtk_tree_model_get_path (
526 // GTK_TREE_MODEL(process_list->list_store),
527 // &iter)));
528 process_list->number_of_process++;
529
530 *height = get_cell_height(GTK_TREE_VIEW(process_list->process_list_widget))
531 * process_list->number_of_process ;
532
533
534 return 0;
535
536}
537
538int processlist_remove( ProcessList *process_list,
539 guint pid,
540 guint cpu,
541 LttTime *birth,
542 guint trace_num)
543{
544 ProcessInfo Process_Info;
545 gint *path_indices;
546 HashedProcessData *hashed_process_data;
547 GtkTreeIter iter;
548
549 Process_Info.pid = pid;
550 Process_Info.cpu = cpu;
551 Process_Info.birth = *birth;
552 Process_Info.trace_num = trace_num;
553
554
555 if(hashed_process_data =
556 (HashedProcessData*)g_hash_table_lookup(
557 process_list->process_hash,
558 &Process_Info))
559 {
560 GtkTreePath *tree_path;
561
562 tree_path = gtk_tree_row_reference_get_path(
563 hashed_process_data->row_ref);
564
565 gtk_tree_model_get_iter (
566 GTK_TREE_MODEL(process_list->list_store),
567 &iter, tree_path);
568
569 gtk_tree_path_free(tree_path);
570
571 gtk_list_store_remove (process_list->list_store, &iter);
572
573 g_hash_table_remove(process_list->process_hash,
574 &Process_Info);
575
576 process_list->number_of_process--;
577
578 return 0;
579 } else {
580 return 1;
581 }
582}
583
584
585guint processlist_get_height(ProcessList *process_list)
586{
587 return get_cell_height(GTK_TREE_VIEW(process_list->process_list_widget))
588 * process_list->number_of_process ;
589}
590
591
592gint processlist_get_process_pixels( ProcessList *process_list,
593 guint pid, guint cpu, LttTime *birth, guint trace_num,
594 guint *y,
595 guint *height,
596 HashedProcessData **pm_hashed_process_data)
597{
598 ProcessInfo Process_Info;
599 gint *path_indices;
600 GtkTreePath *tree_path;
601 HashedProcessData *hashed_process_data = NULL;
602
603 Process_Info.pid = pid;
604 Process_Info.cpu = cpu;
605 Process_Info.birth = *birth;
606 Process_Info.trace_num = trace_num;
607
608 if(hashed_process_data =
609 (HashedProcessData*)g_hash_table_lookup(
610 process_list->process_hash,
611 &Process_Info))
612 {
613 tree_path = gtk_tree_row_reference_get_path(
614 hashed_process_data->row_ref);
615 path_indices = gtk_tree_path_get_indices (tree_path);
616
617 *height = get_cell_height(
618 GTK_TREE_VIEW(process_list->process_list_widget));
619 *y = *height * path_indices[0];
620 *pm_hashed_process_data = hashed_process_data;
621 gtk_tree_path_free(tree_path);
622
623 return 0;
624 } else {
625 *pm_hashed_process_data = hashed_process_data;
626 return 1;
627 }
628
629}
630
631
632gint processlist_get_pixels_from_data( ProcessList *process_list,
633 ProcessInfo *process_info,
634 HashedProcessData *hashed_process_data,
635 guint *y,
636 guint *height)
637{
638 gint *path_indices;
639 GtkTreePath *tree_path;
640
641 tree_path = gtk_tree_row_reference_get_path(
642 hashed_process_data->row_ref);
643 path_indices = gtk_tree_path_get_indices (tree_path);
644
645 *height = get_cell_height(
646 GTK_TREE_VIEW(process_list->process_list_widget));
647 *y = *height * path_indices[0];
648 gtk_tree_path_free(tree_path);
649
650 return 0;
651
652}
653
654static guint get_cpu_number_from_name(GQuark name)
655{
656 /* remember / */
657 const gchar *string;
658 char *begin;
659 guint cpu;
660
661 string = g_quark_to_string(name);
662
663 begin = strrchr(string, '/');
664 begin++;
665
666 g_assert(begin != '\0');
667
668 cpu = strtoul(begin, NULL, 10);
669
670 return cpu;
671}
672
This page took 0.024011 seconds and 4 git commands to generate.