Changed counters and added setup for new logging. Everything but tests is broken.
authorDmitriy Morozov <morozov@cs.duke.edu>
Thu, 13 Sep 2007 04:42:39 -0400
changeset 27 b0d6c9162de3
parent 23 cb700b407c0d
child 28 b2cb83a28329
Changed counters and added setup for new logging. Everything but tests is broken.
CMakeLists.txt
include/utilities/counter.h
include/utilities/counter.hpp
include/utilities/log.h
tests/utilities/CMakeLists.txt
tests/utilities/test-counters.cpp
--- a/CMakeLists.txt	Thu Aug 16 17:12:52 2007 -0400
+++ b/CMakeLists.txt	Thu Sep 13 04:42:39 2007 -0400
@@ -1,7 +1,8 @@
 project						(Dionysus)
 
+option						(logging 			"Build Dionysus with logging on" 		OFF)
+option						(counters			"Build Dionysus with counters on" 		OFF)
 option						(debug				"Build Dionysus with debugging on" 		OFF)
-option						(counters			"Build Dionysus with counters on" 		OFF)
 option						(optimize			"Build Dionysus with optimization"		ON)
 
 # Find everything that's always required
@@ -45,16 +46,26 @@
 
 # Debugging
 if							(debug)
-	find_library			(cwd_LIBRARY				NAMES cwd)
-	find_path				(cwd_INCLUDE_DIR			libcwd/debug.h)
-	set 					(cwd_INCLUDE_DIR			${cwd_INCLUDE_DIR}/libcwd)
-	add_definitions			(-DCWDEBUG -g)
-	set						(external_sources			${CMAKE_CURRENT_SOURCE_DIR}/src/debug.cpp)
-	set						(libraries 					${libraries} ${cwd_LIBRARY})
+#	find_library			(cwd_LIBRARY				NAMES cwd)
+#	find_path				(cwd_INCLUDE_DIR			libcwd/debug.h)
+#	set 					(cwd_INCLUDE_DIR			${cwd_INCLUDE_DIR}/libcwd)
+#	add_definitions			(-DCWDEBUG -g)
+#	set						(external_sources			${CMAKE_CURRENT_SOURCE_DIR}/src/debug.cpp)
+#	set						(libraries 					${libraries} ${cwd_LIBRARY})
+	add_definitions			(-g)
 else						(debug)
 	add_definitions			(-DNDEBUG)
 endif						(debug)
 
+# Logging
+if 							(logging)
+	find_library			(rlog_LIBRARY				NAMES rlog)
+	find_path				(rlog_INCLUDE_DIR			rlog/rlog.h)
+	set						(rlog_INCLUDE_DIR			${rlog_INCLUDE_DIR}/rlog)
+	add_definitions			(-DLOGGING -DRLOG_COMPONENT=dionysus)
+	set						(libraries					${libraries} ${rlog_LIBRARY})
+endif						(logging)
+
 # Counters
 if							(counters)
 	add_definitions			(-DCOUNTERS)
@@ -74,7 +85,8 @@
 							 ${CMAKE_CURRENT_SOURCE_DIR}/include
 							 ${Boost_INCLUDE_DIR}
 							 ${dsrpdb_INCLUDE_DIR}
-							 ${cwd_INCLUDE_DIR})
+							 ${cwd_INCLUDE_DIR}
+							 ${rlog_INCLUDE_DIR})
 
 # Doxygen (FIXME)
 if							(DOXYGEN_FOUND)
--- a/include/utilities/counter.h	Thu Aug 16 17:12:52 2007 -0400
+++ b/include/utilities/counter.h	Thu Sep 13 04:42:39 2007 -0400
@@ -1,68 +1,64 @@
 /*
  * Author: Dmitriy Morozov
- * Department of Computer Science, Duke University, 2005 -- 2006
+ * Department of Computer Science, Duke University, 2005 -- 2007
  */
 
 #ifndef __COUNTER_H__
 #define __COUNTER_H__
 
-/* This should be integrated with the logging facilities. Written in the same framework. */
+#ifndef COUNTERS
+	#define 	GetCounter(path) 		0
+	#define 	Count(x)
+	#define		SetFrequency(x, freq)
+#else // COUNTERS
 
 #include <map>
 #include <string>
 #include <iostream>
 
-#ifndef COUNTERS
-#define Count(x)
-#define CountNum(x,y)
-#else // COUNTERS
-#define Count(x) counters.inc(x)
-#define CountNum(x,y) counters.inc(x,y)
-#endif
+#define 	GetCounter(path) 			get_counter(path)
+#define 	Count(x) 					do { x->count++; if ((x->count % x->frequency == 0)) x->print(); } while (0);
+#define		SetFrequency(x, freq)		do { x->frequency = freq; } while (0);
+
+#include <limits>
+
 
