xref: /aosp_15_r20/external/blktrace/btt/dip_rb.c (revision 1a3d31e37cc95e9919fd86900a2b6a555f55952c)
1*1a3d31e3SAndroid Build Coastguard Worker /*
2*1a3d31e3SAndroid Build Coastguard Worker  * blktrace output analysis: generate a timeline & gather statistics
3*1a3d31e3SAndroid Build Coastguard Worker  *
4*1a3d31e3SAndroid Build Coastguard Worker  * Copyright (C) 2006 Alan D. Brunelle <[email protected]>
5*1a3d31e3SAndroid Build Coastguard Worker  *
6*1a3d31e3SAndroid Build Coastguard Worker  *  This program is free software; you can redistribute it and/or modify
7*1a3d31e3SAndroid Build Coastguard Worker  *  it under the terms of the GNU General Public License as published by
8*1a3d31e3SAndroid Build Coastguard Worker  *  the Free Software Foundation; either version 2 of the License, or
9*1a3d31e3SAndroid Build Coastguard Worker  *  (at your option) any later version.
10*1a3d31e3SAndroid Build Coastguard Worker  *
11*1a3d31e3SAndroid Build Coastguard Worker  *  This program is distributed in the hope that it will be useful,
12*1a3d31e3SAndroid Build Coastguard Worker  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13*1a3d31e3SAndroid Build Coastguard Worker  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*1a3d31e3SAndroid Build Coastguard Worker  *  GNU General Public License for more details.
15*1a3d31e3SAndroid Build Coastguard Worker  *
16*1a3d31e3SAndroid Build Coastguard Worker  *  You should have received a copy of the GNU General Public License
17*1a3d31e3SAndroid Build Coastguard Worker  *  along with this program; if not, write to the Free Software
18*1a3d31e3SAndroid Build Coastguard Worker  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19*1a3d31e3SAndroid Build Coastguard Worker  *
20*1a3d31e3SAndroid Build Coastguard Worker  */
21*1a3d31e3SAndroid Build Coastguard Worker #include <stdio.h>
22*1a3d31e3SAndroid Build Coastguard Worker #include "globals.h"
23*1a3d31e3SAndroid Build Coastguard Worker 
rb_insert(struct rb_root * root,struct io * iop)24*1a3d31e3SAndroid Build Coastguard Worker int rb_insert(struct rb_root *root, struct io *iop)
25*1a3d31e3SAndroid Build Coastguard Worker {
26*1a3d31e3SAndroid Build Coastguard Worker 	struct io *__iop;
27*1a3d31e3SAndroid Build Coastguard Worker 	struct rb_node *parent = NULL;
28*1a3d31e3SAndroid Build Coastguard Worker 	struct rb_node **p = &root->rb_node;
29*1a3d31e3SAndroid Build Coastguard Worker 	__u64 __s, s = BIT_START(iop);
30*1a3d31e3SAndroid Build Coastguard Worker 
31*1a3d31e3SAndroid Build Coastguard Worker 	while (*p) {
32*1a3d31e3SAndroid Build Coastguard Worker 		parent = *p;
33*1a3d31e3SAndroid Build Coastguard Worker 		__iop = rb_entry(parent, struct io, rb_node);
34*1a3d31e3SAndroid Build Coastguard Worker 		__s = BIT_START(__iop);
35*1a3d31e3SAndroid Build Coastguard Worker 
36*1a3d31e3SAndroid Build Coastguard Worker 		if (s < __s)
37*1a3d31e3SAndroid Build Coastguard Worker 			p = &(*p)->rb_left;
38*1a3d31e3SAndroid Build Coastguard Worker 		else if (s > __s)
39*1a3d31e3SAndroid Build Coastguard Worker 			p = &(*p)->rb_right;
40*1a3d31e3SAndroid Build Coastguard Worker 		else {
41*1a3d31e3SAndroid Build Coastguard Worker 			rb_replace_node(parent, &iop->rb_node, root);
42*1a3d31e3SAndroid Build Coastguard Worker 			return 1;
43*1a3d31e3SAndroid Build Coastguard Worker 		}
44*1a3d31e3SAndroid Build Coastguard Worker 	}
45*1a3d31e3SAndroid Build Coastguard Worker 
46*1a3d31e3SAndroid Build Coastguard Worker 	rb_link_node(&iop->rb_node, parent, p);
47*1a3d31e3SAndroid Build Coastguard Worker 	rb_insert_color(&iop->rb_node, root);
48*1a3d31e3SAndroid Build Coastguard Worker 	return 1;
49*1a3d31e3SAndroid Build Coastguard Worker }
50*1a3d31e3SAndroid Build Coastguard Worker 
rb_find_sec(struct rb_root * root,__u64 sec)51*1a3d31e3SAndroid Build Coastguard Worker struct io *rb_find_sec(struct rb_root *root, __u64 sec)
52*1a3d31e3SAndroid Build Coastguard Worker {
53*1a3d31e3SAndroid Build Coastguard Worker 	struct io *__iop;
54*1a3d31e3SAndroid Build Coastguard Worker 	struct rb_node *n = root->rb_node;
55*1a3d31e3SAndroid Build Coastguard Worker 
56*1a3d31e3SAndroid Build Coastguard Worker 	while (n) {
57*1a3d31e3SAndroid Build Coastguard Worker 		__iop = rb_entry(n, struct io, rb_node);
58*1a3d31e3SAndroid Build Coastguard Worker 		if (sec < BIT_START(__iop))
59*1a3d31e3SAndroid Build Coastguard Worker 			n = n->rb_left;
60*1a3d31e3SAndroid Build Coastguard Worker 		else if (sec >= BIT_END(__iop))
61*1a3d31e3SAndroid Build Coastguard Worker 			n = n->rb_right;
62*1a3d31e3SAndroid Build Coastguard Worker 		else
63*1a3d31e3SAndroid Build Coastguard Worker 			return __iop;
64*1a3d31e3SAndroid Build Coastguard Worker 	}
65*1a3d31e3SAndroid Build Coastguard Worker 
66*1a3d31e3SAndroid Build Coastguard Worker 	return NULL;
67*1a3d31e3SAndroid Build Coastguard Worker }
68*1a3d31e3SAndroid Build Coastguard Worker 
rb_foreach(struct rb_node * n,struct io * iop,void (* fnc)(struct io * iop,struct io * this),struct list_head * head)69*1a3d31e3SAndroid Build Coastguard Worker void rb_foreach(struct rb_node *n, struct io *iop,
70*1a3d31e3SAndroid Build Coastguard Worker 		      void (*fnc)(struct io *iop, struct io *this),
71*1a3d31e3SAndroid Build Coastguard Worker 		      struct list_head *head)
72*1a3d31e3SAndroid Build Coastguard Worker {
73*1a3d31e3SAndroid Build Coastguard Worker 	if (n) {
74*1a3d31e3SAndroid Build Coastguard Worker 		struct io *this = rb_entry(n, struct io, rb_node);
75*1a3d31e3SAndroid Build Coastguard Worker 		__u64 iop_s = BIT_START(iop), iop_e = BIT_END(iop);
76*1a3d31e3SAndroid Build Coastguard Worker 		__u64 this_s = BIT_START(this), this_e = BIT_END(this);
77*1a3d31e3SAndroid Build Coastguard Worker 
78*1a3d31e3SAndroid Build Coastguard Worker 		if ((iop_s <= this_s) && (this_e <= iop_e)) {
79*1a3d31e3SAndroid Build Coastguard Worker 			if (fnc) fnc(iop, this);
80*1a3d31e3SAndroid Build Coastguard Worker 			if (head)
81*1a3d31e3SAndroid Build Coastguard Worker 				list_add_tail(&this->f_head, head);
82*1a3d31e3SAndroid Build Coastguard Worker 		}
83*1a3d31e3SAndroid Build Coastguard Worker 		if (iop_s < this_s)
84*1a3d31e3SAndroid Build Coastguard Worker 			rb_foreach(n->rb_left, iop, fnc, head);
85*1a3d31e3SAndroid Build Coastguard Worker 		if (this_e < iop_e)
86*1a3d31e3SAndroid Build Coastguard Worker 			rb_foreach(n->rb_right, iop, fnc, head);
87*1a3d31e3SAndroid Build Coastguard Worker 	}
88*1a3d31e3SAndroid Build Coastguard Worker }
89