[mathicgb] 120/393: Added new logging framework with fine-grained control of what is logged along with -log options and a "help log" feature for listing all log domains compiled into the executable.Currently there is only a single log domain defined, but eventually this should replace tracing level or alternatively tracing level could be defined in terms of log domains. The main feature here is that the logging overhead is precisely zero if a logging domain is turned off at compile time and each logging domain has a macro that can be used to turn it on/off in this way and the default setting is configurable. In this way very expensive logging can be done without worry of overhead as the overhead is zero in the non-logging case. Logs can also be turned off at runtime, though in that case there is an overhead for checking whether something needs to be output or not - a single well-predicted branch per logging event. The code that constructs the strings to be logged is not executed when logging is turned off - so it's not just that the strings are not displayed, they are not considered or constructed at all. Upcoming feature for logging is support for timing code and for counters ("int logs") that disappear if turned off at compile-time just like string logs do.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:58:45 UTC 2015


This is an automated email from the git hooks/post-receive script.

dtorrance-guest pushed a commit to branch upstream
in repository mathicgb.

commit 52ca2f2562d0535717a5923c885be4d64e1b9f5e
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date:   Fri Dec 7 21:42:03 2012 +0100

    Added new logging framework with fine-grained control of what is logged along with -log options and a "help log" feature for listing all log domains compiled into the executable.Currently there is only a single log domain defined, but eventually this should replace tracing level or alternatively tracing level could be defined in terms of log domains. The main feature here is that the logging overhead is precisely zero if a logging domain is turned off at compile time and each logging  [...]
---
 Makefile.am                                        |  6 +-
 build/vs12/mathicgb-exe/mathicgb-exe.vcxproj       |  2 +
 .../vs12/mathicgb-exe/mathicgb-exe.vcxproj.filters |  6 ++
 build/vs12/mathicgb-lib/mathicgb-lib.vcxproj       |  3 +
 .../vs12/mathicgb-lib/mathicgb-lib.vcxproj.filters |  9 ++
 src/cli/CommonParams.cpp                           | 28 ++++++
 src/cli/CommonParams.hpp                           |  1 +
 src/cli/GBMain.cpp                                 |  3 +-
 src/cli/HelpAction.cpp                             | 20 +++++
 src/cli/HelpAction.hpp                             | 11 +++
 src/mathicgb/F4MatrixReducer.cpp                   | 12 ++-
 src/mathicgb/LogDomain.cpp                         | 21 +++++
 src/mathicgb/LogDomain.hpp                         | 99 ++++++++++++++++++++++
 src/mathicgb/LogDomainSet.hpp                      | 39 +++++++++
 src/mathicgb/stdinc.h                              | 12 +++
 15 files changed, 266 insertions(+), 6 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 386bde9..65b39f3 100755
--- a/Makefile.am
+++ b/Makefile.am
@@ -62,7 +62,8 @@ libmathicgb_la_SOURCES = src/mathicgb/BjarkeGeobucket2.cpp				\
   src/mathicgb/F4MatrixReducer.hpp src/mathicgb/MonomialMap.hpp			\
   src/mathicgb/RawVector.hpp src/mathicgb/Atomic.hpp					\
   src/mathicgb/FixedSizeMonomialMap.hpp src/mathicgb/CFile.hpp			\
-  src/mathicgb/CFile.cpp
+  src/mathicgb/CFile.cpp src/mathicgb/LogDomain.hpp						\
+  src/mathicgb/LogDomain.cpp src/mathicgb/LogDomainSet.hpp
 
 # When making a distribution file, Automake knows to include all files
 # that are necessary to build the project. EXTRA_DIST specifies files
@@ -78,7 +79,8 @@ mgb_SOURCES = src/cli/GBMain.cpp src/cli/CommonParams.hpp				\
   src/cli/CommonParams.cpp src/cli/GBAction.hpp src/cli/GBAction.cpp	\
   src/cli/GBCommonParams.hpp src/cli/GBCommonParams.cpp					\
   src/cli/MatrixAction.cpp src/cli/MatrixAction.hpp						\
