get:
Show a patch.

patch:
Update a patch.

put:
Update a patch.

GET /api/patches/16966/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 16966,
    "url": "https://patches.dpdk.org/api/patches/16966/?format=api",
    "web_url": "https://patches.dpdk.org/project/dpdk/patch/1478524474-7154-1-git-send-email-reshma.pattan@intel.com/",
    "project": {
        "id": 1,
        "url": "https://patches.dpdk.org/api/projects/1/?format=api",
        "name": "DPDK",
        "link_name": "dpdk",
        "list_id": "dev.dpdk.org",
        "list_email": "dev@dpdk.org",
        "web_url": "http://core.dpdk.org",
        "scm_url": "git://dpdk.org/dpdk",
        "webscm_url": "http://git.dpdk.org/dpdk",
        "list_archive_url": "https://inbox.dpdk.org/dev",
        "list_archive_url_format": "https://inbox.dpdk.org/dev/{}",
        "commit_url_format": ""
    },
    "msgid": "<1478524474-7154-1-git-send-email-reshma.pattan@intel.com>",
    "list_archive_url": "https://inbox.dpdk.org/dev/1478524474-7154-1-git-send-email-reshma.pattan@intel.com",
    "date": "2016-11-07T13:14:34",
    "name": "[dpdk-dev,v4] latencystats: added new library for latency stats",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": true,
    "hash": "14a0ceaf131d26596226fe9e20e695f4153373c0",
    "submitter": {
        "id": 70,
        "url": "https://patches.dpdk.org/api/people/70/?format=api",
        "name": "Pattan, Reshma",
        "email": "reshma.pattan@intel.com"
    },
    "delegate": null,
    "mbox": "https://patches.dpdk.org/project/dpdk/patch/1478524474-7154-1-git-send-email-reshma.pattan@intel.com/mbox/",
    "series": [],
    "comments": "https://patches.dpdk.org/api/patches/16966/comments/",
    "check": "warning",
    "checks": "https://patches.dpdk.org/api/patches/16966/checks/",
    "tags": {},
    "related": [],
    "headers": {
        "Return-Path": "<dev-bounces@dpdk.org>",
        "X-Original-To": "patchwork@dpdk.org",
        "Delivered-To": "patchwork@dpdk.org",
        "Received": [
            "from [92.243.14.124] (localhost [IPv6:::1])\n\tby dpdk.org (Postfix) with ESMTP id 755AF2BD0;\n\tMon,  7 Nov 2016 14:14:40 +0100 (CET)",
            "from mga04.intel.com (mga04.intel.com [192.55.52.120])\n\tby dpdk.org (Postfix) with ESMTP id 725652B9B\n\tfor <dev@dpdk.org>; Mon,  7 Nov 2016 14:14:38 +0100 (CET)",
            "from fmsmga005.fm.intel.com ([10.253.24.32])\n\tby fmsmga104.fm.intel.com with ESMTP; 07 Nov 2016 05:14:37 -0800",
            "from sivswdev02.ir.intel.com (HELO localhost.localdomain)\n\t([10.237.217.46])\n\tby fmsmga005.fm.intel.com with ESMTP; 07 Nov 2016 05:14:36 -0800"
        ],
        "X-ExtLoop1": "1",
        "X-IronPort-AV": "E=Sophos;i=\"5.31,606,1473145200\"; d=\"scan'208\";a=\"28358107\"",
        "From": "Reshma Pattan <reshma.pattan@intel.com>",
        "To": "dev@dpdk.org",
        "Cc": "Reshma Pattan <reshma.pattan@intel.com>",
        "Date": "Mon,  7 Nov 2016 13:14:34 +0000",
        "Message-Id": "<1478524474-7154-1-git-send-email-reshma.pattan@intel.com>",
        "X-Mailer": "git-send-email 1.7.0.7",
        "In-Reply-To": "<1476711584-25377-1-git-send-email-reshma.pattan@intel.com>",
        "References": "<1476711584-25377-1-git-send-email-reshma.pattan@intel.com>",
        "Subject": "[dpdk-dev] [PATCH v4] latencystats: added new library for latency\n\tstats",
        "X-BeenThere": "dev@dpdk.org",
        "X-Mailman-Version": "2.1.15",
        "Precedence": "list",
        "List-Id": "patches and discussions about DPDK <dev.dpdk.org>",
        "List-Unsubscribe": "<http://dpdk.org/ml/options/dev>,\n\t<mailto:dev-request@dpdk.org?subject=unsubscribe>",
        "List-Archive": "<http://dpdk.org/ml/archives/dev/>",
        "List-Post": "<mailto:dev@dpdk.org>",
        "List-Help": "<mailto:dev-request@dpdk.org?subject=help>",
        "List-Subscribe": "<http://dpdk.org/ml/listinfo/dev>,\n\t<mailto:dev-request@dpdk.org?subject=subscribe>",
        "Errors-To": "dev-bounces@dpdk.org",
        "Sender": "\"dev\" <dev-bounces@dpdk.org>"
    },
    "content": "Library is designed to calculate latency stats and report them to the\napplication when queried. Library measures minimum, average, maximum\nlatencies and jitter in nano seconds.\nCurrent implementation supports global latency stats, i.e. per application\nstats.\n\nAdded new field to mbuf struct to mark the packet arrival time on Rx and\nuse the timestamp to measure the latency on Tx.\n\nModified testpmd code to initialize/uninitialize latency stats calulation.\nModified dpdk-procinfo process to display the newly added metrics info.\n\nThis pacth is dependent on http://dpdk.org/dev/patchwork/patch/16927/ .\n\nAPIs:\n\nAdded APIs to initialize and un initialize latency stats\ncalculation.\nAdded API to retrieve latency stats names and values.\n\nFunctionality:\n\n*Library will register ethdev Rx/Tx callbacks for each active port,\nqueue combinations.\n*Library will register latency stats names with new metrics library.\nhttp://dpdk.org/dev/patchwork/patch/16927/\n*Rx packets will be marked with time stamp on each sampling interval.\n*On Tx side, packets with time stamp will be considered for calculating\nthe minimum, maximum, average latencies and jitter.\n*Average latency is calculated using exponential weighted moving average\nmethod.\n*Minimum and maximum latencies will be low and high latency values observed\nso far.\n*Jitter calculation is done based on inter packet delay variation.\n*Measured stats are reported to the metrics library in a separate pthread.\n*Measured stats can be retrieved via get API of the libray (or)\nby calling generic get API of the new metrics library.\n\ndocuments yet to be updated.\n\nSigned-off-by: Reshma Pattan <reshma.pattan@intel.com>\n---\n MAINTAINERS                                        |   4 +\n app/proc_info/main.c                               |  70 ++++\n app/test-pmd/testpmd.c                             |  10 +\n config/common_base                                 |   5 +\n lib/Makefile                                       |   1 +\n lib/librte_latencystats/Makefile                   |  57 ++++\n lib/librte_latencystats/rte_latencystats.c         | 380 +++++++++++++++++++++\n lib/librte_latencystats/rte_latencystats.h         | 141 ++++++++\n .../rte_latencystats_version.map                   |  10 +\n lib/librte_mbuf/rte_mbuf.h                         |   3 +\n mk/rte.app.mk                                      |   2 +\n 11 files changed, 683 insertions(+)\n create mode 100644 lib/librte_latencystats/Makefile\n create mode 100644 lib/librte_latencystats/rte_latencystats.c\n create mode 100644 lib/librte_latencystats/rte_latencystats.h\n create mode 100644 lib/librte_latencystats/rte_latencystats_version.map",
    "diff": "diff --git a/MAINTAINERS b/MAINTAINERS\nindex ba12d1b..2567448 100644\n--- a/MAINTAINERS\n+++ b/MAINTAINERS\n@@ -702,3 +702,7 @@ F: examples/tep_termination/\n F: examples/vmdq/\n F: examples/vmdq_dcb/\n F: doc/guides/sample_app_ug/vmdq_dcb_forwarding.rst\n+\n+Latency Stats\n+M: Reshma Pattan <reshma.pattan@intel.com>\n+F: lib/librte_latencystats/\ndiff --git a/app/proc_info/main.c b/app/proc_info/main.c\nindex 2c56d10..37d5ae4 100644\n--- a/app/proc_info/main.c\n+++ b/app/proc_info/main.c\n@@ -57,6 +57,7 @@\n #include <rte_atomic.h>\n #include <rte_branch_prediction.h>\n #include <rte_string_fns.h>\n+#include <rte_metrics.h>\n \n /* Maximum long option length for option parsing. */\n #define MAX_LONG_OPT_SZ 64\n@@ -68,6 +69,8 @@ static uint32_t enabled_port_mask;\n static uint32_t enable_stats;\n /**< Enable xstats. */\n static uint32_t enable_xstats;\n+/**< Enable metrics. */\n+static uint32_t enable_metrics;\n /**< Enable stats reset. */\n static uint32_t reset_stats;\n /**< Enable xstats reset. */\n@@ -85,6 +88,8 @@ proc_info_usage(const char *prgname)\n \t\t\"  --stats: to display port statistics, enabled by default\\n\"\n \t\t\"  --xstats: to display extended port statistics, disabled by \"\n \t\t\t\"default\\n\"\n+\t\t\"  --metrics: to display derived metrics of the ports, disabled by \"\n+\t\t\t\"default\\n\"\n \t\t\"  --stats-reset: to reset port statistics\\n\"\n \t\t\"  --xstats-reset: to reset port extended statistics\\n\",\n \t\tprgname);\n@@ -127,6 +132,7 @@ proc_info_parse_args(int argc, char **argv)\n \t\t{\"stats\", 0, NULL, 0},\n \t\t{\"stats-reset\", 0, NULL, 0},\n \t\t{\"xstats\", 0, NULL, 0},\n+\t\t{\"metrics\", 0, NULL, 0},\n \t\t{\"xstats-reset\", 0, NULL, 0},\n \t\t{NULL, 0, 0, 0}\n \t};\n@@ -159,6 +165,10 @@ proc_info_parse_args(int argc, char **argv)\n \t\t\telse if (!strncmp(long_option[option_index].name, \"xstats\",\n \t\t\t\t\tMAX_LONG_OPT_SZ))\n \t\t\t\tenable_xstats = 1;\n+\t\t\telse if (!strncmp(long_option[option_index].name,\n+\t\t\t\t\t\"metrics\",\n+\t\t\t\t\tMAX_LONG_OPT_SZ))\n+\t\t\t\tenable_metrics = 1;\n \t\t\t/* Reset stats */\n \t\t\tif (!strncmp(long_option[option_index].name, \"stats-reset\",\n \t\t\t\t\tMAX_LONG_OPT_SZ))\n@@ -301,6 +311,60 @@ nic_xstats_clear(uint8_t port_id)\n \tprintf(\"\\n  NIC extended statistics for port %d cleared\\n\", port_id);\n }\n \n+static void\n+metrics_display(int port_id)\n+{\n+\tstruct rte_stat_value *stats;\n+\tstruct rte_metric_name *names;\n+\tint len, ret;\n+\tstatic const char *nic_stats_border = \"########################\";\n+\n+\tmemset(&stats, 0, sizeof(struct rte_stat_value));\n+\tlen = rte_metrics_get_names(NULL, 0);\n+\tif (len < 0) {\n+\t\tprintf(\"Cannot get metrics count\\n\");\n+\t\treturn;\n+\t}\n+\n+\tstats = malloc(sizeof(struct rte_stat_value) * len);\n+\tif (stats == NULL) {\n+\t\tprintf(\"Cannot allocate memory for metrics\\n\");\n+\t\treturn;\n+\t}\n+\n+\tnames =  malloc(sizeof(struct rte_metric_name) * len);\n+\tif (names == NULL) {\n+\t\tprintf(\"Cannot allocate memory for metrcis names\\n\");\n+\t\tfree(stats);\n+\t\treturn;\n+\t}\n+\n+\tif (len != rte_metrics_get_names(names, len)) {\n+\t\tprintf(\"Cannot get metrics names\\n\");\n+\t\tfree(stats);\n+\t\tfree(names);\n+\t\treturn;\n+\t}\n+\n+\tprintf(\"###### metrics for port %-2d #########\\n\", port_id);\n+\tprintf(\"%s############################\\n\", nic_stats_border);\n+\tret = rte_metrics_get_values(port_id, stats, len);\n+\tif (ret < 0 || ret > len) {\n+\t\tprintf(\"Cannot get metrics values\\n\");\n+\t\tfree(stats);\n+\t\tfree(names);\n+\t\treturn;\n+\t}\n+\n+\tint i;\n+\tfor (i = 0; i < len; i++)\n+\t\tprintf(\"%s: %\"PRIu64\"\\n\", names[i].name, stats[i].value);\n+\n+\tprintf(\"%s############################\\n\", nic_stats_border);\n+\tfree(stats);\n+\tfree(names);\n+}\n+\n int\n main(int argc, char **argv)\n {\n@@ -360,8 +424,14 @@ main(int argc, char **argv)\n \t\t\t\tnic_stats_clear(i);\n \t\t\telse if (reset_xstats)\n \t\t\t\tnic_xstats_clear(i);\n+\t\t\telse if (enable_metrics)\n+\t\t\t\tmetrics_display(i);\n \t\t}\n \t}\n \n+\t/* print port independent stats */\n+\tif (enable_metrics)\n+\t\tmetrics_display(RTE_METRICS_NONPORT);\n+\n \treturn 0;\n }\ndiff --git a/app/test-pmd/testpmd.c b/app/test-pmd/testpmd.c\nindex 6185be6..0efab4e 100644\n--- a/app/test-pmd/testpmd.c\n+++ b/app/test-pmd/testpmd.c\n@@ -78,6 +78,10 @@\n #ifdef RTE_LIBRTE_PDUMP\n #include <rte_pdump.h>\n #endif\n+#include <rte_metrics.h>\n+#ifdef RTE_LIBRTE_LATENCY_STATS\n+#include <rte_latencystats.h>\n+#endif\n \n #include \"testpmd.h\"\n \n@@ -2070,6 +2074,9 @@ signal_handler(int signum)\n \t\t/* uninitialize packet capture framework */\n \t\trte_pdump_uninit();\n #endif\n+#ifdef RTE_LIBRTE_LATENCY_STATS\n+\t\trte_latencystats_uninit();\n+#endif\n \t\tforce_quit();\n \t\t/* exit with the expected status */\n \t\tsignal(signum, SIG_DFL);\n@@ -2127,6 +2134,9 @@ main(int argc, char** argv)\n \t/* set all ports to promiscuous mode by default */\n \tFOREACH_PORT(port_id, ports)\n \t\trte_eth_promiscuous_enable(port_id);\n+#ifdef RTE_LIBRTE_LATENCY_STATS\n+\trte_latencystats_init(1, NULL);\n+#endif\n \n #ifdef RTE_LIBRTE_CMDLINE\n \tif (interactive == 1) {\ndiff --git a/config/common_base b/config/common_base\nindex 21d18f8..d9002b8 100644\n--- a/config/common_base\n+++ b/config/common_base\n@@ -589,3 +589,8 @@ CONFIG_RTE_APP_TEST_RESOURCE_TAR=n\n CONFIG_RTE_TEST_PMD=y\n CONFIG_RTE_TEST_PMD_RECORD_CORE_CYCLES=n\n CONFIG_RTE_TEST_PMD_RECORD_BURST_STATS=n\n+\n+#\n+# Compile the latency statistics library\n+#\n+CONFIG_RTE_LIBRTE_LATENCY_STATS=y\ndiff --git a/lib/Makefile b/lib/Makefile\nindex 990f23a..2111349 100644\n--- a/lib/Makefile\n+++ b/lib/Makefile\n@@ -58,6 +58,7 @@ DIRS-$(CONFIG_RTE_LIBRTE_TABLE) += librte_table\n DIRS-$(CONFIG_RTE_LIBRTE_PIPELINE) += librte_pipeline\n DIRS-$(CONFIG_RTE_LIBRTE_REORDER) += librte_reorder\n DIRS-$(CONFIG_RTE_LIBRTE_PDUMP) += librte_pdump\n+DIRS-$(CONFIG_RTE_LIBRTE_LATENCY_STATS) += librte_latencystats\n \n ifeq ($(CONFIG_RTE_EXEC_ENV_LINUXAPP),y)\n DIRS-$(CONFIG_RTE_LIBRTE_KNI) += librte_kni\ndiff --git a/lib/librte_latencystats/Makefile b/lib/librte_latencystats/Makefile\nnew file mode 100644\nindex 0000000..f744da6\n--- /dev/null\n+++ b/lib/librte_latencystats/Makefile\n@@ -0,0 +1,57 @@\n+#   BSD LICENSE\n+#\n+#   Copyright(c) 2016 Intel Corporation. All rights reserved.\n+#   All rights reserved.\n+#\n+#   Redistribution and use in source and binary forms, with or without\n+#   modification, are permitted provided that the following conditions\n+#   are met:\n+#\n+#     * Redistributions of source code must retain the above copyright\n+#       notice, this list of conditions and the following disclaimer.\n+#     * Redistributions in binary form must reproduce the above copyright\n+#       notice, this list of conditions and the following disclaimer in\n+#       the documentation and/or other materials provided with the\n+#       distribution.\n+#     * Neither the name of Intel Corporation nor the names of its\n+#       contributors may be used to endorse or promote products derived\n+#       from this software without specific prior written permission.\n+#\n+#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n+#   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n+#   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n+#   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n+#   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n+#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n+#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n+#   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n+#   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n+#   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n+#   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n+\n+include $(RTE_SDK)/mk/rte.vars.mk\n+\n+# library name\n+LIB = librte_latencystats.a\n+\n+CFLAGS += $(WERROR_FLAGS) -I$(SRCDIR) -O3\n+LDLIBS += -lm\n+LDLIBS += -lpthread\n+\n+EXPORT_MAP := rte_latencystats_version.map\n+\n+LIBABIVER := 1\n+\n+# all source are stored in SRCS-y\n+SRCS-$(CONFIG_RTE_LIBRTE_LATENCY_STATS) := rte_latencystats.c\n+\n+# install this header file\n+SYMLINK-$(CONFIG_RTE_LIBRTE_LATENCY_STATS)-include := rte_latencystats.h\n+\n+# this lib depends upon:\n+DEPDIRS-$(CONFIG_RTE_LIBRTE_LATENCY_STATS) += lib/librte_mbuf\n+DEPDIRS-$(CONFIG_RTE_LIBRTE_LATENCY_STATS) += lib/librte_eal\n+DEPDIRS-$(CONFIG_RTE_LIBRTE_LATENCY_STATS) += lib/librte_ether\n+DEPDIRS-$(CONFIG_RTE_LIBRTE_LATENCY_STATS) += lib/librte_metrics\n+\n+include $(RTE_SDK)/mk/rte.lib.mk\ndiff --git a/lib/librte_latencystats/rte_latencystats.c b/lib/librte_latencystats/rte_latencystats.c\nnew file mode 100644\nindex 0000000..0bcb09f\n--- /dev/null\n+++ b/lib/librte_latencystats/rte_latencystats.c\n@@ -0,0 +1,380 @@\n+/*-\n+ *   BSD LICENSE\n+ *\n+ *   Copyright(c) 2016 Intel Corporation. All rights reserved.\n+ *   All rights reserved.\n+ *\n+ *   Redistribution and use in source and binary forms, with or without\n+ *   modification, are permitted provided that the following conditions\n+ *   are met:\n+ *\n+ *     * Redistributions of source code must retain the above copyright\n+ *       notice, this list of conditions and the following disclaimer.\n+ *     * Redistributions in binary form must reproduce the above copyright\n+ *       notice, this list of conditions and the following disclaimer in\n+ *       the documentation and/or other materials provided with the\n+ *       distribution.\n+ *     * Neither the name of Intel Corporation nor the names of its\n+ *       contributors may be used to endorse or promote products derived\n+ *       from this software without specific prior written permission.\n+ *\n+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n+ *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n+ */\n+\n+#include <unistd.h>\n+#include <sys/types.h>\n+#include <stdbool.h>\n+#include <math.h>\n+#include <pthread.h>\n+\n+#include <rte_mbuf.h>\n+#include <rte_log.h>\n+#include <rte_cycles.h>\n+#include <rte_ethdev.h>\n+#include <rte_metrics.h>\n+#include <rte_memzone.h>\n+#include <rte_lcore.h>\n+#include <rte_timer.h>\n+\n+#include \"rte_latencystats.h\"\n+\n+/** Nano seconds per second */\n+#define NS_PER_SEC 1E9\n+\n+/** Clock cycles per nano second */\n+#define CYCLES_PER_NS (rte_get_timer_hz() / NS_PER_SEC)\n+\n+/* Macros for printing using RTE_LOG */\n+#define RTE_LOGTYPE_LATENCY_STATS RTE_LOGTYPE_USER1\n+\n+static pthread_t latency_stats_thread;\n+static const char *MZ_RTE_LATENCY_STATS = \"rte_latencystats\";\n+static int latency_stats_index;\n+static uint64_t sampIntvl;\n+static uint64_t timer_tsc;\n+static uint64_t prev_tsc;\n+\n+struct rte_latency_stats {\n+\tfloat min_latency; /**< Minimum latency in nano seconds */\n+\tfloat avg_latency; /**< Average latency in nano seconds */\n+\tfloat max_latency; /**< Maximum latency in nano seconds */\n+\tfloat jitter; /** Latency variation between to packets */\n+\tuint64_t total_sampl_pkts;\n+} *glob_stats;\n+\n+static struct rxtx_cbs {\n+\tstruct rte_eth_rxtx_callback *cb;\n+} rx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT],\n+\ttx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT];\n+\n+struct latency_stats_nameoff {\n+\tchar name[RTE_ETH_XSTATS_NAME_SIZE];\n+\tunsigned int offset;\n+};\n+\n+static const struct latency_stats_nameoff lat_stats_strings[] = {\n+\t{\"min_latency_ns\", offsetof(struct rte_latency_stats, min_latency)},\n+\t{\"avg_latency_ns\", offsetof(struct rte_latency_stats, avg_latency)},\n+\t{\"max_latency_ns\", offsetof(struct rte_latency_stats, max_latency)},\n+\t{\"jitter_ns\", offsetof(struct rte_latency_stats, jitter)},\n+};\n+\n+#define NUM_LATENCY_STATS (sizeof(lat_stats_strings) / \\\n+\t\t\t\tsizeof(lat_stats_strings[0]))\n+\n+static __attribute__((noreturn)) void *\n+report_latency_stats(__rte_unused void *arg)\n+{\n+\tfor (;;) {\n+\t\tunsigned int i;\n+\t\tfloat *stats_ptr = NULL;\n+\t\tuint64_t values[NUM_LATENCY_STATS] = {0};\n+\t\tint ret;\n+\n+\t\tfor (i = 0; i < NUM_LATENCY_STATS; i++) {\n+\t\t\tstats_ptr = RTE_PTR_ADD(glob_stats,\n+\t\t\t\t\tlat_stats_strings[i].offset);\n+\t\t\tvalues[i] = (uint64_t)floor((*stats_ptr)/\n+\t\t\t\t\tCYCLES_PER_NS);\n+\t\t}\n+\n+\t\tret = rte_metrics_update_metrics(RTE_METRICS_NONPORT,\n+\t\t\t\t\t\tlatency_stats_index,\n+\t\t\t\t\t\tvalues, NUM_LATENCY_STATS);\n+\t\tif (ret < 0)\n+\t\t\tRTE_LOG(INFO, LATENCY_STATS,\n+\t\t\t\t\"Failed to push the stats\\n\");\n+\t}\n+}\n+\n+static void\n+rte_latencystats_fill_values(struct rte_stat_value *values)\n+{\n+\tunsigned int i;\n+\tfloat *stats_ptr = NULL;\n+\n+\tfor (i = 0; i < NUM_LATENCY_STATS; i++) {\n+\t\tstats_ptr = RTE_PTR_ADD(glob_stats,\n+\t\t\t\tlat_stats_strings[i].offset);\n+\t\tvalues[i].key = i;\n+\t\tvalues[i].value = (uint64_t)floor((*stats_ptr)/\n+\t\t\t\t\t\tCYCLES_PER_NS);\n+\t}\n+}\n+\n+static uint16_t\n+add_time_stamps(uint8_t pid __rte_unused,\n+\t\tuint16_t qid __rte_unused,\n+\t\tstruct rte_mbuf **pkts,\n+\t\tuint16_t nb_pkts,\n+\t\tuint16_t max_pkts __rte_unused,\n+\t\tvoid *user_cb __rte_unused)\n+{\n+\tunsigned int i;\n+\tuint64_t diff_tsc, now;\n+\n+\t/* for every sample interval,\n+\t * time stamp is marked on one received packet\n+\t */\n+\tnow = rte_rdtsc();\n+\tfor (i = 0; i < nb_pkts; i++) {\n+\t\tdiff_tsc = now - prev_tsc;\n+\t\ttimer_tsc += diff_tsc;\n+\t\tif (timer_tsc >= sampIntvl) {\n+\t\t\tpkts[i]->timestamp = now;\n+\t\t\ttimer_tsc = 0;\n+\t\t}\n+\t\tprev_tsc = now;\n+\t\tnow = rte_rdtsc();\n+\t}\n+\n+\treturn nb_pkts;\n+}\n+\n+static uint16_t\n+calc_latency(uint8_t pid __rte_unused,\n+\t\tuint16_t qid __rte_unused,\n+\t\tstruct rte_mbuf **pkts,\n+\t\tuint16_t nb_pkts,\n+\t\tvoid *_ __rte_unused)\n+{\n+\tunsigned int i, cnt = 0;\n+\tuint64_t now;\n+\tfloat latency[nb_pkts];\n+\tstatic float prev_latency;\n+\tconst float alpha = 0.2;\n+\n+\tnow = rte_rdtsc();\n+\tfor (i = 0; i < nb_pkts; i++) {\n+\t\tif (pkts[i]->timestamp)\n+\t\t\tlatency[cnt++] = now - pkts[i]->timestamp;\n+\t}\n+\n+\tfor (i = 0; i < cnt; i++) {\n+\t\t/**\n+\t\t*The jitter is calculated as statistical mean of interpacket\n+\t\t*delay variation. The \"jitter estimate\" is computed by taking\n+\t\t*the absolute values of the ipdv sequence and applying an\n+\t\t*exponential filter with parameter 1/16 to generate the\n+\t\t*estimate. i.e J=J+(|D(i-1,i)|-J)/16. Where J is jitter,\n+\t\t*D(i-1,i) is difference in latency of two consecutive packets\n+\t\t*i-1 and i.\n+\t\t*Reference: Calculated as per RFC 5481, sec 4.1,\n+\t\t*RFC 3393 sec 4.5, RFC 1889 sec.\n+\t\t*/\n+\t\tglob_stats->jitter +=  (abs(prev_latency - latency[i])\n+\t\t\t\t\t- glob_stats->jitter)/16;\n+\t\tif (glob_stats->min_latency == 0)\n+\t\t\tglob_stats->min_latency = latency[i];\n+\t\telse if (latency[i] < glob_stats->min_latency)\n+\t\t\tglob_stats->min_latency = latency[i];\n+\t\telse if (latency[i] > glob_stats->max_latency)\n+\t\t\tglob_stats->max_latency = latency[i];\n+\t\t/**\n+\t\t*The average latency is measured using exponential moving\n+\t\t*average, i.e. using EWMA\n+\t\t*https://en.wikipedia.org/wiki/Moving_average\n+\t\t*/\n+\t\tglob_stats->avg_latency +=\n+\t\t\talpha * (latency[i] - glob_stats->avg_latency);\n+\t\tglob_stats->total_sampl_pkts++;\n+\t\tprev_latency = latency[i];\n+\t}\n+\n+\treturn nb_pkts;\n+}\n+\n+int\n+rte_latencystats_init(uint64_t samp_intvl,\n+\t\trte_latency_stats_flow_type_fn user_cb)\n+{\n+\tunsigned int i;\n+\tuint8_t pid;\n+\tuint16_t qid;\n+\tstruct rxtx_cbs *cbs = NULL;\n+\tconst uint8_t nb_ports = rte_eth_dev_count();\n+\tconst char *ptr_strings[NUM_LATENCY_STATS] = {0};\n+\tconst struct rte_memzone *mz = NULL;\n+\tconst unsigned int flags = 0;\n+\n+\t/** Allocate stats in shared memory fo muliti process support */\n+\tmz = rte_memzone_reserve(MZ_RTE_LATENCY_STATS, sizeof(*glob_stats),\n+\t\t\t\t\trte_socket_id(), flags);\n+\tif (mz == NULL) {\n+\t\tRTE_LOG(ERR, LATENCY_STATS, \"Cannot reserve memory: %s:%d\\n\",\n+\t\t\t__func__, __LINE__);\n+\t\treturn -ENOMEM;\n+\t}\n+\n+\tglob_stats = mz->addr;\n+\tsamp_intvl *= CYCLES_PER_NS;\n+\n+\t/** Register latency stats with stats library */\n+\tfor (i = 0; i < NUM_LATENCY_STATS; i++)\n+\t\tptr_strings[i] = lat_stats_strings[i].name;\n+\n+\tlatency_stats_index = rte_metrics_reg_metrics(ptr_strings,\n+\t\t\t\t\t\t\tNUM_LATENCY_STATS);\n+\tif (latency_stats_index < 0) {\n+\t\tRTE_LOG(DEBUG, LATENCY_STATS,\n+\t\t\t\"Failed to register latency stats names\\n\");\n+\t\treturn -1;\n+\t}\n+\n+\t/** Register Rx/Tx callbacks */\n+\tfor (pid = 0; pid < nb_ports; pid++) {\n+\t\tstruct rte_eth_dev_info dev_info;\n+\t\trte_eth_dev_info_get(pid, &dev_info);\n+\t\tfor (qid = 0; qid < dev_info.nb_rx_queues; qid++) {\n+\t\t\tcbs = &rx_cbs[pid][qid];\n+\t\t\tcbs->cb = rte_eth_add_first_rx_callback(pid, qid,\n+\t\t\t\t\tadd_time_stamps, user_cb);\n+\t\t\tif (!cbs->cb)\n+\t\t\t\tRTE_LOG(INFO, LATENCY_STATS, \"Failed to \"\n+\t\t\t\t\t\"register Rx callback for pid=%d, \"\n+\t\t\t\t\t\"qid=%d\\n\", pid, qid);\n+\t\t}\n+\t\tfor (qid = 0; qid < dev_info.nb_tx_queues; qid++) {\n+\t\t\tcbs = &tx_cbs[pid][qid];\n+\t\t\tcbs->cb =  rte_eth_add_tx_callback(pid, qid,\n+\t\t\t\t\tcalc_latency, user_cb);\n+\t\t\tif (!cbs->cb)\n+\t\t\t\tRTE_LOG(INFO, LATENCY_STATS, \"Failed to \"\n+\t\t\t\t\t\"register Tx callback for pid=%d, \"\n+\t\t\t\t\t\"qid=%d\\n\", pid, qid);\n+\t\t}\n+\t}\n+\n+\tint ret = 0;\n+\tchar thread_name[RTE_MAX_THREAD_NAME_LEN];\n+\n+\t/** Create the host thread to update latency stats to stats library */\n+\tret = pthread_create(&latency_stats_thread, NULL, report_latency_stats,\n+\t\t\t\tNULL);\n+\tif (ret != 0) {\n+\t\tRTE_LOG(ERR, LATENCY_STATS,\n+\t\t\t\"Failed to create the latency stats thread:%s, %s:%d\\n\",\n+\t\t\tstrerror(errno), __func__, __LINE__);\n+\t\treturn -1;\n+\t}\n+\t/** Set thread_name for aid in debugging */\n+\tsnprintf(thread_name, RTE_MAX_THREAD_NAME_LEN, \"latency-stats-thread\");\n+\tret = rte_thread_setname(latency_stats_thread, thread_name);\n+\tif (ret != 0)\n+\t\tRTE_LOG(DEBUG, LATENCY_STATS,\n+\t\t\t\"Failed to set thread name for latency stats handling\\n\");\n+\n+\treturn 0;\n+}\n+\n+int\n+rte_latencystats_uninit(void)\n+{\n+\tuint8_t pid;\n+\tuint16_t qid;\n+\tint ret = 0;\n+\tstruct rxtx_cbs *cbs = NULL;\n+\tconst uint8_t nb_ports = rte_eth_dev_count();\n+\n+\t/** De register Rx/Tx callbacks */\n+\tfor (pid = 0; pid < nb_ports; pid++) {\n+\t\tstruct rte_eth_dev_info dev_info;\n+\t\trte_eth_dev_info_get(pid, &dev_info);\n+\t\tfor (qid = 0; qid < dev_info.nb_rx_queues; qid++) {\n+\t\t\tcbs = &rx_cbs[pid][qid];\n+\t\t\tret = rte_eth_remove_rx_callback(pid, qid, cbs->cb);\n+\t\t\tif (ret)\n+\t\t\t\tRTE_LOG(INFO, LATENCY_STATS, \"failed to \"\n+\t\t\t\t\t\"remove Rx callback for pid=%d, \"\n+\t\t\t\t\t\"qid=%d\\n\", pid, qid);\n+\t\t}\n+\t\tfor (qid = 0; qid < dev_info.nb_tx_queues; qid++) {\n+\t\t\tcbs = &tx_cbs[pid][qid];\n+\t\t\tret = rte_eth_remove_tx_callback(pid, qid, cbs->cb);\n+\t\t\tif (ret)\n+\t\t\t\tRTE_LOG(INFO, LATENCY_STATS, \"failed to \"\n+\t\t\t\t\t\"remove Tx callback for pid=%d, \"\n+\t\t\t\t\t\"qid=%d\\n\", pid, qid);\n+\t\t}\n+\t}\n+\n+\t/** Cancel the thread */\n+\tret = pthread_cancel(latency_stats_thread);\n+\tif (ret != 0) {\n+\t\tRTE_LOG(ERR, LATENCY_STATS,\n+\t\t\t\"Failed to cancel latency stats update thread:\"\n+\t\t\t\"%s,%s:%d\\n\",\n+\t\t\tstrerror(errno), __func__, __LINE__);\n+\t\treturn -1;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+int\n+rte_latencystats_get_names(struct rte_metric_name *names, uint16_t size)\n+{\n+\tunsigned int i;\n+\n+\tif (names == NULL || size < NUM_LATENCY_STATS)\n+\t\treturn NUM_LATENCY_STATS;\n+\n+\tfor (i = 0; i < NUM_LATENCY_STATS; i++)\n+\t\tsnprintf(names[i].name, sizeof(names[i].name),\n+\t\t\t\t\"%s\", lat_stats_strings[i].name);\n+\n+\treturn NUM_LATENCY_STATS;\n+}\n+\n+int\n+rte_latencystats_get(struct rte_stat_value *values, uint16_t size)\n+{\n+\tif (size < NUM_LATENCY_STATS || values == NULL)\n+\t\treturn NUM_LATENCY_STATS;\n+\n+\tif (rte_eal_process_type() == RTE_PROC_SECONDARY) {\n+\t\tconst struct rte_memzone *mz;\n+\t\tmz = rte_memzone_lookup(MZ_RTE_LATENCY_STATS);\n+\t\tif (mz == NULL) {\n+\t\t\tRTE_LOG(ERR, LATENCY_STATS,\n+\t\t\t\t\"Latency stats memzone not found\\n\");\n+\t\t\treturn -ENOMEM;\n+\t\t}\n+\t\tglob_stats =  mz->addr;\n+\t}\n+\n+\t/* Retrieve latency stats */\n+\trte_latencystats_fill_values(values);\n+\n+\treturn NUM_LATENCY_STATS;\n+}\ndiff --git a/lib/librte_latencystats/rte_latencystats.h b/lib/librte_latencystats/rte_latencystats.h\nnew file mode 100644\nindex 0000000..7b1e72a\n--- /dev/null\n+++ b/lib/librte_latencystats/rte_latencystats.h\n@@ -0,0 +1,141 @@\n+/*-\n+ *   BSD LICENSE\n+ *\n+ *   Copyright(c) 2016 Intel Corporation. All rights reserved.\n+ *   All rights reserved.\n+ *\n+ *   Redistribution and use in source and binary forms, with or without\n+ *   modification, are permitted provided that the following conditions\n+ *   are met:\n+ *\n+ *     * Redistributions of source code must retain the above copyright\n+ *       notice, this list of conditions and the following disclaimer.\n+ *     * Redistributions in binary form must reproduce the above copyright\n+ *       notice, this list of conditions and the following disclaimer in\n+ *       the documentation and/or other materials provided with the\n+ *       distribution.\n+ *     * Neither the name of Intel Corporation nor the names of its\n+ *       contributors may be used to endorse or promote products derived\n+ *       from this software without specific prior written permission.\n+ *\n+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n+ *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n+ */\n+\n+#ifndef _RTE_LATENCYSTATS_H_\n+#define _RTE_LATENCYSTATS_H_\n+\n+/**\n+ * @file\n+ * RTE latency stats\n+ *\n+ * library to provide application and flow based latency stats.\n+ */\n+\n+#include <rte_metrics.h>\n+\n+#ifdef __cplusplus\n+extern \"C\" {\n+#endif\n+\n+/**\n+ * Function type used for identifting flow types of a Rx packet.\n+ *\n+ * The callback function is called on Rx for each packet.\n+ * This function is used for flow based latency calculations.\n+ *\n+ * @param pkt\n+ *   Packet that has to be identified with its flow types.\n+ * @param user_param\n+ *   The arbitrary user parameter passed in by the application when\n+ *   the callback was originally configured.\n+ * @return\n+ *   The flow_mask, representing the multiple flow types of a packet.\n+ */\n+typedef uint16_t (*rte_latency_stats_flow_type_fn)(struct rte_mbuf *pkt,\n+\t\t\t\t\t\t\tvoid *user_param);\n+\n+/**\n+ * @internal\n+ *  Registers Rx/Tx callbacks for each active port, queue.\n+ *\n+ * @param sampIntvl\n+ *  Sampling time period in nano seconds, at which packet\n+ *  should be marked with time stamp.\n+ * @param user_cb\n+ *  User callback to be called to get flow types of a packet.\n+ *  Used for flow based latency calculation.\n+ *  If the value is NULL, global stats will be calculated,\n+ *  else flow based stats will be calculated.\n+ *  @return\n+ *   -1     : On error\n+ *   -ENOMEM: On error\n+ *    0     : On success\n+ */\n+int rte_latencystats_init(uint64_t samp_intvl,\n+\t\t\trte_latency_stats_flow_type_fn user_cb);\n+\n+/**\n+ * @internal\n+ *  Removes registered Rx/Tx callbacks for each active port, queue.\n+ *  @return\n+ *   -1: On error\n+ *    0: On suces\n+ */\n+int rte_latencystats_uninit(void);\n+\n+/**\n+ * Retrieve names of latency statistics\n+ *\n+ * @param names\n+ *  Block of memory to insert names into. Must be at least size in capacity.\n+ *  If set to NULL, function returns required capacity.\n+ * @param size\n+ *  Capacity of latency stats names (number of names).\n+ * @return\n+ *   - positive value lower or equal to size: success. The return value\n+ *     is the number of entries filled in the stats table.\n+ *   - positive value higher than size: error, the given statistics table\n+ *     is too small. The return value corresponds to the size that should\n+ *     be given to succeed. The entries in the table are not valid and\n+ *     shall not be used by the caller.\n+ */\n+int rte_latencystats_get_names(struct rte_metric_name *names,\n+\t\t\t\tuint16_t size);\n+\n+/**\n+ * Retrieve latency statistics.\n+ *\n+ * @param values\n+ *   A pointer to a table of structure of type *rte_stat_value*\n+ *   to be filled with latency statistics ids and values.\n+ *   This parameter can be set to NULL if size is 0.\n+ * @param size\n+ *   The size of the stats table, which should be large enough to store\n+ *   all the latency stats.\n+ * @return\n+ *   - positive value lower or equal to size: success. The return value\n+ *     is the number of entries filled in the stats table.\n+ *   - positive value higher than size: error, the given statistics table\n+ *     is too small. The return value corresponds to the size that should\n+ *     be given to succeed. The entries in the table are not valid and\n+ *     shall not be used by the caller.\n+ *   -ENOMEM: On failure.\n+ */\n+int rte_latencystats_get(struct rte_stat_value *values,\n+\t\t\tuint16_t size);\n+\n+#ifdef __cplusplus\n+}\n+#endif\n+\n+#endif /* _RTE_LATENCYSTATS_H_ */\ndiff --git a/lib/librte_latencystats/rte_latencystats_version.map b/lib/librte_latencystats/rte_latencystats_version.map\nnew file mode 100644\nindex 0000000..82dc5a7\n--- /dev/null\n+++ b/lib/librte_latencystats/rte_latencystats_version.map\n@@ -0,0 +1,10 @@\n+DPDK_16.11 {\n+\tglobal:\n+\n+\trte_latencystats_get;\n+\trte_latencystats_get_names;\n+\trte_latencystats_init;\n+\trte_latencystats_uninit;\n+\n+\tlocal: *;\n+};\ndiff --git a/lib/librte_mbuf/rte_mbuf.h b/lib/librte_mbuf/rte_mbuf.h\nindex 109e666..cc3bf65 100644\n--- a/lib/librte_mbuf/rte_mbuf.h\n+++ b/lib/librte_mbuf/rte_mbuf.h\n@@ -493,6 +493,9 @@ struct rte_mbuf {\n \n \t/** Timesync flags for use with IEEE1588. */\n \tuint16_t timesync;\n+\n+\t/** Timestamp for measuring latency. */\n+\tuint64_t timestamp;\n } __rte_cache_aligned;\n \n /**\ndiff --git a/mk/rte.app.mk b/mk/rte.app.mk\nindex f75f0e2..4e5289a 100644\n--- a/mk/rte.app.mk\n+++ b/mk/rte.app.mk\n@@ -98,6 +98,8 @@ _LDLIBS-$(CONFIG_RTE_LIBRTE_RING)           += -lrte_ring\n _LDLIBS-$(CONFIG_RTE_LIBRTE_EAL)            += -lrte_eal\n _LDLIBS-$(CONFIG_RTE_LIBRTE_CMDLINE)        += -lrte_cmdline\n _LDLIBS-$(CONFIG_RTE_LIBRTE_CFGFILE)        += -lrte_cfgfile\n+_LDLIBS-$(CONFIG_RTE_LIBRTE_LATENCY_STATS)  += -lrte_latencystats\n+\n \n _LDLIBS-$(CONFIG_RTE_LIBRTE_PMD_BOND)       += -lrte_pmd_bond\n _LDLIBS-$(CONFIG_RTE_LIBRTE_PMD_XENVIRT)    += -lrte_pmd_xenvirt -lxenstore\n",
    "prefixes": [
        "dpdk-dev",
        "v4"
    ]
}