tailq: remove unneeded inclusions
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_thread.c
index 6016142..18bd8e0 100644 (file)
@@ -1,35 +1,34 @@
 /*-
  *   BSD LICENSE
- * 
- *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
+ *
+ *   Copyright(c) 2010-2014 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 <errno.h>
@@ -40,6 +39,7 @@
 #include <pthread.h>
 #include <sched.h>
 #include <sys/queue.h>
+#include <sys/syscall.h>
 
 #include <rte_debug.h>
 #include <rte_atomic.h>
@@ -48,7 +48,6 @@
 #include <rte_memory.h>
 #include <rte_memzone.h>
 #include <rte_per_lcore.h>
-#include <rte_tailq.h>
 #include <rte_eal.h>
 #include <rte_per_lcore.h>
 #include <rte_lcore.h>
@@ -56,7 +55,9 @@
 #include "eal_private.h"
 #include "eal_thread.h"
 
-RTE_DEFINE_PER_LCORE(unsigned, _lcore_id);
+RTE_DEFINE_PER_LCORE(unsigned, _lcore_id) = LCORE_ID_ANY;
+RTE_DEFINE_PER_LCORE(unsigned, _socket_id) = (unsigned)SOCKET_ID_ANY;
+RTE_DEFINE_PER_LCORE(rte_cpuset_t, _cpuset);
 
 /*
  * Send a message to a slave lcore identified by slave_id to call a
@@ -85,7 +86,6 @@ rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned slave_id)
                rte_panic("cannot write on configuration pipe\n");
 
        /* wait ack */
-       n = 0;
        do {
                n = read(s2m, &c, 1);
        } while (n < 0 && errno == EINTR);
@@ -96,62 +96,17 @@ rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned slave_id)
        return 0;
 }
 
-/* set affinity for current thread */
+/* set affinity for current EAL thread */
 static int
 eal_thread_set_affinity(void)
 {
-       int s;
-       pthread_t thread;
-
-/*
- * According to the section VERSIONS of the CPU_ALLOC man page:
- *
- * The CPU_ZERO(), CPU_SET(), CPU_CLR(), and CPU_ISSET() macros were added
- * in glibc 2.3.3.
- *
- * CPU_COUNT() first appeared in glibc 2.6.
- *
- * CPU_AND(),     CPU_OR(),     CPU_XOR(),    CPU_EQUAL(),    CPU_ALLOC(),
- * CPU_ALLOC_SIZE(), CPU_FREE(), CPU_ZERO_S(),  CPU_SET_S(),  CPU_CLR_S(),
- * CPU_ISSET_S(),  CPU_AND_S(), CPU_OR_S(), CPU_XOR_S(), and CPU_EQUAL_S()
- * first appeared in glibc 2.7.
- */
-#if defined(CPU_ALLOC)
-       size_t size;
-       cpu_set_t *cpusetp;
-
-       cpusetp = CPU_ALLOC(RTE_MAX_LCORE);
-       if (cpusetp == NULL) {
-               RTE_LOG(ERR, EAL, "CPU_ALLOC failed\n");
-               return -1;
-       }
+       unsigned lcore_id = rte_lcore_id();
 
-       size = CPU_ALLOC_SIZE(RTE_MAX_LCORE);
-       CPU_ZERO_S(size, cpusetp);
-       CPU_SET_S(rte_lcore_id(), size, cpusetp);
+       /* acquire system unique id  */
+       rte_gettid();
 
-       thread = pthread_self();
-       s = pthread_setaffinity_np(thread, size, cpusetp);
-       if (s != 0) {
-               RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
-               CPU_FREE(cpusetp);
-               return -1;
-       }
-
-       CPU_FREE(cpusetp);
-#else /* CPU_ALLOC */
-       cpu_set_t cpuset;
-       CPU_ZERO( &cpuset );
-       CPU_SET( rte_lcore_id(), &cpuset );
-
-       thread = pthread_self();
-       s = pthread_setaffinity_np(thread, sizeof( cpuset ), &cpuset);
-       if (s != 0) {
-               RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
-               return -1;
-       }
-#endif
-       return 0;
+       /* update EAL thread core affinity */
+       return rte_thread_set_affinity(&lcore_config[lcore_id].cpuset);
 }
 
 void eal_thread_init_master(unsigned lcore_id)
@@ -173,6 +128,7 @@ eal_thread_loop(__attribute__((unused)) void *arg)
        unsigned lcore_id;
        pthread_t thread_id;
        int m2s, s2m;
+       char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 
        thread_id = pthread_self();
 
@@ -184,9 +140,6 @@ eal_thread_loop(__attribute__((unused)) void *arg)
        if (lcore_id == RTE_MAX_LCORE)
                rte_panic("cannot retrieve lcore id\n");
 
-       RTE_LOG(DEBUG, EAL, "Core %u is ready (tid=%x)\n",
-               lcore_id, (int)thread_id);
-
        m2s = lcore_config[lcore_id].pipe_master2slave[0];
        s2m = lcore_config[lcore_id].pipe_slave2master[1];
 
@@ -197,12 +150,16 @@ eal_thread_loop(__attribute__((unused)) void *arg)
        if (eal_thread_set_affinity() < 0)
                rte_panic("cannot set affinity\n");
 
+       ret = eal_thread_dump_affinity(cpuset, RTE_CPU_AFFINITY_STR_LEN);
+
+       RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%x;cpuset=[%s%s])\n",
+               lcore_id, (int)thread_id, cpuset, ret == 0 ? "" : "...");
+
        /* read on our pipe to get commands */
        while (1) {
                void *fct_arg;
 
                /* wait command */
-               n = 0;
                do {
                        n = read(m2s, &c, 1);
                } while (n < 0 && errno == EINTR);
@@ -234,3 +191,9 @@ eal_thread_loop(__attribute__((unused)) void *arg)
        /* pthread_exit(NULL); */
        /* return NULL; */
 }
+
+/* require calling thread tid by gettid() */
+int rte_sys_gettid(void)
+{
+       return (int)syscall(SYS_gettid);
+}