Skip to content

Commit

Permalink
Merge pull request #5238 from myk002/myk_unpaused_ms
Browse files Browse the repository at this point in the history
add Core API for getting unpaused ms
  • Loading branch information
myk002 authored Feb 3, 2025
2 parents ab791e8 + 38e91d5 commit a556886
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Template for new versions:
## Documentation

## API
- ``Core::getUnpausedMs``: new API for getting unpaused ms since load in a fort-mode game

## Lua

Expand Down
14 changes: 9 additions & 5 deletions library/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,20 @@ bool PerfCounters::getIgnorePauseState() {
return ignore_pause_state;
}

void PerfCounters::registerTick(uint32_t baseline_ms) {
uint32_t PerfCounters::registerTick(uint32_t baseline_ms) {
if (!World::isFortressMode() || World::ReadPauseState()) {
last_tick_baseline_ms = 0;
return;
return 0;
}

// only update when the tick counter has advanced
if (!world || last_frame_counter == world->frame_counter)
return;
return 0;
last_frame_counter = world->frame_counter;

if (last_tick_baseline_ms == 0) {
last_tick_baseline_ms = baseline_ms;
return;
return 0;
}

uint32_t elapsed_ms = baseline_ms - last_tick_baseline_ms;
Expand All @@ -173,6 +173,8 @@ void PerfCounters::registerTick(uint32_t baseline_ms) {

recent_ticks.history[recent_ticks.head_idx] = elapsed_ms;
recent_ticks.sum_ms += elapsed_ms;

return elapsed_ms;
}

uint32_t PerfCounters::getUnpausedFps() {
Expand Down Expand Up @@ -1705,6 +1707,7 @@ bool Core::InitMainThread() {
}

perf_counters.reset();
unpaused_ms = 0;

return true;
}
Expand Down Expand Up @@ -2141,7 +2144,7 @@ int Core::Update()
}

uint32_t start_ms = p->getTickCount();
perf_counters.registerTick(start_ms);
unpaused_ms += perf_counters.registerTick(start_ms);
doUpdate(out);
perf_counters.incCounter(perf_counters.total_update_ms, start_ms);
}
Expand Down Expand Up @@ -2337,6 +2340,7 @@ void Core::onStateChange(color_ostream &out, state_change_event event)
case SC_WORLD_LOADED:
{
perf_counters.reset();
unpaused_ms = 0;
Persistence::Internal::load(out);
plug_mgr->doLoadWorldData(out);
loadModScriptPaths(out);
Expand Down
9 changes: 7 additions & 2 deletions library/include/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ namespace DFHack
bool getIgnorePauseState();

// noop if game is paused and getIgnorePauseState() returns false
void incCounter(uint32_t &perf_counter, uint32_t baseline_ms);
void incCounter(uint32_t &counter, uint32_t baseline_ms);

// returns number of unpaused ms since last tick
uint32_t registerTick(uint32_t baseline_ms);

void registerTick(uint32_t baseline_ms);
uint32_t getUnpausedFps();

private:
Expand Down Expand Up @@ -219,6 +221,7 @@ namespace DFHack
static void cheap_tokenise(std::string const& input, std::vector<std::string> &output);

PerfCounters perf_counters;
uint32_t getUnpausedMs() { return unpaused_ms; }

lua_State* getLuaState(bool bypass_assertion = false) {
assert(bypass_assertion || isSuspended());
Expand Down Expand Up @@ -334,6 +337,8 @@ namespace DFHack

lua_State* State;

uint32_t unpaused_ms; // reset to 0 on map load

friend class CoreService;
friend class ServerConnection;
friend class CoreSuspender;
Expand Down

0 comments on commit a556886

Please sign in to comment.