Add config.h support : will fix the LARGEFILE problem
[lttv.git] / ltt / branches / poly / lttv / modules / gui / controlflow / processlist.c
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 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <gtk/gtk.h>
24 #include <glib.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <math.h>
28
29 #include "processlist.h"
30 #include "drawing.h"
31 #include "drawitem.h"
32
33 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
34 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
35
36 /* Preallocated Size of the index_to_pixmap array */
37 #define ALLOCATE_PROCESSES 1000
38
39 /*****************************************************************************
40 * Methods to synchronize process list *
41 *****************************************************************************/
42
43
44 gint process_sort_func ( GtkTreeModel *model,
45 GtkTreeIter *it_a,
46 GtkTreeIter *it_b,
47 gpointer user_data)
48 {
49 gchar *a_name;
50 guint a_pid, a_ppid, a_cpu;
51 gulong a_birth_s, a_birth_ns;
52 gulong a_trace;
53
54 gchar *b_name;
55 guint b_pid, b_ppid, b_cpu;
56 gulong b_birth_s, b_birth_ns;
57 gulong b_trace;
58
59 gtk_tree_model_get(model,
60 it_a,
61 0, &a_name,
62 1, &a_pid,
63 2, &a_ppid,
64 3, &a_cpu,
65 4, &a_birth_s,
66 5, &a_birth_ns,
67 6, &a_trace,
68 -1);
69
70 gtk_tree_model_get(model,
71 it_b,
72 0, &b_name,
73 1, &b_pid,
74 2, &b_ppid,
75 3, &b_cpu,
76 4, &b_birth_s,
77 5, &b_birth_ns,
78 6, &b_trace,
79 -1);
80
81
82 /* Order by PID */
83 if(a_pid == 0 && b_pid == 0) {
84 /* If 0, order by CPU */
85 if(a_cpu > b_cpu) return 1;
86 if(a_cpu < b_cpu) return -1;
87
88 } else { /* if not 0, order by pid */
89
90 if(a_pid > b_pid) return 1;
91 if(a_pid < b_pid) return -1;
92 }
93
94 /* Order by birth second */
95
96 if(a_birth_s > b_birth_s) return 1;
97 if(a_birth_s < b_birth_s) return -1;
98
99
100 /* Order by birth nanosecond */
101 if(a_birth_ns > b_birth_ns) return 1;
102 if(a_birth_ns < b_birth_ns) return -1;
103
104 /* Order by trace_num */
105 if(a_trace > b_trace) return 1;
106 if(a_trace < b_trace) return -1;
107
108 return 0;
109
110 }
111
112 static guint process_list_hash_fct(gconstpointer key)
113 {
114 guint pid = ((const ProcessInfo*)key)->pid;
115 return ((pid>>8 ^ pid>>4 ^ pid>>2 ^ pid) ^ ((const ProcessInfo*)key)->cpu);
116 }
117
118 /* If hash is good, should be different */
119 static gboolean process_list_equ_fct(gconstpointer a, gconstpointer b)
120 {
121 const ProcessInfo *pa = (const ProcessInfo*)a;
122 const ProcessInfo *pb = (const ProcessInfo*)b;
123
124 gboolean ret = TRUE;
125
126 if(likely(pa->pid != pb->pid))
127 ret = FALSE;
128 else if(likely((pa->pid == 0 && (pa->cpu != pb->cpu))))
129 ret = FALSE;
130 else if(unlikely(ltt_time_compare(pa->birth, pb->birth) != 0))
131 ret = FALSE;
132 else if(unlikely(pa->trace_num != pb->trace_num))
133 ret = FALSE;
134
135 return ret;
136 }
137
138 void destroy_hash_key(gpointer key);
139
140 void destroy_hash_data(gpointer data);
141
142
143 static void update_index_to_pixmap_each(ProcessInfo *key,
144 HashedProcessData *value,
145 ProcessList *process_list)
146 {
147 guint array_index = processlist_get_index_from_data(process_list, value);
148
149 g_assert(array_index < process_list->index_to_pixmap->len);
150
151 GdkPixmap **pixmap =
152 (GdkPixmap**)&g_ptr_array_index(process_list->index_to_pixmap, array_index);
153
154 *pixmap = value->pixmap;
155 }
156
157
158 void update_index_to_pixmap(ProcessList *process_list)
159 {
160 g_ptr_array_set_size(process_list->index_to_pixmap,
161 g_hash_table_size(process_list->process_hash));
162 g_hash_table_foreach(process_list->process_hash,
163 (GHFunc)update_index_to_pixmap_each,
164 process_list);
165 }
166
167
168 static void update_pixmap_size_each(ProcessInfo *key,
169 HashedProcessData *value,
170 guint width)
171 {
172 GdkPixmap *old_pixmap = value->pixmap;
173
174 value->pixmap =
175 gdk_pixmap_new(old_pixmap,
176 width,
177 value->height,
178 -1);
179
180 gdk_pixmap_unref(old_pixmap);
181 }
182
183
184 void update_pixmap_size(ProcessList *process_list, guint width)
185 {
186 g_hash_table_foreach(process_list->process_hash,
187 (GHFunc)update_pixmap_size_each,
188 (gpointer)width);
189 }
190
191
192 typedef struct _CopyPixmap {
193 GdkDrawable *dest;
194 GdkGC *gc;
195 GdkDrawable *src;
196 gint xsrc, ysrc, xdest, ydest, width, height;
197 } CopyPixmap;
198
199 static void copy_pixmap_region_each(ProcessInfo *key,
200 HashedProcessData *value,
201 CopyPixmap *cp)
202 {
203 GdkPixmap *src = cp->src;
204 GdkPixmap *dest = cp->dest;
205
206 if(dest == NULL)
207 dest = value->pixmap;
208 if(src == NULL)
209 src = value->pixmap;
210
211 gdk_draw_drawable (dest,
212 cp->gc,
213 src,
214 cp->xsrc, cp->ysrc,
215 cp->xdest, cp->ydest,
216 cp->width, cp->height);
217 }
218
219
220
221
222 void copy_pixmap_region(ProcessList *process_list, GdkDrawable *dest,
223 GdkGC *gc, GdkDrawable *src,
224 gint xsrc, gint ysrc,
225 gint xdest, gint ydest, gint width, gint height)
226 {
227 CopyPixmap cp = { dest, gc, src, xsrc, ysrc, xdest, ydest, width, height };
228
229 g_hash_table_foreach(process_list->process_hash,
230 (GHFunc)copy_pixmap_region_each,
231 &cp);
232 }
233
234
235
236 typedef struct _RectanglePixmap {
237 gboolean filled;
238 gint x, y, width, height;
239 GdkGC *gc;
240 } RectanglePixmap;
241
242 static void rectangle_pixmap_each(ProcessInfo *key,
243 HashedProcessData *value,
244 RectanglePixmap *rp)
245 {
246 if(rp->height == -1)
247 rp->height = value->height;
248
249 gdk_draw_rectangle (value->pixmap,
250 rp->gc,
251 rp->filled,
252 rp->x, rp->y,
253 rp->width, rp->height);
254 }
255
256
257
258
259 void rectangle_pixmap(ProcessList *process_list, GdkGC *gc,
260 gboolean filled, gint x, gint y, gint width, gint height)
261 {
262 RectanglePixmap rp = { filled, x, y, width, height, gc };
263
264 g_hash_table_foreach(process_list->process_hash,
265 (GHFunc)rectangle_pixmap_each,
266 &rp);
267 }
268
269
270 /* Renders each pixmaps into on big drawable */
271 void copy_pixmap_to_screen(ProcessList *process_list,
272 GdkDrawable *dest,
273 GdkGC *gc,
274 gint x, gint y,
275 gint width, gint height)
276 {
277 if(process_list->index_to_pixmap->len == 0) return;
278 guint cell_height = process_list->cell_height;
279
280 /* Get indexes */
281 gint begin = floor(y/(double)cell_height);
282 gint end = MIN(ceil((y+height)/(double)cell_height),
283 process_list->index_to_pixmap->len);
284 gint i;
285
286 for(i=begin; i<end; i++) {
287 g_assert(i<process_list->index_to_pixmap->len);
288 /* Render the pixmap to the screen */
289 GdkPixmap *pixmap =
290 (GdkPixmap*)g_ptr_array_index(process_list->index_to_pixmap, i);
291
292 gdk_draw_drawable (dest,
293 gc,
294 pixmap,
295 x, 0,
296 x, i*cell_height,
297 width, cell_height);
298
299 }
300
301
302 }
303
304
305
306
307
308
309
310
311
312 ProcessList *processlist_construct(void)
313 {
314 GtkTreeViewColumn *column;
315 GtkCellRenderer *renderer;
316
317 ProcessList* process_list = g_new(ProcessList,1);
318
319 process_list->number_of_process = 0;
320
321 process_list->current_hash_data = NULL;
322
323 /* Create the Process list */
324 process_list->list_store = gtk_list_store_new ( N_COLUMNS,
325 G_TYPE_STRING,
326 G_TYPE_UINT,
327 G_TYPE_UINT,
328 G_TYPE_UINT,
329 G_TYPE_ULONG,
330 G_TYPE_ULONG,
331 G_TYPE_ULONG);
332
333
334 process_list->process_list_widget =
335 gtk_tree_view_new_with_model
336 (GTK_TREE_MODEL (process_list->list_store));
337
338 g_object_unref (G_OBJECT (process_list->list_store));
339
340 gtk_tree_sortable_set_default_sort_func(
341 GTK_TREE_SORTABLE(process_list->list_store),
342 process_sort_func,
343 NULL,
344 NULL);
345
346
347 gtk_tree_sortable_set_sort_column_id(
348 GTK_TREE_SORTABLE(process_list->list_store),
349 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
350 GTK_SORT_ASCENDING);
351
352
353 process_list->process_hash = g_hash_table_new_full(
354 process_list_hash_fct, process_list_equ_fct,
355 destroy_hash_key, destroy_hash_data
356 );
357
358
359 gtk_tree_view_set_headers_visible(
360 GTK_TREE_VIEW(process_list->process_list_widget), TRUE);
361
362 /* Create a column, associating the "text" attribute of the
363 * cell_renderer to the first column of the model */
364 /* Columns alignment : 0.0 : Left 0.5 : Center 1.0 : Right */
365 renderer = gtk_cell_renderer_text_new ();
366 process_list->renderer = renderer;
367
368 gtk_cell_renderer_get_size(renderer,
369 GTK_WIDGET(process_list->process_list_widget),
370 NULL,
371 NULL,
372 NULL,
373 NULL,
374 &process_list->cell_height);
375
376 guint ypad;
377 g_object_get(G_OBJECT(renderer), "ypad", &ypad, NULL);
378
379 process_list->cell_height += ypad;
380
381 column = gtk_tree_view_column_new_with_attributes ( "Process",
382 renderer,
383 "text",
384 PROCESS_COLUMN,
385 NULL);
386 gtk_tree_view_column_set_alignment (column, 0.0);
387 gtk_tree_view_column_set_fixed_width (column, 45);
388 gtk_tree_view_append_column (
389 GTK_TREE_VIEW (process_list->process_list_widget), column);
390
391 process_list->button = column->button;
392
393 column = gtk_tree_view_column_new_with_attributes ( "PID",
394 renderer,
395 "text",
396 PID_COLUMN,
397 NULL);
398 gtk_tree_view_append_column (
399 GTK_TREE_VIEW (process_list->process_list_widget), column);
400
401 column = gtk_tree_view_column_new_with_attributes ( "PPID",
402 renderer,
403 "text",
404 PPID_COLUMN,
405 NULL);
406 gtk_tree_view_append_column (
407 GTK_TREE_VIEW (process_list->process_list_widget), column);
408
409 column = gtk_tree_view_column_new_with_attributes ( "CPU",
410 renderer,
411 "text",
412 CPU_COLUMN,
413 NULL);
414 gtk_tree_view_append_column (
415 GTK_TREE_VIEW (process_list->process_list_widget), column);
416
417 column = gtk_tree_view_column_new_with_attributes ( "Birth sec",
418 renderer,
419 "text",
420 BIRTH_S_COLUMN,
421 NULL);
422 gtk_tree_view_append_column (
423 GTK_TREE_VIEW (process_list->process_list_widget), column);
424
425 //gtk_tree_view_column_set_visible(column, 0);
426 //
427 column = gtk_tree_view_column_new_with_attributes ( "Birth nsec",
428 renderer,
429 "text",
430 BIRTH_NS_COLUMN,
431 NULL);
432 gtk_tree_view_append_column (
433 GTK_TREE_VIEW (process_list->process_list_widget), column);
434
435 column = gtk_tree_view_column_new_with_attributes ( "TRACE",
436 renderer,
437 "text",
438 TRACE_COLUMN,
439 NULL);
440 gtk_tree_view_append_column (
441 GTK_TREE_VIEW (process_list->process_list_widget), column);
442
443
444 //gtk_tree_view_column_set_visible(column, 0);
445
446 g_object_set_data_full(
447 G_OBJECT(process_list->process_list_widget),
448 "process_list_Data",
449 process_list,
450 (GDestroyNotify)processlist_destroy);
451
452 process_list->index_to_pixmap = g_ptr_array_sized_new(ALLOCATE_PROCESSES);
453
454 return process_list;
455 }
456
457 void processlist_destroy(ProcessList *process_list)
458 {
459 g_debug("processlist_destroy %p", process_list);
460 g_hash_table_destroy(process_list->process_hash);
461 process_list->process_hash = NULL;
462 g_ptr_array_free(process_list->index_to_pixmap, TRUE);
463
464 g_free(process_list);
465 g_debug("processlist_destroy end");
466 }
467
468 static gboolean remove_hash_item(ProcessInfo *process_info,
469 HashedProcessData *hashed_process_data,
470 ProcessList *process_list)
471 {
472 GtkTreeIter iter;
473
474 iter = hashed_process_data->y_iter;
475
476 gtk_list_store_remove (process_list->list_store, &iter);
477 gdk_pixmap_unref(hashed_process_data->pixmap);
478
479 if(likely(process_list->current_hash_data != NULL)) {
480 if(likely(hashed_process_data ==
481 process_list->current_hash_data[process_info->cpu]))
482 process_list->current_hash_data[process_info->cpu] = NULL;
483 }
484 return TRUE; /* remove the element from the hash table */
485 }
486
487 void processlist_clear(ProcessList *process_list)
488 {
489 g_info("processlist_clear %p", process_list);
490
491 g_hash_table_foreach_remove(process_list->process_hash,
492 (GHRFunc)remove_hash_item,
493 (gpointer)process_list);
494 process_list->number_of_process = 0;
495 update_index_to_pixmap(process_list);
496 }
497
498
499 GtkWidget *processlist_get_widget(ProcessList *process_list)
500 {
501 return process_list->process_list_widget;
502 }
503
504
505 void destroy_hash_key(gpointer key)
506 {
507 g_free(key);
508 }
509
510 void destroy_hash_data(gpointer data)
511 {
512 g_free(data);
513 }
514
515 int processlist_add( ProcessList *process_list,
516 Drawing_t *drawing,
517 guint pid,
518 guint cpu,
519 guint ppid,
520 LttTime *birth,
521 guint trace_num,
522 const gchar *name,
523 guint *height,
524 ProcessInfo **pm_process_info,
525 HashedProcessData **pm_hashed_process_data)
526 {
527 ProcessInfo *Process_Info = g_new(ProcessInfo, 1);
528 HashedProcessData *hashed_process_data = g_new(HashedProcessData, 1);
529 *pm_hashed_process_data = hashed_process_data;
530 *pm_process_info = Process_Info;
531
532 Process_Info->pid = pid;
533 if(pid == 0)
534 Process_Info->cpu = cpu;
535 else
536 Process_Info->cpu = 0;
537 Process_Info->ppid = ppid;
538 Process_Info->birth = *birth;
539 Process_Info->trace_num = trace_num;
540
541 /* When we create it from before state update, we are sure that the
542 * last event occured before the beginning of the global area.
543 *
544 * If it is created after state update, this value (0) will be
545 * overriden by the new state before anything is drawn.
546 */
547 hashed_process_data->x.over = 0;
548 hashed_process_data->x.over_used = FALSE;
549 hashed_process_data->x.over_marked = FALSE;
550 hashed_process_data->x.middle = 0;
551 hashed_process_data->x.middle_used = FALSE;
552 hashed_process_data->x.middle_marked = FALSE;
553 hashed_process_data->x.under = 0;
554 hashed_process_data->x.under_used = FALSE;
555 hashed_process_data->x.under_marked = FALSE;
556 hashed_process_data->next_good_time = ltt_time_zero;
557
558 /* Add a new row to the model */
559 gtk_list_store_append ( process_list->list_store,
560 &hashed_process_data->y_iter);
561
562 gtk_list_store_set ( process_list->list_store, &hashed_process_data->y_iter,
563 PROCESS_COLUMN, name,
564 PID_COLUMN, pid,
565 PPID_COLUMN, ppid,
566 CPU_COLUMN, cpu,
567 BIRTH_S_COLUMN, birth->tv_sec,
568 BIRTH_NS_COLUMN, birth->tv_nsec,
569 TRACE_COLUMN, trace_num,
570 -1);
571 //gtk_tree_view_set_model(GTK_TREE_VIEW(process_list->process_list_widget),
572 // GTK_TREE_MODEL(process_list->list_store));
573 //gtk_container_resize_children(GTK_CONTAINER(process_list->process_list_widget));
574
575 g_hash_table_insert(process_list->process_hash,
576 (gpointer)Process_Info,
577 (gpointer)hashed_process_data);
578
579 process_list->number_of_process++;
580
581 hashed_process_data->height = process_list->cell_height;
582
583 g_assert(hashed_process_data->height != 0);
584
585 *height = hashed_process_data->height * process_list->number_of_process;
586
587 hashed_process_data->pixmap =
588 gdk_pixmap_new(drawing->drawing_area->window,
589 drawing->alloc_width,
590 hashed_process_data->height,
591 -1);
592
593 // Clear the image
594 gdk_draw_rectangle (hashed_process_data->pixmap,
595 drawing->drawing_area->style->black_gc,
596 TRUE,
597 0, 0,
598 drawing->alloc_width,
599 hashed_process_data->height);
600
601 update_index_to_pixmap(process_list);
602
603
604 return 0;
605 }
606
607 int processlist_remove( ProcessList *process_list,
608 guint pid,
609 guint cpu,
610 LttTime *birth,
611 guint trace_num)
612 {
613 ProcessInfo process_info;
614 HashedProcessData *hashed_process_data;
615 GtkTreeIter iter;
616
617 process_info.pid = pid;
618 if(pid == 0)
619 process_info.cpu = cpu;
620 else
621 process_info.cpu = 0;
622 process_info.birth = *birth;
623 process_info.trace_num = trace_num;
624
625
626 hashed_process_data =
627 (HashedProcessData*)g_hash_table_lookup(
628 process_list->process_hash,
629 &process_info);
630 if(likely(hashed_process_data != NULL))
631 {
632 iter = hashed_process_data->y_iter;
633
634 gtk_list_store_remove (process_list->list_store, &iter);
635
636 g_hash_table_remove(process_list->process_hash,
637 &process_info);
638
639 if(likely(process_list->current_hash_data != NULL)) {
640 if(likely(hashed_process_data == process_list->current_hash_data[cpu])) {
641 process_list->current_hash_data[cpu] = NULL;
642 }
643 }
644
645 gdk_pixmap_unref(hashed_process_data->pixmap);
646
647 update_index_to_pixmap(process_list);
648
649 process_list->number_of_process--;
650
651 return 0;
652 } else {
653 return 1;
654 }
655 }
656
657
658 #if 0
659 static inline guint get_cpu_number_from_name(GQuark name)
660 {
661 const gchar *string;
662 char *begin;
663 guint cpu;
664
665 string = g_quark_to_string(name);
666
667 begin = strrchr(string, '/');
668 begin++;
669
670 g_assert(begin != '\0');
671
672 cpu = strtoul(begin, NULL, 10);
673
674 return cpu;
675 }
676 #endif //0
This page took 0.042861 seconds and 4 git commands to generate.