-  src/cli/SigGBAction.hpp src/cli/SigGBAction.cpp
+  src/cli/SigGBAction.hpp src/cli/SigGBAction.cpp						\
+  src/cli/HelpAction.hpp src/cli/HelpAction.cpp
 mgb_LDADD = $(top_builddir)/libmathicgb.la
 
 # set up tests to run on "make check"
diff --git a/build/vs12/mathicgb-exe/mathicgb-exe.vcxproj b/build/vs12/mathicgb-exe/mathicgb-exe.vcxproj
index 426958b..b3fecb4 100755
--- a/build/vs12/mathicgb-exe/mathicgb-exe.vcxproj
+++ b/build/vs12/mathicgb-exe/mathicgb-exe.vcxproj
@@ -47,6 +47,7 @@
     <ClCompile Include="..\..\..\src\cli\GBAction.cpp" />
     <ClCompile Include="..\..\..\src\cli\GBCommonParams.cpp" />
     <ClCompile Include="..\..\..\src\cli\GBMain.cpp" />
+    <ClCompile Include="..\..\..\src\cli\HelpAction.cpp" />
     <ClCompile Include="..\..\..\src\cli\MatrixAction.cpp" />
     <ClCompile Include="..\..\..\src\cli\SigGBAction.cpp" />
   </ItemGroup>
@@ -65,6 +66,7 @@
     <ClInclude Include="..\..\..\src\cli\CommonParams.hpp" />
     <ClInclude Include="..\..\..\src\cli\GBAction.hpp" />
     <ClInclude Include="..\..\..\src\cli\GBCommonParams.hpp" />
+    <ClInclude Include="..\..\..\src\cli\HelpAction.hpp" />
     <ClInclude Include="..\..\..\src\cli\MatrixAction.hpp" />
     <ClInclude Include="..\..\..\src\cli\SigGBAction.hpp" />
   </ItemGroup>
diff --git a/build/vs12/mathicgb-exe/mathicgb-exe.vcxproj.filters b/build/vs12/mathicgb-exe/mathicgb-exe.vcxproj.filters
index 0a02513..e515509 100755
--- a/build/vs12/mathicgb-exe/mathicgb-exe.vcxproj.filters
+++ b/build/vs12/mathicgb-exe/mathicgb-exe.vcxproj.filters
@@ -33,6 +33,9 @@
     <ClCompile Include="..\..\..\src\cli\CommonParams.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\..\src\cli\HelpAction.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\..\..\src\cli\GBCommonParams.hpp">
@@ -50,5 +53,8 @@
     <ClInclude Include="..\..\..\src\cli\CommonParams.hpp">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="..\..\..\src\cli\HelpAction.hpp">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>
\ No newline at end of file
diff --git a/build/vs12/mathicgb-lib/mathicgb-lib.vcxproj b/build/vs12/mathicgb-lib/mathicgb-lib.vcxproj
index 6b2adbb..87369e7 100755
--- a/build/vs12/mathicgb-lib/mathicgb-lib.vcxproj
+++ b/build/vs12/mathicgb-lib/mathicgb-lib.vcxproj
@@ -454,6 +454,7 @@
     <ClCompile Include="..\..\..\src\mathicgb\Ideal.cpp" />
     <ClCompile Include="..\..\..\src\mathicgb\io-util.cpp" />
     <ClCompile Include="..\..\..\src\mathicgb\KoszulQueue.cpp" />
+    <ClCompile Include="..\..\..\src\mathicgb\LogDomain.cpp" />
     <ClCompile Include="..\..\..\src\mathicgb\MonTableNaive.cpp" />
     <ClCompile Include="..\..\..\src\mathicgb\MTArray.cpp" />
     <ClCompile Include="..\..\..\src\mathicgb\PairTriangle.cpp" />
@@ -495,6 +496,8 @@
     <ClInclude Include="..\..\..\src\mathicgb\Ideal.hpp" />
     <ClInclude Include="..\..\..\src\mathicgb\io-util.hpp" />
     <ClInclude Include="..\..\..\src\mathicgb\KoszulQueue.hpp" />
