1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2021 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
8 #ifndef DM_IO_TRACKER_H
9 #define DM_IO_TRACKER_H
11 #include <linux/jiffies.h>
13 struct dm_io_tracker
{
17 * Sectors of in-flight IO.
22 * The time, in jiffies, when this device became idle
23 * (if it is indeed idle).
25 unsigned long idle_time
;
26 unsigned long last_update_time
;
29 static inline void dm_iot_init(struct dm_io_tracker
*iot
)
31 spin_lock_init(&iot
->lock
);
34 iot
->last_update_time
= jiffies
;
37 static inline bool dm_iot_idle_for(struct dm_io_tracker
*iot
, unsigned long j
)
41 spin_lock_irq(&iot
->lock
);
43 r
= time_after(jiffies
, iot
->idle_time
+ j
);
44 spin_unlock_irq(&iot
->lock
);
49 static inline unsigned long dm_iot_idle_time(struct dm_io_tracker
*iot
)
53 spin_lock_irq(&iot
->lock
);
55 r
= jiffies
- iot
->idle_time
;
56 spin_unlock_irq(&iot
->lock
);
61 static inline void dm_iot_io_begin(struct dm_io_tracker
*iot
, sector_t len
)
63 spin_lock_irq(&iot
->lock
);
64 iot
->in_flight
+= len
;
65 spin_unlock_irq(&iot
->lock
);
68 static inline void dm_iot_io_end(struct dm_io_tracker
*iot
, sector_t len
)
75 spin_lock_irqsave(&iot
->lock
, flags
);
76 iot
->in_flight
-= len
;
78 iot
->idle_time
= jiffies
;
79 spin_unlock_irqrestore(&iot
->lock
, flags
);