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