aboutsummaryrefslogtreecommitdiff
path: root/src/util.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.h')
-rw-r--r--src/util.h119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/util.h b/src/util.h
new file mode 100644
index 0000000..42c0135
--- /dev/null
+++ b/src/util.h
@@ -0,0 +1,119 @@
+#ifndef _HOTSTUFF_UTIL_H
+#define _HOTSTUFF_UTIL_H
+
+#include "salticidae/util.h"
+
+namespace hotstuff {
+
+class Logger: public salticidae::Logger {
+ public:
+ using salticidae::Logger::Logger;
+};
+
+extern Logger logger;
+
+#ifdef HOTSTUFF_DEBUG_LOG
+#define HOTSTUFF_NORMAL_LOG
+#define HOTSTUFF_ENABLE_LOG_DEBUG
+#endif
+
+#ifdef HOTSTUFF_NORMAL_LOG
+#define HOTSTUFF_ENABLE_LOG_INFO
+#define HOTSTUFF_ENABLE_LOG_WARN
+#endif
+
+#ifdef HOTSTUFF_ENABLE_LOG_INFO
+#define HOTSTUFF_LOG_INFO(...) hotstuff::logger.info(__VA_ARGS__)
+#else
+#define HOTSTUFF_LOG_INFO(...) ((void)0)
+#endif
+
+#ifdef HOTSTUFF_ENABLE_LOG_DEBUG
+#define HOTSTUFF_LOG_DEBUG(...) hotstuff::logger.debug(__VA_ARGS__)
+#else
+#define HOTSTUFF_LOG_DEBUG(...) ((void)0)
+#endif
+
+#ifdef HOTSTUFF_ENABLE_LOG_WARN
+#define HOTSTUFF_LOG_WARN(...) hotstuff::logger.warning(__VA_ARGS__)
+#else
+#define HOTSTUFF_LOG_WARN(...) ((void)0)
+#endif
+
+#define HOTSTUFF_LOG_ERROR(...) hotstuff::logger.error(__VA_ARGS__)
+
+#ifdef HOTSTUFF_ENABLE_BLK_PROFILE
+class BlockProfiler {
+ enum BlockState {
+ BLK_SEEN,
+ BLK_FETCH,
+ BLK_CC
+ };
+
+ struct BlockProfile {
+ bool is_local; /* is the block proposed by the replica itself? */
+ BlockState state;
+ double hash_seen_time; /* the first time to see block hash */
+ double full_fetch_time; /* the first time to get full block content */
+ double cc_time; /* the time when it receives cc */
+ double commit_time; /* the time when it commits */
+ };
+
+ std::unordered_map<const uint256, BlockProfile> blocks;
+ ElapsedTime timer;
+
+ public:
+ BlockProfiler() { timer.start(); }
+
+ auto rec_blk(const uint256 &blk_hash, bool is_local) {
+ auto it = blocks.find(blk_hash);
+ assert(it == blocks.end());
+ timer.stop(false);
+ return blocks.insert(std::make_pair(blk_hash,
+ BlockProfile{is_local, BLK_SEEN, timer.elapsed_sec, 0, 0, 0})).first;
+ }
+
+ void get_blk(const uint256 &blk_hash) {
+ auto it = blocks.find(blk_hash);
+ if (it == blocks.end())
+ it = rec_blk(blk_hash, false);
+ BlockProfile &blkp = it->second;
+ assert(blkp.state == BLK_SEEN);
+ timer.stop(false);
+ blkp.full_fetch_time = timer.elapsed_sec;
+ blkp.state = BLK_FETCH;
+ }
+
+ void have_cc(const uint256 &blk_hash) {
+ auto it = blocks.find(blk_hash);
+ assert(it != blocks.end());
+ BlockProfile &blkp = it->second;
+ assert(blkp.state == BLK_FETCH);
+ timer.stop(false);
+ blkp.polling_start_time = timer.elapsed_sec;
+ blkp.state = BLK_CC;
+ }
+
+ const char *decide_blk(const uint256 &blk_hash) {
+ static char buff[1024];
+ auto it = blocks.find(blk_hash);
+ assert(it != blocks.end());
+ BlockProfile &blkp = it->second;
+ assert(blkp.state == BLK_CC);
+ timer.stop(false);
+ blkp.commit_time = timer.elapsed_sec;
+ snprintf(buff, sizeof buff, "(%d,%.4f,%.4f,%.4f,%.4f,%.4f)",
+ blkp.is_local,
+ blkp.hash_seen_time, blkp.full_fetch_time,
+ blkp.polling_start_time, blkp.polling_end_time,
+ blkp.commit_time);
+ blocks.erase(it);
+ return buff;
+ }
+};
+
+#endif
+
+}
+
+#endif