~vjsamuel/drizzle/fix-bug-651256

« back to all changes in this revision

Viewing changes to plugin/performance_dictionary/query_usage.h

  • Committer: lbieber
  • Date: 2010-09-23 20:19:20 UTC
  • mfrom: (1789.1.2 session_usage)
  • Revision ID: lbieber@orisndriz08-20100923201920-helm9tnadwf8p9dg
Merge Brian - Merge in session performance table (this is not built by default)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2010 Brian Aker
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
#ifndef PLUGIN_PERFORMANCE_DICTIONARY_QUERY_USAGE_H
 
22
#define PLUGIN_PERFORMANCE_DICTIONARY_QUERY_USAGE_H
 
23
 
 
24
#include "drizzled/internal/getrusage.h"
 
25
 
 
26
#include "drizzled/session.h"
 
27
 
 
28
namespace performance_dictionary {
 
29
 
 
30
#define USAGE_MAX_KEPT 5
 
31
 
 
32
struct query_usage {
 
33
  std::string query;
 
34
  struct rusage start;
 
35
  struct rusage buffer;
 
36
 
 
37
  query_usage()
 
38
  {
 
39
    memset(&start, 0, sizeof(struct rusage));
 
40
    memset(&buffer, 0, sizeof(struct rusage));
 
41
  }
 
42
 
 
43
  void set(const std::string &sql, const struct rusage &arg)
 
44
  {
 
45
    if (getrusage(RUSAGE_THREAD, &buffer))
 
46
    {
 
47
      memset(&start, 0, sizeof(struct rusage));
 
48
      memset(&buffer, 0, sizeof(struct rusage));
 
49
      return;
 
50
    }
 
51
    query= sql.substr(0, 512);
 
52
    start= arg;
 
53
 
 
54
    buffer.ru_utime.tv_sec -= start.ru_utime.tv_sec;
 
55
    buffer.ru_utime.tv_usec -= start.ru_utime.tv_usec;
 
56
 
 
57
    buffer.ru_stime.tv_sec -= start.ru_stime.tv_sec;
 
58
    buffer.ru_stime.tv_usec -= start.ru_stime.tv_usec;
 
59
 
 
60
    buffer.ru_maxrss -= start.ru_maxrss;
 
61
    buffer.ru_ixrss -= start.ru_ixrss;
 
62
    buffer.ru_idrss -= start.ru_idrss;
 
63
    buffer.ru_isrss -= start.ru_isrss;
 
64
    buffer.ru_minflt -= start.ru_minflt;
 
65
    buffer.ru_majflt -= start.ru_majflt;
 
66
    buffer.ru_nswap -= start.ru_nswap;
 
67
    buffer.ru_inblock -= start.ru_inblock;
 
68
    buffer.ru_oublock -= start.ru_oublock;
 
69
    buffer.ru_msgsnd -= start.ru_msgsnd;
 
70
    buffer.ru_msgrcv -= start.ru_msgrcv;
 
71
    buffer.ru_nsignals -= start.ru_nsignals;
 
72
    buffer.ru_nvcsw -= start.ru_nvcsw;
 
73
    buffer.ru_nivcsw -= start.ru_nivcsw;
 
74
  }
 
75
 
 
76
  const struct rusage &delta(void) const
 
77
  {
 
78
    return buffer;
 
79
  }
 
80
 
 
81
  ~query_usage()
 
82
  { }
 
83
};
 
84
 
 
85
typedef std::list <query_usage> Query_list;
 
86
 
 
87
class QueryUsage : public drizzled::util::Storable {
 
88
public:
 
89
  Query_list query_list;
 
90
 
 
91
  QueryUsage()
 
92
  {
 
93
    query_list.resize(USAGE_MAX_KEPT);
 
94
  }
 
95
 
 
96
  void push(const std::string &sql, const struct rusage &arg)
 
97
  {
 
98
    Query_list::iterator it= query_list.end();
 
99
    it--;
 
100
    query_list.splice(query_list.begin(), query_list, it);
 
101
    query_list.front().set(sql, arg);
 
102
  }
 
103
 
 
104
  Query_list &list(void)
 
105
  {
 
106
    return query_list;
 
107
  }
 
108
};
 
109
 
 
110
 
 
111
} /* namespace performance_dictionary */
 
112
 
 
113
#endif /* PLUGIN_PERFORMANCE_DICTIONARY_QUERY_USAGE_H */