From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id C4EDAF3A740; Wed, 12 Feb 2025 15:22:15 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C4EDAF3A740 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1739362935; bh=dBsAtqpobjM8HicBdvvH1vHc3eu2PHiXrsMyKj6m4O4=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=edmVtn6jOW2R1OrT0o/poEswDTBJuzZst7xrSlHEXt3DoZ98d26tHpG8SbliCOxox GkgY52gVX2EPIRbFLi30Bqlam0ijhvAqsvaYs0MOqIz3SuqItplfw9dKacV9l2NrB2 +GhBr8W4GCVv9CWdEbqSsk8MDVtV+vjLpcXIb5ik= Received: from mail-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.54]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id D7A30F3A740 for ; Wed, 12 Feb 2025 15:22:14 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D7A30F3A740 Received: by mail-ej1-f54.google.com with SMTP id a640c23a62f3a-ab7f860a9c6so89414466b.0 for ; Wed, 12 Feb 2025 04:22:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739362934; x=1739967734; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=TU0JcrnCHFtQn7O1XoaHlWGzqdKB3v2z1XbK4OHweqs=; b=LbzU5DX2owa1lOtKF3Lt3NBCHA8KH9sgJ9AvJbquhUwyU8FkLcNDo3HeGfIiXaDm56 22A+Rf3nmiUiP3DMmmD8oBIjFyMQnSPuLfqpDEZCctZ6pwfwvLFO1M1BVaLuZPmVPdnB C/7VWnuRTspZigyduapvT5UiOAnsZjhXNKGWvxO5LMDgRtpfVkyrZtJp8t+Ei0ZGyCrJ tAECa+7Fi1u7sqwprzWQRLts/qPhS3NjcDUStUAH2Y070RQNmYAk/t0mqeps5x7MYZ0l mO76pGYbkE3/bDRLZP3xxPorLMIoXjVj+ZxIA/HaiQashcyCBsOvIgAw4B8Dp+b+ms30 oqmw== X-Gm-Message-State: AOJu0YxFvk1ObDsc3JXr8Sl2lexYZ+TXFUVJXbACJrwclFjAlpuzj4YY /9R8fT2m+VAGPvcK4iyjV/WIinrT+iOrPF9RPV9yubsy53MGtNWde+Cmgw== X-Gm-Gg: ASbGnctWVz+99N7pZNstwlykUiaafuFoyJkeKeTCNTStqEF25dDBG6FzjW0Qi3G/Zj7 EwiGtNPHMG11krP63aGDPM8wFd3IEnrnYqDHssZ19r1Mjv8KwJbi4/xNNTFoMLtP0zxKZIHTCAe CpCv15OOWcqmRuf1iPbhS/U7YgJ105aMS4fqgDIB6/hZHQNpmDj+KHJqxHrQWMm+VE+wPr/JazE ntzVrwxGafwtwUcOpfdFVtHUs+Ai06A7mxnUZfD5boRPKaJ8tdIxcZBHTiXNCSacfePbeJATMLa rSUK9Q== X-Google-Smtp-Source: AGHT+IGsqo0z0jvwNGv0YXGBwga31d/5bGY/RGfnRS2uUz61zGJUP2xPsfHCy4S1xrZJqU4ItXMxqw== X-Received: by 2002:a17:907:2cc6:b0:ab7:6d59:3b4c with SMTP id a640c23a62f3a-ab7f339ee62mr242744666b.21.1739362933391; Wed, 12 Feb 2025 04:22:13 -0800 (PST) Received: from localhost ([5.181.62.98]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ab7e2711937sm307765366b.25.2025.02.12.04.22.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Feb 2025 04:22:12 -0800 (PST) X-Google-Original-From: Sergey Bronnikov To: tarantool-patches@dev.tarantool.org, Sergey Kaplun Date: Wed, 12 Feb 2025 15:20:22 +0300 Message-Id: <80360fb2e570ce8d54ff59ccf0bcd5dfb6b98527.1739362814.git.sergeyb@tarantool.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit][v2] Always close profiler output file. X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Mike Pall Reported by Guilherme Batalheiro. (cherry picked from commit fca66335d131669cf017420af6963a7565babb58) Before the patch, a function `prof_finish` wrote a string `No samples collected` to a profiler output file and then exited. Due to early exit, thethe output file handle stay open. This patch fixes the condition and the file handle is closed even if the number of samples is equal to 0. Sergey Bronnikov: * added the description and the test for the problem Part of tarantool/tarantool#11055 --- Changes v2: - Added fixes according to comments by Sergey Kaplun. Branch: https://github.com/tarantool/luajit/tree/ligurio/gh-xxxx-close-file-profiler Related issues: - https://github.com/luajIT/luajIT/issues/1304 - https://github.com/tarantool/tarantool/issues/11055 src/jit/p.lua | 4 +-- ...close-profile-dump-with-0-samples.test.lua | 31 +++++++++++++++++++ .../script.lua | 19 ++++++++++++ test/tarantool-tests/utils/tools.lua | 4 +++ 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 test/tarantool-tests/lj-1304-close-profile-dump-with-0-samples.test.lua create mode 100644 test/tarantool-tests/lj-1304-close-profile-dump-with-0-samples/script.lua diff --git a/src/jit/p.lua b/src/jit/p.lua index 4569d69e..89b49584 100644 --- a/src/jit/p.lua +++ b/src/jit/p.lua @@ -228,9 +228,7 @@ local function prof_finish() local samples = prof_samples if samples == 0 then if prof_raw ~= true then out:write("[No samples collected]\n") end - return - end - if prof_ann then + elseif prof_ann then prof_annotate(prof_count1, samples) else prof_top(prof_count1, prof_count2, samples, "") diff --git a/test/tarantool-tests/lj-1304-close-profile-dump-with-0-samples.test.lua b/test/tarantool-tests/lj-1304-close-profile-dump-with-0-samples.test.lua new file mode 100644 index 00000000..614ecc69 --- /dev/null +++ b/test/tarantool-tests/lj-1304-close-profile-dump-with-0-samples.test.lua @@ -0,0 +1,31 @@ +local tap = require('tap') +local test = tap.test('lj-1304-close-profile-dump-with-0-samples'):skipcond({ + ['Test requires /proc filesystem'] = jit.os == 'OSX', +}) +local utils = require('utils') + +test:plan(1) + +-- Test file to demonstrate LuaJIT incorrect behaviour with missed +-- closing a file handle for the profile output file. +-- See also: https://github.com/LuaJIT/LuaJIT/issues/1304 + +local p_postfix = 'sysprofdata' +local p_filename = utils.tools.profilename(p_postfix) + +-- runs %testname%/script.lua by +-- with the given environment, launch options and CLI arguments. +local script = utils.exec.makecmd(arg) +-- Execute a Lua script with start and stop LuaJIT profiler, +-- it is expected no samples found by profiler. The script's +-- output is suppressed, it is not interested. +local _ = script(p_filename, p_postfix) + +local p_content = io.open(p_filename):read('a*') +test:is(utils.tools.trim(p_content), '[No samples collected]', + 'profile dump has no samples') + +-- Teardown. +os.remove(p_filename) + +test:done(true) diff --git a/test/tarantool-tests/lj-1304-close-profile-dump-with-0-samples/script.lua b/test/tarantool-tests/lj-1304-close-profile-dump-with-0-samples/script.lua new file mode 100644 index 00000000..864958f5 --- /dev/null +++ b/test/tarantool-tests/lj-1304-close-profile-dump-with-0-samples/script.lua @@ -0,0 +1,19 @@ +local jit_p = require('jit.p') + +-- Using a bigger interval to make sure that there will be no +-- samples collected. +local p_options = 'i99999' +local p_filename = assert(arg[1], 'filename argument is missing') +local p_postfix = assert(arg[2], 'postfix argument is missing') + +jit_p.start(p_options, p_filename) + +-- No code to generate profiling samples. + +-- Stop profiler to execute `jit/p.lua:prof_fmt()`. With zero +-- samples it triggers early return without closing the file. +jit_p.stop() + +-- Make sure LuaJIT profiler is close a file handle. +local ls_output = io.popen('ls -l /proc/$$/fd'):read('a*') +assert(ls_output:find(p_postfix) == nil, 'file is open') diff --git a/test/tarantool-tests/utils/tools.lua b/test/tarantool-tests/utils/tools.lua index 33fcae78..9cb65daf 100644 --- a/test/tarantool-tests/utils/tools.lua +++ b/test/tarantool-tests/utils/tools.lua @@ -21,4 +21,8 @@ function M.read_file(path) return content end +function M.trim(str) + return (str:gsub('^%s*(.-)%s*$', '%1')) +end + return M -- 2.34.1