2 # vim:ts=4:sw=4:expandtab
7 use Time::HiRes qw(sleep);
10 our @EXPORT = qw(start_xdummy);
12 my $x_socketpath = '/tmp/.X11-unix/X';
14 # reads in a whole file
16 open(my $fh, '<', shift) or return '';
21 # forks an Xdummy or Xdmx process
23 my $displaynum = shift;
25 die "Could not fork: $!" unless defined($pid);
27 # Child, close stdout/stderr, then start Xdummy.
34 push(@complete_run::CLEANUP, sub {
36 # Unlink the X11 socket, Xdmx seems to leave it there.
37 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_xdummy($parallel)
59 Starts C<$parallel> (or number of cores * 2 if undef) Xdummy processes (see
60 the file ./Xdummy) and returns two arrayrefs: a list of X11 display numbers to
61 the Xdummy processes and a list of PIDs of the processes.
66 my ($parallel, $numtests) = @_;
71 # Yeah, I know it’s non-standard, but Perl’s POSIX module doesn’t have
72 # _SC_NPROCESSORS_CONF.
73 my $cpuinfo = slurp('/proc/cpuinfo');
74 my $num_cores = scalar grep { /model name/ } split("\n", $cpuinfo);
75 # If /proc/cpuinfo does not exist, we fall back to 2 cores.
78 # If unset, we use num_cores * 2.
79 $parallel ||= ($num_cores * 2);
81 # If we are running a small number of tests, don’t over-parallelize.
82 $parallel = $numtests if $numtests < $parallel;
84 # First get the last used display number, then increment it by one.
85 # Effectively falls back to 1 if no X server is running.
86 my ($displaynum) = map { /(\d+)$/ } reverse sort glob($x_socketpath . '*');
89 say "Starting $parallel Xdummy instances, starting at :$displaynum...";
92 for (1 .. $parallel) {
93 # We use -config /dev/null to prevent Xdummy from using the system
94 # Xorg configuration. The tests should be independant from the
95 # actual system X configuration.
96 my $socket = fork_xserver($displaynum, './Xdummy', ":$displaynum",
97 '-config', '/dev/null', '-nolisten', 'tcp');
98 push(@displays, ":$displaynum");
99 push(@sockets_waiting, $socket);
103 wait_for_x(\@sockets_waiting);