Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp1082855rdf; Wed, 22 Nov 2023 05:20:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IGWssHOH8brOpP5YCaU2pS3wRnVaNwHMIxbnpHGxBgVSt2WjqTIbzOXyWyEQ0ljHDEMATHP X-Received: by 2002:a05:6830:1d77:b0:6b9:a84a:a393 with SMTP id l23-20020a0568301d7700b006b9a84aa393mr2474475oti.37.1700659204021; Wed, 22 Nov 2023 05:20:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700659203; cv=none; d=google.com; s=arc-20160816; b=x4Zfbev+JMQTWR0kHymfpHQCeSOEd57rf+F4z/5WLoeQVx4s5oO7eEpYy7c7iErdKL xxa+8SwrPrd2DRRdIKI7losTnrZLOLvWNGRbqGgrWU9fSSdst24i9gQ7hygsy+Tinvoy B8angR0uK8HVkCg811fzs97sR/lvv5MnVZIk9vGHkupIhgtTQH+kacZClXajwPCqEpzk AunxTM57qorFmf05BpKSlgPJubU5n3uKIQs2L9bM1NBR1T5RnyMpKm+zTPMuzLY2yIDa c6Gv7+i44RqGk1JlZktP3mtjB15uPNIYT3NNBbQVtEFRYE8TOL+wBzyXghUiys+fe7xT Rr2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=vB3h17ZiZ1W4dA7P94tMnhpktj3v+3WDCq3z7CXQplU=; fh=xujvjhh60PEBENTxTXKkH4RNvmRMsPutx62hcb2Jd1Q=; b=USI9YSq0QhlcsAjJEcMcsO3TM+ytg8ezCGnmXevXgI7ymeipS9gGpcuG248G0XKLsb 4HDK7cI6q7C19Ttm+gbjWy7+Ak56TxM6ov7G5zrTB/NPpXcEy/BVq/qrN4XvEr41QK1x F/V/mRdGjvM2eZs8pc2zrq07lm9cFsgWUpx/uhlUBasumhh3VHO2Gzarxc/pD5JtA+Mg 1wnGTEtzm3rJvzcU6/iNo9PZ6Z37v0xNvni50EK9GN7u/jspQjVG5SkHTGRjmUlsQdCx Z93BrI6X1sqYBAksHna+Mp8udGrWOYr2mAXgMsDIe7yKPJXGpsTYVfeFGz1zxpaiOJ15 ky5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Um7ABIw7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id a65-20020a639044000000b005c1cd55bf05si12674984pge.882.2023.11.22.05.20.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 05:20:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Um7ABIw7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id A9DE381BC4AF; Wed, 22 Nov 2023 05:20:00 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344092AbjKVNTf (ORCPT + 99 others); Wed, 22 Nov 2023 08:19:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344098AbjKVNTd (ORCPT ); Wed, 22 Nov 2023 08:19:33 -0500 Received: from mail-yw1-x1131.google.com (mail-yw1-x1131.google.com [IPv6:2607:f8b0:4864:20::1131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 365B01A5 for ; Wed, 22 Nov 2023 05:19:29 -0800 (PST) Received: by mail-yw1-x1131.google.com with SMTP id 00721157ae682-5cbbe2cba33so16136177b3.1 for ; Wed, 22 Nov 2023 05:19:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1700659168; x=1701263968; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=vB3h17ZiZ1W4dA7P94tMnhpktj3v+3WDCq3z7CXQplU=; b=Um7ABIw75/7Zu3kE8hojnBlSlnTXIKIykW86iovazp6BwkDpkOUMOSgtpbKeVoGkdV Ywz8rSaTv22fikUJyychvl9sDWuDsMmgPgxDvi5QpJ8rbw4CVlKJGnPQF5OkSrsBE+iW DJuAr8NfdV6SwJwogoYlQwtbTR+sZP5eheq2E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700659168; x=1701263968; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=vB3h17ZiZ1W4dA7P94tMnhpktj3v+3WDCq3z7CXQplU=; b=koFw9CJVo1L1HbLHtxAkTYCqpzulbJH2sKQILjfoDS8iQNoAQ2UTIgXgf+W9/XyLXg DMNTouisr00VeZ918eYhkWQSJr8aQC23ShL2r3LiTJEEURYBKsS1Yo1fA6lkkGuUJJac Tjs1qcLQf14ADqbt5pxcvwgedpIgWD1wIC2gaW9xbY1B5etiz25/MjO0ALnVumU+wqk/ zZ92Bp6axP/8KAeNW0Uo75sViuTHvFVOm4NY6XfE79GBKx8w0jd42K64Dwed2RuTs482 wPKzyou6dcJUBcIauBm5ae3Xv8WrcrtL5VlSiQnSbqvF4Nc25rsuxP6cxtEkZeEjSuGk v25g== X-Gm-Message-State: AOJu0Yzn+JJFm0PtT8SZ7c8Q4NOoUsvm/vDX6305oKbILJHbOBoNfW6X 1+9dJmTj5rJG7AziNOeiWqU28aN2hPbtZ3FUYYc/yA== X-Received: by 2002:a81:798f:0:b0:5cc:a937:e with SMTP id u137-20020a81798f000000b005cca937000emr937875ywc.19.1700659168067; Wed, 22 Nov 2023 05:19:28 -0800 (PST) Received: from mail-yb1-f177.google.com (mail-yb1-f177.google.com. [209.85.219.177]) by smtp.gmail.com with ESMTPSA id en15-20020a05690c2b8f00b005cab0d7b0f1sm1769368ywb.6.2023.11.22.05.19.27 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Nov 2023 05:19:27 -0800 (PST) Received: by mail-yb1-f177.google.com with SMTP id 3f1490d57ef6-db048181cd3so6611167276.3 for ; Wed, 22 Nov 2023 05:19:27 -0800 (PST) X-Received: by 2002:a25:6605:0:b0:da3:ab41:33c6 with SMTP id a5-20020a256605000000b00da3ab4133c6mr1946922ybc.65.1700659167137; Wed, 22 Nov 2023 05:19:27 -0800 (PST) MIME-Version: 1.0 References: <20231106-uvc-event-v3-1-c2d2fdaa2e2c@chromium.org> <995f8f26-ed20-44d2-a76a-e354fc68d65e@xs4all.nl> In-Reply-To: <995f8f26-ed20-44d2-a76a-e354fc68d65e@xs4all.nl> From: Ricardo Ribalda Date: Wed, 22 Nov 2023 14:19:14 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3] media: uvcvideo: Implement V4L2_EVENT_FRAME_SYNC To: Hans Verkuil Cc: Laurent Pinchart , Mauro Carvalho Chehab , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Esker Wong Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Wed, 22 Nov 2023 05:20:00 -0800 (PST) Hi Hans On Tue, 21 Nov 2023 at 11:37, Hans Verkuil wrote: > > Hi Ricardo, > > Sorry, I missed the whole discussion for the v2 patch. I've now read it and > will reply here. > > On 11/6/23 12:00, Ricardo Ribalda wrote: > > Add support for the frame_sync event, so user-space can become aware > > earlier of new frames. Will fix the description in the next version thanks! > > I don't think this describes why you want this. Specifically, you want to use > this to measure latency inside the driver between the arrival of the first USB > packet and the time the buffer is dequeued. > > And this is presumably meant for debugging/measuring, but not for normal > capturing. Right? > > Based on the discussion it looks like everyone is OK with this for the BULK > case, and V4L2_EVENT_FRAME_SYNC makes sense to me there. You want to see the > sequence number in the event, and the description of the event matches what > happens. > > The problem is the ISOC case where it is debatable if this adds anything. > > Perhaps in the ISOC case this event shouldn't be supported? Unless you can > show that it does provide useful information in the ISOC case. I didn't see > that in the v2 discussion, but I might have missed it. There are the following times - t_exposure: Time when the exposure happens. We can get it from v4l2_buffer.timestamp based on the hw timestamp - t_usb_first: Time when the first usb frame arrives at the usb host. We cannot get it (or better said.. I do not know how to get it) - t_urb: Time when the first urb is processed by the driver. Implemented as V4L2_EVENT_FRAME_SYNC in this driver - t_dqbuf: Time when the buffer can be dequeded by userspace. Implemented a timestamp in userspace when the syscall finishes. What we would like to measure is (t_dqbuf-t_usb_first), but we cannot obtain t_usb_first. (t_urb-t_usb_first) is relatively small so it can be ignored: For ISO the max we have measured is 1.8 msec vs 31 msec (t_dqbuf-t_urb) (t_urb-t_usb_first) is also always constant. If you are measuring trends, you do not care about an offset. There are two proposed alternatives to this patch: - that we use (t_dqbuf-t_exposure), but that measurement is dependent on the exposure time, so we cannot use that measurement. - use ftrace: but we will have to use different userspace methods for every driver, which defeats the purpose of V4L2_EVENT_FRAME_SYNC, and the metric will be as "bad" as the current proposal. If you are curious you can take a look at a trace here: https://ui.perfetto.dev/#!/?s=061a0fb7ebb0333e5dcbe35f487c18980e8e00a6e1b227c98d5e2569163924e0 Thanks! > > Regards, > > Hans > > > > > Suggested-by: Esker Wong > > Tested-by: Esker Wong > > Signed-off-by: Ricardo Ribalda > > --- > > We have measured a latency of around 30msecs between frame sync > > and dqbuf. > > --- > > Changes in v3: > > - Sent wrong patch as v2 sorry :S > > - Link to v2: https://lore.kernel.org/r/20231106-uvc-event-v2-1-7d8e36f0df16@chromium.org > > > > Changes in v2: > > - Suggested by Laurent. Split sequence++ and event init. > > - Link to v1: https://lore.kernel.org/r/20231020-uvc-event-v1-1-3baa0e9f6952@chromium.org > > --- > > drivers/media/usb/uvc/uvc_v4l2.c | 2 ++ > > drivers/media/usb/uvc/uvc_video.c | 7 +++++++ > > 2 files changed, 9 insertions(+) > > > > diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c > > index f4988f03640a..9f3fb5fd2375 100644 > > --- a/drivers/media/usb/uvc/uvc_v4l2.c > > +++ b/drivers/media/usb/uvc/uvc_v4l2.c > > @@ -1352,6 +1352,8 @@ static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh, > > switch (sub->type) { > > case V4L2_EVENT_CTRL: > > return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops); > > + case V4L2_EVENT_FRAME_SYNC: > > + return v4l2_event_subscribe(fh, sub, 0, NULL); > > default: > > return -EINVAL; > > } > > diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c > > index 28dde08ec6c5..6a9410133908 100644 > > --- a/drivers/media/usb/uvc/uvc_video.c > > +++ b/drivers/media/usb/uvc/uvc_video.c > > @@ -1073,9 +1073,16 @@ static int uvc_video_decode_start(struct uvc_streaming *stream, > > * that discontinuous sequence numbers always indicate lost frames. > > */ > > if (stream->last_fid != fid) { > > + struct v4l2_event event = { > > + .type = V4L2_EVENT_FRAME_SYNC, > > + }; > > + > > stream->sequence++; > > if (stream->sequence) > > uvc_video_stats_update(stream); > > + > > + event.u.frame_sync.frame_sequence = stream->sequence; > > + v4l2_event_queue(&stream->vdev, &event); > > } > > > > uvc_video_clock_decode(stream, buf, data, len); > > > > --- > > base-commit: ce55c22ec8b223a90ff3e084d842f73cfba35588 > > change-id: 20231020-uvc-event-d3d1bbbdcb2f > > > > Best regards, > -- Ricardo Ribalda