xref: /OK3568_Linux_fs/kernel/block/blk-cgroup-rwstat.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun  *
3*4882a593Smuzhiyun  * Legacy blkg rwstat helpers enabled by CONFIG_BLK_CGROUP_RWSTAT.
4*4882a593Smuzhiyun  * Do not use in new code.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun #ifndef _BLK_CGROUP_RWSTAT_H
7*4882a593Smuzhiyun #define _BLK_CGROUP_RWSTAT_H
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/blk-cgroup.h>
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun enum blkg_rwstat_type {
12*4882a593Smuzhiyun 	BLKG_RWSTAT_READ,
13*4882a593Smuzhiyun 	BLKG_RWSTAT_WRITE,
14*4882a593Smuzhiyun 	BLKG_RWSTAT_SYNC,
15*4882a593Smuzhiyun 	BLKG_RWSTAT_ASYNC,
16*4882a593Smuzhiyun 	BLKG_RWSTAT_DISCARD,
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun 	BLKG_RWSTAT_NR,
19*4882a593Smuzhiyun 	BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
20*4882a593Smuzhiyun };
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /*
23*4882a593Smuzhiyun  * blkg_[rw]stat->aux_cnt is excluded for local stats but included for
24*4882a593Smuzhiyun  * recursive.  Used to carry stats of dead children.
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun struct blkg_rwstat {
27*4882a593Smuzhiyun 	struct percpu_counter		cpu_cnt[BLKG_RWSTAT_NR];
28*4882a593Smuzhiyun 	atomic64_t			aux_cnt[BLKG_RWSTAT_NR];
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun struct blkg_rwstat_sample {
32*4882a593Smuzhiyun 	u64				cnt[BLKG_RWSTAT_NR];
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun 
blkg_rwstat_read_counter(struct blkg_rwstat * rwstat,unsigned int idx)35*4882a593Smuzhiyun static inline u64 blkg_rwstat_read_counter(struct blkg_rwstat *rwstat,
36*4882a593Smuzhiyun 		unsigned int idx)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	return atomic64_read(&rwstat->aux_cnt[idx]) +
39*4882a593Smuzhiyun 		percpu_counter_sum_positive(&rwstat->cpu_cnt[idx]);
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun int blkg_rwstat_init(struct blkg_rwstat *rwstat, gfp_t gfp);
43*4882a593Smuzhiyun void blkg_rwstat_exit(struct blkg_rwstat *rwstat);
44*4882a593Smuzhiyun u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
45*4882a593Smuzhiyun 			 const struct blkg_rwstat_sample *rwstat);
46*4882a593Smuzhiyun u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
47*4882a593Smuzhiyun 		       int off);
48*4882a593Smuzhiyun void blkg_rwstat_recursive_sum(struct blkcg_gq *blkg, struct blkcg_policy *pol,
49*4882a593Smuzhiyun 		int off, struct blkg_rwstat_sample *sum);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /**
53*4882a593Smuzhiyun  * blkg_rwstat_add - add a value to a blkg_rwstat
54*4882a593Smuzhiyun  * @rwstat: target blkg_rwstat
55*4882a593Smuzhiyun  * @op: REQ_OP and flags
56*4882a593Smuzhiyun  * @val: value to add
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  * Add @val to @rwstat.  The counters are chosen according to @rw.  The
59*4882a593Smuzhiyun  * caller is responsible for synchronizing calls to this function.
60*4882a593Smuzhiyun  */
blkg_rwstat_add(struct blkg_rwstat * rwstat,unsigned int op,uint64_t val)61*4882a593Smuzhiyun static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
62*4882a593Smuzhiyun 				   unsigned int op, uint64_t val)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	struct percpu_counter *cnt;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	if (op_is_discard(op))
67*4882a593Smuzhiyun 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_DISCARD];
68*4882a593Smuzhiyun 	else if (op_is_write(op))
69*4882a593Smuzhiyun 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_WRITE];
70*4882a593Smuzhiyun 	else
71*4882a593Smuzhiyun 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_READ];
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH);
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	if (op_is_sync(op))
76*4882a593Smuzhiyun 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_SYNC];
77*4882a593Smuzhiyun 	else
78*4882a593Smuzhiyun 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_ASYNC];
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH);
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun /**
84*4882a593Smuzhiyun  * blkg_rwstat_read - read the current values of a blkg_rwstat
85*4882a593Smuzhiyun  * @rwstat: blkg_rwstat to read
86*4882a593Smuzhiyun  *
87*4882a593Smuzhiyun  * Read the current snapshot of @rwstat and return it in the aux counts.
88*4882a593Smuzhiyun  */
blkg_rwstat_read(struct blkg_rwstat * rwstat,struct blkg_rwstat_sample * result)89*4882a593Smuzhiyun static inline void blkg_rwstat_read(struct blkg_rwstat *rwstat,
90*4882a593Smuzhiyun 		struct blkg_rwstat_sample *result)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	int i;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	for (i = 0; i < BLKG_RWSTAT_NR; i++)
95*4882a593Smuzhiyun 		result->cnt[i] =
96*4882a593Smuzhiyun 			percpu_counter_sum_positive(&rwstat->cpu_cnt[i]);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun /**
100*4882a593Smuzhiyun  * blkg_rwstat_total - read the total count of a blkg_rwstat
101*4882a593Smuzhiyun  * @rwstat: blkg_rwstat to read
102*4882a593Smuzhiyun  *
103*4882a593Smuzhiyun  * Return the total count of @rwstat regardless of the IO direction.  This
104*4882a593Smuzhiyun  * function can be called without synchronization and takes care of u64
105*4882a593Smuzhiyun  * atomicity.
106*4882a593Smuzhiyun  */
blkg_rwstat_total(struct blkg_rwstat * rwstat)107*4882a593Smuzhiyun static inline uint64_t blkg_rwstat_total(struct blkg_rwstat *rwstat)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	struct blkg_rwstat_sample tmp = { };
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	blkg_rwstat_read(rwstat, &tmp);
112*4882a593Smuzhiyun 	return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun /**
116*4882a593Smuzhiyun  * blkg_rwstat_reset - reset a blkg_rwstat
117*4882a593Smuzhiyun  * @rwstat: blkg_rwstat to reset
118*4882a593Smuzhiyun  */
blkg_rwstat_reset(struct blkg_rwstat * rwstat)119*4882a593Smuzhiyun static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	int i;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	for (i = 0; i < BLKG_RWSTAT_NR; i++) {
124*4882a593Smuzhiyun 		percpu_counter_set(&rwstat->cpu_cnt[i], 0);
125*4882a593Smuzhiyun 		atomic64_set(&rwstat->aux_cnt[i], 0);
126*4882a593Smuzhiyun 	}
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun /**
130*4882a593Smuzhiyun  * blkg_rwstat_add_aux - add a blkg_rwstat into another's aux count
131*4882a593Smuzhiyun  * @to: the destination blkg_rwstat
132*4882a593Smuzhiyun  * @from: the source
133*4882a593Smuzhiyun  *
134*4882a593Smuzhiyun  * Add @from's count including the aux one to @to's aux count.
135*4882a593Smuzhiyun  */
blkg_rwstat_add_aux(struct blkg_rwstat * to,struct blkg_rwstat * from)136*4882a593Smuzhiyun static inline void blkg_rwstat_add_aux(struct blkg_rwstat *to,
137*4882a593Smuzhiyun 				       struct blkg_rwstat *from)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun 	u64 sum[BLKG_RWSTAT_NR];
140*4882a593Smuzhiyun 	int i;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	for (i = 0; i < BLKG_RWSTAT_NR; i++)
143*4882a593Smuzhiyun 		sum[i] = percpu_counter_sum_positive(&from->cpu_cnt[i]);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	for (i = 0; i < BLKG_RWSTAT_NR; i++)
146*4882a593Smuzhiyun 		atomic64_add(sum[i] + atomic64_read(&from->aux_cnt[i]),
147*4882a593Smuzhiyun 			     &to->aux_cnt[i]);
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun #endif	/* _BLK_CGROUP_RWSTAT_H */
150