]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
common: accessors for list of perf counters
authorJohn Spray <john.spray@redhat.com>
Thu, 30 Jun 2016 13:06:42 +0000 (14:06 +0100)
committerJohn Spray <john.spray@redhat.com>
Thu, 29 Sep 2016 16:26:54 +0000 (17:26 +0100)
...and store the list by a string path, for
consumption by the world outside of integer
perf counter/subsystem IDs.

This is for consumption by MgrClient.

Signed-off-by: John Spray <john.spray@redhat.com>
src/common/perf_counters.cc
src/common/perf_counters.h

index 2b80a25c029deb9a42f7c478e56695d0941a345a..499ee89d59b31a78953289c3cb96e63681ab2e4d 100644 (file)
@@ -55,11 +55,32 @@ void PerfCountersCollection::add(class PerfCounters *l)
   }
 
   m_loggers.insert(l);
+
+  for (unsigned int i = 0; i < l->m_data.size(); ++i) {
+    PerfCounters::perf_counter_data_any_d &data = l->m_data[i];
+
+    std::string path = l->get_name();
+    path += ".";
+    path += data.name;
+
+    by_path[path] = &data;
+  }
 }
 
 void PerfCountersCollection::remove(class PerfCounters *l)
 {
   Mutex::Locker lck(m_lock);
+
+  for (unsigned int i = 0; i < l->m_data.size(); ++i) {
+    PerfCounters::perf_counter_data_any_d &data = l->m_data[i];
+
+    std::string path = l->get_name();
+    path += ".";
+    path += data.name;
+
+    by_path.erase(path);
+  }
+
   perf_counters_set_t::iterator i = m_loggers.find(l);
   assert(i != m_loggers.end());
   m_loggers.erase(i);
@@ -73,6 +94,8 @@ void PerfCountersCollection::clear()
   for (; i != i_end; ) {
     m_loggers.erase(i++);
   }
+
+  by_path.clear();
 }
 
 bool PerfCountersCollection::reset(const std::string &name)
@@ -132,6 +155,14 @@ void PerfCountersCollection::dump_formatted(
   f->close_section();
 }
 
+void PerfCountersCollection::with_counters(std::function<void(
+      const PerfCountersCollection::CounterMap &)> fn) const
+{
+  Mutex::Locker lck(m_lock);
+
+  fn(by_path);
+}
+
 // ---------------------------
 
 PerfCounters::~PerfCounters()
@@ -449,3 +480,4 @@ PerfCounters *PerfCountersBuilder::create_perf_counters()
   m_perf_counters = NULL;
   return ret;
 }
+
index 5a462f968d436f1252ecf1375aacee13fb70cfa9..dc3a21a68a846997f753e9ddec8bfa8f4484e91b 100644 (file)
@@ -68,52 +68,6 @@ enum perfcounter_type_d
 class PerfCounters
 {
 public:
-  template <typename T>
-  struct avg_tracker {
-    pair<uint64_t, T> last;
-    pair<uint64_t, T> cur;
-    avg_tracker() : last(0, 0), cur(0, 0) {}
-    T avg() const {
-      if (cur.first == last.first)
-       return cur.first ?
-         cur.second / cur.first :
-         0; // no change, report avg over all time
-      return (cur.second - last.second) / (cur.first - last.first);
-    }
-    void consume_next(const pair<uint64_t, T> &next) {
-      last = cur;
-      cur = next;
-    }
-  };
-
-  ~PerfCounters();
-
-  void inc(int idx, uint64_t v = 1);
-  void dec(int idx, uint64_t v = 1);
-  void set(int idx, uint64_t v);
-  uint64_t get(int idx) const;
-
-  void tset(int idx, utime_t v);
-  void tinc(int idx, utime_t v);
-  void tinc(int idx, ceph::timespan v);
-  utime_t tget(int idx) const;
-
-  void reset();
-  void dump_formatted(ceph::Formatter *f, bool schema,
-      const std::string &counter = "");
-  pair<uint64_t, uint64_t> get_tavg_ms(int idx) const;
-
-  const std::string& get_name() const;
-  void set_name(std::string s) {
-    m_name = s;
-  }
-
-private:
-  PerfCounters(CephContext *cct, const std::string &name,
-            int lower_bound, int upper_bound);
-  PerfCounters(const PerfCounters &rhs);
-  PerfCounters& operator=(const PerfCounters &rhs);
-
   /** Represents a PerfCounters data element. */
   struct perf_counter_data_any_d {
     perf_counter_data_any_d()
@@ -176,6 +130,53 @@ private:
       return make_pair(sum, count);
     }
   };
+
+  template <typename T>
+  struct avg_tracker {
+    pair<uint64_t, T> last;
+    pair<uint64_t, T> cur;
+    avg_tracker() : last(0, 0), cur(0, 0) {}
+    T avg() const {
+      if (cur.first == last.first)
+       return cur.first ?
+         cur.second / cur.first :
+         0; // no change, report avg over all time
+      return (cur.second - last.second) / (cur.first - last.first);
+    }
+    void consume_next(const pair<uint64_t, T> &next) {
+      last = cur;
+      cur = next;
+    }
+  };
+
+  ~PerfCounters();
+
+  void inc(int idx, uint64_t v = 1);
+  void dec(int idx, uint64_t v = 1);
+  void set(int idx, uint64_t v);
+  uint64_t get(int idx) const;
+
+  void tset(int idx, utime_t v);
+  void tinc(int idx, utime_t v);
+  void tinc(int idx, ceph::timespan v);
+  utime_t tget(int idx) const;
+
+  void reset();
+  void dump_formatted(ceph::Formatter *f, bool schema,
+      const std::string &counter = "");
+  pair<uint64_t, uint64_t> get_tavg_ms(int idx) const;
+
+  const std::string& get_name() const;
+  void set_name(std::string s) {
+    m_name = s;
+  }
+
+private:
+  PerfCounters(CephContext *cct, const std::string &name,
+            int lower_bound, int upper_bound);
+  PerfCounters(const PerfCounters &rhs);
+  PerfCounters& operator=(const PerfCounters &rhs);
+
   typedef std::vector<perf_counter_data_any_d> perf_counter_data_vec_t;
 
   CephContext *m_cct;
@@ -190,6 +191,7 @@ private:
   perf_counter_data_vec_t m_data;
 
   friend class PerfCountersBuilder;
+  friend class PerfCountersCollection;
 };
 
 class SortPerfCountersByName {
@@ -218,6 +220,12 @@ public:
       bool schema,
       const std::string &logger = "",
       const std::string &counter = "");
+
+  typedef std::map<std::string,
+          PerfCounters::perf_counter_data_any_d *> CounterMap;
+
+  void with_counters(std::function<void(const CounterMap &)>) const;
+
 private:
   CephContext *m_cct;
 
@@ -226,6 +234,8 @@ private:
 
   perf_counters_set_t m_loggers;
 
+  std::map<std::string, PerfCounters::perf_counter_data_any_d *> by_path; 
+
   friend class PerfCountersCollectionTest;
 };