+    <ClInclude Include="..\..\..\src\mathicgb\LogDomain.hpp" />
+    <ClInclude Include="..\..\..\src\mathicgb\LogDomainSet.hpp" />
     <ClInclude Include="..\..\..\src\mathicgb\MonomialHashTable.hpp" />
     <ClInclude Include="..\..\..\src\mathicgb\MonomialMap.hpp" />
     <ClInclude Include="..\..\..\src\mathicgb\MonTableDivList.hpp" />
diff --git a/build/vs12/mathicgb-lib/mathicgb-lib.vcxproj.filters b/build/vs12/mathicgb-lib/mathicgb-lib.vcxproj.filters
index a88b0f8..1b55392 100755
--- a/build/vs12/mathicgb-lib/mathicgb-lib.vcxproj.filters
+++ b/build/vs12/mathicgb-lib/mathicgb-lib.vcxproj.filters
@@ -123,6 +123,9 @@
     <ClCompile Include="..\..\..\src\mathicgb\CFile.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\..\src\mathicgb\LogDomain.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\..\..\src\mathicgb\BjarkeGeobucket.hpp">
@@ -281,5 +284,11 @@
     <ClInclude Include="..\..\..\src\mathicgb\CFile.hpp">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="..\..\..\src\mathicgb\LogDomain.hpp">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="..\..\..\src\mathicgb\LogDomainSet.hpp">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>
\ No newline at end of file
diff --git a/src/cli/CommonParams.cpp b/src/cli/CommonParams.cpp
index 3b02ed9..d3cf0b0 100644
--- a/src/cli/CommonParams.cpp
+++ b/src/cli/CommonParams.cpp
@@ -1,6 +1,9 @@
 #include "mathicgb/stdinc.h"
 #include "CommonParams.hpp"
 
+#include "mathicgb/LogDomain.hpp"
+#include "mathicgb/LogDomainSet.hpp"
+
 CommonParams::CommonParams(size_t minDirectParams, size_t maxDirectParams):
   mTracingLevel("tracingLevel",
     "How much information to print out about what the program does. No "
@@ -12,6 +15,11 @@ CommonParams::CommonParams(size_t minDirectParams, size_t maxDirectParams):
     "Specifies how many threads to use at a time.",
     1),
 
