State.pm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. # ex:ts=8 sw=4:
  2. # $OpenBSD: State.pm,v 1.20 2017/04/14 16:39:32 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::InteractiveStub;
  20. sub new
  21. {
  22. my $class = shift;
  23. bless {}, $class;
  24. }
  25. sub is_interactive
  26. {
  27. return 0;
  28. }
  29. sub want_report
  30. {
  31. return 1;
  32. }
  33. sub may_ask_for_commands
  34. {
  35. return 0;
  36. }
  37. package DPB::State;
  38. our @ISA = qw(OpenBSD::State);
  39. use OpenBSD::State;
  40. use OpenBSD::Paths;
  41. use DPB::Heuristics;
  42. use DPB::PkgPath;
  43. use DPB::Logger;
  44. use DPB::Config;
  45. use File::Path;
  46. use File::Basename;
  47. use DPB::Core;
  48. use DPB::Core::Init;
  49. use DPB::Locks;
  50. use DPB::Serialize;
  51. sub define_present
  52. {
  53. my ($self, $k) = @_;
  54. return defined $self->{subst}{$k};
  55. }
  56. sub init
  57. {
  58. my $self = shift;
  59. $self->SUPER::init;
  60. $self->{no_exports} = 1;
  61. $self->{heuristics} = DPB::Heuristics->new($self);
  62. $self->{make} = $ENV{MAKE} || OpenBSD::Paths->make;
  63. $self->{starttime} = time();
  64. $self->{interactive} = DPB::InteractiveStub->new;
  65. return $self;
  66. }
  67. sub is_interactive
  68. {
  69. my $self = shift;
  70. return $self->{interactive}->is_interactive;
  71. }
  72. sub want_report
  73. {
  74. my $self = shift;
  75. return $self->{interactive}->want_report;
  76. }
  77. sub may_ask_for_commands
  78. {
  79. my $self = shift;
  80. return $self->{interactive}->may_ask_for_commands;
  81. }
  82. sub startdate
  83. {
  84. my $self = shift;
  85. my @l = gmtime $self->{starttime};
  86. $l[5] += 1900;
  87. $l[4] ++;
  88. return sprintf '%04d-%02d-%02d@%02d:%02d:%02d', @l[5,4,3,2,1,0];
  89. }
  90. sub anchor
  91. {
  92. my ($self, $path) = @_;
  93. if ($self->{chroot}) {
  94. return join('/', $self->{chroot}, $path);
  95. } else {
  96. return $path;
  97. }
  98. }
  99. sub expand_path
  100. {
  101. my ($self, $path) = @_;
  102. $path =~ s/\%L/$self->{logdir}/g;
  103. $path =~ s/\%p/$self->{realports}/g;
  104. $path =~ s/\%h/DPB::Core::Local->hostname/ge;
  105. $path =~ s/\%a/$self->{arch}/g;
  106. $path =~ s/\%t/$self->{starttime}/g;
  107. $path =~ s/\%d/$self->startdate/ge;
  108. $path =~ s/\%f/$self->{realdistdir}/g;
  109. return $path;
  110. }
  111. sub expand_chrooted_path
  112. {
  113. my ($self, $path) = @_;
  114. $path =~ s/\%L/$self->{logdir}/g;
  115. $path =~ s/\%p/$self->{ports}/g;
  116. $path =~ s/\%h/DPB::Core::Local->hostname/ge;
  117. $path =~ s/\%a/$self->{arch}/g;
  118. $path =~ s/\%t/$self->{starttime}/g;
  119. $path =~ s/\%d/$self->startdate/ge;
  120. $path =~ s/\%f/$self->{distdir}/g;
  121. return $path;
  122. }
  123. sub interpret_path
  124. {
  125. my ($state, $path, $do, $scale) = @_;
  126. my $weight;
  127. if ($path =~ s/\=(.*)//) {
  128. $weight = $1;
  129. }
  130. if ($path =~ s/\*(\d+)$//) {
  131. $scale = $1;
  132. }
  133. $path =~ s/\/+$//;
  134. $path =~ s/^\.\/+//;
  135. my $p = DPB::PkgPath->new($path);
  136. if (defined $scale) {
  137. $p->{scaled} = $scale;
  138. }
  139. for my $d (@{$state->{portspath}}) {
  140. if (-d join('/', $d , $p->pkgpath)) {
  141. &$do($p, $weight);
  142. return;
  143. }
  144. }
  145. $state->usage("Bad package path: #1", $path);
  146. }
  147. sub interpret_paths
  148. {
  149. my $state = shift;
  150. my $do = pop;
  151. for my $file (@_) {
  152. my $scale;
  153. if ($file =~ s/\*(\d+)$//) {
  154. $scale = $1;
  155. }
  156. my $fh = $state->logger->open('<', $file);
  157. if (defined $fh) {
  158. while (<$fh>) {
  159. chomp;
  160. s/\s*(?:\#.*)?$//;
  161. next if m/^$/;
  162. $state->interpret_path($_, $do, $scale);
  163. }
  164. } else {
  165. $state->interpret_path($file, $do);
  166. }
  167. }
  168. }
  169. sub handle_options
  170. {
  171. my $state = shift;
  172. DPB::Config->parse_command_line($state);
  173. # at this point, we should know all our ids!
  174. $state->{logger} = DPB::Logger->new($state);
  175. $state->{locker} = DPB::Locks->new($state);
  176. DPB::Core::Init->init_cores($state);
  177. DPB::Core->reap;
  178. $state->sizer->parse_size_file;
  179. DPB::Limiter->setup($state->logger);
  180. $state->{concurrent} = $state->{logger}->append("concurrent");
  181. DPB::Core->reap;
  182. }
  183. sub SUPER_handle_options
  184. {
  185. my ($state, @p) = @_;
  186. $state->SUPER::handle_options(@p);
  187. }
  188. sub logger
  189. {
  190. return shift->{logger};
  191. }
  192. sub heuristics
  193. {
  194. return shift->{heuristics};
  195. }
  196. sub sizer
  197. {
  198. return shift->{sizer};
  199. }
  200. sub locker
  201. {
  202. return shift->{locker};
  203. }
  204. sub stalelocks
  205. {
  206. return shift->locker->{stalelocks};
  207. }
  208. sub builder
  209. {
  210. return shift->{builder};
  211. }
  212. sub engine
  213. {
  214. return shift->{engine};
  215. }
  216. sub grabber
  217. {
  218. return shift->{grabber};
  219. }
  220. sub fetch
  221. {
  222. return shift->{grabber}{fetch};
  223. }
  224. sub make
  225. {
  226. return shift->{make};
  227. }
  228. sub make_args
  229. {
  230. my $self = shift;
  231. my @l = ($self->{make}, "-C", $self->{ports});
  232. if ($self->{build_once}) {
  233. push(@l, 'BUILD_ONCE=Yes');
  234. }
  235. return @l;
  236. }
  237. sub ports
  238. {
  239. return shift->{ports};
  240. }
  241. sub fullrepo
  242. {
  243. return shift->{fullrepo};
  244. }
  245. sub distdir
  246. {
  247. return shift->{realdistdir};
  248. }
  249. sub localarch
  250. {
  251. return shift->{localarch};
  252. }
  253. sub arch
  254. {
  255. return shift->{arch};
  256. }
  257. sub logdir
  258. {
  259. return shift->{logdir};
  260. }
  261. sub parse_build_line
  262. {
  263. return split(/\s+/, shift);
  264. }
  265. sub parse_build_file
  266. {
  267. my ($state, $fname) = @_;
  268. if (!-f $fname) {
  269. my $arch = $state->arch;
  270. if (-f "$fname/$arch/build.log") {
  271. $fname = "$fname/$arch/build.log";
  272. } elsif (-f "$fname/build.log") {
  273. $fname = "$fname/build.log";
  274. }
  275. }
  276. open my $fh, '<', $fname or return;
  277. while (<$fh>) {
  278. next if m/!$/;
  279. my $s = DPB::Serialize::Build->read($_);
  280. next if !defined $s->{size};
  281. my $o = DPB::PkgPath->new($s->{pkgpath});
  282. push(@{$o->{stats}}, $s);
  283. }
  284. }
  285. sub add_build_info
  286. {
  287. my ($state, @consumers) = @_;
  288. for my $p (DPB::PkgPath->seen) {
  289. next unless defined $p->{stats};
  290. my ($i, $time, $sz, $host);
  291. for my $s (@{$p->{stats}}) {
  292. $time += $s->{time};
  293. $sz += $s->{size};
  294. $i++;
  295. $host = $s->{host}; # XXX
  296. }
  297. for my $c (@consumers) {
  298. $c->add_build_info($p, $host, $time/$i, $sz/$i);
  299. }
  300. }
  301. }
  302. sub rewrite_build_info
  303. {
  304. my ($state, $filename) = @_;
  305. $state->{log_user}->rewrite_file($state, $filename,
  306. sub {
  307. my $f = shift;
  308. for my $p (sort {$a->fullpkgpath cmp $b->fullpkgpath}
  309. DPB::PkgPath->seen) {
  310. next unless defined $p->{stats};
  311. shift @{$p->{stats}} while @{$p->{stats}} > 10;
  312. for my $s (@{$p->{stats}}) {
  313. print $f DPB::Serialize::Build->write($s), "\n";
  314. }
  315. delete $p->{stats};
  316. }
  317. });
  318. }
  319. sub handle_build_files
  320. {
  321. my $state = shift;
  322. return if $state->{fetch_only};
  323. return unless defined $state->{build_files};
  324. print "Reading build stats...";
  325. for my $file (@{$state->{build_files}}) {
  326. $state->parse_build_file($file);
  327. }
  328. $state->heuristics->calibrate(DPB::Core::Init->cores);
  329. $state->add_build_info($state->heuristics, "DPB::Job::Port");
  330. if (defined $state->{permanent_log}) {
  331. print "zapping old stuff...";
  332. $state->rewrite_build_info($state->{permanent_log});
  333. print "Done\n";
  334. }
  335. $state->heuristics->finished_parsing;
  336. }
  337. sub find_window_size
  338. {
  339. my ($state, $cont) = @_;
  340. $state->SUPER::find_window_size;
  341. if (defined $state->{reporter}) {
  342. $state->{reporter}->sig_received($cont);
  343. }
  344. }
  345. 1;