~drizzle-trunk/drizzle/development

868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
1
/* Copyright (C) 2006 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
16
#include <drizzled/server_includes.h>
942.2.5 by Brian Aker
Fix, again, the thread lock issues for unlink_session
17
#include <drizzled/atomics.h>
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
18
#include <drizzled/gettext.h>
19
#include <drizzled/error.h>
960.2.22 by Monty Taylor
Renamed a bunch of plugin files.
20
#include <drizzled/plugin/scheduler.h>
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
21
#include <drizzled/sql_parse.h>
22
#include <drizzled/session.h>
23
#include <string>
960.1.2 by Monty Taylor
Moved pthread_attrib into multi_thread class.
24
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
25
using namespace std;
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
26
using namespace drizzled;
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
27
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
28
/* Configuration variables. */
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
29
static uint32_t max_threads;
30
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
31
/**
32
 * Function to be run as a thread for each session.
33
 */
971.3.67 by Eric Day
Switched static funtion to anonymous namespace + extern C for Sun CC.
34
namespace
35
{
36
  extern "C" pthread_handler_t session_thread(void *arg);
37
}
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
38
39
class MultiThreadScheduler: public plugin::Scheduler
960.1.1 by Monty Taylor
First pass at scheduler plugin.
40
{
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
41
private:
968.2.3 by Monty Taylor
Replacd use of tbb:: namespace with drizzled::
42
  drizzled::atomic<uint32_t> thread_count;
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
43
  pthread_attr_t attr;
960.1.1 by Monty Taylor
First pass at scheduler plugin.
44
45
public:
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
46
  MultiThreadScheduler(): Scheduler()
960.1.2 by Monty Taylor
Moved pthread_attrib into multi_thread class.
47
  {
1093.1.54 by Jay Pipes
Reverts {0}; back to memset() due to compilation errors on GCC 4.2 Mac AND Solaris SunStudio.
48
    struct sched_param tmp_sched_param;
49
50
    memset(&tmp_sched_param, 0, sizeof(struct sched_param));
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
51
52
    /* Setup attribute parameter for session threads. */
53
    (void) pthread_attr_init(&attr);
54
    (void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
55
    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
1093.1.57 by Jay Pipes
Merge and resolved conflicts for Monty's plugins/slots reorganization
56
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
57
    tmp_sched_param.sched_priority= WAIT_PRIOR;
58
    (void) pthread_attr_setschedparam(&attr, &tmp_sched_param);
59
960.1.3 by Monty Taylor
Initialize thread_count to zero.
60
    thread_count= 0;
960.1.2 by Monty Taylor
Moved pthread_attrib into multi_thread class.
61
  }
62
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
63
  ~MultiThreadScheduler()
960.1.2 by Monty Taylor
Moved pthread_attrib into multi_thread class.
64
  {
65
    (void) pthread_mutex_lock(&LOCK_thread_count);
66
    while (thread_count)
67
    {
68
      pthread_cond_wait(&COND_thread_count, &LOCK_thread_count);
69
    }
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
70
960.1.2 by Monty Taylor
Moved pthread_attrib into multi_thread class.
71
    (void) pthread_mutex_unlock(&LOCK_thread_count);
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
72
    (void) pthread_attr_destroy(&attr);
960.1.2 by Monty Taylor
Moved pthread_attrib into multi_thread class.
73
  }
960.1.1 by Monty Taylor
First pass at scheduler plugin.
74
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
75
  virtual bool addSession(Session *session)
960.1.1 by Monty Taylor
First pass at scheduler plugin.
76
  {
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
77
    if (thread_count >= max_threads)
78
      return true;
79
960.1.1 by Monty Taylor
First pass at scheduler plugin.
80
    thread_count++;
81
  
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
82
    if (pthread_create(&session->real_id, &attr, session_thread,
83
                       static_cast<void*>(session)))
84
    {
85
      thread_count--;
960.1.1 by Monty Taylor
First pass at scheduler plugin.
86
      return true;
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
87
    }
960.1.1 by Monty Taylor
First pass at scheduler plugin.
88
  
89
    return false;
90
  }
91
  
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
92
  void runSession(Session *session)
93
  {
94
    if (my_thread_init())
95
    {
96
      session->disconnect(ER_OUT_OF_RESOURCES, true);
97
      statistic_increment(aborted_connects, &LOCK_status);
98
      killSessionNow(session);
99
    }
100
101
    session->thread_stack= (char*) &session;
102
    session->run();
103
    killSessionNow(session);
104
  }
105
106
  void killSessionNow(Session *session)
107
  {
108
    /* Locks LOCK_thread_count and deletes session */
109
    unlink_session(session);
960.1.1 by Monty Taylor
First pass at scheduler plugin.
110
    thread_count--;
111
    my_thread_end();
112
    pthread_exit(0);
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
113
    /* We should never reach this point. */
960.1.1 by Monty Taylor
First pass at scheduler plugin.
114
  }
115
};
929.1.1 by Brian Aker
Push thread count out to the scheduler.
116
971.3.67 by Eric Day
Switched static funtion to anonymous namespace + extern C for Sun CC.
117
namespace
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
118
{
971.3.67 by Eric Day
Switched static funtion to anonymous namespace + extern C for Sun CC.
119
  extern "C" pthread_handler_t session_thread(void *arg)
120
  {
121
    Session *session= static_cast<Session*>(arg);
122
    MultiThreadScheduler *scheduler= static_cast<MultiThreadScheduler*>(session->scheduler);
123
    scheduler->runSession(session);
124
    return NULL;
125
  }
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
126
}
127
128
class MultiThreadFactory : public plugin::SchedulerFactory
971.1.46 by Monty Taylor
Made plugin registration go through Plugin_registry.
129
{
130
public:
994.2.2 by Monty Taylor
Store a Registry of SchedulerFactories and set one of them at startup for better error messages earlier.
131
  MultiThreadFactory() : SchedulerFactory("multi_thread")
132
  {
133
    addAlias("multi-thread");
134
  }
135
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
136
  ~MultiThreadFactory()
137
  {
138
    if (scheduler != NULL)
139
      delete scheduler;
140
  }
141
142
  plugin::Scheduler *operator() ()
971.1.46 by Monty Taylor
Made plugin registration go through Plugin_registry.
143
  {
144
    if (scheduler == NULL)
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
145
      scheduler= new MultiThreadScheduler();
971.1.46 by Monty Taylor
Made plugin registration go through Plugin_registry.
146
    return scheduler;
147
  }
148
};
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
149
971.1.51 by Monty Taylor
New-style plugin registration now works.
150
static MultiThreadFactory *factory= NULL;
971.1.46 by Monty Taylor
Made plugin registration go through Plugin_registry.
151
1110.1.5 by Monty Taylor
Renamed PluginRegistry to plugin::Registry.
152
static int init(drizzled::plugin::Registry &registry)
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
153
{
971.1.51 by Monty Taylor
New-style plugin registration now works.
154
  factory= new MultiThreadFactory();
971.1.52 by Monty Taylor
Did the finalizers. Renamed plugin_registry.
155
  registry.add(factory);
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
156
  return 0;
157
}
158
1110.1.5 by Monty Taylor
Renamed PluginRegistry to plugin::Registry.
159
static int deinit(drizzled::plugin::Registry &registry)
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
160
{
971.1.51 by Monty Taylor
New-style plugin registration now works.
161
  if (factory)
971.1.52 by Monty Taylor
Did the finalizers. Renamed plugin_registry.
162
  {
163
    registry.remove(factory);
971.1.51 by Monty Taylor
New-style plugin registration now works.
164
    delete factory;
971.1.52 by Monty Taylor
Did the finalizers. Renamed plugin_registry.
165
  }
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
166
  return 0;
167
}
168
169
static DRIZZLE_SYSVAR_UINT(max_threads, max_threads,
170
                           PLUGIN_VAR_RQCMDARG,
171
                           N_("Maximum number of user threads available."),
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
172
                           NULL, NULL, 2048, 1, 4096, 0);
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
173
174
static struct st_mysql_sys_var* system_variables[]= {
175
  DRIZZLE_SYSVAR(max_threads),
176
  NULL
177
};
178
179
drizzle_declare_plugin(multi_thread)
180
{
181
  "multi_thread",
182
  "0.1",
183
  "Brian Aker",
960.1.1 by Monty Taylor
First pass at scheduler plugin.
184
  "One Thread Per Session Scheduler",
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
185
  PLUGIN_LICENSE_GPL,
186
  init, /* Plugin Init */
187
  deinit, /* Plugin Deinit */
188
  NULL,   /* status variables */
189
  system_variables,   /* system variables */
190
  NULL    /* config options */
191
}
192
drizzle_declare_plugin_end;