+  mLogs("logs",
+    "Enable the specified log. Do \"help logs\" to see all available logs. "
+    "To enable logs X, Y and Z, do \"-logs x,y,z\".",
+    ""),
+
   mMinDirectParams(minDirectParams),
   mMaxDirectParams(maxDirectParams)
 {
@@ -31,11 +39,31 @@ void CommonParams::directOptions(
 void CommonParams::pushBackParameters(
   std::vector<mathic::CliParameter*>& parameters
 ) {
+  parameters.push_back(&mLogs);
   parameters.push_back(&mTracingLevel);
   parameters.push_back(&mThreadCount);
 }
 
 void CommonParams::perform() {
+  size_t offset = 0;
+  const auto& logStr = mLogs.value();
+  auto& logs = LogDomainSet::singleton();
+  while (offset < logStr.size()) {
+    size_t next = logStr.find(',', offset);
+    const auto name = logStr.substr(offset, next - offset);
+    auto log = logs.logDomain(name.c_str());
+    if (log == 0) {
+      std::ostringstream out;
+      out << "Unknown log domain \"" << name <<
+        "\" extracted from -log param \"" << logStr << "\".\n";
+      mathic::reportError(out.str());
+    }
+    log->setEnabled(true);
+    if (next < logStr.size())
+      ++next;
+    offset = next;
+  }
+
   tracingLevel = mTracingLevel.value();
 
   // delete the old init object first to make the new one take control.
diff --git a/src/cli/CommonParams.hpp b/src/cli/CommonParams.hpp
index ea8450d..108a151 100644
--- a/src/cli/CommonParams.hpp
+++ b/src/cli/CommonParams.hpp
@@ -39,6 +39,7 @@ public:
 private:
   mathic::IntegerParameter mTracingLevel;
   mathic::IntegerParameter mThreadCount;
+  mathic::StringParameter mLogs;
 
   std::vector<std::string> mExtensions; // to recognize file type
   std::unique_ptr<tbb::task_scheduler_init> mTbbInit; // to set thread count
diff --git a/src/cli/GBMain.cpp b/src/cli/GBMain.cpp
index 3c10e58..c8403ab 100755
--- a/src/cli/GBMain.cpp
+++ b/src/cli/GBMain.cpp
@@ -3,6 +3,7 @@
 #include "GBAction.hpp"
 #include "SigGBAction.hpp"
 #include "MatrixAction.hpp"
+#include "HelpAction.hpp"
 #include <mathic.h>
 #include <cctype>
 #include <iostream>
@@ -14,7 +15,7 @@ int main(int argc, char **argv) {
     parser.registerAction<SigGBAction>();
     parser.registerAction<GBAction>();
     parser.registerAction<MatrixAction>();
-    parser.registerAction<mathic::HelpAction>();
+    parser.registerAction<HelpAction>();
 
     std::vector<std::string> commandLine(argv, argv + argc);
     commandLine.erase(commandLine.begin());
diff --git a/src/cli/HelpAction.cpp b/src/cli/HelpAction.cpp
new file mode 100755
index 0000000..9c5c2a7
--- /dev/null
+++ b/src/cli/HelpAction.cpp
@@ -0,0 +1,20 @@
+#include "mathicgb/stdinc.h"
+#include "HelpAction.hpp"
+
+#include "mathicgb/LogDomain.hpp"
+#include "mathicgb/LogDomainSet.hpp"
+
+#include <iostream>
+
+void HelpAction::performAction() {
+  if (topic() != "logs") {
+    mathic::HelpAction::performAction();
+    return;
+  }
+
+  auto& logs = LogDomainSet::singleton().logDomains();
+  for (auto it = logs.begin(); it != logs.end(); ++it) {
+    std::cout << "\n " << (*it)->name() << '\n';
+    mathic::display((*it)->description(), "   ");
+  }
+}
diff --git a/src/cli/HelpAction.hpp b/src/cli/HelpAction.hpp
new file mode 100755
index 0000000..5bba19c
--- /dev/null
+++ b/src/cli/HelpAction.hpp
@@ -0,0 +1,11 @@
+#ifndef MATHICGB_HELP_ACTION_GUARD
+#define MATHICGB_HELP_ACTION_GUARD
+
+#include <mathic.h>
+
+class HelpAction : public mathic::HelpAction {
+public:
+  virtual void performAction();
+};
+
+#endif
diff --git a/src/mathicgb/F4MatrixReducer.cpp b/src/mathicgb/F4MatrixReducer.cpp
index d8680ed..41c33a9 100755
--- a/src/mathicgb/F4MatrixReducer.cpp
+++ b/src/mathicgb/F4MatrixReducer.cpp
@@ -4,6 +4,8 @@
 #include "QuadMatrix.hpp"
 #include "SparseMatrix.hpp"
 #include "PolyRing.hpp"
+#include "LogDomain.hpp"
+
 #include <tbb/tbb.h>
 #include <algorithm>
 #include <vector>
@@ -13,6 +15,11 @@
 #include <cstdio>
 #include <iostream>
 
+MATHICGB_DEFINE_LOG_DOMAIN(
+  F4MatrixReduce,
+  "Displays statistics about matrices that are row reduced."
+);
+
 namespace {
   template<class T>
   class DenseRow {
@@ -368,11 +375,10 @@ namespace {
   }
 }
 
-
 SparseMatrix F4MatrixReducer::reduceToBottomRight(const QuadMatrix& matrix) {
   MATHICGB_ASSERT(matrix.debugAssertValid());
-  if (tracingLevel >= 3)
-    matrix.printSizes(std::cerr);
+  if (::logs::F4MatrixReduce.enabled())
+    matrix.printSizes(::logs::F4MatrixReduce.stream());
   return reduce(matrix, mModulus);
 }
 
diff --git a/src/mathicgb/LogDomain.cpp b/src/mathicgb/LogDomain.cpp
new file mode 100755
index 0000000..235623a
--- /dev/null
+++ b/src/mathicgb/LogDomain.cpp
@@ -0,0 +1,21 @@
+#include "stdinc.h"
+#include "LogDomain.hpp"
+
+#include "LogDomainSet.hpp"
+#include <iostream>
+
+LogDomain<true>::LogDomain(
+  const char* const name,
+  const char* const description,
+  const bool enabled
+):
+  mName(name),
+  mDescription(description),
+  mEnabled(enabled)
+{
+  LogDomainSet::singleton().registerLogDomain(*this);
+}
+
+std::ostream& LogDomain<true>::stream() {
+  return std::cerr;
+}
diff --git a/src/mathicgb/LogDomain.hpp b/src/mathicgb/LogDomain.hpp
new file mode 100755
index 0000000..a47fd71
--- /dev/null
+++ b/src/mathicgb/LogDomain.hpp
@@ -0,0 +1,99 @@
+#ifndef MATHICGB_LOG_DOMAIN_GUARD
+#define MATHICGB_LOG_DOMAIN_GUARD
+
+#include <ostream>
+
+template<bool Enabled>
+class LogDomain {};
+
+template<>
+class LogDomain<true> {
+public:
+  static const bool compileTimeEnabled = true;
+
+  LogDomain(
+    const char* const name,
+    const char* const description,
+    const bool enabled
+  );
+
+  const char* name() const {return mName;}
+  const char* description() const {return mDescription;}
+  bool enabled() const {return mEnabled;}
+
+  void setEnabled(const bool enabled) {mEnabled = enabled;}
+
+  std::ostream& stream();
+
+private:
+  const char* mName;
+  const char* mDescription;
+  bool mEnabled;
+};
+
+template<>
+class LogDomain<false> {
+public:
+  static const bool compileTimeEnabled = false;
+
+  LogDomain(const char* const, const char* const, const bool) {}
+
+  bool enabled() const {return false;}
+
+  std::ostream& stream() {
+    MATHICGB_ASSERT(false);
+    return *static_cast<std::ostream*>(0);
+  }
+};
+
+namespace LogDomainInternal {
+  template<class Tag, bool Default>
+  struct SelectValue {static const bool value = Default;};
+
+  template<class> struct Tag_ {};
+  template<class> struct Tag_0 {};
+  template<class> struct Tag_1 {};
+
+  template<bool Default>
+  struct SelectValue<Tag_0<int>, Default> {static const bool value = false;};
+
+  template<bool Default>
+  struct SelectValue<Tag_1<int>, Default> {static const bool value = true;};
+}
+
+/// Defines LogDomainInternal::value_##NAME to be equal to the value of
+/// the macro MATHICGB_LOG_##NAME if that macro expands to 0 or 1. Otherwise
+/// the macro MATHICGB_LOG_##NAME is ignored and instead DEFAULT_VALUE is used.
+#define MATHICGB_CAPTURE_LOG_ENABLED(NAME, DEFAULT_VALUE) \
+  namespace LogDomainInternal { \
+    template<class> struct Tag_MATHICGB_LOG_##NAME {}; \
+    typedef MATHICGB_CONCATENATE_AFTER_EXPANSION(Tag_, MATHICGB_LOG_##NAME)<int> \
+      SelectedTag_##NAME; \
+    static const bool value_##NAME = \
+      SelectValue<SelectedTag_##NAME, DEFAULT_VALUE>::value; \
+  }
+
+/// Defines logs::obj##NAME to be a LogDomain that is compile-time
+/// enabled depending on MATHICGB_LOG_##NAME (see MATHICGB_CAPTURE_LOG_ENABLED)
+/// and that is initially runtime enabled depending on the value of
+/// DEFAULT_RUNTIME_ENABLED.
+#define MATHICGB_DEFINE_LOG_DOMAIN_WITH_DEFAULTS(NAME, DESCRIPTION, DEFAULT_RUNTIME_ENABLED, DEFAULT_COMPILE_TIME_ENABLED) \
+  MATHICGB_CAPTURE_LOG_ENABLED(NAME, DEFAULT_COMPILE_TIME_ENABLED); \
+  namespace logs { \
+    typedef LogDomain<::LogDomainInternal::value_##NAME> Type##NAME; \
+    Type##NAME NAME(#NAME, DESCRIPTION, DEFAULT_RUNTIME_ENABLED); \
+  }
+
+/// Defines logs::##NAME to be a LogDomain. It is compile-time enabled
+/// by default and runtime disabled by default.
+#define MATHICGB_DEFINE_LOG_DOMAIN(NAME, DESCRIPTION) \
+  MATHICGB_DEFINE_LOG_DOMAIN_WITH_DEFAULTS(NAME, DESCRIPTION, 0, 1);
+
+/// Have the code X in the program if and only if logging is not globally
+/// compile-time disabled.
+#define MATHICGB_IF_LOG(X) X
+
+#define MATHICGB_LOG(DOMAIN) \
+  if (::logs::##NAME.enabled()) ::logs::##NAME.stream()
+
+#endif
diff --git a/src/mathicgb/LogDomainSet.hpp b/src/mathicgb/LogDomainSet.hpp
new file mode 100755
index 0000000..20e8665
--- /dev/null
+++ b/src/mathicgb/LogDomainSet.hpp
@@ -0,0 +1,39 @@
+#ifndef MATHICGB_LOG_DOMAIN_SET_GUARD
+#define MATHICGB_LOG_DOMAIN_SET_GUARD
+
+#include "LogDomain.hpp"
+#include <vector>
+#include <algorithm>
+
+class LogDomainSet {
+public:
+  void registerLogDomain(LogDomain<true>& domain) {
+    mLogDomains.push_back(&domain);
+  }
+
+  void registerLogDomain(const LogDomain<false>& domain) {
+  }
+
+  LogDomain<true>* logDomain(const char* const name) {
+    const auto func = [&](const LogDomain<true>* const ld){
+      return std::strcmp(ld->name(), name) == 0;
+    };
+    const auto it = std::find_if(mLogDomains.begin(), mLogDomains.end(), func);
+    return it == mLogDomains.end() ? static_cast<LogDomain<true>*>(0) : *it;
+  }
+
+  const std::vector<LogDomain<true>*> logDomains() const {return mLogDomains;}
+
+  static LogDomainSet& singleton() {
+    static LogDomainSet set;
+    return set;
+  }
+
+private:
+  LogDomainSet() {}
+
+  std::vector<LogDomain<true>*> mLogDomains;
+};
+
+
+#endif
diff --git a/src/mathicgb/stdinc.h b/src/mathicgb/stdinc.h
index d8d2628..4d24cc0 100755
--- a/src/mathicgb/stdinc.h
+++ b/src/mathicgb/stdinc.h
@@ -144,6 +144,18 @@
 #define MATHICGB_SLOW_ASSERT(X)
 #endif
 
+/// Concatenates A to B without expanding A and B. This is achieved since
+/// token pasting (##) defeats macro expansion.
+#define MATHICGB_CONCATENATE(A,B) A##B
+
+/// Concatenates A to B after expanding A and B. This is achieved since
+/// macro parameters are expanded before expanding the macro itself,
+/// so the token pasting inside MATHICGB_CONCATENATE does not defeat
+/// expansion of the parameters. So even though this macro just evaluates
+/// directly to MATHICGB_CONCATENATE(A,B) it does not do the same thing
+/// as that macro does.
+#define MATHICGB_CONCATENATE_AFTER_EXPANSION(A,B) MATHICGB_CONCATENATE(A,B)
+
 #include <utility>
 /*
 See http://herbsutter.com/gotw/_102/ for a reason to have a

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mathicgb.git



More information about the debian-science-commits mailing list