doc: whitespace changes in licenses
[dpdk.git] / app / test / test_timer.c
index 8060299..87c072e 100644 (file)
@@ -4,32 +4,31 @@
  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
  *   All rights reserved.
  * 
- *   Redistribution and use in source and binary forms, with or without 
- *   modification, are permitted provided that the following conditions 
+ *   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 
+ *     * 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 
+ *     * 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 
+ *     * 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 
+ *   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.
- * 
  */
 
 /*
 #include <stdint.h>
 #include <inttypes.h>
 #include <sys/queue.h>
+#include <math.h>
 
 #include <cmdline_parse.h>
 
 
 #include "test.h"
 
-#define TEST_DURATION_S 30 /* in seconds */
+#define TEST_DURATION_S 20 /* in seconds */
 #define NB_TIMER 4
 
 #define RTE_LOGTYPE_TESTTIMER RTE_LOGTYPE_USER3
@@ -140,7 +140,7 @@ static struct mytimerinfo mytiminfo[NB_TIMER];
 static void timer_basic_cb(struct rte_timer *tim, void *arg);
 
 static void
-mytimer_reset(struct mytimerinfo *timinfo, unsigned ticks,
+mytimer_reset(struct mytimerinfo *timinfo, uint64_t ticks,
              enum rte_timer_type type, unsigned tim_lcore,
              rte_timer_cb_t fct)
 {
@@ -155,7 +155,7 @@ timer_stress_cb(__attribute__((unused)) struct rte_timer *tim,
 {
        long r;
        unsigned lcore_id = rte_lcore_id();
-       uint64_t hz = rte_get_hpet_hz();
+       uint64_t hz = rte_get_timer_hz();
 
        if (rte_timer_pending(tim))
                return;
@@ -178,7 +178,7 @@ timer_stress_cb(__attribute__((unused)) struct rte_timer *tim,
 static int
 timer_stress_main_loop(__attribute__((unused)) void *arg)
 {
-       uint64_t hz = rte_get_hpet_hz();
+       uint64_t hz = rte_get_timer_hz();
        unsigned lcore_id = rte_lcore_id();
        uint64_t cur_time;
        int64_t diff = 0;
@@ -204,7 +204,7 @@ timer_stress_main_loop(__attribute__((unused)) void *arg)
                else if ((r & 0xff) == 1) {
                        rte_timer_stop_sync(&mytiminfo[0].tim);
                }
-               cur_time = rte_get_hpet_cycles();
+               cur_time = rte_get_timer_cycles();
                diff = end_time - cur_time;
        }
 
@@ -219,9 +219,9 @@ static void
 timer_basic_cb(struct rte_timer *tim, void *arg)
 {
        struct mytimerinfo *timinfo = arg;
-       uint64_t hz = rte_get_hpet_hz();
+       uint64_t hz = rte_get_timer_hz();
        unsigned lcore_id = rte_lcore_id();
-       uint64_t cur_time = rte_get_hpet_cycles();
+       uint64_t cur_time = rte_get_timer_cycles();
 
        if (rte_timer_pending(tim))
                return;
@@ -274,7 +274,7 @@ timer_basic_cb(struct rte_timer *tim, void *arg)
 static int
 timer_basic_main_loop(__attribute__((unused)) void *arg)
 {
-       uint64_t hz = rte_get_hpet_hz();
+       uint64_t hz = rte_get_timer_hz();
        unsigned lcore_id = rte_lcore_id();
        uint64_t cur_time;
        int64_t diff = 0;
@@ -301,7 +301,7 @@ timer_basic_main_loop(__attribute__((unused)) void *arg)
                 * (3 us = 6000 cycles at 2 Ghz) */
                rte_delay_us(3);
 
-               cur_time = rte_get_hpet_cycles();
+               cur_time = rte_get_timer_cycles();
                diff = end_time - cur_time;
        }
        RTE_LOG(INFO, TESTTIMER, "core %u finished\n", lcore_id);
@@ -309,6 +309,43 @@ timer_basic_main_loop(__attribute__((unused)) void *arg)
        return 0;
 }
 
+static int
+timer_sanity_check(void)
+{
+#ifdef RTE_LIBEAL_USE_HPET
+       if (eal_timer_source != EAL_TIMER_HPET) {
+               printf("Not using HPET, can't sanity check timer sources\n");
+               return 0;
+       }
+
+       const uint64_t t_hz = rte_get_tsc_hz();
+       const uint64_t h_hz = rte_get_hpet_hz();
+       printf("Hertz values: TSC = %"PRIu64", HPET = %"PRIu64"\n", t_hz, h_hz);
+
+       const uint64_t tsc_start = rte_get_tsc_cycles();
+       const uint64_t hpet_start = rte_get_hpet_cycles();
+       rte_delay_ms(100); /* delay 1/10 second */
+       const uint64_t tsc_end = rte_get_tsc_cycles();
+       const uint64_t hpet_end = rte_get_hpet_cycles();
+       printf("Measured cycles: TSC = %"PRIu64", HPET = %"PRIu64"\n",
+                       tsc_end-tsc_start, hpet_end-hpet_start);
+
+       const double tsc_time = (double)(tsc_end - tsc_start)/t_hz;
+       const double hpet_time = (double)(hpet_end - hpet_start)/h_hz;
+       /* get the percentage that the times differ by */
+       const double time_diff = fabs(tsc_time - hpet_time)*100/tsc_time;
+       printf("Measured time: TSC = %.4f, HPET = %.4f\n", tsc_time, hpet_time);
+
+       printf("Elapsed time measured by TSC and HPET differ by %f%%\n",
+                       time_diff);
+       if (time_diff > 0.1) {
+               printf("Error times differ by >0.1%%");
+               return -1;
+       }
+#endif
+       return 0;
+}
+
 int
 test_timer(void)
 {
@@ -316,6 +353,12 @@ test_timer(void)
        uint64_t cur_time;
        uint64_t hz;
 
+       /* sanity check our timer sources and timer config values */
+       if (timer_sanity_check() < 0) {
+               printf("Timer sanity checks failed\n");
+               return -1;
+       }
+
        if (rte_lcore_count() < 2) {
                printf("not enough lcores for this test\n");
                return -1;
@@ -329,8 +372,8 @@ test_timer(void)
        }
 
        /* calculate the "end of test" time */
-       cur_time = rte_get_hpet_cycles();
-       hz = rte_get_hpet_hz();
+       cur_time = rte_get_timer_cycles();
+       hz = rte_get_timer_hz();
        end_time = cur_time + (hz * TEST_DURATION_S);
 
        /* start other cores */
@@ -342,8 +385,8 @@ test_timer(void)
        rte_timer_stop_sync(&mytiminfo[0].tim);
 
        /* calculate the "end of test" time */
-       cur_time = rte_get_hpet_cycles();
-       hz = rte_get_hpet_hz();
+       cur_time = rte_get_timer_cycles();
+       hz = rte_get_timer_hz();
        end_time = cur_time + (hz * TEST_DURATION_S);
 
        /* start other cores */