]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
denc: rewrite DENC_DUMP_{PRE,POST} using template
authorKefu Chai <kchai@redhat.com>
Mon, 22 Apr 2019 13:13:03 +0000 (21:13 +0800)
committerKefu Chai <kchai@redhat.com>
Wed, 24 Apr 2019 08:26:07 +0000 (16:26 +0800)
* for better readability
* for better debugging experience
* so we can `friend` it in a class in a follow-up change

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/include/buffer.h
src/include/denc.h
src/os/bluestore/bluefs_types.h

index 9f35de613564e72eb7880f576103c2639da68867..6e354b04a5637eeb2ec423e84a92f6b5dec89315 100644 (file)
@@ -77,6 +77,8 @@ template<uint8_t S>
 struct sha_digest_t;
 using sha1_digest_t = sha_digest_t<20>;
 
+template<typename T> class DencDumper;
+
 namespace ceph {
 
 template <class T>
@@ -808,6 +810,7 @@ inline namespace v14_2_0 {
       }
 
       friend class list;
+      template<typename Type> friend class ::DencDumper;
 
     public:
       ~contiguous_appender() {
index 7d4839224a5579b772a14f88d57cf6dee2315756..01ac8e1e88aa31deaaf7da6064bc0ccc10dad943 100644 (file)
@@ -77,37 +77,60 @@ inline constexpr bool denc_supported = denc_traits<T>::supported;
 # include <sys/types.h>
 # include <sys/stat.h>
 # include <fcntl.h>
+
 # define ENCODE_STR(x) #x
 # define ENCODE_STRINGIFY(x) ENCODE_STR(x)
-# define DENC_DUMP_PRE(Type)                   \
-  char *__denc_dump_pre = p.get_pos();
-  // this hackery with bits below is just to get a semi-reasonable
-  // distribution across time.  it is somewhat exponential but not
-  // quite.
-# define DENC_DUMP_POST(Type)                  \
-  do {                                                                 \
-    static int i = 0;                                                  \
-    i++;                                                               \
-    int bits = 0;                                                      \
-    for (unsigned t = i; t; bits++)                                    \
-      t &= t - 1;                                                      \
-    if (bits > 2)                                                      \
-      break;                                                           \
-    char fn[PATH_MAX];                                                 \
-    ::snprintf(fn, sizeof(fn),                                         \
-            ENCODE_STRINGIFY(ENCODE_DUMP_PATH) "/%s__%d.%x", #Type,            \
-            getpid(), i++);                                            \
-    int fd = ::open(fn, O_WRONLY|O_TRUNC|O_CREAT|O_CLOEXEC, 0644);             \
-    if (fd >= 0) {                                                     \
-      size_t len = p.get_pos() - __denc_dump_pre;                      \
-      int r = ::write(fd, __denc_dump_pre, len);                       \
-      (void)r;                                                         \
-      ::close(fd);                                                     \
-    }                                                                  \
-  } while (0)
+
+template<typename T>
+class DencDumper {
+public:
+  DencDumper(const char* name,
+            ceph::bufferlist::contiguous_appender& appender)
+    : name{name},
+      appender{appender},
+      start{appender.get_pos()}
+  {}
+  ~DencDumper() {
+    if (do_sample()) {
+      dump();
+    }
+  }
+private:
+  static bool do_sample() {
+    // this hackery with bits below is just to get a semi-reasonable
+    // distribution across time.  it is somewhat exponential but not
+    // quite.
+    i++;
+    int bits = 0;
+    for (unsigned t = i; t; bits++)
+      t &= t - 1;
+    return bits <= 2;
+  }
+  void dump() {
+    char fn[PATH_MAX];
+    ::snprintf(fn, sizeof(fn),
+              ENCODE_STRINGIFY(ENCODE_DUMP_PATH) "/%s__%d.%x", name,
+              getpid(), i++);
+    int fd = ::open(fn, O_WRONLY|O_TRUNC|O_CREAT|O_CLOEXEC, 0644);
+    if (fd < 0) {
+      return;
+    }
+    size_t len = appender.get_pos() - start;
+    [[maybe_unused]] int r = ::write(fd, start, len);
+    ::close(fd);
+  }
+  const char* name;
+  ceph::bufferlist::contiguous_appender& appender;
+  const char* start;
+  static int i;
+};
+
+template<typename T> int DencDumper<T>::i = 0;
+
+# define DENC_DUMP_PRE(Type)                                           \
+  DencDumper<Type> _denc_dumper{#Type, p};
 #else
 # define DENC_DUMP_PRE(Type)
-# define DENC_DUMP_POST(Type)
 #endif
 
 
@@ -1700,7 +1723,6 @@ inline std::enable_if_t<traits::supported && !traits::featured> decode_nohead(
   void encode(::ceph::buffer::list::contiguous_appender& p) const {    \
     DENC_DUMP_PRE(Type);                                               \
     _denc_friend(*this, p);                                            \
-    DENC_DUMP_POST(Type);                                              \
   }                                                                    \
   void decode(::ceph::buffer::ptr::const_iterator& p) {                        \
     _denc_friend(*this, p);                                            \
@@ -1718,7 +1740,6 @@ inline std::enable_if_t<traits::supported && !traits::featured> decode_nohead(
   void encode(::ceph::buffer::list::contiguous_appender& p, uint64_t f) const { \
     DENC_DUMP_PRE(Type);                                               \
     _denc_friend(*this, p, f);                                         \
-    DENC_DUMP_POST(Type);                                              \
   }                                                                    \
   void decode(::ceph::buffer::ptr::const_iterator& p, uint64_t f=0) {  \
     _denc_friend(*this, p, f);                                         \
index aad4ade443cabe3e38513a658b7c162b84b61c47..43246f6419544781eaf88877170fb9418b5b1c3b 100644 (file)
@@ -61,7 +61,6 @@ struct bluefs_fnode_t {
   void encode(bufferlist::contiguous_appender& p) const {
     DENC_DUMP_PRE(bluefs_fnode_t);
     _denc_friend(*this, p);
-    DENC_DUMP_POST(bluefs_fnode_t);
   }
   void decode(buffer::ptr::const_iterator& p) {
     _denc_friend(*this, p);