]> git.proxmox.com Git - mirror_ovs.git/commitdiff
dpif-netdev-perf: Fix TSC frequency for non-DPDK case.
authorIlya Maximets <i.maximets@samsung.com>
Fri, 5 Jul 2019 12:43:15 +0000 (08:43 -0400)
committerIlya Maximets <i.maximets@samsung.com>
Fri, 6 Sep 2019 08:45:39 +0000 (11:45 +0300)
Unlike 'rte_get_tsc_cycles()' which doesn't need any specific
initialization, 'rte_get_tsc_hz()' could be used only after successfull
call to 'rte_eal_init()'. 'rte_eal_init()' estimates the TSC frequency
for later use by 'rte_get_tsc_hz()'.  Fairly said, we're not allowed
to use 'rte_get_tsc_cycles()' before initializing DPDK too, but it
works this way for now and provides correct results.

This patch provides TSC frequency estimation code that will be used
in two cases:
  * DPDK is not compiled in, i.e. DPDK_NETDEV not defined.
  * DPDK compiled in but not initialized,
    i.e. other_config:dpdk-init=false

This change is mostly useful for AF_XDP netdev support, i.e. allows
to use dpif-netdev/pmd-perf-show command and various PMD perf metrics.

Signed-off-by: Ilya Maximets <i.maximets@samsung.com>
Reviewed-by: David Marchand <david.marchand@redhat.com>
Acked-by: William Tu <u9012063@gmail.com>
lib/dpdk-stub.c
lib/dpdk.c
lib/dpdk.h
lib/dpif-netdev-perf.c
lib/dpif-netdev-perf.h
lib/dpif-netdev.c

index e55be5750da8e5d6c0b85920aece184c2cb6ef3a..c332c217cbfd80aac93e91309eb23e7bfb818261 100644 (file)
@@ -68,6 +68,12 @@ dpdk_per_port_memory(void)
     return false;
 }
 
+bool
+dpdk_available(void)
+{
+    return false;
+}
+
 void
 print_dpdk_version(void)
 {
index f31e1580c5315a80b753e73fa20fc7fafc2e5007..fc58de55ae19d1d3286ce70d4e93b7d9c1b8b3fa 100644 (file)
@@ -518,6 +518,12 @@ dpdk_per_port_memory(void)
     return per_port_memory;
 }
 
+bool
+dpdk_available(void)
+{
+    return dpdk_initialized;
+}
+
 void
 dpdk_set_lcore_id(unsigned cpu)
 {
index 7dab837751e4c1631496a50bcd26b22e15e00c1c..736a64279e3119bb285be522d99fd09fab66e385 100644 (file)
@@ -41,6 +41,7 @@ const char *dpdk_get_vhost_sock_dir(void);
 bool dpdk_vhost_iommu_enabled(void);
 bool dpdk_vhost_postcopy_enabled(void);
 bool dpdk_per_port_memory(void);
+bool dpdk_available(void);
 void print_dpdk_version(void);
 void dpdk_status(const struct ovsrec_open_vswitch *);
 #endif /* dpdk.h */
index e7ed49e7e2524c00323e3ec9ba276e8ad3a6bb3b..baf90b0f47624760a7aa7cf0cd5f9fa9f3937929 100644 (file)
 #include <config.h>
 #include <stdint.h>
 
+#include "dpdk.h"
 #include "dpif-netdev-perf.h"
 #include "openvswitch/dynamic-string.h"
 #include "openvswitch/vlog.h"
+#include "ovs-numa.h"
 #include "ovs-thread.h"
 #include "timeval.h"
 
@@ -43,21 +45,59 @@ uint64_t iter_cycle_threshold;
 
 static struct vlog_rate_limit latency_rl = VLOG_RATE_LIMIT_INIT(600, 600);
 
-#ifdef DPDK_NETDEV
-static uint64_t
-get_tsc_hz(void)
-{
-    return rte_get_tsc_hz();
-}
-#else
-/* This function is only invoked from PMD threads which depend on DPDK.
- * A dummy function is sufficient when building without DPDK_NETDEV. */
-static uint64_t
-get_tsc_hz(void)
+static uint64_t tsc_hz = 1;
+
+void
+pmd_perf_estimate_tsc_frequency(void)
 {
-    return 1;
-}
+#ifdef DPDK_NETDEV
+    if (dpdk_available()) {
+        tsc_hz = rte_get_tsc_hz();
+    }
+    if (tsc_hz > 1) {
+        VLOG_INFO("DPDK provided TSC frequency: %"PRIu64" KHz", tsc_hz / 1000);
+        return;
+    }
 #endif
+    struct ovs_numa_dump *affinity;
+    struct pmd_perf_stats s;
+    uint64_t start, stop;
+
+    /* DPDK is not available or returned unreliable value.
+     * Trying to estimate. */
+    affinity = ovs_numa_thread_getaffinity_dump();
+    if (affinity) {
+        const struct ovs_numa_info_core *core;
+
+        FOR_EACH_CORE_ON_DUMP (core, affinity) {
+            /* Setting affinity to a single core from the affinity mask to
+             * avoid re-scheduling to another core while sleeping. */
+            ovs_numa_thread_setaffinity_core(core->core_id);
+            break;
+        }
+    }
+
+    start = cycles_counter_update(&s);
+    /* Using xnanosleep as it's interrupt resistant.
+     * Sleeping only 100 ms to avoid holding the main thread for too long. */
+    xnanosleep(1E8);
+    stop = cycles_counter_update(&s);
+
+    if (affinity) {
+        /* Restoring previous affinity. */
+        ovs_numa_thread_setaffinity_dump(affinity);
+        ovs_numa_dump_destroy(affinity);
+    }
+
+    if (stop <= start) {
+        VLOG_WARN("TSC source is unreliable.");
+        tsc_hz = 1;
+    } else {
+        tsc_hz = (stop - start) * 10;
+    }
+
+    VLOG_INFO("Estimated TSC frequency: %"PRIu64" KHz", tsc_hz / 1000);
+}
 
 /* Histogram functions. */
 
