1# Copyright 2021, Kay Hayen, mailto:kay.hayen@gmail.com 2# 3# Part of "Nuitka", an optimizing Python compiler that is compatible and 4# integrates with CPython, but also works on its own. 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18""" Valgrind tool usage. 19 20We are using it for benchmarking purposes, as it's an analysis tool at the 21same time and gives deterministic results. 22""" 23 24import shutil 25import subprocess 26import sys 27 28from nuitka.Tracing import my_print 29from nuitka.utils.Execution import check_output 30from nuitka.utils.FileOperations import getFileContentByLine, withTemporaryFile 31from nuitka.utils.Utils import isWin32Windows 32 33 34def runValgrind(descr, tool, args, include_startup, save_logfilename=None): 35 # Many cases to deal with, pylint: disable=too-many-branches 36 37 if isWin32Windows(): 38 sys.exit("Error, valgrind is not available on Windows.") 39 40 if descr: 41 my_print(descr, tool, file=sys.stderr, end="... ") 42 43 with withTemporaryFile() as log_file: 44 log_filename = log_file.name 45 46 command = ["valgrind", "-q"] 47 48 if tool == "callgrind": 49 command += ("--tool=callgrind", "--callgrind-out-file=%s" % log_filename) 50 elif tool == "massif": 51 command += ("--tool=massif", "--massif-out-file=%s" % log_filename) 52 else: 53 sys.exit("Error, no support for tool '%s' yet." % tool) 54 55 # Do not count things before main module starts its work. 56 if not include_startup: 57 command += ( 58 "--zero-before=init__main__()", 59 "--zero-before=init__main__", 60 "--zero-before=PyInit___main__", 61 "--zero-before=PyInit___main__()", 62 ) 63 64 command.extend(args) 65 66 process = subprocess.Popen( 67 args=command, stdout=subprocess.PIPE, stderr=subprocess.PIPE 68 ) 69 70 _stdout_valgrind, stderr_valgrind = process.communicate() 71 exit_valgrind = process.returncode 72 73 assert exit_valgrind == 0, stderr_valgrind 74 if descr: 75 my_print("OK", file=sys.stderr) 76 77 if save_logfilename is not None: 78 shutil.copyfile(log_filename, save_logfilename) 79 80 max_mem = None 81 82 for line in getFileContentByLine(log_filename): 83 if tool == "callgrind" and line.startswith("summary:"): 84 return int(line.split()[1]) 85 elif tool == "massif" and line.startswith("mem_heap_B="): 86 mem = int(line.split("=")[1]) 87 88 if max_mem is None: 89 max_mem = 0 90 91 max_mem = max(mem, max_mem) 92 93 if tool == "massif" and max_mem is not None: 94 return max_mem 95 96 sys.exit("Error, didn't parse Valgrind log file successfully.") 97 98 99def getBinarySizes(filename): 100 command = ["size", filename] 101 sizes = check_output(command).strip() 102 sizes = sizes.split(b"\n")[-1].replace(b"\t", b"").split() 103 104 return int(sizes[0]), int(sizes[1]) 105