2 # vim:ts=4:sw=4:expandtab
7 use Time::HiRes qw(sleep);
8 use i3test::Util qw(slurp);
11 our @EXPORT = qw(start_xserver);
14 my $x_socketpath = '/tmp/.X11-unix/X';
16 # forks an X server process
18 my $keep_xserver_output = shift;
19 my $displaynum = shift;
21 die "Could not fork: $!" unless defined($pid);
23 # Child, close stdout/stderr, then start Xephyr
24 if (!$keep_xserver_output) {
32 push(@complete_run::CLEANUP, sub {
34 # Unlink the X11 socket, Xdmx seems to leave it there.
35 unlink($x_socketpath . $displaynum);
40 return $x_socketpath . $displaynum;
43 # Blocks until the socket paths specified in the given array reference actually
46 my ($sockets_waiting) = @_;
48 # Wait until Xdmx actually runs. Pretty ugly solution, but as long as we
49 # can’t socket-activate X11…
51 @$sockets_waiting = grep { ! -S $_ } @$sockets_waiting;
52 last unless @$sockets_waiting;
57 =head2 start_xserver($parallel)
59 Starts C<$parallel> (or number of cores * 2 if undef) Xephyr processes (see
60 http://www.freedesktop.org/wiki/Software/Xephyr/) and returns two arrayrefs: a
61 list of X11 display numbers to the Xephyr processes and a list of PIDs of the
67 my ($parallel, $numtests, $keep_xserver_output) = @_;
73 my $child = waitpid -1, POSIX::WNOHANG;
74 @pids = grep { $_ != $child } @pids;
75 return unless @pids == 0;
76 print STDERR "All X server processes died.\n";
77 print STDERR "Use ./complete-run.pl --parallel 1 --keep-xserver-output\n";
81 # Yeah, I know it’s non-standard, but Perl’s POSIX module doesn’t have
82 # _SC_NPROCESSORS_CONF.
84 if (-e '/proc/cpuinfo') {
85 my $cpuinfo = slurp('/proc/cpuinfo');
86 $num_cores = scalar grep { /model name/ } split("\n", $cpuinfo);
88 # If /proc/cpuinfo does not exist, we fall back to 2 cores.
91 # If unset, we use num_cores * 2.
92 $parallel ||= ($num_cores * 2);
94 # If we are running a small number of tests, don’t over-parallelize.
95 $parallel = $numtests if $numtests < $parallel;
97 # First get the last used display number, then increment it by one.
98 # Effectively falls back to 1 if no X server is running.
99 my ($displaynum) = map { /(\d+)$/ } reverse sort glob($x_socketpath . '*');
102 say "Starting $parallel Xephyr instances, starting at :$displaynum...";
105 for (1 .. $parallel) {
106 my $socket = fork_xserver($keep_xserver_output, $displaynum,
107 'Xephyr', ":$displaynum", '-screen', '1280x800',
109 push(@displays, ":$displaynum");
110 push(@sockets_waiting, $socket);
114 wait_for_x(\@sockets_waiting);