- if (opt.o_bandwidth != 0)
- /* sleep if needed, to honor bandwidth limits */
- bandwidth_ctl_delay(wsize);
+ now = time(NULL);
+ /* sleep if needed, to honor bandwidth limits */
+ if (opt.o_bandwidth != 0) {
+ unsigned long long write_theory;
+
+ write_theory = (now - start_time) * opt.o_bandwidth;
+
+ if (write_theory < write_total) {
+ unsigned long long excess;
+ struct timespec delay;
+
+ excess = write_total - write_theory;
+
+ delay.tv_sec = excess / opt.o_bandwidth;
+ delay.tv_nsec = (excess % opt.o_bandwidth) *
+ NSEC_PER_SEC / opt.o_bandwidth;
+
+ if (now >= last_bw_print + opt.o_report_int) {
+ CT_TRACE("bandwith control: %lluB/s "
+ "excess=%llu sleep for "
+ "%lld.%09lds",
+ opt.o_bandwidth, excess,
+ (long long)delay.tv_sec,
+ delay.tv_nsec);
+ last_bw_print = now;
+ }
+
+ do {
+ rc = nanosleep(&delay, &delay);
+ } while (rc < 0 && errno == EINTR);
+ if (rc < 0) {
+ CT_ERROR(errno, "delay for bandwidth "
+ "control failed to sleep: "
+ "residual=%lld.%09lds",
+ (long long)delay.tv_sec,
+ delay.tv_nsec);
+ rc = 0;
+ }
+ }
+ }