From 227a754b215d348f64f565fef4bb3072e7ca7394 Mon Sep 17 00:00:00 2001 From: jyelon Date: Fri, 17 Dec 2021 16:21:56 -0500 Subject: [PATCH] Add some support for CPU profiling --- luprex/core/cpp/lpxclient.cpp | 12 ++++++++++++ luprex/core/cpp/luaconsole.cpp | 4 ++++ luprex/core/cpp/util.cpp | 15 ++++++++++++++- luprex/core/cpp/util.hpp | 3 +++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/luprex/core/cpp/lpxclient.cpp b/luprex/core/cpp/lpxclient.cpp index 8343d5be..c7b16869 100644 --- a/luprex/core/cpp/lpxclient.cpp +++ b/luprex/core/cpp/lpxclient.cpp @@ -150,6 +150,17 @@ public: rescan_lua_source(); } + void do_work_command(const util::StringVec &words) { + int reps = 10000; + int64_t t1 = util::profiling_clock(); + for (int i = 0; i < reps; i++) { + world_to_synchronous(); + world_to_asynchronous(); + } + int64_t t2 = util::profiling_clock(); + stdostream() << "Snapshot/rollback took " << ((t2-t1)/reps) << " nanosec." << std::endl; + } + void do_quit_command(const util::StringVec &words) { abandon_server(); stop_driver(); @@ -165,6 +176,7 @@ public: else if (words[0] == "choose") do_choose_command(words); else if (words[0] == "tick") do_tick_command(words); else if (words[0] == "cpl") do_cpl_command(words); + else if (words[0] == "work") do_work_command(words); else if (words[0] == "quit") do_quit_command(words); else { stdostream() << "Unsupported command: " << words[0] << std::endl; diff --git a/luprex/core/cpp/luaconsole.cpp b/luprex/core/cpp/luaconsole.cpp index e8f99bbd..b03737df 100644 --- a/luprex/core/cpp/luaconsole.cpp +++ b/luprex/core/cpp/luaconsole.cpp @@ -86,6 +86,10 @@ void LuaConsole::simplify(const StringVec &words) { if (words.size() != 1) { synerr("/cpl takes no arguments"); } + } else if (words[0] == "work") { + if (words.size() != 1) { + synerr("/work takes no arguments"); + } } else { synerr("unrecognized command"); } diff --git a/luprex/core/cpp/util.cpp b/luprex/core/cpp/util.cpp index 69f7ebd4..8629652c 100644 --- a/luprex/core/cpp/util.cpp +++ b/luprex/core/cpp/util.cpp @@ -10,13 +10,26 @@ #include #include +#ifdef WIN32 +#endif + #ifndef WIN32 +#include #include -#define stat _stat #endif namespace util { +int64_t profiling_clock() { +#ifdef WIN32 + return 0.0; +#else + struct timespec ts; + assert(0 == clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)); + return int64_t(ts.tv_nsec) + (int64_t(ts.tv_sec)*int64_t(1000000000)); +#endif +} + static bool ascii_isalpha(char c) { return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')); } diff --git a/luprex/core/cpp/util.hpp b/luprex/core/cpp/util.hpp index ac968505..3ef38b38 100644 --- a/luprex/core/cpp/util.hpp +++ b/luprex/core/cpp/util.hpp @@ -37,6 +37,9 @@ using LuaSourcePtr = std::unique_ptr; using HashValue = std::pair; using IdVector = std::vector; +// Return nanoseconds elapsed, for profiling purposes. +int64_t profiling_clock(); + // Return true if the string is a valid lua identifier. bool is_identifier(const std::string &str);