File: perf-stats.cc

package info (click to toggle)
weakforced 3.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,040 kB
  • sloc: cpp: 20,397; python: 2,002; sh: 700; makefile: 432
file content (243 lines) | stat: -rw-r--r-- 6,731 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * This file is part of PowerDNS or weakforced.
 * Copyright -- PowerDNS.COM B.V. and its contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 3 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * In addition, for the avoidance of any doubt, permission is granted to
 * link this program with OpenSSL and to (re)distribute the binaries
 * produced as the result of such linking.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <iostream>
#include <unistd.h>
#include <thread>
#include <sstream>
#include "perf-stats.hh"
#include "twmap.hh"
#include "dolog.hh"

#define STATS_NUM_WINDOWS 20
#define STATS_WINDOW_SIZE 15

using namespace PerfStats;

TWStatsDB<unsigned int> g_perfStats("perfStats", STATS_WINDOW_SIZE, STATS_NUM_WINDOWS);

const static FieldMap fm = { { wtw_0_1_str, "int" },
                { wtw_1_10_str, "int" },
                { wtw_10_100_str, "int" },
                { wtw_100_1000_str, "int" },
                { wtw_slow_str, "int" },
                { wtr_0_1_str, "int" },
                { wtr_1_10_str, "int" },
                { wtr_10_100_str, "int" },
                { wtr_100_1000_str, "int" },
                { wtr_slow_str, "int" },
                { command_str, "countmin"},
                { custom_str, "countmin"} };

std::map<PerfStat, std::string> lookupPerfStat = { { WorkerThreadWait_0_1, wtw_0_1_str },
                                                   { WorkerThreadWait_1_10, wtw_1_10_str }, 
                                                   { WorkerThreadWait_10_100, wtw_10_100_str },
                                                   { WorkerThreadWait_100_1000,  wtw_100_1000_str},
                                                   { WorkerThreadWait_Slow, wtw_slow_str }, 
                                                   { WorkerThreadRun_0_1, wtr_0_1_str },
                                                   { WorkerThreadRun_1_10, wtr_1_10_str },
                                                   { WorkerThreadRun_10_100, wtr_10_100_str },
                                                   { WorkerThreadRun_100_1000, wtr_100_1000_str },
                                                   { WorkerThreadRun_Slow, wtr_slow_str } };

void initPerfStats()
{
  g_perfStats.setFields(fm);
}

void incStat(PerfStat stat)
{
  auto i = lookupPerfStat.find(stat);
  if (i != lookupPerfStat.end())
    g_perfStats.add(1, i->second, 1);
}

void addWTWStat(unsigned int num_ms) {
  if (num_ms <= 1)
    incStat(WorkerThreadWait_0_1);
  else if (num_ms <= 10)
    incStat(WorkerThreadWait_1_10);
  else if (num_ms <= 100)
    incStat(WorkerThreadWait_10_100);
  else if (num_ms <= 1000)
    incStat(WorkerThreadWait_100_1000);
  else
    incStat(WorkerThreadWait_Slow);
}

void addWTRStat(unsigned int num_ms) {
  if (num_ms <= 1)
    incStat(WorkerThreadRun_0_1);
  else if (num_ms <= 10)
    incStat(WorkerThreadRun_1_10);
  else if (num_ms <= 100)
    incStat(WorkerThreadRun_10_100);
  else if (num_ms <= 1000)
    incStat(WorkerThreadRun_100_1000);
  else
    incStat(WorkerThreadRun_Slow);
}

int getStat(PerfStat stat)
{
  auto i = lookupPerfStat.find(stat);
  if (i != lookupPerfStat.end())
    return g_perfStats.get(1, i->second);
  else
    return 0;
}

static std::set<std::string> command_stats;

void addCommandStat(const std::string& command_name)
{
  command_stats.insert(command_name);
}

void incCommandStat(const std::string& command_name)
{
  g_perfStats.add(1, "Command", command_name, 1);
}

int getCommandStat(const std::string& command_name)
{
  return g_perfStats.get(1, "Command", command_name);
}

static std::set<std::string> custom_stats;

void addCustomStat(const std::string& custom_name)
{
  custom_stats.insert(custom_name);
}

void incCustomStat(const std::string& custom_name)
{
  g_perfStats.add(1, "Custom", custom_name, 1);
}

int getCustomStat(const std::string& custom_name)
{
  return g_perfStats.get(1, "Custom", custom_name);
}

void statsReportingThread()
{
  setThreadName("wf/perf-stats");

  int interval = STATS_WINDOW_SIZE*STATS_NUM_WINDOWS;

  for (;;) {
    std::stringstream ss;
    
    sleep(interval);

    ss << "perf stats last " << interval << " secs: ";
    for (auto i=lookupPerfStat.begin(); i!=lookupPerfStat.end(); ++i) {
      ss << i->second << "=" << getStat(i->first) << " ";
    }
    noticelog("%s", ss.str());

    if (command_stats.size() != 0) {
      ss.str(std::string());
      ss.clear();
      ss << "command stats last " << interval << " secs: ";
      for (const auto& i : command_stats) {
        ss << i << "=" << getCommandStat(i) << " ";
      }
      noticelog("%s", ss.str());
    }

    if (custom_stats.size() != 0) {
      ss.str(std::string());
      ss.clear();
      ss << "custom stats last " << interval << " secs: ";
      for (const auto& i : custom_stats) {
        ss << i << "=" << getCustomStat(i) << " ";
      }
      noticelog("%s", ss.str());
    }
  }
}

void startStatsThread()
{
  infolog("Starting stats reporting thread");
  initPerfStats();
  thread t(statsReportingThread);
  t.detach();
}

std::string getPerfStatsString()
{
  std::stringstream ss;
  for (auto i=lookupPerfStat.begin(); i!=lookupPerfStat.end(); ++i) {
    ss << i->second << "=" << getStat(i->first) << "\n";
  }
  return ss.str();
}

std::string getCommandStatsString()
{
  std::ostringstream ss;
  for (const auto& i : command_stats) {
    ss << i << "=" << getCommandStat(i) << endl;
  }
  return ss.str();
}

std::string getCustomStatsString()
{
  std::ostringstream ss;
  for (const auto& i : custom_stats) {
    ss << i << "=" << getCustomStat(i) << endl;
  }
  return ss.str();
}

using namespace json11;
Json perfStatsToJson()
{
  Json::object jattrs;
  for (auto i=lookupPerfStat.begin(); i!=lookupPerfStat.end(); ++i) {
    jattrs.insert(std::make_pair(i->second, getStat(i->first)));
  }
  return jattrs;
}

Json commandStatsToJson()
{
  Json::object jattrs;
  for (const auto& i : command_stats) {
    jattrs.insert(std::make_pair(i, getCommandStat(i)));
  }
  return jattrs;
}

Json customStatsToJson()
{
  Json::object jattrs;
  for (const auto& i : custom_stats) {
    jattrs.insert(std::make_pair(i, getCustomStat(i)));
  }
  return jattrs;
}