summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorSage Weil <sage@inktank.com>2012-05-30 21:10:08 -0700
committerSage Weil <sage@inktank.com>2012-05-30 22:22:34 -0700
commit594f2bbddace08cf534b1ceefa960b9867ea57ff (patch)
tree490a7e3820690c189d34b00879952682193a7605 /src/common
parentea1c0698cd195ebcf26b9ece280c3b9bf552e3e3 (diff)
downloadceph-594f2bbddace08cf534b1ceefa960b9867ea57ff.tar.gz
perf_counters: use bufferlist instead of vector<char>
bufferlist allocates page-sized chunks and avoids realloc, which'll be more efficient than vector resize doubling just about always. Signed-off-by: Sage Weil <sage@inktank.com>
Diffstat (limited to 'src/common')
-rw-r--r--src/common/ceph_context.cc8
-rw-r--r--src/common/perf_counters.cc31
-rw-r--r--src/common/perf_counters.h5
3 files changed, 16 insertions, 28 deletions
diff --git a/src/common/ceph_context.cc b/src/common/ceph_context.cc
index 1f0fcac8b83..e98f91b716f 100644
--- a/src/common/ceph_context.cc
+++ b/src/common/ceph_context.cc
@@ -164,15 +164,11 @@ void CephContext::do_command(std::string command, std::string args, bufferlist *
lgeneric_dout(this, 1) << "do_command '" << command << "' '" << args << "'" << dendl;
if (command == "perfcounters_dump" || command == "1" ||
command == "perf dump") {
- std::vector<char> v;
- _perf_counters_collection->write_json_to_buf(v, false);
- out->append(&v[0], v.size());
+ _perf_counters_collection->write_json_to_buf(*out, false);
}
else if (command == "perfcounters_schema" || command == "2" ||
command == "perf schema") {
- std::vector<char> v;
- _perf_counters_collection->write_json_to_buf(v, true);
- out->append(&v[0], v.size());
+ _perf_counters_collection->write_json_to_buf(*out, true);
}
else if (command == "config show") {
ostringstream ss;
diff --git a/src/common/perf_counters.cc b/src/common/perf_counters.cc
index 6e41d723805..985a5364620 100644
--- a/src/common/perf_counters.cc
+++ b/src/common/perf_counters.cc
@@ -72,22 +72,21 @@ void PerfCountersCollection::clear()
}
}
-void PerfCountersCollection::write_json_to_buf(std::vector <char> &buffer, bool schema)
+void PerfCountersCollection::write_json_to_buf(bufferlist& bl, bool schema)
{
Mutex::Locker lck(m_lock);
- buffer.push_back('{');
+ bl.append('{');
perf_counters_set_t::iterator l = m_loggers.begin();
perf_counters_set_t::iterator l_end = m_loggers.end();
if (l != l_end) {
while (true) {
- (*l)->write_json_to_buf(buffer, schema);
+ (*l)->write_json_to_buf(bl, schema);
if (++l == l_end)
break;
- buffer.push_back(',');
+ bl.append(',');
}
}
- buffer.push_back('}');
- buffer.push_back('\0');
+ bl.append('}');
}
// ---------------------------
@@ -170,26 +169,18 @@ double PerfCounters::fget(int idx) const
return data.u.dbl;
}
-static inline void append_to_vector(std::vector <char> &buffer, char *buf)
-{
- size_t strlen_buf = strlen(buf);
- std::vector<char>::size_type sz = buffer.size();
- buffer.resize(sz + strlen_buf);
- memcpy(&buffer[sz], buf, strlen_buf);
-}
-
-void PerfCounters::write_json_to_buf(std::vector <char> &buffer, bool schema)
+void PerfCounters::write_json_to_buf(bufferlist& bl, bool schema)
{
char buf[512];
Mutex::Locker lck(m_lock);
snprintf(buf, sizeof(buf), "\"%s\":{", m_name.c_str());
- append_to_vector(buffer, buf);
+ bl.append(buf);
perf_counter_data_vec_t::const_iterator d = m_data.begin();
perf_counter_data_vec_t::const_iterator d_end = m_data.end();
if (d == d_end) {
- buffer.push_back('}');
+ bl.append('}');
return;
}
while (true) {
@@ -200,12 +191,12 @@ void PerfCounters::write_json_to_buf(std::vector <char> &buffer, bool schema)
else
data.write_json(buf, sizeof(buf));
- append_to_vector(buffer, buf);
+ bl.append(buf);
if (++d == d_end)
break;
- buffer.push_back(',');
+ bl.append(',');
}
- buffer.push_back('}');
+ bl.append('}');
}
const std::string &PerfCounters::get_name() const
diff --git a/src/common/perf_counters.h b/src/common/perf_counters.h
index 31bfce3456e..87fca4a11d1 100644
--- a/src/common/perf_counters.h
+++ b/src/common/perf_counters.h
@@ -18,6 +18,7 @@
#include "common/config_obs.h"
#include "common/Mutex.h"
+#include "include/buffer.h"
#include <stdint.h>
#include <string>
@@ -73,7 +74,7 @@ public:
void finc(int idx, double v);
double fget(int idx) const;
- void write_json_to_buf(std::vector <char> &buffer, bool schema);
+ void write_json_to_buf(ceph::bufferlist& bl, bool schema);
const std::string& get_name() const;
void set_name(std::string s) {
@@ -136,7 +137,7 @@ public:
void add(class PerfCounters *l);
void remove(class PerfCounters *l);
void clear();
- void write_json_to_buf(std::vector <char> &buffer, bool schema);
+ void write_json_to_buf(ceph::bufferlist& bl, bool schema);
private:
bool init(const std::string &uri);
void shutdown();