X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Flibrte_latencystats%2Frte_latencystats.c;h=ab8db7a1398245fd995ae4df01556b9eab17bcfb;hb=5b38d8cd4663;hp=ce029a12c2dea2e6584bcae933ea39bb169fa113;hpb=a4dad8a0c1f4c93f53e2bc3982e957ae5e00bcb0;p=dpdk.git diff --git a/lib/librte_latencystats/rte_latencystats.c b/lib/librte_latencystats/rte_latencystats.c index ce029a12c2..ab8db7a139 100644 --- a/lib/librte_latencystats/rte_latencystats.c +++ b/lib/librte_latencystats/rte_latencystats.c @@ -1,33 +1,5 @@ -/*- - * BSD LICENSE - * - * Copyright(c) 2017 Intel Corporation. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Intel Corporation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2018 Intel Corporation */ #include @@ -35,7 +7,9 @@ #include #include +#include #include +#include #include #include #include @@ -58,6 +32,16 @@ latencystat_cycles_per_ns(void) /* Macros for printing using RTE_LOG */ #define RTE_LOGTYPE_LATENCY_STATS RTE_LOGTYPE_USER1 +static uint64_t timestamp_dynflag; +static int timestamp_dynfield_offset = -1; + +static inline rte_mbuf_timestamp_t * +timestamp_dynfield(struct rte_mbuf *mbuf) +{ + return RTE_MBUF_DYNFIELD(mbuf, + timestamp_dynfield_offset, rte_mbuf_timestamp_t *); +} + static const char *MZ_RTE_LATENCY_STATS = "rte_latencystats"; static int latency_stats_index; static uint64_t samp_intvl; @@ -69,12 +53,13 @@ struct rte_latency_stats { float avg_latency; /**< Average latency in nano seconds */ float max_latency; /**< Maximum latency in nano seconds */ float jitter; /** Latency variation */ + rte_spinlock_t lock; /** Latency calculation lock */ }; static struct rte_latency_stats *glob_stats; struct rxtx_cbs { - struct rte_eth_rxtx_callback *cb; + const struct rte_eth_rxtx_callback *cb; }; static struct rxtx_cbs rx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT]; @@ -135,7 +120,7 @@ rte_latencystats_fill_values(struct rte_metric_value *values) } static uint16_t -add_time_stamps(uint8_t pid __rte_unused, +add_time_stamps(uint16_t pid __rte_unused, uint16_t qid __rte_unused, struct rte_mbuf **pkts, uint16_t nb_pkts, @@ -153,8 +138,11 @@ add_time_stamps(uint8_t pid __rte_unused, for (i = 0; i < nb_pkts; i++) { diff_tsc = now - prev_tsc; timer_tsc += diff_tsc; - if (timer_tsc >= samp_intvl) { - pkts[i]->timestamp = now; + + if ((pkts[i]->ol_flags & timestamp_dynflag) == 0 + && (timer_tsc >= samp_intvl)) { + *timestamp_dynfield(pkts[i]) = now; + pkts[i]->ol_flags |= timestamp_dynflag; timer_tsc = 0; } prev_tsc = now; @@ -165,7 +153,7 @@ add_time_stamps(uint8_t pid __rte_unused, } static uint16_t -calc_latency(uint8_t pid __rte_unused, +calc_latency(uint16_t pid __rte_unused, uint16_t qid __rte_unused, struct rte_mbuf **pkts, uint16_t nb_pkts, @@ -184,10 +172,11 @@ calc_latency(uint8_t pid __rte_unused, now = rte_rdtsc(); for (i = 0; i < nb_pkts; i++) { - if (pkts[i]->timestamp) - latency[cnt++] = now - pkts[i]->timestamp; + if (pkts[i]->ol_flags & timestamp_dynflag) + latency[cnt++] = now - *timestamp_dynfield(pkts[i]); } + rte_spinlock_lock(&glob_stats->lock); for (i = 0; i < cnt; i++) { /* * The jitter is calculated as statistical mean of interpacket @@ -217,6 +206,7 @@ calc_latency(uint8_t pid __rte_unused, alpha * (latency[i] - glob_stats->avg_latency); prev_latency = latency[i]; } + rte_spinlock_unlock(&glob_stats->lock); return nb_pkts; } @@ -226,13 +216,13 @@ rte_latencystats_init(uint64_t app_samp_intvl, rte_latency_stats_flow_type_fn user_cb) { unsigned int i; - uint8_t pid; + uint16_t pid; uint16_t qid; struct rxtx_cbs *cbs = NULL; - const uint8_t nb_ports = rte_eth_dev_count(); const char *ptr_strings[NUM_LATENCY_STATS] = {0}; const struct rte_memzone *mz = NULL; const unsigned int flags = 0; + int ret; if (rte_memzone_lookup(MZ_RTE_LATENCY_STATS)) return -EEXIST; @@ -247,6 +237,7 @@ rte_latencystats_init(uint64_t app_samp_intvl, } glob_stats = mz->addr; + rte_spinlock_init(&glob_stats->lock); samp_intvl = app_samp_intvl * latencystat_cycles_per_ns(); /** Register latency stats with stats library */ @@ -261,10 +252,28 @@ rte_latencystats_init(uint64_t app_samp_intvl, return -1; } + /* Register mbuf field and flag for Rx timestamp */ + ret = rte_mbuf_dyn_rx_timestamp_register(×tamp_dynfield_offset, + ×tamp_dynflag); + if (ret != 0) { + RTE_LOG(ERR, LATENCY_STATS, + "Cannot register mbuf field/flag for timestamp\n"); + return -rte_errno; + } + /** Register Rx/Tx callbacks */ - for (pid = 0; pid < nb_ports; pid++) { + RTE_ETH_FOREACH_DEV(pid) { struct rte_eth_dev_info dev_info; - rte_eth_dev_info_get(pid, &dev_info); + + ret = rte_eth_dev_info_get(pid, &dev_info); + if (ret != 0) { + RTE_LOG(INFO, LATENCY_STATS, + "Error during getting device (port %u) info: %s\n", + pid, strerror(-ret)); + + continue; + } + for (qid = 0; qid < dev_info.nb_rx_queues; qid++) { cbs = &rx_cbs[pid][qid]; cbs->cb = rte_eth_add_first_rx_callback(pid, qid, @@ -290,16 +299,25 @@ rte_latencystats_init(uint64_t app_samp_intvl, int rte_latencystats_uninit(void) { - uint8_t pid; + uint16_t pid; uint16_t qid; int ret = 0; struct rxtx_cbs *cbs = NULL; - const uint8_t nb_ports = rte_eth_dev_count(); + const struct rte_memzone *mz = NULL; /** De register Rx/Tx callbacks */ - for (pid = 0; pid < nb_ports; pid++) { + RTE_ETH_FOREACH_DEV(pid) { struct rte_eth_dev_info dev_info; - rte_eth_dev_info_get(pid, &dev_info); + + ret = rte_eth_dev_info_get(pid, &dev_info); + if (ret != 0) { + RTE_LOG(INFO, LATENCY_STATS, + "Error during getting device (port %u) info: %s\n", + pid, strerror(-ret)); + + continue; + } + for (qid = 0; qid < dev_info.nb_rx_queues; qid++) { cbs = &rx_cbs[pid][qid]; ret = rte_eth_remove_rx_callback(pid, qid, cbs->cb); @@ -318,6 +336,11 @@ rte_latencystats_uninit(void) } } + /* free up the memzone */ + mz = rte_memzone_lookup(MZ_RTE_LATENCY_STATS); + if (mz) + rte_memzone_free(mz); + return 0; } @@ -330,8 +353,8 @@ rte_latencystats_get_names(struct rte_metric_name *names, uint16_t size) return NUM_LATENCY_STATS; for (i = 0; i < NUM_LATENCY_STATS; i++) - snprintf(names[i].name, sizeof(names[i].name), - "%s", lat_stats_strings[i].name); + strlcpy(names[i].name, lat_stats_strings[i].name, + sizeof(names[i].name)); return NUM_LATENCY_STATS; }