FetchQueue.pm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # ex:ts=8 sw=4:
  2. # $OpenBSD: FetchQueue.pm,v 1.2 2013/10/06 13:33:37 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. package DPB::Heuristics::FetchQueue;
  20. our @ISA = qw(DPB::Heuristics::Queue);
  21. sub new
  22. {
  23. my ($class, $h) = @_;
  24. $class->SUPER::new($h)->set_h1;
  25. }
  26. sub set_h1
  27. {
  28. bless shift, "DPB::Heuristics::FetchQueue1";
  29. }
  30. sub set_h2
  31. {
  32. bless shift, "DPB::Heuristics::FetchQueue2";
  33. }
  34. sub set_fetchonly
  35. {
  36. bless shift, "DPB::Heuristics::FetchOnlyQueue";
  37. }
  38. sub sorted
  39. {
  40. my $self = shift;
  41. if ($self->{results}++ > 50 ||
  42. defined $self->{sorted} && @{$self->{sorted}} < 10) {
  43. $self->{results} = 0;
  44. undef $self->{sorted};
  45. }
  46. return $self->{sorted} //= DPB::Heuristics::SimpleSorter->new($self);
  47. }
  48. package DPB::Heuristics::FetchQueue1;
  49. our @ISA = qw(DPB::Heuristics::FetchQueue);
  50. # heuristic 1: grab the smallest distfiles that can build directly
  51. # so that we avoid queue starvation
  52. sub sorted_values
  53. {
  54. my $self = shift;
  55. my @l = grep {$_->{path}{has} == 0} values %{$self->{o}};
  56. if (!@l) {
  57. @l = grep {$_->{path}{has} < 2} values %{$self->{o}};
  58. if (!@l) {
  59. @l = values %{$self->{o}};
  60. }
  61. }
  62. return [sort {$b->{sz} <=> $a->{sz}} @l];
  63. }
  64. package DPB::Heuristics::FetchQueue2;
  65. our @ISA = qw(DPB::Heuristics::FetchQueue);
  66. # heuristic 2: assume we're running good enough, grab distfiles that allow
  67. # build to proceed as usual
  68. # we don't care so much about multiple distfiles
  69. sub sorted_values
  70. {
  71. my $self = shift;
  72. my @l = grep {$_->{path}{has} < 2} values %{$self->{o}};
  73. if (!@l) {
  74. @l = values %{$self->{o}};
  75. }
  76. my $h = $self->{h};
  77. return [sort
  78. {$h->measure($a->{path}) <=> $h->measure($b->{path})}
  79. @l];
  80. }
  81. package DPB::Heuristics::FetchOnlyQueue;
  82. our @ISA = qw(DPB::Heuristics::FetchQueue);
  83. # for fetch-only, grab all files, largest ones first.
  84. sub sorted_values
  85. {
  86. my $self = shift;
  87. return [sort {$a->{sz} <=> $b->{sz}} values %{$self->{o}}];
  88. }
  89. 1;