[PATCH] Add Alan's btt tool
[blktrace.git] / btt / proc.c
1 /*
2  * blktrace output analysis: generate a timeline & gather statistics
3  *
4  * Copyright (C) 2006 Alan D. Brunelle <Alan.Brunelle@hp.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (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 General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21 #include <string.h>
22
23 #include "globals.h"
24
25 #define N_PID_HASH      128
26 #define PID_HASH(pid)   ((pid) & (N_PID_HASH-1))
27 struct list_head        *pid_heads = NULL;
28
29 static void init_pid_heads(void)
30 {
31         int i;
32
33         pid_heads = zmalloc(N_PID_HASH * sizeof(struct list_head));
34         for (i = 0; i < N_PID_HASH; i++)
35                 INIT_LIST_HEAD(&pid_heads[i]);
36 }
37
38 static inline struct p_info *pid_to_proc(__u32 pid)
39 {
40         struct p_pid *pp;
41         struct list_head *p, *head;
42
43         if (pid_heads == NULL) init_pid_heads();
44
45         head = &pid_heads[PID_HASH(pid)];
46         __list_for_each(p, head) {
47                 pp = list_entry(p, struct p_pid, head);
48                 if (pp->pid == pid)
49                         return pp->pip;
50         }
51
52         return NULL;
53 }
54
55 void insert_proc_hash(struct p_info *pip, __u32 pid)
56 {
57         struct p_pid *pp = zmalloc(sizeof(*pp));
58
59         if (pid_heads == NULL) init_pid_heads();
60
61         pp->pip = pip;
62         pp->pid = pid;
63
64         list_add_tail(&pp->head, &pid_heads[PID_HASH(pid)]);
65 }
66
67 int __find_process_pid(__u32 pid)
68 {
69         return pid_to_proc(pid) != NULL;
70 }
71
72 struct p_info *find_process(__u32 pid, char *name)
73 {
74         struct p_info *pip;
75         struct list_head *p;
76
77         if (pid != ((__u32)-1) && (pip = pid_to_proc(pid)))
78                 return pip;
79
80         if (name) {
81                 __list_for_each(p, &all_procs) {
82                         pip = list_entry(p, struct p_info, head);
83                         if (name && !strcmp(pip->name, name)) {
84                                 if (pid != ((__u32)-1))
85                                         insert_proc_hash(pip, pid);
86                                 return pip;
87                         }
88                 }
89         }
90
91         return NULL;
92 }
93
94 void add_process(__u32 pid, char *name)
95 {
96         struct p_info *pip = find_process(pid, name);
97
98         if (pip == NULL) {
99                 pip = zmalloc(sizeof(*pip));
100
101                 list_add_tail(&pip->head, &all_procs);
102                 insert_proc_hash(pip, pid);
103                 pip->last_q = (__u64)-1;
104                 pip->name = strdup(name);
105                 init_region(&pip->regions);
106         }
107 }
108
109 void pip_update_q(struct io *iop)
110 {
111         if (iop->pip) {
112                 update_lq(&iop->pip->last_q, &iop->pip->avgs.q2q, iop->t.time);
113                 update_qregion(&iop->pip->regions, iop->t.time);
114         }
115 }