Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp2109899ybt; Fri, 3 Jul 2020 00:43:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwpT2lVqDZour1smZ5YKTK1e3tyJZITJbYxS/xXc85HjATzfmrRoBWL/XZl/Ovo6HwS3nNl X-Received: by 2002:aa7:d792:: with SMTP id s18mr39974212edq.7.1593762225858; Fri, 03 Jul 2020 00:43:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593762225; cv=none; d=google.com; s=arc-20160816; b=BChsIEum2LdC1wcWzkPxViFpItYLUfhXvOWOU209QGCEBqwmqhxerRTonS4jt/CJRJ 7MldjFLtwqtSIfaacSwJXePVHnPGrR1bI4l615ZoDJjd9vOT1oQGa85nm7FVK1uqerMW kirOSR73xW4prVp7HcKVqQust0SKKzEEejow3cKnNsZMKyN33bKFQAGoinqE9P8EpnXn KR0eBVckmDseDERBr/BoatK9yXleEVB7gDyPH6xC/uIu2Q+KPg6wJzIcjWEn7IMSATq8 IaayiYfIAqsooZooFapkUF3nG/YhjZkfuwE0O+NMnx48vmDSSIUIE4bL41IE7MsGbXi8 KOEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:organization:references:cc:to:from:subject:ironport-sdr :ironport-sdr; bh=Zo90bIm15SVTEUVQXxovoMWfpyIM0p8c4QBwh7QqNwQ=; b=VfuHGrPScpMIbb3AOURDzNiLjEX2biiWkPgyF4unqsDzMgpgGqZQ/Cze56H6C3M2hj kc7HS0N7qHzLMHT+FcqOruo+0PFgVXeqXU9aaM9NdYEaYWrU6tY7ht5tPN0Zmgzq8nP0 9A9Wo2jm8M8YFPm1AnOln3cycjamOXllDmJrZ9/CBGWdd+AG9rimfkP9gqQuGRnoNM5W sZWy8ZflQh5rWoOUPyPQuv3JWfS0Pwf53XUd0mXqGhpc4E5t9szKjlIpO0lFji9kFCWS 9hyZwOvEX3pZevk295bA/9EHIIvp03XAeHgsIoMUy6ks1eRiHGNoeRDzq247rEVvzfWw 9wWw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q27si7397551eja.643.2020.07.03.00.43.23; Fri, 03 Jul 2020 00:43:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726208AbgGCHlL (ORCPT + 99 others); Fri, 3 Jul 2020 03:41:11 -0400 Received: from mga18.intel.com ([134.134.136.126]:1900 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725786AbgGCHlL (ORCPT ); Fri, 3 Jul 2020 03:41:11 -0400 IronPort-SDR: EnZ/DYdGQihTUnMLtADm/LoiibOzS82zVObqhc9a3EouVqfJ/AtfieUAZOBxLNuIB61uzNLb0S ZuB3bZWvm8ow== X-IronPort-AV: E=McAfee;i="6000,8403,9670"; a="134568072" X-IronPort-AV: E=Sophos;i="5.75,307,1589266800"; d="scan'208";a="134568072" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jul 2020 00:41:10 -0700 IronPort-SDR: MDyv9VQ2IIy6XBdNtztElGXRi80rdNI7mDcStk3vWG7fiPpU3SW3WVDgArsSftlQVTCZNFBXbi azll2LPcV0iw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,307,1589266800"; d="scan'208";a="387557133" Received: from linux.intel.com ([10.54.29.200]) by fmsmga001.fm.intel.com with ESMTP; 03 Jul 2020 00:41:10 -0700 Received: from [10.249.231.67] (abudanko-mobl.ccr.corp.intel.com [10.249.231.67]) by linux.intel.com (Postfix) with ESMTP id 1CCD2580784; Fri, 3 Jul 2020 00:41:07 -0700 (PDT) Subject: [PATCH v9 01/15] tools/libperf: avoid fds moving by fdarray__filter() From: Alexey Budankov To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Namhyung Kim , Alexander Shishkin , Peter Zijlstra , Ingo Molnar , Andi Kleen , linux-kernel References: Organization: Intel Corp. Message-ID: <11f89bd4-ddec-d625-7740-2c3dd1f59b23@linux.intel.com> Date: Fri, 3 Jul 2020 10:41:07 +0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Avoid fds moving by fdarray__filter() so fds indices returned by fdarray__add() can be used for access and processing of objects at struct pollfd *entries. Signed-off-by: Alexey Budankov --- tools/lib/api/fd/array.c | 11 +++++------ tools/perf/tests/fdarray.c | 20 ++------------------ 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/tools/lib/api/fd/array.c b/tools/lib/api/fd/array.c index 58d44d5eee31..89f9a2193c2d 100644 --- a/tools/lib/api/fd/array.c +++ b/tools/lib/api/fd/array.c @@ -93,22 +93,21 @@ int fdarray__filter(struct fdarray *fda, short revents, return 0; for (fd = 0; fd < fda->nr; ++fd) { + if (!fda->entries[fd].events) + continue; + if (fda->entries[fd].revents & revents) { if (entry_destructor) entry_destructor(fda, fd, arg); + fda->entries[fd].revents = fda->entries[fd].events = 0; continue; } - if (fd != nr) { - fda->entries[nr] = fda->entries[fd]; - fda->priv[nr] = fda->priv[fd]; - } - ++nr; } - return fda->nr = nr; + return nr; } int fdarray__poll(struct fdarray *fda, int timeout) diff --git a/tools/perf/tests/fdarray.c b/tools/perf/tests/fdarray.c index c7c81c4a5b2b..d0c8a05aab2f 100644 --- a/tools/perf/tests/fdarray.c +++ b/tools/perf/tests/fdarray.c @@ -12,6 +12,7 @@ static void fdarray__init_revents(struct fdarray *fda, short revents) for (fd = 0; fd < fda->nr; ++fd) { fda->entries[fd].fd = fda->nr - fd; + fda->entries[fd].events = revents; fda->entries[fd].revents = revents; } } @@ -29,7 +30,7 @@ static int fdarray__fprintf_prefix(struct fdarray *fda, const char *prefix, FILE int test__fdarray__filter(struct test *test __maybe_unused, int subtest __maybe_unused) { - int nr_fds, expected_fd[2], fd, err = TEST_FAIL; + int nr_fds, err = TEST_FAIL; struct fdarray *fda = fdarray__new(5, 5); if (fda == NULL) { @@ -55,7 +56,6 @@ int test__fdarray__filter(struct test *test __maybe_unused, int subtest __maybe_ fdarray__init_revents(fda, POLLHUP); fda->entries[2].revents = POLLIN; - expected_fd[0] = fda->entries[2].fd; pr_debug("\nfiltering all but fda->entries[2]:"); fdarray__fprintf_prefix(fda, "before", stderr); @@ -66,17 +66,9 @@ int test__fdarray__filter(struct test *test __maybe_unused, int subtest __maybe_ goto out_delete; } - if (fda->entries[0].fd != expected_fd[0]) { - pr_debug("\nfda->entries[0].fd=%d != %d\n", - fda->entries[0].fd, expected_fd[0]); - goto out_delete; - } - fdarray__init_revents(fda, POLLHUP); fda->entries[0].revents = POLLIN; - expected_fd[0] = fda->entries[0].fd; fda->entries[3].revents = POLLIN; - expected_fd[1] = fda->entries[3].fd; pr_debug("\nfiltering all but (fda->entries[0], fda->entries[3]):"); fdarray__fprintf_prefix(fda, "before", stderr); @@ -88,14 +80,6 @@ int test__fdarray__filter(struct test *test __maybe_unused, int subtest __maybe_ goto out_delete; } - for (fd = 0; fd < 2; ++fd) { - if (fda->entries[fd].fd != expected_fd[fd]) { - pr_debug("\nfda->entries[%d].fd=%d != %d\n", fd, - fda->entries[fd].fd, expected_fd[fd]); - goto out_delete; - } - } - pr_debug("\n"); err = 0; -- 2.24.1