root/trunk/pdns/pdns/rec_channel_rec.cc @ 647

Revision 647, 4.7 KB (checked in by ahu, 7 years ago)

lower default buffer size, add cpu-use counters to rec_control & rrd graphs, fix broken error message on buffer sizing

Line 
1#include "rec_channel.hh"
2#include <boost/lexical_cast.hpp>
3#include <boost/bind.hpp>
4#include <vector>
5#include "misc.hh"
6#include "recursor_cache.hh"
7#include "syncres.hh"
8#include <boost/function.hpp>
9#include <boost/optional.hpp>
10#include <boost/tuple/tuple.hpp>
11#include <sys/types.h>
12#include <sys/stat.h>
13#include <fcntl.h>
14#include <sys/time.h>
15#include <sys/resource.h>
16#include "logger.hh"
17
18using namespace std;
19using namespace boost;
20map<string, const uint32_t*> d_get32bitpointers;
21map<string, const uint64_t*> d_get64bitpointers;
22map<string, function< uint32_t() > >  d_get32bitmembers;
23
24void addGetStat(const string& name, const uint32_t* place)
25{
26  d_get32bitpointers[name]=place;
27}
28void addGetStat(const string& name, const uint64_t* place)
29{
30  d_get64bitpointers[name]=place;
31}
32void addGetStat(const string& name, function<uint32_t ()> f ) 
33{
34  d_get32bitmembers[name]=f;
35}
36
37optional<uint64_t> get(const string& name) 
38{
39  optional<uint64_t> ret;
40
41  if(d_get32bitpointers.count(name))
42    return *d_get32bitpointers.find(name)->second;
43  if(d_get64bitpointers.count(name))
44    return *d_get64bitpointers.find(name)->second;
45  if(d_get32bitmembers.count(name))
46    return d_get32bitmembers.find(name)->second();
47
48  return ret;
49}
50
51
52template<typename T>
53string doGet(T begin, T end)
54{
55  string ret;
56
57  for(T i=begin; i != end; ++i) {
58    optional<uint64_t> num=get(*i);
59    if(num)
60      ret+=lexical_cast<string>(*num)+"\n";
61    else
62      ret+="UNKNOWN\n";
63  }
64  return ret;
65}
66
67template<typename T>
68string doDumpCache(T begin, T end)
69{
70  T i=begin;
71  string fname;
72
73  if(i!=end) 
74    fname=*i;
75
76  int fd=open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660);
77  if(fd < 0) 
78    return "Error opening dump file for writing: "+string(strerror(errno))+"\n";
79
80  RC.doDumpAndClose(fd); 
81
82  return "done\n";
83}
84
85template<typename T>
86string doWipeCache(T begin, T end)
87{
88  for(T i=begin; i != end; ++i)
89    RC.doWipeCache(*i);
90
91  return "done\n";
92}
93
94
95uint32_t getQueryRate()
96{
97  struct timeval now;
98  gettimeofday(&now, 0);
99  optional<float> delay=g_stats.queryrate.get(now, 10);
100  if(delay)
101    return (uint32_t)(1000000/(*delay));
102  else
103    return 0;
104}
105
106
107static uint64_t getSysTimeMsec()
108{
109  struct rusage ru;
110  getrusage(RUSAGE_SELF, &ru);
111  return(ru.ru_stime.tv_sec*1000 + ru.ru_stime.tv_usec/1000);
112}
113
114static uint64_t getUserTimeMsec()
115{
116  struct rusage ru;
117  getrusage(RUSAGE_SELF, &ru);
118  return(ru.ru_utime.tv_sec*1000 + ru.ru_utime.tv_usec/1000);
119}
120
121
122RecursorControlParser::RecursorControlParser()
123{
124  addGetStat("questions", &g_stats.qcounter);
125  addGetStat("tcp-questions", &g_stats.tcpqcounter);
126
127  addGetStat("cache-hits", &RC.cacheHits);
128  addGetStat("cache-misses", &RC.cacheMisses);
129
130  addGetStat("cache-entries", boost::bind(&MemRecursorCache::size, ref(RC)));
131  addGetStat("servfail-answers", &g_stats.servFails);
132  addGetStat("nxdomain-answers", &g_stats.nxDomains);
133  addGetStat("noerror-answers", &g_stats.noErrors);
134
135  addGetStat("answers0-1", &g_stats.answers0_1);
136  addGetStat("answers1-10", &g_stats.answers1_10);
137  addGetStat("answers10-100", &g_stats.answers10_100);
138  addGetStat("answers100-1000", &g_stats.answers100_1000);
139  addGetStat("answers-slow", &g_stats.answersSlow);
140
141  addGetStat("qa-latency", &g_stats.avgLatencyUsec);
142
143  addGetStat("negcache-entries", boost::bind(&SyncRes::negcache_t::size, ref(SyncRes::s_negcache)));
144  addGetStat("throttle-entries", boost::bind(&SyncRes::throttle_t::size, ref(SyncRes::s_throttle)));
145  addGetStat("nsspeeds-entries", boost::bind(&SyncRes::nsspeeds_t::size, ref(SyncRes::s_nsSpeeds)));
146
147  addGetStat("concurrent-queries", boost::bind(&MTasker<PacketID,string>::numProcesses, ref(MT)));
148  addGetStat("outgoing-timeouts", &SyncRes::s_outgoingtimeouts);
149  addGetStat("tcp-outqueries", &SyncRes::s_tcpoutqueries);
150  addGetStat("all-outqueries", &SyncRes::s_outqueries);
151  addGetStat("throttled-outqueries", &SyncRes::s_throttledqueries);
152  addGetStat("throttled-out", &SyncRes::s_throttledqueries);
153
154  addGetStat("query-rate", getQueryRate);
155
156  addGetStat("user-msec", getUserTimeMsec);
157  addGetStat("sys-msec", getSysTimeMsec);
158}
159
160static void doExit()
161{
162  L<<Logger::Error<<"Exiting on user request"<<endl;
163  exit(1);
164}
165
166string RecursorControlParser::getAnswer(const string& question, RecursorControlParser::func_t** command)
167{
168  *command=nop;
169  vector<string> words;
170  stringtok(words, question);
171
172  if(words.empty())
173    return "invalid command\n";
174
175  string cmd=toLower(words[0]);
176  vector<string>::const_iterator begin=words.begin()+1, end=words.end();
177
178  if(cmd=="get") 
179    return doGet(begin, end);
180
181  if(cmd=="quit") {
182    *command=&doExit;
183    return "bye\n";
184  }
185
186  if(cmd=="dump-cache") 
187    return doDumpCache(begin, end);
188
189  if(cmd=="wipe-cache") 
190    return doWipeCache(begin, end);
191 
192  return "Unknown command '"+cmd+"'\n";
193
194}
Note: See TracBrowser for help on using the browser.