@@ -170,7 +210,6 @@ pmd_perf_format_overall_stats(struct ds *str, struct pmd_perf_stats *s,
                               double duration)
 {
     uint64_t stats[PMD_N_STATS];
-    uint64_t tsc_hz = get_tsc_hz();
     double us_per_cycle = 1000000.0 / tsc_hz;
 
     if (duration == 0) {
@@ -555,7 +594,7 @@ pmd_perf_end_iteration(struct pmd_perf_stats *s, int rx_packets,
             cum_ms->timestamp = now;
         }
         /* Do the next check after 4 us (10K cycles at 2.5 GHz TSC clock). */
-        s->next_check_tsc = cycles_counter_update(s) + get_tsc_hz() / 250000;
+        s->next_check_tsc = cycles_counter_update(s) + tsc_hz / 250000;
     }
 }
 
@@ -585,7 +624,7 @@ pmd_perf_set_log_susp_iteration(struct pmd_perf_stats *s,
                 " duration=%"PRIu64" us\n",
                 s->log_reason,
                 susp->timestamp,
-                (1000000L * susp->cycles) / get_tsc_hz());
+                (1000000L * susp->cycles) / tsc_hz);
 
         new_end_it = history_add(s->iterations.idx, log_it_after + 1);
         new_range = history_sub(new_end_it, s->log_begin_it);
@@ -615,7 +654,7 @@ pmd_perf_log_susp_iteration_neighborhood(struct pmd_perf_stats *s)
                  " duration=%"PRIu64" us\n",
                  s->log_reason,
                  susp->timestamp,
-                 (1000000L * susp->cycles) / get_tsc_hz());
+                 (1000000L * susp->cycles) / tsc_hz);
 
     pmd_perf_format_iteration_history(&log, s, range);
     VLOG_WARN_RL(&latency_rl,
@@ -729,7 +768,7 @@ pmd_perf_log_set_cmd(struct unixctl_conn *conn,
     log_it_after = it_after;
     log_q_thr = q_thr;
     log_us_thr = us_thr;
-    iter_cycle_threshold = (log_us_thr * get_tsc_hz()) / 1000000L;
+    iter_cycle_threshold = (log_us_thr * tsc_hz) / 1000000L;
 
     unixctl_command_reply(conn, "");
 }
index 244813ffe1685f8c519e4291ca6866f5cfcf1962..ce369375b88222ddb50879cd2867509944ddf603 100644 (file)
@@ -233,6 +233,8 @@ cycles_counter_get(struct pmd_perf_stats *s)
     return s->last_tsc;
 }
 
+void pmd_perf_estimate_tsc_frequency(void);
+
 /* A nestable timer for measuring execution time in TSC cycles.
  *
  * Usage:
index 75d85b2fda9a1b9ec1f23c97e9a6eaaa5eaf0f95..17323696f5cbe6c186da7d30280cd4aa63319768 100644 (file)
@@ -1517,9 +1517,18 @@ create_dp_netdev(const char *name, const struct dpif_class *class,
                  struct dp_netdev **dpp)
     OVS_REQUIRES(dp_netdev_mutex)
 {
+    static struct ovsthread_once tsc_freq_check = OVSTHREAD_ONCE_INITIALIZER;
     struct dp_netdev *dp;
     int error;
 
+    /* Avoid estimating TSC frequency for dummy datapath to not slow down
+     * unit tests. */
+    if (!dpif_netdev_class_is_dummy(class)
+        && ovsthread_once_start(&tsc_freq_check)) {
+        pmd_perf_estimate_tsc_frequency();
+        ovsthread_once_done(&tsc_freq_check);
+    }
+
     dp = xzalloc(sizeof *dp);
     shash_add(&dp_netdevs, name, dp);