Drizzled Public API Documentation

session_usage.cc
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2010 Brian Aker
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/performance_dictionary/dictionary.h>
00024 
00025 #include <drizzled/atomics.h>
00026 #include <drizzled/session.h>
00027 
00028 #include <sys/resource.h>
00029 
00030 using namespace drizzled;
00031 using namespace std;
00032 
00033 #define FUNCTION_NAME_LEN 64
00034 
00035 performance_dictionary::SessionUsage::SessionUsage() :
00036   plugin::TableFunction("DATA_DICTIONARY", "SESSION_USAGE")
00037 {
00038   add_field("QUERY", plugin::TableFunction::STRING, FUNCTION_NAME_LEN, false);
00039   add_field("USER_TIME_USED_SECONDS", plugin::TableFunction::NUMBER, false);
00040   add_field("USER_TIME_USED_MICRO_SECONDS", plugin::TableFunction::NUMBER, false);
00041   add_field("SYSTEM_TIME_USED_SECONDS", plugin::TableFunction::NUMBER, false);
00042   add_field("SYSTEM_TIME_USED_MICRO_SECONDS", plugin::TableFunction::NUMBER, false);
00043   add_field("INTEGRAL_MAX_RESIDENT_SET_SIZE", plugin::TableFunction::NUMBER, 0, false);
00044   add_field("INTEGRAL_SHARED_TEXT_MEMORY_SIZE", plugin::TableFunction::NUMBER, 0, false);
00045   add_field("INTEGRAL_UNSHARED_DATA_SIZE", plugin::TableFunction::NUMBER, 0, false);
00046   add_field("INTEGRAL_UNSHARED_STACK_SIZE", plugin::TableFunction::NUMBER, 0, false);
00047   add_field("PAGE_RECLAIMS", plugin::TableFunction::NUMBER, 0, false);
00048   add_field("PAGE_FAULTS", plugin::TableFunction::NUMBER, 0, false);
00049   add_field("SWAPS", plugin::TableFunction::NUMBER, 0, false);
00050   add_field("BLOCK_INPUT_OPERATIONS", plugin::TableFunction::NUMBER, 0, false);
00051   add_field("BLOCK_OUTPUT_OPERATIONS", plugin::TableFunction::NUMBER, 0, false);
00052   add_field("MESSAGES_SENT", plugin::TableFunction::NUMBER, 0, false);
00053   add_field("MESSAGES_RECEIVED", plugin::TableFunction::NUMBER, 0, false);
00054   add_field("SIGNALS_RECEIVED", plugin::TableFunction::NUMBER, 0, false);
00055   add_field("VOLUNTARY_CONTEXT_SWITCHES", plugin::TableFunction::NUMBER, 0, false);
00056   add_field("INVOLUNTARY_CONTEXT_SWITCHES", plugin::TableFunction::NUMBER, 0, false);
00057 }
00058 
00059 
00060 performance_dictionary::SessionUsage::Generator::Generator(drizzled::Field **arg) :
00061   drizzled::plugin::TableFunction::Generator(arg),
00062   usage_cache(0)
00063 { 
00064   usage_cache= static_cast<QueryUsage *>(getSession().getProperty("query_usage"));
00065   if (usage_cache)
00066     query_iter= usage_cache->list().rbegin();
00067 }
00068 
00069 bool performance_dictionary::SessionUsage::Generator::populate()
00070 {
00071   if (not usage_cache)
00072     return false;
00073 
00074   if (query_iter ==  usage_cache->list().rend())
00075     return false;
00076 
00077   publish((*query_iter).query, (*query_iter).delta());
00078   query_iter++;
00079 
00080   return true;
00081 }
00082 
00083 void performance_dictionary::SessionUsage::Generator::publish(const std::string &sql, const struct rusage &usage_arg)
00084 {
00085   /* SQL */
00086   push(sql.substr(0, FUNCTION_NAME_LEN));
00087 
00088   /* USER_TIME_USED_SECONDS */
00089   push(static_cast<int64_t>(usage_arg.ru_utime.tv_sec));
00090 
00091   /* USER_TIME_USED_MICRO_SECONDS */
00092   push(static_cast<int64_t>(usage_arg.ru_utime.tv_usec));
00093 
00094   /* SYSTEM_TIME_USED_SECONDS */
00095   push(static_cast<int64_t>(usage_arg.ru_stime.tv_sec));
00096 
00097   /* SYSTEM_TIME_USED_MICRO_SECONDS */
00098   push(static_cast<int64_t>(usage_arg.ru_stime.tv_usec));
00099 
00100   /* INTEGRAL_MAX_RESIDENT_SET_SIZE */
00101   push(static_cast<int64_t>(usage_arg.ru_maxrss));
00102 
00103   /* INTEGRAL_SHARED_TEXT_MEMORY_SIZE */
00104   push(static_cast<int64_t>(usage_arg.ru_ixrss));
00105 
00106   /* INTEGRAL_UNSHARED_DATA_SIZE */
00107   push(static_cast<int64_t>(usage_arg.ru_idrss));
00108 
00109   /* INTEGRAL_UNSHARED_STACK_SIZE */
00110   push(static_cast<int64_t>(usage_arg.ru_isrss));
00111 
00112   /* PAGE_RECLAIMS */
00113   push(static_cast<int64_t>(usage_arg.ru_minflt));
00114 
00115   /* PAGE_FAULTS */
00116   push(static_cast<int64_t>(usage_arg.ru_majflt));
00117 
00118   /* SWAPS */
00119   push(static_cast<int64_t>(usage_arg.ru_nswap));
00120 
00121   /* BLOCK_INPUT_OPERATIONS */
00122   push(static_cast<int64_t>(usage_arg.ru_inblock));
00123 
00124   /* BLOCK_OUTPUT_OPERATIONS */
00125   push(static_cast<int64_t>(usage_arg.ru_oublock));
00126 
00127   /* MESSAGES_SENT */
00128   push(static_cast<int64_t>(usage_arg.ru_msgsnd));
00129 
00130   /* MESSAGES_RECEIVED */
00131   push(static_cast<int64_t>(usage_arg.ru_msgrcv));
00132 
00133   /* SIGNALS_RECEIVED */
00134   push(static_cast<int64_t>(usage_arg.ru_nsignals));
00135 
00136   /* VOLUNTARY_CONTEXT_SWITCHES */
00137   push(static_cast<int64_t>(usage_arg.ru_nvcsw));
00138 
00139   /* INVOLUNTARY_CONTEXT_SWITCHES */
00140   push(static_cast<int64_t>(usage_arg.ru_nivcsw));
00141 }