Limiter.pm 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # ex:ts=8 sw=4:
  2. # $OpenBSD: Limiter.pm,v 1.7 2015/05/10 08:14:14 espie Exp $
  3. #
  4. # Copyright (c) 2010-2013 Marc Espie <espie@openbsd.org>
  5. #
  6. # Permission to use, copy, modify, and distribute this software for any
  7. # purpose with or without fee is hereby granted, provided that the above
  8. # copyright notice and this permission notice appear in all copies.
  9. #
  10. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. use strict;
  18. use warnings;
  19. # rate-limit some computation run.
  20. # this is a mixin-class.
  21. package DPB::Limiter;
  22. use Time::HiRes qw(time);
  23. use DPB::Util;
  24. use DPB::Clock;
  25. my $temp;
  26. sub setup
  27. {
  28. my ($self, $logger) = @_;
  29. $temp //= DPB::Util->make_hot($logger->append("performance"));
  30. }
  31. sub limit
  32. {
  33. my ($self, $forced, $factor, $tag, $cond, $code) = @_;
  34. $self->{ts} = time();
  35. $self->{start} = 0; # so we can register ourselves
  36. $self->{next_check} //= $self->{ts};
  37. DPB::Clock->register($self);
  38. print $temp "$$\@$self->{ts}: $tag";
  39. if (!($forced && $self->{unchecked}) &&
  40. $self->{ts} < $self->{next_check} && $cond) {
  41. print $temp "-\n";
  42. $self->{unchecked} = 1;
  43. return 0;
  44. }
  45. delete $self->{unchecked};
  46. # actual computation
  47. $self->{start} = time();
  48. &$code;
  49. $self->{end} = time();
  50. # adjust values for next time
  51. my $check_interval = $factor * ($self->{end} - $self->{start});
  52. my $offset = $self->{ts} - $self->{next_check};
  53. $offset /= 2;
  54. $self->{next_check} = $self->{ts} + $check_interval;
  55. if ($offset > 0) {
  56. $self->{next_check} -= $offset;
  57. }
  58. if ($self->{next_check} < $self->{end}) {
  59. $self->{next_check} = $self->{end};
  60. }
  61. print $temp
  62. sprintf("%s %.2f %.2f\n", $forced ? '!' : '+',
  63. $self->{next_check}, $check_interval);
  64. return $check_interval;
  65. }
  66. sub stopped_clock
  67. {
  68. my ($self, $gap, $stopped) = @_;
  69. $self->{start} += $gap;
  70. if ($self->{ts} >= $stopped) {
  71. $self->{ts} += $gap;
  72. }
  73. if ($self->{next_check} >= $stopped) {
  74. $self->{next_check} += $gap;
  75. }
  76. }
  77. 1