1*01826a49SYabin Cui#!/usr/bin/env python3 2*01826a49SYabin Cui# ############################################################################# 3*01826a49SYabin Cui# Copyright (c) 2018-present lzutao <taolzu(at)gmail.com> 4*01826a49SYabin Cui# All rights reserved. 5*01826a49SYabin Cui# 6*01826a49SYabin Cui# This source code is licensed under both the BSD-style license (found in the 7*01826a49SYabin Cui# LICENSE file in the root directory of this source tree) and the GPLv2 (found 8*01826a49SYabin Cui# in the COPYING file in the root directory of this source tree). 9*01826a49SYabin Cui# ############################################################################# 10*01826a49SYabin Cuiimport os 11*01826a49SYabin Cuiimport subprocess 12*01826a49SYabin Cuiimport tempfile 13*01826a49SYabin Cui 14*01826a49SYabin Cui 15*01826a49SYabin Cuidef valgrindTest(valgrind, datagen, fuzzer, zstd, fullbench): 16*01826a49SYabin Cui VALGRIND_ARGS = [valgrind, '--leak-check=full', '--show-leak-kinds=all', '--error-exitcode=1'] 17*01826a49SYabin Cui 18*01826a49SYabin Cui print('\n ---- valgrind tests : memory analyzer ----') 19*01826a49SYabin Cui 20*01826a49SYabin Cui subprocess.check_call([*VALGRIND_ARGS, datagen, '-g50M'], stdout=subprocess.DEVNULL) 21*01826a49SYabin Cui 22*01826a49SYabin Cui if subprocess.call([*VALGRIND_ARGS, zstd], 23*01826a49SYabin Cui stdout=subprocess.DEVNULL) == 0: 24*01826a49SYabin Cui raise subprocess.SubprocessError('zstd without argument should have failed') 25*01826a49SYabin Cui 26*01826a49SYabin Cui with subprocess.Popen([datagen, '-g80'], stdout=subprocess.PIPE) as p1, \ 27*01826a49SYabin Cui subprocess.Popen([*VALGRIND_ARGS, zstd, '-', '-c'], 28*01826a49SYabin Cui stdin=p1.stdout, 29*01826a49SYabin Cui stdout=subprocess.DEVNULL) as p2: 30*01826a49SYabin Cui p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits. 31*01826a49SYabin Cui p2.communicate() 32*01826a49SYabin Cui if p2.returncode != 0: 33*01826a49SYabin Cui raise subprocess.SubprocessError() 34*01826a49SYabin Cui 35*01826a49SYabin Cui with subprocess.Popen([datagen, '-g16KB'], stdout=subprocess.PIPE) as p1, \ 36*01826a49SYabin Cui subprocess.Popen([*VALGRIND_ARGS, zstd, '-vf', '-', '-c'], 37*01826a49SYabin Cui stdin=p1.stdout, 38*01826a49SYabin Cui stdout=subprocess.DEVNULL) as p2: 39*01826a49SYabin Cui p1.stdout.close() 40*01826a49SYabin Cui p2.communicate() 41*01826a49SYabin Cui if p2.returncode != 0: 42*01826a49SYabin Cui raise subprocess.SubprocessError() 43*01826a49SYabin Cui 44*01826a49SYabin Cui with tempfile.NamedTemporaryFile() as tmp_fd: 45*01826a49SYabin Cui with subprocess.Popen([datagen, '-g2930KB'], stdout=subprocess.PIPE) as p1, \ 46*01826a49SYabin Cui subprocess.Popen([*VALGRIND_ARGS, zstd, '-5', '-vf', '-', '-o', tmp_fd.name], 47*01826a49SYabin Cui stdin=p1.stdout) as p2: 48*01826a49SYabin Cui p1.stdout.close() 49*01826a49SYabin Cui p2.communicate() 50*01826a49SYabin Cui if p2.returncode != 0: 51*01826a49SYabin Cui raise subprocess.SubprocessError() 52*01826a49SYabin Cui 53*01826a49SYabin Cui subprocess.check_call([*VALGRIND_ARGS, zstd, '-vdf', tmp_fd.name, '-c'], 54*01826a49SYabin Cui stdout=subprocess.DEVNULL) 55*01826a49SYabin Cui 56*01826a49SYabin Cui with subprocess.Popen([datagen, '-g64MB'], stdout=subprocess.PIPE) as p1, \ 57*01826a49SYabin Cui subprocess.Popen([*VALGRIND_ARGS, zstd, '-vf', '-', '-c'], 58*01826a49SYabin Cui stdin=p1.stdout, 59*01826a49SYabin Cui stdout=subprocess.DEVNULL) as p2: 60*01826a49SYabin Cui p1.stdout.close() 61*01826a49SYabin Cui p2.communicate() 62*01826a49SYabin Cui if p2.returncode != 0: 63*01826a49SYabin Cui raise subprocess.SubprocessError() 64*01826a49SYabin Cui 65*01826a49SYabin Cui subprocess.check_call([*VALGRIND_ARGS, fuzzer, '-T1mn', '-t1']) 66*01826a49SYabin Cui subprocess.check_call([*VALGRIND_ARGS, fullbench, '-i1']) 67*01826a49SYabin Cui 68*01826a49SYabin Cui 69*01826a49SYabin Cuidef main(): 70*01826a49SYabin Cui import argparse 71*01826a49SYabin Cui parser = argparse.ArgumentParser(description='Valgrind tests : memory analyzer') 72*01826a49SYabin Cui parser.add_argument('valgrind', help='valgrind path') 73*01826a49SYabin Cui parser.add_argument('zstd', help='zstd path') 74*01826a49SYabin Cui parser.add_argument('datagen', help='datagen path') 75*01826a49SYabin Cui parser.add_argument('fuzzer', help='fuzzer path') 76*01826a49SYabin Cui parser.add_argument('fullbench', help='fullbench path') 77*01826a49SYabin Cui 78*01826a49SYabin Cui args = parser.parse_args() 79*01826a49SYabin Cui 80*01826a49SYabin Cui valgrind = args.valgrind 81*01826a49SYabin Cui zstd = args.zstd 82*01826a49SYabin Cui datagen = args.datagen 83*01826a49SYabin Cui fuzzer = args.fuzzer 84*01826a49SYabin Cui fullbench = args.fullbench 85*01826a49SYabin Cui 86*01826a49SYabin Cui valgrindTest(valgrind, datagen, fuzzer, zstd, fullbench) 87*01826a49SYabin Cui 88*01826a49SYabin Cui 89*01826a49SYabin Cuiif __name__ == '__main__': 90*01826a49SYabin Cui main() 91