summaryrefslogtreecommitdiff
path: root/btt/proc.c
blob: aac49cbd0e2a852a25c8ba51ec00cdef7dddf7ed (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
/*
 * blktrace output analysis: generate a timeline & gather statistics
 *
 * Copyright (C) 2006 Alan D. Brunelle <Alan.Brunelle@hp.com>
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include <string.h>

#include "globals.h"

struct pn_info {
	struct rb_node rb_node;
	struct p_info *pip;
	union {
		char *name;
		__u32 pid;
	}  u;
};

struct rb_root root_pid, root_name;

static void __foreach(struct rb_node *n, void (*f)(struct p_info *, void *),
			void *arg)
{
	if (n) {
		__foreach(n->rb_left, f, arg);
		f(rb_entry(n, struct pn_info, rb_node)->pip, arg);
		__foreach(n->rb_right, f, arg);
	}
}

static void __destroy(struct rb_node *n, int free_name, int free_pip)
{
	if (n) {
		struct pn_info *pnp = rb_entry(n, struct pn_info, rb_node);

		__destroy(n->rb_left, free_name, free_pip);
		__destroy(n->rb_right, free_name, free_pip);

		if (free_name)
			free(pnp->u.name);
		if (free_pip) {
			free(pnp->pip->name);
			region_exit(&pnp->pip->regions);
			free(pnp->pip);
		}
		free(pnp);
	}
}

struct p_info * __find_process_pid(__u32 pid)
{
	struct pn_info *this;
	struct rb_node *n = root_pid.rb_node;

	while (n) {
		this = rb_entry(n, struct pn_info, rb_node);
		if (pid < this->u.pid)
			n = n->rb_left;
		else if (pid > this->u.pid)
			n = n->rb_right;
		else
			return this->pip;
	}

	return NULL;
}

struct p_info *__find_process_name(char *name)
{
	int cmp;
	struct pn_info *this;
	struct rb_node *n = root_name.rb_node;

	while (n) {
		this = rb_entry(n, struct pn_info, rb_node);
		cmp = strcmp(name, this->u.name);
		if (cmp < 0)
			n = n->rb_left;
		else if (cmp > 0)
			n = n->rb_right;
		else
			return this->pip;
	}

	return NULL;
}

static void insert_pid(struct p_info *that, __u32 pid)
{
	struct pn_info *this;
	struct rb_node *parent = NULL;
	struct rb_node **p = &root_pid.rb_node;

	while (*p) {
		parent = *p;
		this = rb_entry(parent, struct pn_info, rb_node);

		if (pid < this->u.pid)
			p = &(*p)->rb_left;
		else if (pid > this->u.pid)
			p = &(*p)->rb_right;
		else
			return;	// Already there
	}

	this = malloc(sizeof(struct pn_info));
	this->u.pid = pid;
	this->pip = that;

	rb_link_node(&this->rb_node, parent, p);
	rb_insert_color(&this->rb_node, &root_pid);
}

static void insert_name(struct p_info *that)
{
	int cmp;
	struct pn_info *this;
	struct rb_node *parent = NULL;
	struct rb_node **p = &root_name.rb_node;

	while (*p) {
		parent = *p;
		this = rb_entry(parent, struct pn_info, rb_node);
		cmp = strcmp(that->name, this->u.name);

		if (cmp < 0)
			p = &(*p)->rb_left;
		else if (cmp > 0)
			p = &(*p)->rb_right;
		else
			return;	// Already there...
	}

	this = malloc(sizeof(struct pn_info));
	this->u.name = strdup(that->name);
	this->pip = that;

	rb_link_node(&this->rb_node, parent, p);
	rb_insert_color(&this->rb_node, &root_name);
}

static void insert(struct p_info *pip)
{
	insert_pid(pip, pip->pid);
	insert_name(pip);
}

static inline struct p_info *pip_alloc(void)
{
	return memset(malloc(sizeof(struct p_info)), 0, sizeof(struct p_info));
}

struct p_info *find_process(__u32 pid, char *name)
{
	struct p_info *pip;

	if (pid != ((__u32)-1)) {
		if ((pip = __find_process_pid(pid)) != NULL)
			return pip;
		else if (name) {
			pip = __find_process_name(name);

			if (pip && pid != pip->pid) {
				/*
				 * This is a process with the same name
				 * as another, but a different PID.
				 *
				 * We'll store a reference in the PID
				 * tree...
				 */
				insert_pid(pip, pid);
			}
			return pip;
		}

		/*
		 * We're here because we have a pid, and no name, but
		 * we didn't find a process ...
		 *
		 * We'll craft one using the pid...
		 */

		name = alloca(256);
		sprintf(name, "pid%09u", pid);
		process_alloc(pid, name);
		return __find_process_pid(pid);
	}

	return __find_process_name(name);
}

void process_alloc(__u32 pid, char *name)
{
	struct p_info *pip = find_process(pid, name);

	if (pip == NULL) {
		pip = pip_alloc();
		pip->pid = pid;
		region_init(&pip->regions);
		pip->last_q = (__u64)-1;
		pip->name = strdup(name);

		insert(pip);
	}
}

void pip_update_q(struct io *iop)
{
	if (iop->pip) {
		if (remapper_dev(iop->dip->device))
			update_lq(&iop->pip->last_q, &iop->pip->avgs.q2q_dm,
								iop->t.time);
		else
			update_lq(&iop->pip->last_q, &iop->pip->avgs.q2q,
								iop->t.time);
		update_qregion(&iop->pip->regions, iop->t.time);
	}
}

void pip_foreach_out(void (*f)(struct p_info *, void *), void *arg)
{
	if (exes == NULL)
		__foreach(root_name.rb_node, f, arg);
	else {
		struct p_info *pip;
		char *exe, *p, *next, *exes_save = strdup(exes);

		p = exes_save;
		while (exes_save != NULL) {
			exe = exes_save;
			if ((next = strchr(exes_save, ',')) != NULL) {
				*next = '\0';
				exes_save = next+1;
			} else
				exes_save = NULL;

			pip = __find_process_name(exe);
			if (pip)
				f(pip, arg);
		}
	}
}

void pip_exit(void)
{
	__destroy(root_pid.rb_node, 0, 0);
	__destroy(root_name.rb_node, 1, 1);
}