From patchwork Fri Jul 28 17:59:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 707611 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 233E5C0015E for ; Fri, 28 Jul 2023 17:59:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234792AbjG1R7o (ORCPT ); Fri, 28 Jul 2023 13:59:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234529AbjG1R7l (ORCPT ); Fri, 28 Jul 2023 13:59:41 -0400 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C0F2421D for ; Fri, 28 Jul 2023 10:59:40 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-686b879f605so1695068b3a.1 for ; Fri, 28 Jul 2023 10:59:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690567179; x=1691171979; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=9QYMljA49yaTSa3X/JOrYWbbF31/d8XdDkCc11R7mxo=; b=Ihsw1MBP/XSBhYI1JIzzA4EK3q4Gs9lHkh9lo0Z/8DTVuOb7fC/Em5LeL6G3fqdpqF olPgEoMCIdL3LpMWYMJFM2iLTbPskivQUxvwGy5tClI6IQ/Vvq5ZG2SGFdchqTRIJOKG 6MgGMhvLBmSfT2A0YdR4WSmIiQL4SLtz0YvJ57EGD0DsBz0Fh811k+BbnQz1/2m7nKZi ZXsgUnxpBNuCWWyZ85x4ajqXprf6cnNJi74/OmfsZoAQ/rgLMf81EVAKvPfk1zZgZKH2 IqUek9yS1IV8XQACJtZmcYU8iyaezoZCFhWF3y/9l5vsR+3/QelTCTmUdG+DOtM+b1d7 uYow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690567179; x=1691171979; 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=9QYMljA49yaTSa3X/JOrYWbbF31/d8XdDkCc11R7mxo=; b=SdvfsaTpH682Q3iSxeye+enYQkd1uDX7th9UBwCmR4VMcPoYPeX7odWjuxX7JCA2zl AzaYMVM4VgJesYZ1DroX1ECw/5rKdKPGc6/AiObKucLENnbcIsF6/oh3/0bl6CvmRb4S g0jU4gxaG+hpauipSDw5f6Ux003Zu7Gr8Uw6rDbv1eDxXNB4ZeWxe8SKx/cvQLpjsIW/ DSPKJ0LpfwkPod03O5zgFlP0HjFxHYG7qPVh+WKa5+5sVBsnmwICZdp9IWVHn8s8mGwd quV0bl2uSB0OIHXG6YbsP7fPvR1rMeqqQHHCk4iQpvpSazV84Fw7qm5LLOGQXQ88e2Uw 6tVg== X-Gm-Message-State: ABy/qLbYGkNg12ahKAa8o/TApUZO7pflKf2PfEtgYLeFJnwaSN39KM0g BOoUwdkbDfb+EAg6iGDfWnK0IaCAG8U= X-Google-Smtp-Source: APBJJlF1N7sgL/JuoOtD/PcjgxgU7B2Uq1LGDQUmU804qzEuqdKXC8faAt5tUfA2ILwOOhsTNTXvkg== X-Received: by 2002:a05:6a00:1150:b0:675:ef91:7922 with SMTP id b16-20020a056a00115000b00675ef917922mr2354967pfm.4.1690567178768; Fri, 28 Jul 2023 10:59:38 -0700 (PDT) Received: from lvondent-mobl4.. (c-71-236-201-58.hsd1.or.comcast.net. [71.236.201.58]) by smtp.gmail.com with ESMTPSA id d7-20020aa78147000000b0068718aadda7sm427796pfn.108.2023.07.28.10.59.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jul 2023 10:59:38 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [BlueZ PATCH v4 1/3] monitor: Use gnuplot to plot graph of Latency-Packets Date: Fri, 28 Jul 2023 10:59:35 -0700 Message-ID: <20230728175937.42818-1-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz This make use of gnuplot when using -a/--analyze to plot a graph of Latency-Packets: Found BR-ACL connection with handle 256 Address: XX:XX:XX:XX:XX:XX (Sony Home Entertainment&Sound Products Inc) 60 RX packets 22548 TX packets 22547 TX completed packets 3 msec min latency 73 msec max latency 11 msec median latency 6 octets TX min packet size 850 octets TX max packet size 847 octets TX median packet size 10000 +------------------------------------------------------------------+ |+ + | |+ |+ Packets +-----+ | |+ || | 1000 |-+ +||++ | |+ |||||+ + | |+ ||||||+| | |+ ||||||||+ | |+ |||||||||+++ | 100 |-+ |||||||||||++++ | |+ ||||||||||||||| + | |+ ||||||||||||||| ++ | |+ + |||||||||||||||+|| | 10 |-+| ||||||||||||||||||+++ | |+ | ||||||||||||||||||||| | |+ | ||||||||||||||||||||| + | |+ | ++|||||||||||||||||||||++|++++ + | | | |||||||||||||||||||||||||||||| | | 1 |-+|+|||||||||||||||||||||||||||||| + + | + + + | +------------------------------------------------------------------+ 0 10 20 30 40 50 60 70 80 Latency (ms) --- monitor/analyze.c | 98 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 87 insertions(+), 11 deletions(-) diff --git a/monitor/analyze.c b/monitor/analyze.c index a20ba98b0be4..29aee5ad1680 100644 --- a/monitor/analyze.c +++ b/monitor/analyze.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "lib/bluetooth.h" @@ -28,6 +29,9 @@ #include "monitor/packet.h" #include "monitor/analyze.h" +#define TIMEVAL_MSEC(_tv) \ + (long long)((_tv)->tv_sec * 1000 + (_tv)->tv_usec / 1000) + struct hci_dev { uint16_t index; uint8_t type; @@ -69,12 +73,18 @@ struct hci_conn { struct timeval tx_lat_min; struct timeval tx_lat_max; struct timeval tx_lat_med; + struct queue *plot; uint16_t tx_pkt_min; uint16_t tx_pkt_max; uint16_t tx_pkt_med; struct queue *chan_list; }; +struct plot { + long long x_msec; + size_t y_count; +}; + struct l2cap_chan { uint16_t cid; uint16_t psm; @@ -135,6 +145,47 @@ static struct l2cap_chan *chan_lookup(struct hci_conn *conn, uint16_t cid, return chan; } +static void tmp_write(void *data, void *user_data) +{ + struct plot *plot = data; + FILE *tmp = user_data; + + fprintf(tmp, "%lld %zu\n", plot->x_msec, plot->y_count); +} + +static void plot_draw(struct queue *queue) +{ + const char *filename = "analyze.tmp"; + FILE *gplot = popen("gnuplot", "w"); + FILE *tmp; + + if (!gplot) + return; + + if (queue_isempty(queue)) + goto done; + + tmp = fopen(filename, "w"); + if (!tmp) + goto done; + + queue_foreach(queue, tmp_write, tmp); + + fprintf(gplot, "set terminal dumb enhanced ansi\n"); + fprintf(gplot, "set xlabel 'Latency (ms)'\n"); + fprintf(gplot, "set tics nomirror\n"); + fprintf(gplot, "set log y\n"); + fprintf(gplot, "set yrange [0.5:*]\n"); + fprintf(gplot, "plot './%s' using 1:2 t 'Packets' w impulses\n", + filename); + fflush(gplot); + + fclose(tmp); +done: + pclose(gplot); + unlink(filename); +} + static void conn_destroy(void *data) { struct hci_conn *conn = data; @@ -172,21 +223,17 @@ static void conn_destroy(void *data) print_field("%lu RX packets", conn->rx_num); print_field("%lu TX packets", conn->tx_num); print_field("%lu TX completed packets", conn->tx_num_comp); - print_field("%lld msec min latency", - (long long) - (conn->tx_lat_min.tv_sec * 1000 + - conn->tx_lat_min.tv_usec / 1000)); - print_field("%lld msec max latency", - (long long) - (conn->tx_lat_max.tv_sec * 1000 + - conn->tx_lat_max.tv_usec / 1000)); + print_field("%lld msec min latency", TIMEVAL_MSEC(&conn->tx_lat_min)); + print_field("%lld msec max latency", TIMEVAL_MSEC(&conn->tx_lat_max)); print_field("%lld msec median latency", - (long long) - (conn->tx_lat_med.tv_sec * 1000 + - conn->tx_lat_med.tv_usec / 1000)); + TIMEVAL_MSEC(&conn->tx_lat_med)); print_field("%u octets TX min packet size", conn->tx_pkt_min); print_field("%u octets TX max packet size", conn->tx_pkt_max); print_field("%u octets TX median packet size", conn->tx_pkt_med); + + plot_draw(conn->plot); + + queue_destroy(conn->plot, free); queue_destroy(conn->chan_list, chan_destroy); queue_destroy(conn->tx_queue, free); @@ -203,6 +250,7 @@ static struct hci_conn *conn_alloc(struct hci_dev *dev, uint16_t handle, conn->handle = handle; conn->type = type; conn->tx_queue = queue_new(); + conn->plot = queue_new(); conn->chan_list = queue_new(); @@ -447,6 +495,32 @@ static void evt_cmd_complete(struct hci_dev *dev, struct timeval *tv, } } +static bool match_plot_latency(const void *data, const void *user_data) +{ + const struct plot *plot = data; + const struct timeval *latency = user_data; + + return TIMEVAL_MSEC(latency) == plot->x_msec; +} + +static void plot_add(struct queue *queue, struct timeval *latency, + uint16_t count) +{ + struct plot *plot; + + plot = queue_find(queue, match_plot_latency, latency); + if (plot) { + plot->y_count += count; + return; + } + + plot = new0(struct plot, 1); + plot->x_msec = TIMEVAL_MSEC(latency); + plot->y_count = count; + + queue_push_tail(queue, plot); +} + static void evt_num_completed_packets(struct hci_dev *dev, struct timeval *tv, const void *data, uint16_t size) { @@ -504,6 +578,8 @@ static void evt_num_completed_packets(struct hci_dev *dev, struct timeval *tv, } else conn->tx_lat_med = res; + plot_add(conn->plot, &res, count); + free(last_tx); } } From patchwork Fri Jul 28 17:59:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 708602 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D327FC001E0 for ; Fri, 28 Jul 2023 17:59:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234554AbjG1R7q (ORCPT ); Fri, 28 Jul 2023 13:59:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234770AbjG1R7n (ORCPT ); Fri, 28 Jul 2023 13:59:43 -0400 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 354863C1F for ; Fri, 28 Jul 2023 10:59:42 -0700 (PDT) Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-686ea67195dso1839770b3a.2 for ; Fri, 28 Jul 2023 10:59:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690567180; x=1691171980; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=Gq/xN/8W0GSGo1Vn6uul++xq/XHjSuMSQtXh1disr3g=; b=rbHJ6S9UaimBrUN9ox/kvzs/5kjCsgOV17MMn/NoiaShd42axpKctPUpZvGCz4nVWH O7bBhn0LmBn6HIA7rfu1ftLHYvsTmVqsnD7OElf4RuhH3cfY93OZcwO27xg5kkqZJVgM FX+60JCQmO0nx9RdG6PT3M6uFd57Op+sKu8EZLX4lr+D4/kBJBVtQ4ID/tAoAOky/nZI iZ4Pyp2n6ciCwJ0hoBhlsnGyLdZOWlobZXEBmwLgNbIGVlkOw795AbinBKzQHeJcZU2T V5A1q8lmbemnoYSwNx8z5wG4HIVRk4IQj3JWF1W8sW/WRD7LHMtTSnvKORhnG9bIlIEw Amwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690567180; x=1691171980; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Gq/xN/8W0GSGo1Vn6uul++xq/XHjSuMSQtXh1disr3g=; b=JiX5wvyg15EFFMfXkSrObZo7lyXFqQN0W0NfxMws2eGI0W8ydkgFNBheLh7i+NT++e +GNrXEkdv35BNftsmAY7t97Cb4av1P2mMIf2pq9QOUy9NrtEYAPhVFzaf3CoKc0kUfUx qTAWDRynY1gjRAnYw+jYzIPEVGUgn2i59v3dSVrYygrbL8RDpuZIiVU8X9DvZrW+Y4Ww 9nNi4RXlOIwv5E0Ic+oIHElec1/eQGRG4Uv/GtssSPrrXg08fY+WIFzQx2e1zS/O1PXO e3aoD0cWj+C46SpDBbA4hYxDCMD2OcbT4/L5rKXEEcmx/69JPh7BDH9ObGmnlRAU2zLc AqhQ== X-Gm-Message-State: ABy/qLYuL0hLIO5bm3TX9ysBWeEbWfFdHahKKhVU8y3Jqnv1G41WmrF7 6DfdnhfgtZBETFolWAWel1HggJGXC9s= X-Google-Smtp-Source: APBJJlGpOMaKApM+xBV9g0DtUhyu8m34alunqobrXcTSuYGXhPZNYy9+peRsc1IURjGHRznksL7B7A== X-Received: by 2002:a05:6a20:1447:b0:138:1980:1dd3 with SMTP id a7-20020a056a20144700b0013819801dd3mr2441488pzi.14.1690567180494; Fri, 28 Jul 2023 10:59:40 -0700 (PDT) Received: from lvondent-mobl4.. (c-71-236-201-58.hsd1.or.comcast.net. [71.236.201.58]) by smtp.gmail.com with ESMTPSA id d7-20020aa78147000000b0068718aadda7sm427796pfn.108.2023.07.28.10.59.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jul 2023 10:59:39 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [BlueZ PATCH v4 2/3] monitor: Update documentation Date: Fri, 28 Jul 2023 10:59:36 -0700 Message-ID: <20230728175937.42818-2-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230728175937.42818-1-luiz.dentz@gmail.com> References: <20230728175937.42818-1-luiz.dentz@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz This updates the documentation to mention gnuplot usage. --- monitor/btmon.rst | 4 +++- monitor/main.c | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/monitor/btmon.rst b/monitor/btmon.rst index 0ab13eb2eb5c..82f9381c151d 100644 --- a/monitor/btmon.rst +++ b/monitor/btmon.rst @@ -33,7 +33,9 @@ OPTIONS -w FILE, --write FILE Save traces in btsnoop format to *FILE*. -a FILE, --analyze FILE Analyze traces in btsnoop format from *FILE*. It displays the devices found in the *FILE* with - its packets by type. + its packets by type. If gnuplot is installed on + the system it also attempts to plot packet latency + graph. -s SOCKET, --server SOCKET Start monitor server socket. -p PRIORITY, --priority PRIORITY Show only priority or lower for user log. diff --git a/monitor/main.c b/monitor/main.c index decf7cc467f2..fa56fcb29f38 100644 --- a/monitor/main.c +++ b/monitor/main.c @@ -51,6 +51,9 @@ static void usage(void) "\t-r, --read Read traces in btsnoop format\n" "\t-w, --write Save traces in btsnoop format\n" "\t-a, --analyze Analyze traces in btsnoop format\n" + "\t If gnuplot is installed on the\n" + "\t system it will also attempt to plot\n" + "\t packet latency graph.\n" "\t-s, --server Start monitor server socket\n" "\t-p, --priority Show only priority or lower\n" "\t-i, --index Show only specified controller\n" From patchwork Fri Jul 28 17:59:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 707610 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C37B3C001DF for ; Fri, 28 Jul 2023 17:59:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234457AbjG1R7r (ORCPT ); Fri, 28 Jul 2023 13:59:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234532AbjG1R7q (ORCPT ); Fri, 28 Jul 2023 13:59:46 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DAC193C33 for ; Fri, 28 Jul 2023 10:59:43 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-666e97fcc60so1782131b3a.3 for ; Fri, 28 Jul 2023 10:59:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690567182; x=1691171982; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=ibXM07c6qhpBPPubrRU/dg29tXbbaLxY26qKKoP83no=; b=ispfaMeyrqAXPdCkImLc8SH6Ix4lUtH23XX3gfzUAI0yrVF7JgLIe437LvfAoq5yQW qMwXqXx2i5VVeCU+ugjJvkuKbqSi8IEOoE4D3iWhIlCmsbmLh19uBbDkwE7gmwi2EOI2 bY2E9iUiNlaSyo2goJUPDax7VXYJVpx2Jo9K7IVr2CyFc2sjKqlemq69MaJJTsNMitmC 0au6Yz0Tk+Hiz418ns0/N+WvOT4UxlJLmUReDVXPtAi+FTEaW/OfKdFxIcQxUFElAPCc ty+E9tEXNTdvoZxO5gCvhBvyiG8PGFmrlHBEvu6wGBYpUOiAg0mTNEg+8mSHR2j4xCm6 CZGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690567182; x=1691171982; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ibXM07c6qhpBPPubrRU/dg29tXbbaLxY26qKKoP83no=; b=Ga/LAUNacUFvwPFYJ4vdSCsvnLAfg+pmJ8md4xdH6Ku76HrQ1AHILmxPmoph8y8h+O 7xWSSGDyEDxGASHmOjncA6tv26YTkTyoNAl0sgdeE36oOfuPaq+XRER0GhabDI/dnaBR CrsoG/Crr+ay5qI6NTfBbcRLMF7PFxiN3UH1wGJA0YX7vRas3BZQM8Ypp+yA2CUzsCXP rcGE5fXWrU+WOYFdZMsdgwxntpUBmfdEKJSITTtfyPt/QY+RsSVOt5sVVB85JNCCNTXl XHm1MzSEMKyCeku+fIP07WZmB1E10vzc/SsEEapacgrxJ+tkLBaw3ZVBkza+7dtPuslx aq+Q== X-Gm-Message-State: ABy/qLZjuvwshWaXwDA0VjVhkSSXzSufmIfErwdTPEISXWro4/SCKQ9N OTyTtnTQ6yqrnStf8QNILIM51h0jBlw= X-Google-Smtp-Source: APBJJlG9O/1+8KUnAvX+ln+CWhzCl8+bq6johSitQt+41LfQtlZdvWv9EF6HOiU5dKICyb7yTywHnw== X-Received: by 2002:a05:6a00:1883:b0:666:b254:1c9c with SMTP id x3-20020a056a00188300b00666b2541c9cmr2739459pfh.27.1690567182420; Fri, 28 Jul 2023 10:59:42 -0700 (PDT) Received: from lvondent-mobl4.. (c-71-236-201-58.hsd1.or.comcast.net. [71.236.201.58]) by smtp.gmail.com with ESMTPSA id d7-20020aa78147000000b0068718aadda7sm427796pfn.108.2023.07.28.10.59.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jul 2023 10:59:40 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [BlueZ PATCH v4 3/3] monitor: Consolidate code around latency calculation Date: Fri, 28 Jul 2023 10:59:37 -0700 Message-ID: <20230728175937.42818-3-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230728175937.42818-1-luiz.dentz@gmail.com> References: <20230728175937.42818-1-luiz.dentz@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz This move latency calculations to a helper function called packet_latency_add so it can get reused by the likes of l2cap.c and analyze.c --- monitor/analyze.c | 44 ++++++----------------------------- monitor/l2cap.c | 36 ++++------------------------- monitor/packet.c | 59 +++++++++++++++++++++++++---------------------- monitor/packet.h | 12 +++++++--- 4 files changed, 51 insertions(+), 100 deletions(-) diff --git a/monitor/analyze.c b/monitor/analyze.c index 29aee5ad1680..1c5e2557add5 100644 --- a/monitor/analyze.c +++ b/monitor/analyze.c @@ -70,9 +70,7 @@ struct hci_conn { unsigned long tx_num_comp; size_t tx_bytes; struct queue *tx_queue; - struct timeval tx_lat_min; - struct timeval tx_lat_max; - struct timeval tx_lat_med; + struct packet_latency tx_l; struct queue *plot; uint16_t tx_pkt_min; uint16_t tx_pkt_max; @@ -223,13 +221,11 @@ static void conn_destroy(void *data) print_field("%lu RX packets", conn->rx_num); print_field("%lu TX packets", conn->tx_num); print_field("%lu TX completed packets", conn->tx_num_comp); - print_field("%lld msec min latency", TIMEVAL_MSEC(&conn->tx_lat_min)); - print_field("%lld msec max latency", TIMEVAL_MSEC(&conn->tx_lat_max)); - print_field("%lld msec median latency", - TIMEVAL_MSEC(&conn->tx_lat_med)); - print_field("%u octets TX min packet size", conn->tx_pkt_min); - print_field("%u octets TX max packet size", conn->tx_pkt_max); - print_field("%u octets TX median packet size", conn->tx_pkt_med); + print_field("%lld-%lld msec (~%lld msec) TX Latency", + TV_MSEC(conn->tx_l.min), TV_MSEC(conn->tx_l.max), + TV_MSEC(conn->tx_l.med)); + print_field("%u-%u octets (~%u octets) TX packet size", + conn->tx_pkt_min, conn->tx_pkt_max, conn->tx_pkt_med); plot_draw(conn->plot); @@ -550,33 +546,7 @@ static void evt_num_completed_packets(struct hci_dev *dev, struct timeval *tv, if (last_tx) { timersub(tv, last_tx, &res); - if ((!timerisset(&conn->tx_lat_min) || - timercmp(&res, &conn->tx_lat_min, <)) && - res.tv_sec >= 0 && res.tv_usec >= 0) - conn->tx_lat_min = res; - - if (!timerisset(&conn->tx_lat_max) || - timercmp(&res, &conn->tx_lat_max, >)) - conn->tx_lat_max = res; - - if (timerisset(&conn->tx_lat_med)) { - struct timeval tmp; - - timeradd(&conn->tx_lat_med, &res, &tmp); - - tmp.tv_sec /= 2; - tmp.tv_usec /= 2; - if (tmp.tv_sec % 2) { - tmp.tv_usec += 500000; - if (tmp.tv_usec >= 1000000) { - tmp.tv_sec++; - tmp.tv_usec -= 1000000; - } - } - - conn->tx_lat_med = tmp; - } else - conn->tx_lat_med = res; + packet_latency_add(&conn->tx_l, &res); plot_add(conn->plot, &res, count); diff --git a/monitor/l2cap.c b/monitor/l2cap.c index 8258475d26dc..8f3d8e65b510 100644 --- a/monitor/l2cap.c +++ b/monitor/l2cap.c @@ -100,9 +100,7 @@ struct chan_data { uint8_t ext_ctrl; uint8_t seq_num; uint16_t sdu; - struct timeval tx_min; - struct timeval tx_max; - struct timeval tx_med; + struct packet_latency tx_l; }; static struct chan_data chan_list[MAX_CHAN]; @@ -2798,8 +2796,6 @@ void l2cap_packet(uint16_t index, bool in, uint16_t handle, uint8_t flags, } } -#define TV_MSEC(_tv) (long long)((_tv)->tv_sec * 1000 + (_tv)->tv_usec / 1000) - void l2cap_dequeue_frame(struct timeval *delta, struct packet_conn_data *conn) { struct l2cap_frame *frame; @@ -2813,39 +2809,15 @@ void l2cap_dequeue_frame(struct timeval *delta, struct packet_conn_data *conn) if (!chan) return; - if ((!timerisset(&chan->tx_min) || timercmp(delta, &chan->tx_min, <)) - && delta->tv_sec >= 0 && delta->tv_usec >= 0) - chan->tx_min = *delta; - - if (!timerisset(&chan->tx_max) || timercmp(delta, &chan->tx_max, >)) - chan->tx_max = *delta; - - if (timerisset(&chan->tx_med)) { - struct timeval tmp; - - timeradd(&chan->tx_med, delta, &tmp); - - tmp.tv_sec /= 2; - tmp.tv_usec /= 2; - if (tmp.tv_sec % 2) { - tmp.tv_usec += 500000; - if (tmp.tv_usec >= 1000000) { - tmp.tv_sec++; - tmp.tv_usec -= 1000000; - } - } - - chan->tx_med = tmp; - } else - chan->tx_med = *delta; + packet_latency_add(&chan->tx_l, delta); print_field("Channel: %d [PSM %d mode %s (0x%02x)] {chan %d}", frame->cid, frame->psm, mode2str(frame->mode), frame->mode, frame->chan); print_field("Channel Latency: %lld msec (%lld-%lld msec ~%lld msec)", - TV_MSEC(delta), TV_MSEC(&chan->tx_min), - TV_MSEC(&chan->tx_max), TV_MSEC(&chan->tx_med)); + TV_MSEC(*delta), TV_MSEC(chan->tx_l.min), + TV_MSEC(chan->tx_l.max), TV_MSEC(chan->tx_l.med)); free(frame); } diff --git a/monitor/packet.c b/monitor/packet.c index f581a8e7233d..b905080edc60 100644 --- a/monitor/packet.c +++ b/monitor/packet.c @@ -10336,7 +10336,34 @@ static void role_change_evt(struct timeval *tv, uint16_t index, print_role(evt->role); } -#define TV_MSEC(_tv) (long long)(_tv.tv_sec * 1000 + _tv.tv_usec / 1000) +void packet_latency_add(struct packet_latency *latency, struct timeval *delta) +{ + if ((!timerisset(&latency->min) || timercmp(delta, &latency->min, <)) + && delta->tv_sec >= 0 && delta->tv_usec >= 0) + latency->min = *delta; + + if (!timerisset(&latency->max) || timercmp(delta, &latency->max, >)) + latency->max = *delta; + + if (timerisset(&latency->med)) { + struct timeval tmp; + + timeradd(&latency->med, delta, &tmp); + + tmp.tv_sec /= 2; + tmp.tv_usec /= 2; + if (tmp.tv_sec % 2) { + tmp.tv_usec += 500000; + if (tmp.tv_usec >= 1000000) { + tmp.tv_sec++; + tmp.tv_usec -= 1000000; + } + } + + latency->med = tmp; + } else + latency->med = *delta; +} static void packet_dequeue_tx(struct timeval *tv, uint16_t handle) { @@ -10354,35 +10381,11 @@ static void packet_dequeue_tx(struct timeval *tv, uint16_t handle) timersub(tv, tx, &delta); - if ((!timerisset(&conn->tx_min) || timercmp(&delta, &conn->tx_min, <)) - && delta.tv_sec >= 0 && delta.tv_usec >= 0) - conn->tx_min = delta; - - if (!timerisset(&conn->tx_max) || timercmp(&delta, &conn->tx_max, >)) - conn->tx_max = delta; - - if (timerisset(&conn->tx_med)) { - struct timeval tmp; - - timeradd(&conn->tx_med, &delta, &tmp); - - tmp.tv_sec /= 2; - tmp.tv_usec /= 2; - if (tmp.tv_sec % 2) { - tmp.tv_usec += 500000; - if (tmp.tv_usec >= 1000000) { - tmp.tv_sec++; - tmp.tv_usec -= 1000000; - } - } - - conn->tx_med = tmp; - } else - conn->tx_med = delta; + packet_latency_add(&conn->tx_l, &delta); print_field("Latency: %lld msec (%lld-%lld msec ~%lld msec)", - TV_MSEC(delta), TV_MSEC(conn->tx_min), - TV_MSEC(conn->tx_max), TV_MSEC(conn->tx_med)); + TV_MSEC(delta), TV_MSEC(conn->tx_l.min), + TV_MSEC(conn->tx_l.max), TV_MSEC(conn->tx_l.med)); l2cap_dequeue_frame(&delta, conn); diff --git a/monitor/packet.h b/monitor/packet.h index 88f4c8241ff6..12cb68d508f8 100644 --- a/monitor/packet.h +++ b/monitor/packet.h @@ -23,6 +23,13 @@ #define PACKET_FILTER_SHOW_A2DP_STREAM (1 << 6) #define PACKET_FILTER_SHOW_MGMT_SOCKET (1 << 7) #define PACKET_FILTER_SHOW_ISO_DATA (1 << 8) +#define TV_MSEC(_tv) (long long)((_tv).tv_sec * 1000 + (_tv).tv_usec / 1000) + +struct packet_latency { + struct timeval min; + struct timeval max; + struct timeval med; +}; struct packet_conn_data { uint16_t index; @@ -33,14 +40,13 @@ struct packet_conn_data { uint8_t dst_type; struct queue *tx_q; struct queue *chan_q; - struct timeval tx_min; - struct timeval tx_max; - struct timeval tx_med; + struct packet_latency tx_l; void *data; void (*destroy)(void *data); }; struct packet_conn_data *packet_get_conn_data(uint16_t handle); +void packet_latency_add(struct packet_latency *latency, struct timeval *delta); bool packet_has_filter(unsigned long filter); void packet_set_filter(unsigned long filter);