-typedef 		unsigned long 		CounterType;
-typedef			std::string			KeyType;
-
-class CounterFactory
+class Counter
 {
-	private:
-		typedef				std::map<int, CounterType> 			CountersMap;
-		typedef				std::map<KeyType, CountersMap>		KeyMap;
-		KeyMap				ctrs;
-		
+	public:
+		typedef 				unsigned long 							CounterType;
+		typedef					std::map<std::string, Counter*>			SubCounterMap;
+
 	public:
-		~CounterFactory()
-		{
-#ifdef COUNTERS
-			std::cout << "Counters:" << std::endl;
-			for (KeyMap::const_iterator cur = ctrs.begin(); cur != ctrs.end(); ++cur)
-			{
-				std::cout << cur->first << ": ";
-				if (cur->second.size() == 1)
-				{
-					std::cout << cur->second.begin()->second << std::endl;
-					continue;
-				}
-				std::cout << std::endl;
-				for (CountersMap::const_iterator ccur = cur->second.begin();
-														  ccur != cur->second.end();
-														  ++ccur)
-					std::cout << "  " << ccur->first << ": " << ccur->second << std::endl;
-			}
-#endif // COUNTERS
-		}
+		CounterType				count;
+		CounterType				frequency;
 
-		void inc(const KeyType& key, int num = 0)
-		{
-			ctrs[key][num]++;
-		}
+	public:
+								Counter(const std::string& full_name = "",
+										CounterType freq = std::numeric_limits<CounterType>::max()):
+										count(0), frequency(freq), full_name_(full_name)
+								{}
+								~Counter();
 
-		CounterType lookup(const KeyType& key, int num = 0) const
-		{
-			return const_cast<KeyMap&>(ctrs)[key][num];
-		}
+		Counter*				get_child(const std::string& path, std::string::size_type pos);
+		void					print();
+
+	private:	
+		SubCounterMap			subcounters_;
+		std::string				full_name_;
+
 };
 
-static CounterFactory counters;
+static		Counter				rootCounter;
+
+Counter*	get_counter(const char* path)
+{
+	return rootCounter.get_child(path, 0);
+}
+
+#include "counter.hpp"
+
+#endif // COUNTERS
+
 
 #endif // __COUNTER_H__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/utilities/counter.hpp	Thu Sep 13 04:42:39 2007 -0400
@@ -0,0 +1,41 @@
+Counter*
+Counter::
+get_child(const std::string& path, std::string::size_type pos)
+{
+	if (pos >= path.size())
+		return this;
+
+	std::string::size_type slash_pos = path.find('/', pos);
+	if (slash_pos == std::string::npos)
+		slash_pos = path.size();
+
+	std::string child_name = path.substr(pos, slash_pos - pos);
+	SubCounterMap::iterator child = subcounters_.find(child_name);
+
+	if (child != subcounters_.end())
+		return child->second->get_child(path, slash_pos + 1);
+	else	
+		return (subcounters_[child_name] = new Counter(path.substr(0, slash_pos)))->get_child(path, slash_pos + 1);
+}
+
+Counter::
+~Counter()
+{ 
+	if (full_name_ == "")
+		print(); 
+
+	for (SubCounterMap::iterator cur = subcounters_.begin(); cur != subcounters_.end(); ++cur)
+		delete cur->second;
+}
+
+inline
+void
+Counter::
+print()
+{
+	// FIXME: add (colored) timestamp
+	std::cout << "Counter [" << full_name_ << "]: " << count << std::endl;
+	for (SubCounterMap::iterator cur = subcounters_.begin(); cur != subcounters_.end(); ++cur)
+		cur->second->print();
+}	
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/utilities/log.h	Thu Sep 13 04:42:39 2007 -0400
@@ -0,0 +1,32 @@
+#ifndef __LOG_H__
+#define __LOG_H__
+
+#if LOGGING
+
+#define RLOG_COMPONENT dionysus
+
+#include <rlog/rlog.h>
+#include <rlog/RLogChannel.h>
+#include <rlog/StdioNode.h>
+#include <sstream>
+
+template<class T>
+std::string tostring(const T& t) { std::ostringstream out; out << t; return out.str(); }
+
+#else // LOGGING
+
+#define rDebug(...)
+#define rInfo(...)
+#define rWarning(...)
+#define rError(...)
+#define rLog(...)
+
+#define rAssert(...)
+#define rAssertSilent(...)
+
+#define DEF_CHANNEL(...) 0
+#define RLOG_CHANNEL(...) 0
+
+#endif // LOGGING
+
+#endif //__LOG_H__
--- a/tests/utilities/CMakeLists.txt	Thu Aug 16 17:12:52 2007 -0400
+++ b/tests/utilities/CMakeLists.txt	Thu Sep 13 04:42:39 2007 -0400
@@ -1,6 +1,7 @@
 set							(targets						
 							 test-consistencylist
-							 test-orderlist)
+							 test-orderlist
+							 test-counters)
 
 foreach 					(t ${targets})
 	add_executable			(${t} ${t}.cpp ${external_sources})
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/utilities/test-counters.cpp	Thu Sep 13 04:42:39 2007 -0400
@@ -0,0 +1,24 @@
+#include <utilities/log.h>
+#include <utilities/counter.h>
+
+static Counter* cTestElaborate = GetCounter("test/elaborate");
+static Counter* cTestBasic = GetCounter("test/basic");
+static Counter* cTestBasicSub = GetCounter("test/basic/sub");
+
+int main()
+{
+	SetFrequency(cTestBasic, 2);
+
+	Count(cTestBasic);
+	Count(cTestBasicSub);
+	Count(cTestBasicSub);
+	Count(cTestBasicSub);
+	Count(cTestElaborate);
+	Count(cTestBasic);
+	Count(cTestElaborate);
+	Count(cTestBasic);
+	Count(cTestBasic);
+	
+	SetFrequency(cTestElaborate, 3);
+	Count(cTestElaborate);
+}