2 # vim:ts=4:sw=4:expandtab
7 use Time::HiRes qw(sleep);
10 our @EXPORT = qw(start_xserver);
13 my $x_socketpath = '/tmp/.X11-unix/X';
15 # reads in a whole file
17 open(my $fh, '<', shift) or return '';
22 # forks an X server process
24 my $keep_xserver_output = shift;
25 my $displaynum = shift;
27 die "Could not fork: $!" unless defined($pid);
29 # Child, close stdout/stderr, then start Xephyr
30 if (!$keep_xserver_output) {
38 push(@complete_run::CLEANUP, sub {
40 # Unlink the X11 socket, Xdmx seems to leave it there.
41 unlink($x_socketpath . $displaynum);
46 return $x_socketpath . $displaynum;
49 # Blocks until the socket paths specified in the given array reference actually
52 my ($sockets_waiting) = @_;
54 # Wait until Xdmx actually runs. Pretty ugly solution, but as long as we
55 # can’t socket-activate X11…
57 @$sockets_waiting = grep { ! -S $_ } @$sockets_waiting;
58 last unless @$sockets_waiting;
63 =head2 start_xserver($parallel)
65 Starts C<$parallel> (or number of cores * 2 if undef) Xephyr processes (see
66 http://www.freedesktop.org/wiki/Software/Xephyr/) and returns two arrayrefs: a
67 list of X11 display numbers to the Xephyr processes and a list of PIDs of the
73 my ($parallel, $numtests, $keep_xserver_output) = @_;
79 my $child = waitpid -1, POSIX::WNOHANG;
80 @pids = grep { $_ != $child } @pids;
81 return unless @pids == 0;
82 print STDERR "All X server processes died.\n";
83 print STDERR "Use ./complete-run.pl --parallel 1 --keep-xserver-output\n";
87 # Yeah, I know it’s non-standard, but Perl’s POSIX module doesn’t have
88 # _SC_NPROCESSORS_CONF.
89 my $cpuinfo = slurp('/proc/cpuinfo');
90 my $num_cores = scalar grep { /model name/ } split("\n", $cpuinfo);
91 # If /proc/cpuinfo does not exist, we fall back to 2 cores.
94 # If unset, we use num_cores * 2.
95 $parallel ||= ($num_cores * 2);
97 # If we are running a small number of tests, don’t over-parallelize.
98 $parallel = $numtests if $numtests < $parallel;
100 # First get the last used display number, then increment it by one.
101 # Effectively falls back to 1 if no X server is running.
102 my ($displaynum) = map { /(\d+)$/ } reverse sort glob($x_socketpath . '*');
105 say "Starting $parallel Xephyr instances, starting at :$displaynum...";
108 for (1 .. $parallel) {
109 my $socket = fork_xserver($keep_xserver_output, $displaynum,
110 'Xephyr', ":$displaynum", '-screen', '1280x800',
112 push(@displays, ":$displaynum");
113 push(@sockets_waiting, $socket);
117 wait_for_x(\@sockets_waiting);