Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp7662889rwl; Tue, 10 Jan 2023 03:56:11 -0800 (PST) X-Google-Smtp-Source: AMrXdXt686xv5Io5xbFXFYrmJ2y2s2ZA/R1j8DS8jOs1oqS1x8jnMrYZVBxoVw8J/Y/71rYCSxG6 X-Received: by 2002:a50:ec85:0:b0:492:8c77:7da9 with SMTP id e5-20020a50ec85000000b004928c777da9mr16679673edr.9.1673351771296; Tue, 10 Jan 2023 03:56:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673351771; cv=none; d=google.com; s=arc-20160816; b=ovj0ocsZvL0no+kGdlOLFKJhBGlx6HuKrrjYYOcUK7Ymif8ZIiCiSa6B9K4v4uf1qI rYZAg7u0KvtakRwehpCYK6mf2poAsbpKrbrT5Qf4MWfdYApYh4nUyvnKgwbKOQHo+24z H2XEztSa5IEAvlemSNjkQZbRPkHDyw6/ZASGvvtZDiCWP6KH2t7U0Q+ofUcWmBL0sLhe QWv5RBO9d/1wMG1OCMrjOKse1Z4abZuA+W+49K4sRax1GhUWNIkIvGN4Lb4kLJk45hQl ZnamdeSaDtkPfuEjZ+hdRFcYspN0jgCmwYnn3jLqLaMZWsLAxrPDRt3wRHYLCHQ6s/nm 0Jew== 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=gvbUlZbRkizbNLG13i/TrEMaNfYnnm+tdXnbq4eQJHE=; b=yUyx3/8UJjjyeKEw9Te1JJoOcOz/M8fqHsINGfUle5xe0fXx/w3p3U4zJR557qjvny PaOtLKWsdgKz9vHh+1YGw6BzPe3Hn//X1JIg9btKZiCr47NCAFpRueNF9JEV4T20c5sH 7RQ5I2/aaVQ7+BeKmigXnpJHI4jHBIuzgfe0ryXTzdUoxObv5GTow8X2V7Oy9IdnSdYE Uym6XWOW3qR9D0F9oNzjFpEM2OV8l51X7NYtqI+R6qVLSI9wWUCO9Bt3vC9D1g4/iFdq ksuK7b/DHiBgqQ6Q3dmNUVeNOKkeJ8ytmI+4f3LGyD5weV3xZD352taGDVNE9qIHBpNr /nrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=cVgRNtqY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h12-20020a056402280c00b00482f01ff579si13424671ede.131.2023.01.10.03.55.58; Tue, 10 Jan 2023 03:56:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=cVgRNtqY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232546AbjAJLuV (ORCPT + 53 others); Tue, 10 Jan 2023 06:50:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238163AbjAJLto (ORCPT ); Tue, 10 Jan 2023 06:49:44 -0500 Received: from mail-yw1-x1133.google.com (mail-yw1-x1133.google.com [IPv6:2607:f8b0:4864:20::1133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C38E1544FD for ; Tue, 10 Jan 2023 03:49:42 -0800 (PST) Received: by mail-yw1-x1133.google.com with SMTP id 00721157ae682-4a2f8ad29d5so150331207b3.8 for ; Tue, 10 Jan 2023 03:49:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=gvbUlZbRkizbNLG13i/TrEMaNfYnnm+tdXnbq4eQJHE=; b=cVgRNtqYIixuNnDAzzPFynB0ej+czZZDE01Jr9Mymv37ApAjz8e+v0nc53E16TmoNF hnXTrsfxPUoOc1VKP8mrf/cSI20DjnU61BvQ6MB4oisGUfXAy8OiPB7RP6yT6HILLhAi DQRu9aLKR/bZjGqtghxWVocK//SXMG5GxPC9ak5M90RtbnuYibg6uo0YTSF6awsS+HYA I/5rKYE/EcgiKBk9xplqTvSV1wJ7LOKj03T9iPWx0vRWd8Rz1KGOj/mmAZNSvq36aLCp eEQxIyYc1NzRVdKO13tnGbY1PqAcgV42kukI4r7qYB6bcvTcfSTy9WANZb98LsiRJM4c GfuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=gvbUlZbRkizbNLG13i/TrEMaNfYnnm+tdXnbq4eQJHE=; b=tr/+zC7A4RLFosZqgi+oQRAFm9NdyQiHl2jl1iYNCAtJH2HsukeIkm9DMZBoxgJFHL q1lIQPULB4psldDD94qtl5wt06Sz/fJ08r/5tmND2AUceNe9AmBiqHV1LRoV5eBNmYPV dlGIwLhC0yNhpDZdLwHqxx0xvI9D96t5a+IKypZG1jwAAEzEPaBrr7TmS7vlBLtt2wjo ZXNwdRuUk0VL1oWaw2FNCm1bHJpo1rE6TBH29IhiWpefcWtDRQDY0jduR8u4wnX/LLTW fBK46FhKJ99zByppxSLuuJjEkBbe/0QLzAxo/IFZejtjNwttdxK/xnWOHi1OfeYcohpN i7tQ== X-Gm-Message-State: AFqh2krxopYXClDPEr/h+DmPEq2H5XIggWLEoCiKUlUhqozpDo7TG4Dm uXpRTz9H1ak6m0qSvnsAkKtZ8tnRsHoTR+/4+iyHUg== X-Received: by 2002:a05:690c:b05:b0:467:2f6:4de5 with SMTP id cj5-20020a05690c0b0500b0046702f64de5mr2220727ywb.278.1673351381761; Tue, 10 Jan 2023 03:49:41 -0800 (PST) MIME-Version: 1.0 References: <20230110091356.1524-1-cuiyunhui@bytedance.com> In-Reply-To: <20230110091356.1524-1-cuiyunhui@bytedance.com> From: Eric Dumazet Date: Tue, 10 Jan 2023 12:49:30 +0100 Message-ID: Subject: Re: [PATCH v5] sock: add tracepoint for send recv length To: Yunhui Cui Cc: rostedt@goodmis.org, mhiramat@kernel.org, davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, kuniyu@amazon.com, xiyou.wangcong@gmail.com, duanxiongchun@bytedance.com, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, dust.li@linux.alibaba.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-16.4 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,NUMERIC_HTTP_ADDR,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 10, 2023 at 10:15 AM Yunhui Cui wrote: > > Add 2 tracepoints to monitor the tcp/udp traffic > of per process and per cgroup. > > Regarding monitoring the tcp/udp traffic of each process, there are two > existing solutions, the first one is https://www.atoptool.nl/netatop.php. > The second is via kprobe/kretprobe. > > Netatop solution is implemented by registering the hook function at the > hook point provided by the netfilter framework. > > These hook functions may be in the soft interrupt context and cannot > directly obtain the pid. Some data structures are added to bind packets > and processes. For example, struct taskinfobucket, struct taskinfo ... > > Every time the process sends and receives packets it needs multiple > hashmaps,resulting in low performance and it has the problem fo inaccurate > tcp/udp traffic statistics(for example: multiple threads share sockets). > > We can obtain the information with kretprobe, but as we know, kprobe gets > the result by trappig in an exception, which loses performance compared > to tracepoint. > > We compared the performance of tracepoints with the above two methods, and > the results are as follows: > > ab -n 1000000 -c 1000 -r http://127.0.0.1/index.html > without trace: > Time per request: 39.660 [ms] (mean) > Time per request: 0.040 [ms] (mean, across all concurrent requests) > > netatop: > Time per request: 50.717 [ms] (mean) > Time per request: 0.051 [ms] (mean, across all concurrent requests) > > kr: > Time per request: 43.168 [ms] (mean) > Time per request: 0.043 [ms] (mean, across all concurrent requests) > > tracepoint: > Time per request: 41.004 [ms] (mean) > Time per request: 0.041 [ms] (mean, across all concurrent requests > > It can be seen that tracepoint has better performance. > > Signed-off-by: Yunhui Cui > Signed-off-by: Xiongchun Duan > --- > include/trace/events/sock.h | 44 +++++++++++++++++++++++++++++++++++++ > net/socket.c | 36 ++++++++++++++++++++++++++---- > 2 files changed, 76 insertions(+), 4 deletions(-) > ... > +static noinline void call_trace_sock_recv_length(struct sock *sk, int ret, int flags) > +{ > + trace_sock_recv_length(sk, !(flags & MSG_PEEK) ? ret : > + (ret < 0 ? ret : 0), flags); Maybe we should only 'fast assign' the two fields (ret and flags), and let this logic happen later at 'print' time ? This would reduce storage by one integer, and make fast path really fast. This also could potentially remove the need for the peculiar construct with these noinline helpers. > +} > + > static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg, > int flags) > { > - return INDIRECT_CALL_INET(sock->ops->recvmsg, inet6_recvmsg, > - inet_recvmsg, sock, msg, msg_data_left(msg), > - flags); > + int ret = INDIRECT_CALL_INET(sock->ops->recvmsg, inet6_recvmsg, > + inet_recvmsg, sock, msg, > + msg_data_left(msg), flags); > + > + if (trace_sock_recv_length_enabled()) > + call_trace_sock_recv_length(sock->sk, !(flags & MSG_PEEK) ? > + ret : (ret < 0 ? ret : 0), flags); > + return ret; > } Maybe you meant : if (trace_sock_recv_length_enabled()) call_trace_sock_recv_length(sock->sk, ret, flags); ? Please make sure to test your patches.