Size.pm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # ex:ts=8 sw=4:
  2. # $OpenBSD: Size.pm,v 1.7 2016/10/21 00:45:43 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. # this package is responsible for recording build sizes
  20. # and using tmpfs accordingly
  21. package DPB::Heuristics::Size;
  22. my (%wrkdir, %pkgname);
  23. use DPB::Serialize;
  24. sub new
  25. {
  26. my ($class, $state) = @_;
  27. bless {state => $state}, $class;
  28. }
  29. sub add_size_info
  30. {
  31. my ($self, $path, $pkgname, $sz) = @_;
  32. $wrkdir{$path->pkgpath_and_flavors} = $sz;
  33. if (defined $pkgname) {
  34. $pkgname{$path->fullpkgpath} = $pkgname;
  35. }
  36. }
  37. sub match_pkgname
  38. {
  39. my ($self, $v) = @_;
  40. my $p = $pkgname{$v->fullpkgpath};
  41. if (!defined $p) {
  42. return 0;
  43. }
  44. if ($p eq $v->fullpkgname) {
  45. return 1;
  46. }
  47. return 0;
  48. }
  49. my $used_memory = {};
  50. my $used_per_host = {};
  51. sub build_in_memory
  52. {
  53. my ($self, $fh, $core, $v) = @_;
  54. my $t = $core->memory;
  55. return 0 if !defined $t;
  56. # first match previous affinity
  57. if ($v->{affinity}) {
  58. print $fh ">>> Matching affinity: ", $v->{affinity}, "\n";
  59. return $v->{mem_affinity};
  60. }
  61. my $p = $v->pkgpath_and_flavors;
  62. # we build in memory if we know this port and it's light enough
  63. if (defined $wrkdir{$p}) {
  64. my $hostname = $core->hostname;
  65. $used_per_host->{$hostname} //= 0;
  66. print $fh ">>> Compare ", $used_per_host->{$hostname}, " + ",
  67. $wrkdir{$p}, " to ", $t, "\n";
  68. if ($used_per_host->{$hostname} + $wrkdir{$p} <= $t) {
  69. $used_per_host->{$hostname} += $wrkdir{$p};
  70. $used_memory->{$p} = $hostname;
  71. return $wrkdir{$p};
  72. }
  73. }
  74. return 0;
  75. }
  76. sub finished
  77. {
  78. my ($self, $v) = @_;
  79. my $p = $v->pkgpath_and_flavors;
  80. if (defined $used_memory->{$p}) {
  81. my $hostname = $used_memory->{$p};
  82. $used_per_host->{$hostname} -= $wrkdir{$p};
  83. }
  84. }
  85. sub parse_size_file
  86. {
  87. my $self = shift;
  88. my $state = $self->{state};
  89. return if $state->{fetch_only};
  90. open my $fh, '<', $state->opt('S') // $state->{size_log} or return;
  91. print "Reading size stats...";
  92. my @rewrite = ();
  93. while (<$fh>) {
  94. chomp;
  95. my $s = DPB::Serialize::Size->read($_);
  96. push(@rewrite, $s);
  97. $self->add_size_info(DPB::PkgPath->new($s->{pkgpath}),
  98. $s->{pkgname}, $s->{size});
  99. }
  100. close $fh;
  101. print "zapping old stuff...";
  102. $state->{log_user}->rewrite_file($state, $state->{size_log},
  103. sub {
  104. my $fh = shift;
  105. for my $p (sort {$a->{pkgpath} cmp $b->{pkgpath}} @rewrite) {
  106. print $fh DPB::Serialize::Size->write($p), "\n";
  107. }
  108. });
  109. print "Done\n";
  110. }
  111. 1;