dm-io-tracker.h 1.5 KB

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