Drizzled Public API Documentation

query_usage.h
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 #pragma once
00022 
00023 #include <drizzled/session.h>
00024 
00025 namespace performance_dictionary {
00026 
00027 #define USAGE_MAX_KEPT 5
00028 
00029 struct query_usage {
00030   std::string query;
00031   struct rusage start;
00032   struct rusage buffer;
00033 
00034   query_usage()
00035   {
00036     memset(&start, 0, sizeof(struct rusage));
00037     memset(&buffer, 0, sizeof(struct rusage));
00038   }
00039 
00040   void set(const std::string &sql, const struct rusage &arg)
00041   {
00042     if (getrusage(RUSAGE_THREAD, &buffer))
00043     {
00044       memset(&start, 0, sizeof(struct rusage));
00045       memset(&buffer, 0, sizeof(struct rusage));
00046       return;
00047     }
00048     query= sql.substr(0, 512);
00049     start= arg;
00050 
00051     buffer.ru_utime.tv_sec -= start.ru_utime.tv_sec;
00052     buffer.ru_utime.tv_usec -= start.ru_utime.tv_usec;
00053 
00054     buffer.ru_stime.tv_sec -= start.ru_stime.tv_sec;
00055     buffer.ru_stime.tv_usec -= start.ru_stime.tv_usec;
00056 
00057     buffer.ru_maxrss -= start.ru_maxrss;
00058     buffer.ru_ixrss -= start.ru_ixrss;
00059     buffer.ru_idrss -= start.ru_idrss;
00060     buffer.ru_isrss -= start.ru_isrss;
00061     buffer.ru_minflt -= start.ru_minflt;
00062     buffer.ru_majflt -= start.ru_majflt;
00063     buffer.ru_nswap -= start.ru_nswap;
00064     buffer.ru_inblock -= start.ru_inblock;
00065     buffer.ru_oublock -= start.ru_oublock;
00066     buffer.ru_msgsnd -= start.ru_msgsnd;
00067     buffer.ru_msgrcv -= start.ru_msgrcv;
00068     buffer.ru_nsignals -= start.ru_nsignals;
00069     buffer.ru_nvcsw -= start.ru_nvcsw;
00070     buffer.ru_nivcsw -= start.ru_nivcsw;
00071   }
00072 
00073   const struct rusage &delta(void) const
00074   {
00075     return buffer;
00076   }
00077 
00078   ~query_usage()
00079   { }
00080 };
00081 
00082 typedef std::list <query_usage> Query_list;
00083 
00084 class QueryUsage : public drizzled::util::Storable {
00085 public:
00086   Query_list query_list;
00087 
00088   QueryUsage()
00089   {
00090     query_list.resize(USAGE_MAX_KEPT);
00091   }
00092 
00093   void push(drizzled::Session::QueryString query_string, const struct rusage &arg);
00094 
00095   Query_list &list(void)
00096   {
00097     return query_list;
00098   }
00099 };
00100 
00101 
00102 } /* namespace performance_dictionary */
00103