Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / md / dm-io-tracker.h
blobbea1ca11855e0cb8e0962af26a092d66b00b188d
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright (C) 2021 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
6 */
8 #ifndef DM_IO_TRACKER_H
9 #define DM_IO_TRACKER_H
11 #include <linux/jiffies.h>
13 struct dm_io_tracker {
14 spinlock_t lock;
17 * Sectors of in-flight IO.
19 sector_t in_flight;
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);
32 iot->in_flight = 0ul;
33 iot->idle_time = 0ul;
34 iot->last_update_time = jiffies;
37 static inline bool dm_iot_idle_for(struct dm_io_tracker *iot, unsigned long j)
39 bool r = false;
41 spin_lock_irq(&iot->lock);
42 if (!iot->in_flight)
43 r = time_after(jiffies, iot->idle_time + j);
44 spin_unlock_irq(&iot->lock);
46 return r;
49 static inline unsigned long dm_iot_idle_time(struct dm_io_tracker *iot)
51 unsigned long r = 0;
53 spin_lock_irq(&iot->lock);
54 if (!iot->in_flight)
55 r = jiffies - iot->idle_time;
56 spin_unlock_irq(&iot->lock);
58 return r;
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)
70 unsigned long flags;
72 if (!len)
73 return;
75 spin_lock_irqsave(&iot->lock, flags);
76 iot->in_flight -= len;
77 if (!iot->in_flight)
78 iot->idle_time = jiffies;
79 spin_unlock_irqrestore(&iot->lock, flags);
82 #endif