diff mbox

[dpdk-dev,v4,07/17] eal: add rte_gettid() to acquire unique system tid

Message ID 1422842559-13617-8-git-send-email-cunming.liang@intel.com (mailing list archive)
State Superseded, archived
Headers show

Commit Message

Cunming Liang Feb. 2, 2015, 2:02 a.m. UTC
The rte_gettid() wraps the linux and freebsd syscall gettid().
It provides a persistent unique thread id for the calling thread.
It will save the unique id in TLS on the first time.

Signed-off-by: Cunming Liang <cunming.liang@intel.com>
---
 lib/librte_eal/bsdapp/eal/eal_thread.c   |  9 +++++++++
 lib/librte_eal/common/include/rte_eal.h  | 27 +++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_thread.c |  7 +++++++
 3 files changed, 43 insertions(+)

Comments

Olivier Matz Feb. 8, 2015, 8 p.m. UTC | #1
Hi,

On 02/02/2015 03:02 AM, Cunming Liang wrote:
> The rte_gettid() wraps the linux and freebsd syscall gettid().
> It provides a persistent unique thread id for the calling thread.
> It will save the unique id in TLS on the first time.
> 
> [...]
>
> +/**
> + * A wrap API for syscall gettid.
> + *
> + * @return
> + *   On success, returns the thread ID of calling process.
> + *   It always successful.
> + */
> +int rte_sys_gettid(void);
> +
> +/**
> + * Get system unique thread id.
> + *
> + * @return
> + *   On success, returns the thread ID of calling process.
> + *   It always successful.
> + */
> +static inline int rte_gettid(void)
> +{
> +	static RTE_DEFINE_PER_LCORE(int, _thread_id) = -1;
> +	if (RTE_PER_LCORE(_thread_id) == -1)
> +		RTE_PER_LCORE(_thread_id) = rte_sys_gettid();
> +	return RTE_PER_LCORE(_thread_id);
> +}

Instead of doing the test each time rte_gettid() is called, why not
having 2 functions:
  rte_init_tid() -> assign the per_lcore variable
  rte_gettid() -> return the per_lcore variable



Regards,
Olivier
Cunming Liang Feb. 10, 2015, 6:57 a.m. UTC | #2
> -----Original Message-----
> From: Olivier MATZ [mailto:olivier.matz@6wind.com]
> Sent: Monday, February 09, 2015 4:01 AM
> To: Liang, Cunming; dev@dpdk.org
> Subject: Re: [dpdk-dev] [PATCH v4 07/17] eal: add rte_gettid() to acquire unique
> system tid
> 
> Hi,
> 
> On 02/02/2015 03:02 AM, Cunming Liang wrote:
> > The rte_gettid() wraps the linux and freebsd syscall gettid().
> > It provides a persistent unique thread id for the calling thread.
> > It will save the unique id in TLS on the first time.
> >
> > [...]
> >
> > +/**
> > + * A wrap API for syscall gettid.
> > + *
> > + * @return
> > + *   On success, returns the thread ID of calling process.
> > + *   It always successful.
> > + */
> > +int rte_sys_gettid(void);
> > +
> > +/**
> > + * Get system unique thread id.
> > + *
> > + * @return
> > + *   On success, returns the thread ID of calling process.
> > + *   It always successful.
> > + */
> > +static inline int rte_gettid(void)
> > +{
> > +	static RTE_DEFINE_PER_LCORE(int, _thread_id) = -1;
> > +	if (RTE_PER_LCORE(_thread_id) == -1)
> > +		RTE_PER_LCORE(_thread_id) = rte_sys_gettid();
> > +	return RTE_PER_LCORE(_thread_id);
> > +}
> 
> Instead of doing the test each time rte_gettid() is called, why not
> having 2 functions:
>   rte_init_tid() -> assign the per_lcore variable
>   rte_gettid() -> return the per_lcore variable

[LCM] The rte_gettid() mainly used in recursive spinlock.
For non-EAL thread, we don't expect new user thread has to explicit call something.
The purpose to call it in EAL thread init, is to lower down the overhead of the first calling for EAL thread.

