xref: /linux/drivers/md/dm-io-tracker.h (revision 39f75da7bcc829ddc4d40bb60d0e95520de7898b)
1 /*
2  * Copyright (C) 2021 Red Hat, Inc. All rights reserved.
3  *
4  * This file is released under the GPL.
5  */
6 
7 #ifndef DM_IO_TRACKER_H
8 #define DM_IO_TRACKER_H
9 
10 #include <linux/jiffies.h>
11 
12 struct dm_io_tracker {
13 	spinlock_t lock;
14 
15 	/*
16 	 * Sectors of in-flight IO.
17 	 */
18 	sector_t in_flight;
19 
20 	/*
21 	 * The time, in jiffies, when this device became idle
22 	 * (if it is indeed idle).
23 	 */
24 	unsigned long idle_time;
25 	unsigned long last_update_time;
26 };
27 
28 static inline void dm_iot_init(struct dm_io_tracker *iot)
29 {
30 	spin_lock_init(&iot->lock);
31 	iot->in_flight = 0ul;
32 	iot->idle_time = 0ul;
33 	iot->last_update_time = jiffies;
34 }
35 
36 static inline bool dm_iot_idle_for(struct dm_io_tracker *iot, unsigned long j)
37 {
38 	bool r = false;
39 
40 	spin_lock_irq(&iot->lock);
41 	if (!iot->in_flight)
42 		r = time_after(jiffies, iot->idle_time + j);
43 	spin_unlock_irq(&iot->lock);
44 
45 	return r;
46 }
47 
48 static inline unsigned long dm_iot_idle_time(struct dm_io_tracker *iot)
49 {
50 	unsigned long r = 0;
51 
52 	spin_lock_irq(&iot->lock);
53 	if (!iot->in_flight)
54 		r = jiffies - iot->idle_time;
55 	spin_unlock_irq(&iot->lock);
56 
57 	return r;
58 }
59 
60 static inline void dm_iot_io_begin(struct dm_io_tracker *iot, sector_t len)
61 {
62 	spin_lock_irq(&iot->lock);
63 	iot->in_flight += len;
64 	spin_unlock_irq(&iot->lock);
65 }
66 
67 static inline void dm_iot_io_end(struct dm_io_tracker *iot, sector_t len)
68 {
69 	unsigned long flags;
70 
71 	if (!len)
72 		return;
73 
74 	spin_lock_irqsave(&iot->lock, flags);
75 	iot->in_flight -= len;
76 	if (!iot->in_flight)
77 		iot->idle_time = jiffies;
78 	spin_unlock_irqrestore(&iot->lock, flags);
79 }
80 
81 #endif
82