You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@trafficserver.apache.org by GitBox <gi...@apache.org> on 2022/04/18 22:39:14 UTC

[GitHub] [trafficserver] cmcfarlen commented on a diff in pull request #8581: Reduce overhead incurred by enabling debug output.

cmcfarlen commented on code in PR #8581:
URL: https://github.com/apache/trafficserver/pull/8581#discussion_r852451453


##########
src/tscore/DbgCtl.cc:
##########
@@ -0,0 +1,111 @@
+/** @file
+
+  Implementation file for DbgCtl class.
+
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+ */
+
+#include <mutex>
+#include <atomic>
+#include <set>
+#include <cstring>
+
+#include <tscore/Diags.h>
+
+// The resistry of fast debug controllers has a ugly implementation to handle the whole-program initialization
+// order problem with C++.

Review Comment:
   Can you explain how this handles the whole-program init order problem? Also, a general summary of the approach here how the debug tags are assembled into this registry and efficiently checked.



##########
include/tscore/DbgCtl.h:
##########
@@ -0,0 +1,56 @@
+/** @file
+
+  DbgCtl class header file.
+
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+ */
+
+#pragma once
+
+#include <ts/ts.h>
+
+// For use with TSDbg().
+//
+class DbgCtl
+{
+public:
+  // Tag is a debug tag.  Debug output associated with this control will be output when debug output
+  // is enabled globally, and the tag matches the configured debug tag regular expression.
+  //
+  DbgCtl(char const *tag) : _ptr(_get_ptr(tag)) {}

Review Comment:
   Could call `TSDbgCtlCreate` instead of `_get_ptr` directly to get the asserts



##########
src/tscore/DbgCtl.cc:
##########
@@ -0,0 +1,111 @@
+/** @file
+
+  Implementation file for DbgCtl class.
+
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+ */
+
+#include <mutex>
+#include <atomic>
+#include <set>
+#include <cstring>
+
+#include <tscore/Diags.h>
+
+// The resistry of fast debug controllers has a ugly implementation to handle the whole-program initialization
+// order problem with C++.
+//
+class DbgCtl::_RegistryAccessor
+{
+private:
+  struct TagCmp {
+    bool
+    operator()(TSDbgCtl const &a, TSDbgCtl const &b) const
+    {
+      return std::strcmp(a.tag, b.tag) < 0;
+    }
+  };
+
+public:
+  _RegistryAccessor() : _lg(data().mtx) {}
+
+  using Set = std::set<TSDbgCtl, TagCmp>;
+
+  struct Data {
+    std::mutex mtx;
+    Set set;
+  };
+
+  static Data &
+  data()
+  {
+    static Data d;
+    return d;
+  }
+
+private:
+  std::lock_guard<std::mutex> _lg;
+};
+
+TSDbgCtl const *
+DbgCtl::_get_ptr(char const *tag)
+{
+  ink_assert(tag != nullptr);
+
+  TSDbgCtl ctl;
+
+  ctl.tag = tag;
+
+  _RegistryAccessor ra;
+
+  auto &d{ra.data()};
+
+  if (auto it = d.set.find(ctl); it != d.set.end()) {
+    return &*it;
+  }
+
+  auto sz = std::strlen(tag);
+
+  ink_assert(sz > 0);
+
+  {
+    char *t = new char[sz + 1]; // Deleted implicitly by program termination.

Review Comment:
   🤔Not sure a memcheck tool would agree.



##########
include/tscore/Diags.h:
##########
@@ -142,50 +156,93 @@ extern Diags *diags;
 /// A Diag version of the above.
 #define Diag(tag, ...)                                        \
   do {                                                        \
-    if (unlikely(diags->on())) {                              \
+    if (unlikely(diags()->on())) {                            \
       static const SourceLocation loc = MakeSourceLocation(); \
       static LogMessage log_message;                          \
       log_message.diag(tag, loc, __VA_ARGS__);                \
     }                                                         \
-  } while (0)
-
-/// A Debug version of the above.
-#define Debug(tag, ...)                                       \
-  do {                                                        \
-    if (unlikely(diags->on())) {                              \
-      static const SourceLocation loc = MakeSourceLocation(); \
-      static LogMessage log_message;                          \
-      log_message.debug(tag, loc, __VA_ARGS__);               \
-    }                                                         \
-  } while (0)
-
-/** Same as Debug above, but this allows a positive override of the tag
- * mechanism by a flag boolean.
- *
- * @param[in] flag True if the message should be logged regardless of tag
- * configuration, false if the logging of the message should respsect the tag
- * configuration.
- */
-#define SpecificDebug(flag, tag, ...)                                                                       \
-  do {                                                                                                      \
-    if (unlikely(diags->on())) {                                                                            \
-      static const SourceLocation loc = MakeSourceLocation();                                               \
-      static LogMessage log_message;                                                                        \
-      flag ? log_message.print(tag, DL_Debug, loc, __VA_ARGS__) : log_message.debug(tag, loc, __VA_ARGS__); \
-    }                                                                                                       \
-  } while (0)
-
-#define is_debug_tag_set(_t) unlikely(diags->on(_t, DiagsTagType_Debug))
-#define is_action_tag_set(_t) unlikely(diags->on(_t, DiagsTagType_Action))
+  } while (false)
+
+inline bool
+is_dbg_ctl_enabled(DbgCtl const &ctl)
+{
+  return unlikely(diags()->on()) && ctl.ptr()->on;
+}
+
+// printf-line debug output.  First parameter must be DbgCtl instance. Assumes debug control is enabled, and
+// debug output globablly enabled.
+//
+#define DbgPrint(ctl__, ...)                                             \
+  do {                                                                   \
+    static const SourceLocation loc__ = MakeSourceLocation();            \
+    static LogMessage log_message__;                                     \
+    log_message__.print(ctl__.ptr()->tag, DL_Debug, loc__, __VA_ARGS__); \
+  } while (false)
+
+// printf-like debug output.  First parameter must be an instance of DbgCtl.
+//
+#define Dbg(ctl__, ...)                               \
+  do {                                                \
+    if (unlikely(diags()->on()) && ctl__.ptr()->on) { \
+      DbgPrint(ctl__, __VA_ARGS__);                   \
+    }                                                 \
+  } while (false)
+
+// printf-like debug output.  First parameter must be tag (C-string literal, or otherwise
+// a constexpr returning char const pointer to null-terminated C-string).
+//
+#define Debug(tag__, ...)             \
+  do {                                \
+    if (unlikely(diags()->on())) {    \
+      static DbgCtl ctl__(tag__);     \

Review Comment:
   identifiers with double underscores are reserved for the implementation.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org