> 
> 
> 
> Regards,
> Olivier
Olivier Matz Feb. 10, 2015, 5:16 p.m. UTC | #3
Hi,

On 02/10/2015 07:57 AM, Liang, Cunming wrote:
>>> +/**
>>> + * Get system unique thread id.
>>> + *
>>> + * @return
>>> + *   On success, returns the thread ID of calling process.
>>> + *   It always successful.
>>> + */
>>> +static inline int rte_gettid(void)
>>> +{
>>> +	static RTE_DEFINE_PER_LCORE(int, _thread_id) = -1;
>>> +	if (RTE_PER_LCORE(_thread_id) == -1)
>>> +		RTE_PER_LCORE(_thread_id) = rte_sys_gettid();
>>> +	return RTE_PER_LCORE(_thread_id);
>>> +}
>>
>> Instead of doing the test each time rte_gettid() is called, why not
>> having 2 functions:
>>    rte_init_tid() -> assign the per_lcore variable
>>    rte_gettid() -> return the per_lcore variable
>
> [LCM] The rte_gettid() mainly used in recursive spinlock.
> For non-EAL thread, we don't expect new user thread has to explicit call something.
> The purpose to call it in EAL thread init, is to lower down the overhead of the first calling for EAL thread.

Got it. So that's fine like you proposed.

Olivier
diff mbox

Patch

diff --git a/lib/librte_eal/bsdapp/eal/eal_thread.c b/lib/librte_eal/bsdapp/eal/eal_thread.c
index 10220c7..d0c077b 100644
--- a/lib/librte_eal/bsdapp/eal/eal_thread.c
+++ b/lib/librte_eal/bsdapp/eal/eal_thread.c
@@ -39,6 +39,7 @@ 
 #include <sched.h>
 #include <pthread_np.h>
 #include <sys/queue.h>
+#include <sys/thr.h>
 
 #include <rte_debug.h>
 #include <rte_atomic.h>
@@ -233,3 +234,11 @@  eal_thread_loop(__attribute__((unused)) void *arg)
 	/* pthread_exit(NULL); */
 	/* return NULL; */
 }
+
+/* require calling thread tid by gettid() */
+int rte_sys_gettid(void)
+{
+	long lwpid;
+	thr_self(&lwpid);
+	return (int)lwpid;
+}
diff --git a/lib/librte_eal/common/include/rte_eal.h b/lib/librte_eal/common/include/rte_eal.h
index f4ecd2e..8ccdd65 100644
--- a/lib/librte_eal/common/include/rte_eal.h
+++ b/lib/librte_eal/common/include/rte_eal.h
@@ -41,6 +41,9 @@ 
  */
 
 #include <stdint.h>
+#include <sched.h>
+
+#include <rte_per_lcore.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -262,6 +265,30 @@  rte_set_application_usage_hook( rte_usage_hook_t usage_func );
  */
 int rte_eal_has_hugepages(void);
 
+/**
+ * A wrap API for syscall gettid.
+ *
+ * @return
+ *   On success, returns the thread ID of calling process.
+ *   It always successful.
+ */
+int rte_sys_gettid(void);
+
+/**
+ * Get system unique thread id.
+ *
+ * @return
+ *   On success, returns the thread ID of calling process.
+ *   It always successful.
+ */
+static inline int rte_gettid(void)
+{
+	static RTE_DEFINE_PER_LCORE(int, _thread_id) = -1;
+	if (RTE_PER_LCORE(_thread_id) == -1)
+		RTE_PER_LCORE(_thread_id) = rte_sys_gettid();
+	return RTE_PER_LCORE(_thread_id);
+}
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/librte_eal/linuxapp/eal/eal_thread.c b/lib/librte_eal/linuxapp/eal/eal_thread.c
index 748a83a..ed20c93 100644
--- a/lib/librte_eal/linuxapp/eal/eal_thread.c
+++ b/lib/librte_eal/linuxapp/eal/eal_thread.c
@@ -39,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>
@@ -233,3 +234,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);
+}