Add some support for CPU profiling

This commit is contained in:
2021-12-17 16:21:56 -05:00
parent cfa6c28d34
commit 227a754b21
4 changed files with 33 additions and 1 deletions

View File

@@ -150,6 +150,17 @@ public:
rescan_lua_source(); 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) { void do_quit_command(const util::StringVec &words) {
abandon_server(); abandon_server();
stop_driver(); stop_driver();
@@ -165,6 +176,7 @@ public:
else if (words[0] == "choose") do_choose_command(words); else if (words[0] == "choose") do_choose_command(words);
else if (words[0] == "tick") do_tick_command(words); else if (words[0] == "tick") do_tick_command(words);
else if (words[0] == "cpl") do_cpl_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 if (words[0] == "quit") do_quit_command(words);
else { else {
stdostream() << "Unsupported command: " << words[0] << std::endl; stdostream() << "Unsupported command: " << words[0] << std::endl;

View File

@@ -86,6 +86,10 @@ void LuaConsole::simplify(const StringVec &words) {
if (words.size() != 1) { if (words.size() != 1) {
synerr("/cpl takes no arguments"); synerr("/cpl takes no arguments");
} }
} else if (words[0] == "work") {
if (words.size() != 1) {
synerr("/work takes no arguments");
}
} else { } else {
synerr("unrecognized command"); synerr("unrecognized command");
} }

View File

@@ -10,13 +10,26 @@
#include <cassert> #include <cassert>
#include <algorithm> #include <algorithm>
#ifdef WIN32
#endif
#ifndef WIN32 #ifndef WIN32
#include <time.h>
#include <unistd.h> #include <unistd.h>
#define stat _stat
#endif #endif
namespace util { 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) { static bool ascii_isalpha(char c) {
return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')); return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
} }

View File

@@ -37,6 +37,9 @@ using LuaSourcePtr = std::unique_ptr<LuaSourceVec>;
using HashValue = std::pair<uint64_t, uint64_t>; using HashValue = std::pair<uint64_t, uint64_t>;
using IdVector = std::vector<int64_t>; using IdVector = std::vector<int64_t>;
// Return nanoseconds elapsed, for profiling purposes.
int64_t profiling_clock();
// Return true if the string is a valid lua identifier. // Return true if the string is a valid lua identifier.
bool is_identifier(const std::string &str); bool is_identifier(const std::string &str);