From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A49BCC433F5 for ; Thu, 23 Sep 2021 13:52:55 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6804B60FA0 for ; Thu, 23 Sep 2021 13:52:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 6804B60FA0 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=dcCb5cvQgAgSBSnf5GTiH9MF17R7cmY+epgCqoC3svo=; b=CTpr1tBhvlIqmk EhHVKdDcGxOjW43fUwApU8RYRY9ocapS0BpuYufaCcWV9ombL9adF3HGki1ZM6NoHMol46J4BoyeN o6BS0b4WUNEoZ5IoOGLgfyoWU0A0YOKY3MeVklcl06nIzL5KaeChU0N4cg5iOrzJMzdwuwwpLsE8n +lTySyxcudHr11Pfi1Nd+7K4rUA8byVWqYS/tLgaDuoLXjEgkbqR8CYMJOvvKN0HY6Tpkeex8rErI 4f4+Zi17SvGFrGGxnzlNv/Z6rv6bqEydO/ZUuk0IgzQVOJ1QqAQa56Bc/S0/9yp0RmHurCELv/xMS byW27+TO/TuRaEN67vtw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mTP7M-00BkIx-OD; Thu, 23 Sep 2021 13:50:28 +0000 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mTP7I-00BkIO-7u for linux-arm-kernel@lists.infradead.org; Thu, 23 Sep 2021 13:50:26 +0000 Received: by mail-pg1-x533.google.com with SMTP id k24so6374104pgh.8 for ; Thu, 23 Sep 2021 06:50:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=dqdBw2q82tw3UxPByQedIfwoN1Q495k4FaINszVdW6A=; b=ne8+owbGduumnIzo43j++kezGdV/UQlnN7zQ64Qlnn/AorO8N94Mox5On8JkjPw5Cn VSJsq3kw9Kw6DdnsTZKKhoqfg1mIKP2Oy76sRfDx6oY0CwwD99jnLdNlly1L2yIkUpaW EG7MSpAEOZV6dDdI+JK+F4mJnBcrWS11/zX4/OoO9XbiTS75fYPlZNxLZJJ9ZUceZZct Ik92ULnXmdiTifnIH5i32JZ2jGlcY0lmbTa+sFEOW5DFoxhPUFgPwulo0a4QJCewp6OS Nr9Tq9cHqBXetjT1e3fQylIBSDswdOpaDpEDkycpLKAMLE7OmyLMWdoS9UNfnspUnFmz lRLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=dqdBw2q82tw3UxPByQedIfwoN1Q495k4FaINszVdW6A=; b=61HxOSI5ZlqpZvLcR0e/rxOOp2E7sQHcGpxW+PDq890ukoLff2cdEsDbru0fd1+VoM CwdGoZlME7N0e3JatPPFdJLxOgYeui8lFUsmxgQC4Hagyc0VN2X5n3pBajFIDGtOkV1I uBVVZuGUo+ZTyXQjQZvmEj58Dr5gzRqYBk4MZH6EMItC2ru1RO29rAZuo1q8/MnZlHKq AOiIYk2z2SwvDFj4A5FreTryeDMPvbiY4DFPXZo0K32HtUbgrOyeIk9NYu51oyMgO78t CgTeMVTM0XAKXR/MphGrFdgqHqQ4cmP0x2PJ1zPvg4gCQSr5dD3MblrLwp5/5yWsmVoF SCJA== X-Gm-Message-State: AOAM53338GIQKb9uNbHz9D6GTAPjM3eivXNN3UZ0z+6AAFDsaHyLc1cS O8zMQC3RRDG/fUoOMa49QlHg6A== X-Google-Smtp-Source: ABdhPJynkQqQpD/56rUtOcDaRhUI9NDqoydH7SmxM88A7sEWkvDSHhaWEIau66t+YdTTHHA86oJVHQ== X-Received: by 2002:a05:6a00:22d5:b0:440:3750:f5f4 with SMTP id f21-20020a056a0022d500b004403750f5f4mr4425202pfj.64.1632405022676; Thu, 23 Sep 2021 06:50:22 -0700 (PDT) Received: from leoy-ThinkPad-X240s ([213.173.35.225]) by smtp.gmail.com with ESMTPSA id q20sm6208475pfc.57.2021.09.23.06.50.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Sep 2021 06:50:22 -0700 (PDT) Date: Thu, 23 Sep 2021 21:50:16 +0800 From: Leo Yan To: German Gomez Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, John Garry , Will Deacon , Mathieu Poirier , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Mike Leach , linux-arm-kernel@lists.infradead.org, coresight@lists.linaro.org Subject: Re: [PATCH 4/5] perf arm-spe: Implement find_snapshot callback Message-ID: <20210923135016.GG400258@leoy-ThinkPad-X240s> References: <20210916154635.1525-1-german.gomez@arm.com> <20210916154635.1525-4-german.gomez@arm.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20210916154635.1525-4-german.gomez@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210923_065024_342581_82688A8F X-CRM114-Status: GOOD ( 43.48 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi German, On Thu, Sep 16, 2021 at 04:46:34PM +0100, German Gomez wrote: > The head pointer of the AUX buffer managed by the arm_spe_pmu.c driver > is not monotonically increasing, therefore the find_snapshot callback is > needed in order to find the trace data within the AUX buffer and avoid > wasting space in the perf.data file. > > The pointer is assumed to have wrapped if the buffer contains non-zero > data at the end. If it has wrapped, the entire contents of the AUX > buffer are stored in the perf.data file. Otherwise only the data up to > the head pointer is stored. > > Reviewed-by: James Clark > Signed-off-by: German Gomez > --- > tools/perf/arch/arm64/util/arm-spe.c | 145 +++++++++++++++++++++++++++ > 1 file changed, 145 insertions(+) > > diff --git a/tools/perf/arch/arm64/util/arm-spe.c b/tools/perf/arch/arm64/util/arm-spe.c > index f8b03d164b42..56785034fc84 100644 > --- a/tools/perf/arch/arm64/util/arm-spe.c > +++ b/tools/perf/arch/arm64/util/arm-spe.c > @@ -23,6 +23,7 @@ > #include "../../../util/auxtrace.h" > #include "../../../util/record.h" > #include "../../../util/arm-spe.h" > +#include // reallocarray > > #define KiB(x) ((x) * 1024) > #define MiB(x) ((x) * 1024 * 1024) > @@ -31,6 +32,8 @@ struct arm_spe_recording { > struct auxtrace_record itr; > struct perf_pmu *arm_spe_pmu; > struct evlist *evlist; > + int wrapped_cnt; > + bool *wrapped; > }; > > static void arm_spe_set_timestamp(struct auxtrace_record *itr, > @@ -299,6 +302,146 @@ static int arm_spe_snapshot_finish(struct auxtrace_record *itr) > return -EINVAL; > } > > +static int arm_spe_alloc_wrapped_array(struct arm_spe_recording *ptr, int idx) > +{ > + bool *wrapped; > + int cnt = ptr->wrapped_cnt, new_cnt, i; > + > + /* > + * No need to allocate, so return early. > + */ > + if (idx < cnt) > + return 0; > + > + /* > + * Make ptr->wrapped as big as idx. > + */ > + new_cnt = idx + 1; > + > + /* > + * Free'ed in arm_spe_recording_free(). > + */ > + wrapped = reallocarray(ptr->wrapped, new_cnt, sizeof(bool)); > + if (!wrapped) > + return -ENOMEM; > + > + /* > + * init new allocated values. > + */ > + for (i = cnt; i < new_cnt; i++) > + wrapped[i] = false; > + > + ptr->wrapped_cnt = new_cnt; > + ptr->wrapped = wrapped; > + > + return 0; > +} > + > +static bool arm_spe_buffer_has_wrapped(unsigned char *buffer, > + size_t buffer_size, u64 head) > +{ > + u64 i, watermark; > + u64 *buf = (u64 *)buffer; > + size_t buf_size = buffer_size; > + > + /* > + * Defensively handle the case where head might be continually increasing - if its value is > + * equal or greater than the size of the ring buffer, then we can safely determine it has > + * wrapped around. Otherwise, continue to detect if head might have wrapped. > + */ > + if (head >= buffer_size) > + return true; > + > + /* > + * We want to look the very last 512 byte (chosen arbitrarily) in the ring buffer. > + */ > + watermark = buf_size - 512; > + > + /* > + * The value of head is somewhere within the size of the ring buffer. This can be that there > + * hasn't been enough data to fill the ring buffer yet or the trace time was so long that > + * head has numerically wrapped around. To find we need to check if we have data at the > + * very end of the ring buffer. We can reliably do this because mmap'ed pages are zeroed > + * out and there is a fresh mapping with every new session. > + */ > + > + /* > + * head is less than 512 byte from the end of the ring buffer. > + */ > + if (head > watermark) > + watermark = head; > + > + /* > + * Speed things up by using 64 bit transactions (see "u64 *buf" above) > + */ > + watermark /= sizeof(u64); > + buf_size /= sizeof(u64); > + > + /* > + * If we find trace data at the end of the ring buffer, head has been there and has > + * numerically wrapped around at least once. > + */ > + for (i = watermark; i < buf_size; i++) > + if (buf[i]) > + return true; > + > + return false; > +} > + > +static int arm_spe_find_snapshot(struct auxtrace_record *itr, int idx, > + struct auxtrace_mmap *mm, unsigned char *data, > + u64 *head, u64 *old) > +{ > + int err; > + bool wrapped; > + struct arm_spe_recording *ptr = > + container_of(itr, struct arm_spe_recording, itr); > + > + /* > + * Allocate memory to keep track of wrapping if this is the first > + * time we deal with this *mm. > + */ > + if (idx >= ptr->wrapped_cnt) { > + err = arm_spe_alloc_wrapped_array(ptr, idx); > + if (err) > + return err; > + } > + > + /* > + * Check to see if *head has wrapped around. If it hasn't only the > + * amount of data between *head and *old is snapshot'ed to avoid > + * bloating the perf.data file with zeros. But as soon as *head has > + * wrapped around the entire size of the AUX ring buffer it taken. > + */ > + wrapped = ptr->wrapped[idx]; > + if (!wrapped && arm_spe_buffer_has_wrapped(data, mm->len, *head)) { > + wrapped = true; > + ptr->wrapped[idx] = true; > + } > + > + pr_debug3("%s: mmap index %d old head %zu new head %zu size %zu\n", > + __func__, idx, (size_t)*old, (size_t)*head, mm->len); > + > + /* > + * No wrap has occurred, we can just use *head and *old. > + */ > + if (!wrapped) > + return 0; > + > + /* > + * *head has wrapped around - adjust *head and *old to pickup the > + * entire content of the AUX buffer. > + */ > + if (*head >= mm->len) { > + *old = *head - mm->len; > + } else { > + *head += mm->len; > + *old = *head - mm->len; > + } > + > + return 0; > +} > + > static u64 arm_spe_reference(struct auxtrace_record *itr __maybe_unused) > { > struct timespec ts; > @@ -313,6 +456,7 @@ static void arm_spe_recording_free(struct auxtrace_record *itr) > struct arm_spe_recording *sper = > container_of(itr, struct arm_spe_recording, itr); > > + free(sper->wrapped); > free(sper); > } > > @@ -336,6 +480,7 @@ struct auxtrace_record *arm_spe_recording_init(int *err, > sper->itr.pmu = arm_spe_pmu; > sper->itr.snapshot_start = arm_spe_snapshot_start; > sper->itr.snapshot_finish = arm_spe_snapshot_finish; > + sper->itr.find_snapshot = arm_spe_find_snapshot; If I understand correctly, this patch copies the code from cs-etm for snapshot handling. About 2 months ago, we removed the Arm cs-etm's specific snapshot callback function and directly use perf's function __auxtrace_mmap__read() to handle 'head' and 'tail' pointers. Please see the commit for details: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=2f01c200d4405c4562e45e8bb4de44a5ce37b217 Before I review more details for snapshot enabling in patches 03 and 04, could you confirm if Arm SPE can use the same way with cs-etm for snapshot handling? From my understanding, this is a better way to handle AUX buffer's 'head' and 'tail'. Thanks, Leo > sper->itr.parse_snapshot_options = arm_spe_parse_snapshot_options; > sper->itr.recording_options = arm_spe_recording_options; > sper->itr.info_priv_size = arm_spe_info_priv_size; > -- > 2.17.1 > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel