Drizzled Public API Documentation

processlist.cc
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 
00023 #include <plugin/session_dictionary/dictionary.h>
00024 
00025 #include <netdb.h>
00026 
00027 #include <drizzled/pthread_globals.h>
00028 #include <drizzled/plugin/client.h>
00029 #include <drizzled/plugin/authorization.h>
00030 #include <drizzled/internal/my_sys.h>
00031 #include <drizzled/internal/thread_var.h>
00032 #include <drizzled/session/state.h>
00033 #include <drizzled/session/times.h>
00034 #include <set>
00035 
00036 using namespace std;
00037 using namespace drizzled;
00038 
00039 ProcesslistTool::ProcesslistTool() :
00040   plugin::TableFunction("DATA_DICTIONARY", "PROCESSLIST")
00041 {
00042   add_field("ID", plugin::TableFunction::NUMBER, 0, false);
00043   add_field("USERNAME", 16);
00044   add_field("HOST", NI_MAXHOST);
00045   add_field("DB", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, true);
00046   add_field("COMMAND", 16);
00047   add_field("TIME", plugin::TableFunction::SIZE, 0, false);
00048   add_field("STATE", plugin::TableFunction::STRING, 256, true);
00049   add_field("INFO", plugin::TableFunction::STRING, PROCESS_LIST_WIDTH, true);
00050   add_field("HAS_GLOBAL_LOCK", plugin::TableFunction::BOOLEAN, 0, false);
00051 }
00052 
00053 ProcesslistTool::Generator::Generator(Field **arg) :
00054   plugin::TableFunction::Generator(arg),
00055   session_generator(*getSession().user())
00056 {
00057 }
00058 
00059 bool ProcesslistTool::Generator::populate()
00060 {
00061   while (Session* tmp= session_generator)
00062   {
00063     boost::shared_ptr<session::State> state(tmp->state());
00064     identifier::user::ptr tmp_sctx= tmp->user();
00065 
00066     /* ID */
00067     push((int64_t) tmp->thread_id);
00068 
00069     /* USER */
00070     if (not tmp_sctx->username().empty())
00071       push(tmp_sctx->username());
00072     else
00073       push(_("no user"));
00074 
00075     /* HOST */
00076     push(tmp_sctx->address());
00077 
00078     /* DB */
00079     util::string::ptr schema(tmp->schema());
00080     if (schema and not schema->empty())
00081     {
00082       push(*schema);
00083     }
00084     else
00085     {
00086       push();
00087     }
00088 
00089     /* COMMAND */
00090     if (tmp->getKilled() == Session::KILL_CONNECTION)
00091     {
00092       push("Killed");
00093     }
00094     else
00095     {
00096       push(getCommandName(tmp->command));
00097     }
00098 
00099     /* type::Time */
00100     boost::posix_time::time_duration duration_result= getSession().times.start_timer() - getSession().times._start_timer;
00101     push(static_cast<uint64_t>(duration_result.is_negative() ? 0 : duration_result.total_seconds()));
00102 
00103     /* STATE */
00104     const char *step= tmp->get_proc_info();
00105     step ? push(step): push();
00106 
00107     /* INFO */
00108     if (state)
00109     {
00110       size_t length;
00111       const char *tmp_ptr= state->query(length);
00112       push(tmp_ptr, length);
00113     }
00114     else
00115     {
00116       push();
00117     }
00118 
00119     /* HAS_GLOBAL_LOCK */
00120     bool has_global_lock= tmp->isGlobalReadLock();
00121     push(has_global_lock);
00122 
00123     return true;
00124   }
00125 
00126   return false;
00127 }