MySQL Lists are EOL. Please join:

List:Commits« Previous MessageNext Message »
From:Mikael Ronstrom Date:March 1 2010 12:19pm
Subject:bzr commit into mysql-5.5-next-mr branch (mikael:3048)
View as plain text  
#At file:///home/mikael/mysql_clones/wl5136_stage/ based on revid:mikael@stripped

 3048 Mikael Ronstrom	2010-03-01
      More docs

    modified:
      sql/scheduler_thread_pool.cc
=== modified file 'sql/scheduler_thread_pool.cc'
--- a/sql/scheduler_thread_pool.cc	2010-03-01 11:18:18 +0000
+++ b/sql/scheduler_thread_pool.cc	2010-03-01 12:19:47 +0000
@@ -51,9 +51,11 @@
 
 #define GROUP_ID_INIT (-1)
 #define GROUP_ID_FINI (-2)
-#define PROCESS_COUNT_INIT (-8)
 
+/* Defines used by process_count */
+#define PROCESS_COUNT_INIT (-8)
 #define STALL_LIMIT 5
+
 /*
   WAIT_ALL_LEVEL should be equal or greater than HIGH_CONCURRENCY_LEVEL
   to have any effect.
@@ -68,6 +70,21 @@ typedef struct _tp_group_low_level_s *tp
  /* thread pool client low level */
 typedef struct _tp_client_low_level_s *tp_client_low_level_t;
 
+/*
+  When we wake threads we have 3 scenarios:
+  WAKE_IF_CONSUMER:
+    This is used when we don't want to use reserved threads and
+    also don't want to start a new thread. We only want to use
+    a consumer thread.
+  WAKE_IF_CREATED:
+    This is used when we can also accept that a reserved thread is
+    started, but we don't accept a new thread to be started. We
+    still prefer consumer thread if there is one.
+  WAKE_OR_CREATE_ONE:
+    This is used when we can also accept to start a new thread.
+    This is only used from stall check thread to avoid that we create
+    new threads too fast which could lead to too many threads started.
+*/
 enum wake_level
 {
   WAKE_IF_CONSUMER = 0,
@@ -79,7 +96,28 @@ typedef struct _tp_thread_s
 {
   pthread_t             thread_id;
   int                   thread_tp_group;
+  /**
+    process_count is used to keep track of how long time a thread has
+    been executing. When a thread isn't actively executing a query the
+    process_count is set to PROCESS_COUNT_INIT. When a thread starts to
+    execute it is set to 0, it is then incremented with 1 every 10ms.
+    The thread is deemed as stalled when this variable reaches the
+    STALL_LIMIT (=5 => 50ms). When a thread is deemed as stalled it is
+    no longer active and the max_active_threads can be incremented to
+    give space for another active thread. This means that only short
+    queries will stop other queries from starting, a long-running query
+    will quickly be deemed as stalled and thereafter the thread pool
+    will more or less treat it as a separate thread until the query
+    completes.
+  */
   int                   process_count;
+  /**
+    not_stalled is used when thd_wait_begin and thd_wait_end is called.
+    If the thread is already stalled it doesn't need to be removed from
+    count of active threads since max_active_threads is already added for
+    stalled threads. This variables indicates whether we should call
+    add_active_threads to increment count when thd_wait_end is called.
+  */
   int                   not_stalled;
   char                  *stack_start;
   tp_client_low_level_t client_low_level_cntx;
@@ -92,37 +130,76 @@ typedef struct _tp_group_events_s
 
 typedef struct _tp_group_s
 {
+  /** Mutex protecting this group */
   mysql_mutex_t         LOCK_group;
+  /** Count of number of threads waiting for consumer condition variable */
   int                   threads_for_consumer;
+  /** Condition variable for consumer thread */
   mysql_cond_t          COND_consumer;
+  /** Count of number of threads waiting for reserve condition variable */
   int                   threads_for_reserve;
+  /** Condition variable for reserve thread */
   mysql_cond_t          COND_reserve;
+  /** RW-lock for active_threads on platforms not supporting atomics */
   my_atomic_rwlock_t    LOCK_threads_active;
 
-
+  /** Id of this thread group */
   int                   group_idx;
+  /** Number of threads initialized */
   int                   threads_initialized;
+  /** Number of high prio queries waiting */
   int                   query_events;
+  /** Number of low prio queries waiting */
   int                   trans_events;
+  /** Number of threads in the group */
   int                   threads_in_group;
+  /** Counter counting total number of milliseconds */
   uint64                check_count;
 
+  /** Low level context for this group */
   tp_group_low_level_t  group_low_level_cntx;
 
   /*
     These are heavily used together in the main loop, so good if they can
-    fit in the same cache line. Align this to 64 byte cache line.
+    fit in the same cache line. Align this to 64 byte cache line if
+    possible.
   */
+  /** First pointer in high prio queries */
   tp_client_low_level_t first_queued_query;
+  /** First pointer in high prio queries */
   tp_client_low_level_t first_queued_trans;
+  /**
+    This variable is used to indicate which thread is currently waiting
+    for new queries to arrive. If it is NULL, no thread is waiting for
+    queries to arrive, this is often the case, but after 20ms (checking
+    process_count in stall check thread) we will wake a thread to take
+    up the waiting.
+  */
   tp_thread_t           *waiting_thread;
+  /**
+    max_active_threads is the maximum number of active threads we are
+    allowing to execute in parallel in this thread group. Whenever a
+    thread is deemed as stalled this number will be incremented and
+    similarly it is decremented when a stalled thread has completed
+    query execution.
+  */
   int                   max_active_threads;
+  /**
+    active_threads keeps track of the current number of active threads.
+    When thd_wait_begin is called this number will be decremented for
+    non-stalled threads. This variable isn't protected by the mutex,
+    it is updated using atomic instructions.
+  */
   volatile int          threads_active;
+  /** Max number of non-stalled threads in group active */
   int                   threads_user_request;
 
+  /** Last pointer in high prio queries */
   tp_client_low_level_t last_queued_query;
+  /** Last pointer in low prio queries */
   tp_client_low_level_t last_queued_trans;
 
+  /** Thread data for the threads in this group */
   tp_thread_t           group_threads[MAX_THREADS_PER_GROUP];
 } tp_group_t;
 
@@ -389,6 +466,12 @@ update_max_active_threads(tp_group_t *my
   mysql_mutex_unlock(&my_tp_group->LOCK_group);
 }
 
+/*
+  init_process_count
+
+  @my_tp_group        Thread group data
+  @my_thread_data     Thread data
+*/
 static void
 init_process_count(tp_group_t *my_tp_group, tp_thread_t *my_thread_data)
 {
@@ -399,6 +482,8 @@ init_process_count(tp_group_t *my_tp_gro
 
 /*
   tp_thd_cleanup
+  This function is called when the connection has been broken or an
+  error requires us to close the connection.
 
   @param thd    THD of connection
   @param end    TRUE -> call end_connection()
@@ -692,7 +777,7 @@ static int tp_create_worker_threads(tp_g
 
 /*
   Wake one of the worker threads that is waiting to process clients commands.
-    Or create a new thread to help process client commands.
+  Or create a new thread to help process client commands.
 
   SYNOPSIS
     tp_wake_thread()


Attachment: [text/bzr-bundle] bzr/mikael@mysql.com-20100301121947-8ngujsza86i40xk8.bundle
Thread
bzr commit into mysql-5.5-next-mr branch (mikael:3048) Mikael Ronstrom1 Mar