vpp_lite: add cpu pinning support (VPP-467)
[vpp.git] / vlib / vlib / threads.h
index 5773ed0..e65794c 100644 (file)
 #define included_vlib_threads_h
 
 #include <vlib/main.h>
+#include <linux/sched.h>
 
 vlib_main_t **vlib_mains;
 
-void
-vlib_set_thread_name (char *name);
+void vlib_set_thread_name (char *name);
 
 /* arg is actually a vlib__thread_t * */
-typedef void (vlib_thread_function_t) (void * arg);
+typedef void (vlib_thread_function_t) (void *arg);
 
-typedef struct vlib_thread_registration_ {
+typedef struct vlib_thread_registration_
+{
   /* constructor generated list of thread registrations */
-  struct vlib_thread_registration_ * next;
+  struct vlib_thread_registration_ *next;
 
   /* config parameters */
-  char * name;
-  char * short_name;
-  vlib_thread_function_t * function;
+  char *name;
+  char *short_name;
+  vlib_thread_function_t *function;
   uword mheap_size;
   int fixed_count;
   u32 count;
   int no_data_structure_clone;
+  u32 frame_queue_nelts;
+
   /* All threads of this type run on pthreads */
   int use_pthreads;
   u32 first_index;
-  uword * coremask;
+  uword *coremask;
 } vlib_thread_registration_t;
 
 /*
@@ -48,25 +51,29 @@ typedef struct vlib_thread_registration_ {
  * Make VLIB_MAX_CPUS a power-of-two, please...
  */
 
+#ifndef VLIB_MAX_CPUS
 #define VLIB_MAX_CPUS 256
+#endif
 
 #if VLIB_MAX_CPUS > CLIB_MAX_MHEAPS
 #error Please increase number of per-cpu mheaps
 #endif
 
-#define VLIB_CPU_MASK (VLIB_MAX_CPUS - 1) /* 0x3f, max */
+#define VLIB_CPU_MASK (VLIB_MAX_CPUS - 1)      /* 0x3f, max */
 #define VLIB_OFFSET_MASK (~VLIB_CPU_MASK)
 
 #define VLIB_LOG2_THREAD_STACK_SIZE (20)
 #define VLIB_THREAD_STACK_SIZE (1<<VLIB_LOG2_THREAD_STACK_SIZE)
 
-typedef enum {
-    VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME,
+typedef enum
+{
+  VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME,
 } vlib_frame_queue_msg_type_t;
 
-typedef struct {
+typedef struct
+{
   volatile u32 valid;
-  u32 msg_type;         
+  u32 msg_type;
   u32 n_vectors;
   u32 last_n_vectors;
 
@@ -74,10 +81,12 @@ typedef struct {
   u32 buffer_index[VLIB_FRAME_SIZE];
 
   /* Pad to a cache line boundary */
-  u8 pad[CLIB_CACHE_LINE_BYTES - 4 * sizeof(u32)];
-} vlib_frame_queue_elt_t;
+  u8 pad[CLIB_CACHE_LINE_BYTES - 4 * sizeof (u32)];
+}
+vlib_frame_queue_elt_t;
 
-typedef struct {
+typedef struct
+{
   /* First cache line */
   volatile u32 *wait_at_barrier;
   volatile u32 *workers_at_barrier;
@@ -85,23 +94,25 @@ typedef struct {
 
   /* Second Cache Line */
   void *thread_mheap;
-  u8 * thread_stack;
-  void (*thread_function)(void *);
-  void * thread_function_arg;
-  i64 recursion_level; 
-  elog_track_t elog_track; 
+  u8 *thread_stack;
+  void (*thread_function) (void *);
+  void *thread_function_arg;
+  i64 recursion_level;
+  elog_track_t elog_track;
   u32 instance_id;
   vlib_thread_registration_t *registration;
   u8 *name;
   u64 barrier_sync_count;
 
   long lwp;
-  int dpdk_lcore_id;
+  int lcore_id;
+  pthread_t thread_id;
 } vlib_worker_thread_t;
 
 vlib_worker_thread_t *vlib_worker_threads;
 
-typedef struct {
+typedef struct
+{
   /* enqueue side */
   volatile u64 tail;
   u64 enqueues;
@@ -109,9 +120,7 @@ typedef struct {
   u64 enqueue_vectors;
   u32 enqueue_full_events;
   u32 enqueue_efd_discards;
-  u8 pad2[CLIB_CACHE_LINE_BYTES 
-          - (2 * sizeof(u32))
-          - (4 * sizeof(u64))];
+  u8 pad2[CLIB_CACHE_LINE_BYTES - (2 * sizeof (u32)) - (4 * sizeof (u64))];
 
   /* dequeue side */
   volatile u64 head;
@@ -120,48 +129,45 @@ typedef struct {
   u64 dequeue_vectors;
   u64 trace;
   u64 vector_threshold;
-  u8 pad4[CLIB_CACHE_LINE_BYTES 
-          - (6 * sizeof(u64))];
+  u8 pad4[CLIB_CACHE_LINE_BYTES - (6 * sizeof (u64))];
 
   /* dequeue hint to enqueue side */
   volatile u64 head_hint;
-  u8 pad5 [CLIB_CACHE_LINE_BYTES - sizeof(u64)];
+  u8 pad5[CLIB_CACHE_LINE_BYTES - sizeof (u64)];
 
   /* read-only, constant, shared */
   vlib_frame_queue_elt_t *elts;
   u32 nelts;
-} vlib_frame_queue_t;
+}
+vlib_frame_queue_t;
 
 vlib_frame_queue_t **vlib_frame_queues;
 
 /* Called early, in thread 0's context */
-clib_error_t * vlib_thread_init (vlib_main_t * vm);
+clib_error_t *vlib_thread_init (vlib_main_t * vm);
 
-vlib_worker_thread_t * vlib_alloc_thread (vlib_main_t * vm);
+vlib_worker_thread_t *vlib_alloc_thread (vlib_main_t * vm);
 
-int vlib_frame_queue_enqueue (vlib_main_t *vm, u32 node_runtime_index,
-                              u32 frame_queue_index, vlib_frame_t *frame,
-                              vlib_frame_queue_msg_type_t type);
+int vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
+                             u32 frame_queue_index, vlib_frame_t * frame,
+                             vlib_frame_queue_msg_type_t type);
 
-int vlib_frame_queue_dequeue (int thread_id, 
-                              vlib_main_t *vm, 
-                              vlib_node_main_t *nm);
+int vlib_frame_queue_dequeue (int thread_id,
+                             vlib_main_t * vm, vlib_node_main_t * nm);
 
 u64 dispatch_node (vlib_main_t * vm,
-                   vlib_node_runtime_t * node,
-                   vlib_node_type_t type,
-                   vlib_node_state_t dispatch_state,
-                   vlib_frame_t * frame,
-                   u64 last_time_stamp);
+                  vlib_node_runtime_t * node,
+                  vlib_node_type_t type,
+                  vlib_node_state_t dispatch_state,
+                  vlib_frame_t * frame, u64 last_time_stamp);
 
 u64 dispatch_pending_node (vlib_main_t * vm,
-                           vlib_pending_frame_t * p,
-                           u64 last_time_stamp);
+                          vlib_pending_frame_t * p, u64 last_time_stamp);
 
-void vlib_worker_thread_node_runtime_update(void);
+void vlib_worker_thread_node_runtime_update (void);
 
-void vlib_create_worker_threads (vlib_main_t *vm, int n, 
-                                 void (*thread_function)(void *));
+void vlib_create_worker_threads (vlib_main_t * vm, int n,
+                                void (*thread_function) (void *));
 
 void vlib_worker_thread_init (vlib_worker_thread_t * w);
 
@@ -175,33 +181,36 @@ void vlib_worker_thread_init (vlib_worker_thread_t * w);
 #define BARRIER_SYNC_TIMEOUT (1.0)
 #endif
 
-void vlib_worker_thread_barrier_sync(vlib_main_t *vm);
-void vlib_worker_thread_barrier_release(vlib_main_t *vm);
+void vlib_worker_thread_barrier_sync (vlib_main_t * vm);
+void vlib_worker_thread_barrier_release (vlib_main_t * vm);
 
-always_inline void vlib_smp_unsafe_warning (void)
+always_inline void
+vlib_smp_unsafe_warning (void)
 {
   if (CLIB_DEBUG > 0)
     {
-      if (os_get_cpu_number())
-        fformat(stderr, "%s: SMP unsafe warning...\n", __FUNCTION__);
+      if (os_get_cpu_number ())
+       fformat (stderr, "%s: SMP unsafe warning...\n", __FUNCTION__);
     }
 }
 
-typedef enum {
-    VLIB_WORKER_THREAD_FORK_FIXUP_ILLEGAL = 0,
-    VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX,
+typedef enum
+{
+  VLIB_WORKER_THREAD_FORK_FIXUP_ILLEGAL = 0,
+  VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX,
 } vlib_fork_fixup_t;
 
 void vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which);
 
-static inline void vlib_worker_thread_barrier_check (void)
+static inline void
+vlib_worker_thread_barrier_check (void)
 {
-    if (PREDICT_FALSE(*vlib_worker_threads->wait_at_barrier))
+  if (PREDICT_FALSE (*vlib_worker_threads->wait_at_barrier))
     {
-        clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
-        while (*vlib_worker_threads->wait_at_barrier)
-            ;
-        clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
+      clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
+      while (*vlib_worker_threads->wait_at_barrier)
+       ;
+      clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
     }
 }
 
@@ -240,32 +249,46 @@ do {                                                                    \
 #define VLIB_EFD_DEF_WORKER_HI_THRESH_PCT   90
 
 /* EFD worker thread settings */
-typedef struct vlib_efd_t {
+typedef struct vlib_efd_t
+{
   u16 enabled;
   u16 queue_hi_thresh;
-  u8  ip_prec_bitmap;
-  u8  mpls_exp_bitmap;
-  u8  vlan_cos_bitmap;
-  u8  pad;
+  u8 ip_prec_bitmap;
+  u8 mpls_exp_bitmap;
+  u8 vlan_cos_bitmap;
+  u8 pad;
 } vlib_efd_t;
 
-typedef struct {
+#define foreach_sched_policy \
+  _(SCHED_OTHER, OTHER, "other") \
+  _(SCHED_BATCH, BATCH, "batch") \
+  _(SCHED_IDLE, IDLE, "idle")   \
+  _(SCHED_FIFO, FIFO, "fifo")   \
+  _(SCHED_RR, RR, "rr")
+
+typedef enum
+{
+#define _(v,f,s) SCHED_POLICY_##f = v,
+  foreach_sched_policy
+#undef _
+    SCHED_POLICY_N,
+} sched_policy_t;
+
+typedef struct
+{
   /* Link list of registrations, built by constructors */
-  vlib_thread_registration_t * next;
-  
-  /* Vector of registrations, w/ non-data-structure clones at the top */
-  vlib_thread_registration_t ** registrations;
+  vlib_thread_registration_t *next;
 
-  uword * thread_registrations_by_name;
+  /* Vector of registrations, w/ non-data-structure clones at the top */
+  vlib_thread_registration_t **registrations;
 
-  vlib_worker_thread_t * worker_threads;
+  uword *thread_registrations_by_name;
 
-  /* thread / cpu / io thread parameters */
-  u32 main_thread_is_io_node;
+  vlib_worker_thread_t *worker_threads;
 
-  /* 
-   * Launch all threads as pthreads, 
-   * not eal_rte_launch (strict affinity) threads 
+  /*
+   * Launch all threads as pthreads,
+   * not eal_rte_launch (strict affinity) threads
    */
   int use_pthreads;
 
@@ -291,13 +314,29 @@ typedef struct {
   u8 main_lcore;
 
   /* Bitmap of available CPU cores */
-  uword * cpu_core_bitmap;
+  uword *cpu_core_bitmap;
 
   /* Bitmap of available CPU sockets (NUMA nodes) */
-  uword * cpu_socket_bitmap;
+  uword *cpu_socket_bitmap;
 
   vlib_efd_t efd;
-  
+
+  /* handoff node index */
+  u32 handoff_dispatch_node_index;
+
+  /* for frame queue tracing */
+  frame_queue_trace_t *frame_queue_traces;
+  frame_queue_nelt_counter_t *frame_queue_histogram;
+
+  /* worker thread initialization barrier */
+  volatile u32 worker_thread_release;
+
+  /* scheduling policy */
+  u32 sched_policy;
+
+  /* scheduling policy priority */
+  u32 sched_priority;
+
 } vlib_thread_main_t;
 
 vlib_thread_main_t vlib_thread_main;
@@ -312,6 +351,14 @@ static void __vlib_add_thread_registration_##x (void)   \
   x.next = tm->next;                                    \
   tm->next = &x;                                        \
 }                                                       \
-__VA_ARGS__ vlib_thread_registration_t x 
+__VA_ARGS__ vlib_thread_registration_t x
 
 #endif /* included_vlib_threads_h */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */