Drizzled Public API Documentation

status_tool.cc
00001 /*
00002  * Copyright (C) 2010 Joseph Daly <skinny.moey@gmail.com>
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *   * Redistributions of source code must retain the above copyright notice,
00009  *     this list of conditions and the following disclaimer.
00010  *   * Redistributions in binary form must reproduce the above copyright notice,
00011  *     this list of conditions and the following disclaimer in the documentation
00012  *     and/or other materials provided with the distribution.
00013  *   * Neither the name of Joseph Daly nor the names of its contributors
00014  *     may be used to endorse or promote products derived from this software
00015  *     without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00027  * THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00054 #include <config.h>
00055 
00056 #include "status_tool.h"
00057 #include <drizzled/status_helper.h>
00058 
00059 #include <vector>
00060 #include <sstream>
00061 
00062 using namespace drizzled;
00063 using namespace plugin;
00064 using namespace std;
00065 
00066 
00067 class ShowVarCmpFunctor 
00068 {
00069   public:
00070   ShowVarCmpFunctor() { }
00071   inline bool operator()(const drizzle_show_var *var1, const drizzle_show_var *var2) const
00072   {
00073     int val= strcmp(var1->name, var2->name);
00074     return (val < 0);
00075   }
00076 };
00077 
00078 StatusTool::StatusTool(LoggingStats *in_logging_stats, bool inIsLocal) :
00079   TableFunction("DATA_DICTIONARY", inIsLocal ? "SESSION_STATUS" : "GLOBAL_STATUS"),
00080   outer_logging_stats(in_logging_stats),
00081   isLocal(inIsLocal)
00082 {
00083   add_field("VARIABLE_NAME");
00084   add_field("VARIABLE_VALUE", 1024);
00085 
00086   drizzle_show_var *var= NULL;
00087   uint32_t count= 0;
00088   std::vector<drizzle_show_var *>::iterator all_status_vars_iterator= all_status_vars.begin();
00089   while (true)
00090   {
00091     var= &StatusHelper::status_vars_defs[count];
00092     if ((var == NULL) || (var->name == NULL))
00093     {
00094       break;
00095     }
00096     all_status_vars_iterator= all_status_vars.insert(all_status_vars_iterator, var);
00097     ++count;
00098   }
00099   sort(all_status_vars.begin(), all_status_vars.end(), ShowVarCmpFunctor());
00100 }
00101 
00102 StatusTool::Generator::Generator(Field **arg, LoggingStats *in_logging_stats,
00103                                  std::vector<drizzle_show_var *> *in_all_status_vars, 
00104                                  bool inIsLocal) :
00105   TableFunction::Generator(arg),
00106   logging_stats(in_logging_stats),
00107   isLocal(inIsLocal)   
00108 {
00109   all_status_vars_it= in_all_status_vars->begin();
00110   all_status_vars_end= in_all_status_vars->end();
00111 
00112   status_var_to_display= NULL;
00113   if (isLocal)
00114   {
00115     ScoreboardSlot *scoreboard_slot= logging_stats->getCurrentScoreboard()->findOurScoreboardSlot(&getSession());
00116 
00117     if (scoreboard_slot != NULL)
00118     {
00119       /* A copy of the current status vars for a particular session */ 
00120       status_var_to_display= new StatusVars(*scoreboard_slot->getStatusVars());
00121 
00122       /* Sum the current scoreboard this will give us a value for any variables that have global meaning */
00123       StatusVars current_scoreboard_status_vars;
00124       CumulativeStats *cumulativeStats= logging_stats->getCumulativeStats();
00125       cumulativeStats->sumCurrentScoreboard(logging_stats->getCurrentScoreboard(), &current_scoreboard_status_vars, NULL);
00126 
00127       /* Copy the above to get a value for any variables that have global meaning */  
00128       status_var_to_display->copyGlobalVariables(logging_stats->getCumulativeStats()->getGlobalStatusVars());
00129       status_var_to_display->copyGlobalVariables(&current_scoreboard_status_vars); 
00130     } 
00131     else 
00132     {
00133       status_var_to_display= NULL;
00134     }
00135   }
00136   else // global status 
00137   {
00138     status_var_to_display= new StatusVars();
00139     CumulativeStats *cumulativeStats= logging_stats->getCumulativeStats();
00140     cumulativeStats->sumCurrentScoreboard(logging_stats->getCurrentScoreboard(), status_var_to_display, NULL);
00141     status_var_to_display->merge(logging_stats->getCumulativeStats()->getGlobalStatusVars());
00142   }
00143 }
00144 
00145 StatusTool::Generator::~Generator()
00146 {
00147   delete status_var_to_display;
00148 }
00149 
00150 bool StatusTool::Generator::populate()
00151 {
00152   if (status_var_to_display == NULL)
00153   {
00154     return false;
00155   }
00156 
00157   while (all_status_vars_it != all_status_vars_end)
00158   {
00159     drizzle_show_var *variables= *all_status_vars_it;
00160 
00161     if ((variables == NULL) || (variables->name == NULL))
00162     {
00163       return false;
00164     }
00165 
00166     drizzle_show_var *var;
00167     MY_ALIGNED_BYTE_ARRAY(buff_data, SHOW_VAR_FUNC_BUFF_SIZE, int64_t);
00168     char * const buff= (char *) &buff_data;
00169 
00170     /*
00171       if var->type is SHOW_FUNC, call the function.
00172       Repeat as necessary, if new var is again SHOW_FUNC
00173     */
00174     {
00175       drizzle_show_var tmp;
00176 
00177       for (var= variables; var->type == SHOW_FUNC; var= &tmp)
00178         ((drizzle_show_var_func)((st_show_var_func_container *)var->value)->func)(&tmp, buff);
00179     }
00180 
00181     if (isWild(variables->name))
00182     {
00183       ++all_status_vars_it;
00184       continue;
00185     }
00186 
00187     fill(variables->name, var->value, var->type);
00188 
00189     ++all_status_vars_it;
00190 
00191     return true;
00192   }
00193 
00194   return false;
00195 }
00196 
00197 void StatusTool::Generator::fill(const string &name, char *value, SHOW_TYPE show_type)
00198 {
00199   struct system_status_var *status_var;
00200   ostringstream oss;
00201   string return_value;
00202 
00203   status_var= status_var_to_display->getStatusVarCounters();
00204 
00205   return_value= StatusHelper::fillHelper(status_var, value, show_type);
00206 
00207   push(name);
00208   if (return_value.length())
00209   {
00210     push(return_value);
00211   }
00212   else
00213   {
00214     push(" ");
00215   }
00216 }