@@ -697,11 +697,40 @@ static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
697
697
return true;
698
698
}
699
699
700
+ static unsigned int calculate_io_allowed (u32 iops_limit ,
701
+ unsigned long jiffy_elapsed )
702
+ {
703
+ unsigned int io_allowed ;
704
+ u64 tmp ;
705
+
706
+ /*
707
+ * jiffy_elapsed should not be a big value as minimum iops can be
708
+ * 1 then at max jiffy elapsed should be equivalent of 1 second as we
709
+ * will allow dispatch after 1 second and after that slice should
710
+ * have been trimmed.
711
+ */
712
+
713
+ tmp = (u64 )iops_limit * jiffy_elapsed ;
714
+ do_div (tmp , HZ );
715
+
716
+ if (tmp > UINT_MAX )
717
+ io_allowed = UINT_MAX ;
718
+ else
719
+ io_allowed = tmp ;
720
+
721
+ return io_allowed ;
722
+ }
723
+
724
+ static u64 calculate_bytes_allowed (u64 bps_limit , unsigned long jiffy_elapsed )
725
+ {
726
+ return mul_u64_u64_div_u64 (bps_limit , (u64 )jiffy_elapsed , (u64 )HZ );
727
+ }
728
+
700
729
/* Trim the used slices and adjust slice start accordingly */
701
730
static inline void throtl_trim_slice (struct throtl_grp * tg , bool rw )
702
731
{
703
- unsigned long nr_slices , time_elapsed , io_trim ;
704
- u64 bytes_trim , tmp ;
732
+ unsigned long time_elapsed , io_trim ;
733
+ u64 bytes_trim ;
705
734
706
735
BUG_ON (time_before (tg -> slice_end [rw ], tg -> slice_start [rw ]));
707
736
@@ -723,19 +752,14 @@ static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
723
752
724
753
throtl_set_slice_end (tg , rw , jiffies + tg -> td -> throtl_slice );
725
754
726
- time_elapsed = jiffies - tg -> slice_start [rw ];
727
-
728
- nr_slices = time_elapsed / tg -> td -> throtl_slice ;
729
-
730
- if (!nr_slices )
755
+ time_elapsed = rounddown (jiffies - tg -> slice_start [rw ],
756
+ tg -> td -> throtl_slice );
757
+ if (!time_elapsed )
731
758
return ;
732
- tmp = tg_bps_limit (tg , rw ) * tg -> td -> throtl_slice * nr_slices ;
733
- do_div (tmp , HZ );
734
- bytes_trim = tmp ;
735
-
736
- io_trim = (tg_iops_limit (tg , rw ) * tg -> td -> throtl_slice * nr_slices ) /
737
- HZ ;
738
759
760
+ bytes_trim = calculate_bytes_allowed (tg_bps_limit (tg , rw ),
761
+ time_elapsed );
762
+ io_trim = calculate_io_allowed (tg_iops_limit (tg , rw ), time_elapsed );
739
763
if (!bytes_trim && !io_trim )
740
764
return ;
741
765
@@ -749,41 +773,13 @@ static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
749
773
else
750
774
tg -> io_disp [rw ] = 0 ;
751
775
752
- tg -> slice_start [rw ] += nr_slices * tg -> td -> throtl_slice ;
776
+ tg -> slice_start [rw ] += time_elapsed ;
753
777
754
778
throtl_log (& tg -> service_queue ,
755
779
"[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu" ,
756
- rw == READ ? 'R' : 'W' , nr_slices , bytes_trim , io_trim ,
757
- tg -> slice_start [rw ], tg -> slice_end [rw ], jiffies );
758
- }
759
-
760
- static unsigned int calculate_io_allowed (u32 iops_limit ,
761
- unsigned long jiffy_elapsed )
762
- {
763
- unsigned int io_allowed ;
764
- u64 tmp ;
765
-
766
- /*
767
- * jiffy_elapsed should not be a big value as minimum iops can be
768
- * 1 then at max jiffy elapsed should be equivalent of 1 second as we
769
- * will allow dispatch after 1 second and after that slice should
770
- * have been trimmed.
771
- */
772
-
773
- tmp = (u64 )iops_limit * jiffy_elapsed ;
774
- do_div (tmp , HZ );
775
-
776
- if (tmp > UINT_MAX )
777
- io_allowed = UINT_MAX ;
778
- else
779
- io_allowed = tmp ;
780
-
781
- return io_allowed ;
782
- }
783
-
784
- static u64 calculate_bytes_allowed (u64 bps_limit , unsigned long jiffy_elapsed )
785
- {
786
- return mul_u64_u64_div_u64 (bps_limit , (u64 )jiffy_elapsed , (u64 )HZ );
780
+ rw == READ ? 'R' : 'W' , time_elapsed / tg -> td -> throtl_slice ,
781
+ bytes_trim , io_trim , tg -> slice_start [rw ], tg -> slice_end [rw ],
782
+ jiffies );
787
783
}
788
784
789
785
static void __tg_update_carryover (struct throtl_grp * tg , bool rw )
0 commit comments