2018-01-17 17:07:32 +03:00
#!/usr/bin/env python3
2017-10-14 03:50:28 +03:00
#
# Copyright (C) 2017 Colin Walters <walters@verbum.org>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
2021-12-07 04:20:55 +03:00
# License along with this library. If not, see <https://www.gnu.org/licenses/>.
2017-10-14 03:50:28 +03:00
2018-01-17 18:03:59 +03:00
from __future__ import division
2017-10-14 03:50:28 +03:00
from __future__ import print_function
import os
import sys
import shutil
import subprocess
from multiprocessing import cpu_count
def fatal ( msg ) :
sys . stderr . write ( msg )
sys . stderr . write ( ' \n ' )
sys . exit ( 1 )
# Create 20 files with content based on @dname + a serial, basically to have
# different files with different checksums.
def mktree ( dname , serial = 0 ) :
print ( ' Creating tree ' , dname , file = sys . stderr )
2018-01-17 17:25:26 +03:00
os . mkdir ( dname , 0o755 )
2018-01-17 17:42:20 +03:00
for v in range ( 20 ) :
2017-10-14 03:50:28 +03:00
with open ( ' {} / {} ' . format ( dname , v ) , ' w ' ) as f :
f . write ( ' {} {} {} \n ' . format ( dname , serial , v ) )
subprocess . check_call ( [ ' ostree ' , ' --repo=repo ' , ' init ' , ' --mode=bare ' ] )
with open ( ' repo/config ' , ' a ' ) as f :
2021-04-19 19:03:57 +03:00
# like the bit in libtest, but let's do it unconditionally since
# it's simpler, and we don't need xattr coverage for this
2017-10-14 03:50:28 +03:00
f . write ( ' disable-xattrs=true \n ' )
2021-04-19 19:03:57 +03:00
# Make any locking errors fail quickly instead of blocking the test
# for 30 seconds.
f . write ( ' lock-timeout-secs=5 \n ' )
2017-10-14 03:50:28 +03:00
def commit ( v ) :
tdir = ' tree {} ' . format ( v )
cmd = [ ' ostree ' , ' --repo=repo ' , ' commit ' , ' --fsync=0 ' , ' -b ' , tdir , ' --tree=dir= ' + tdir ]
proc = subprocess . Popen ( cmd )
print ( ' PID {} ' . format ( proc . pid ) , * cmd , file = sys . stderr )
return proc
def prune ( ) :
cmd = [ ' ostree ' , ' --repo=repo ' , ' prune ' , ' --refs-only ' ]
proc = subprocess . Popen ( cmd )
print ( ' PID {} : ' . format ( proc . pid ) , * cmd , file = sys . stderr )
return proc
def wait_check ( proc ) :
pid = proc . pid
proc . wait ( )
if proc . returncode != 0 :
sys . stderr . write ( " process {} exited with code {} \n " . format ( proc . pid , proc . returncode ) )
return False
else :
sys . stderr . write ( ' PID {} exited OK \n ' . format ( pid ) )
return True
print ( " 1..2 " )
def run ( n_committers , n_pruners ) :
# The number of committers needs to be even since we only create half as
# many trees
n_committers + = n_committers % 2
committers = set ( )
pruners = set ( )
print ( ' n_committers ' , n_committers , ' n_pruners ' , n_pruners , file = sys . stderr )
2018-01-17 18:03:59 +03:00
n_trees = n_committers / / 2
2018-01-17 17:42:20 +03:00
for v in range ( n_trees ) :
2017-10-14 03:50:28 +03:00
mktree ( ' tree {} ' . format ( v ) )
2018-01-17 17:42:20 +03:00
for v in range ( n_committers ) :
2018-01-17 18:03:59 +03:00
committers . add ( commit ( v / / 2 ) )
2018-01-17 17:42:20 +03:00
for v in range ( n_pruners ) :
2017-10-14 03:50:28 +03:00
pruners . add ( prune ( ) )
failed = False
for committer in committers :
if not wait_check ( committer ) :
failed = True
for pruner in pruners :
if not wait_check ( pruner ) :
failed = True
if failed :
fatal ( ' A child process exited abnormally ' )
2018-01-17 17:42:20 +03:00
for v in range ( n_trees ) :
2017-10-14 03:50:28 +03:00
shutil . rmtree ( ' tree {} ' . format ( v ) )
# No concurrent pruning
2018-01-17 18:03:59 +03:00
run ( cpu_count ( ) / / 2 + 2 , 0 )
2017-10-14 03:50:28 +03:00
print ( " ok no concurrent prunes " )
2018-01-17 18:03:59 +03:00
run ( cpu_count ( ) / / 2 + 4 , 3 )
2017-10-14 03:50:28 +03:00
print ( " ok concurrent prunes " )