Skip to content

Commit

Permalink
Add: Show memory allocations by GS and AI in framerate window
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsmh committed May 2, 2019
1 parent f44710e commit 43e18d7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
57 changes: 55 additions & 2 deletions src/framerate_gui.cpp
Expand Up @@ -22,6 +22,9 @@
#include "guitimer_func.h"
#include "company_base.h"
#include "ai/ai_info.hpp"
#include "ai/ai_instance.hpp"
#include "game/game.hpp"
#include "game/game_instance.hpp"

#include "widgets/framerate_widget.h"
#include "safeguards.h"
Expand Down Expand Up @@ -365,6 +368,9 @@ static const NWidgetPart _framerate_window_widgets[] = {
NWidget(WWT_EMPTY, COLOUR_GREY, WID_FRW_TIMES_NAMES), SetScrollbar(WID_FRW_SCROLLBAR),
NWidget(WWT_EMPTY, COLOUR_GREY, WID_FRW_TIMES_CURRENT), SetScrollbar(WID_FRW_SCROLLBAR),
NWidget(WWT_EMPTY, COLOUR_GREY, WID_FRW_TIMES_AVERAGE), SetScrollbar(WID_FRW_SCROLLBAR),
NWidget(NWID_SELECTION, INVALID_COLOUR, WID_FRW_SEL_MEMORY),
NWidget(WWT_EMPTY, COLOUR_GREY, WID_FRW_ALLOCSIZE), SetScrollbar(WID_FRW_SCROLLBAR),
EndContainer(),
EndContainer(),
NWidget(WWT_TEXT, COLOUR_GREY, WID_FRW_INFO_DATA_POINTS), SetDataTip(STR_FRAMERATE_DATA_POINTS, 0x0),
EndContainer(),
Expand All @@ -378,6 +384,7 @@ static const NWidgetPart _framerate_window_widgets[] = {

struct FramerateWindow : Window {
bool small;
bool showing_memory;
GUITimer next_update;
int num_active;
int num_displayed;
Expand Down Expand Up @@ -424,6 +431,7 @@ struct FramerateWindow : Window {
{
this->InitNested(number);
this->small = this->IsShaded();
this->showing_memory = true;
this->UpdateData();
this->num_displayed = this->num_active;
this->next_update.SetInterval(100);
Expand Down Expand Up @@ -453,6 +461,7 @@ struct FramerateWindow : Window {
void UpdateData()
{
double gl_rate = _pf_data[PFE_GAMELOOP].GetRate();
bool have_script = false;
this->rate_gameloop.SetRate(gl_rate, _pf_data[PFE_GAMELOOP].expected_rate);
this->speed_gameloop.SetRate(gl_rate / _pf_data[PFE_GAMELOOP].expected_rate, 1.0);
if (this->small) return; // in small mode, this is everything needed
Expand All @@ -463,7 +472,16 @@ struct FramerateWindow : Window {
for (PerformanceElement e = PFE_FIRST; e < PFE_MAX; e++) {
this->times_shortterm[e].SetTime(_pf_data[e].GetAverageDurationMilliseconds(8), MILLISECONDS_PER_TICK);
this->times_longterm[e].SetTime(_pf_data[e].GetAverageDurationMilliseconds(NUM_FRAMERATE_POINTS), MILLISECONDS_PER_TICK);
if (_pf_data[e].num_valid > 0) new_active++;
if (_pf_data[e].num_valid > 0) {
new_active++;
if (e == PFE_GAMESCRIPT || e >= PFE_AI0) have_script = true;
}
}

if (this->showing_memory != have_script) {
NWidgetStacked *plane = this->GetWidget<NWidgetStacked>(WID_FRW_SEL_MEMORY);
plane->SetDisplayedPlane(have_script ? 0 : SZSP_VERTICAL);
this->showing_memory = have_script;
}

if (new_active != this->num_active) {
Expand Down Expand Up @@ -545,7 +563,8 @@ struct FramerateWindow : Window {
}

case WID_FRW_TIMES_CURRENT:
case WID_FRW_TIMES_AVERAGE: {
case WID_FRW_TIMES_AVERAGE:
case WID_FRW_ALLOCSIZE: {
*size = GetStringBoundingBox(STR_FRAMERATE_CURRENT + (widget - WID_FRW_TIMES_CURRENT));
SetDParam(0, 999999);
SetDParam(1, 2);
Expand Down Expand Up @@ -582,6 +601,37 @@ struct FramerateWindow : Window {
}
}

void DrawElementAllocationsColumn(const Rect &r) const
{
const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR);
uint16 skip = sb->GetPosition();
int drawable = this->num_displayed;
int y = r.top;
DrawString(r.left, r.right, y, STR_FRAMERATE_MEMORYUSE, TC_FROMSTRING, SA_CENTER, true);
y += FONT_HEIGHT_NORMAL + VSPACING;
for (PerformanceElement e : DISPLAY_ORDER_PFE) {
if (_pf_data[e].num_valid == 0) continue;
if (skip > 0) {
skip--;
} else if (e == PFE_GAMESCRIPT || e >= PFE_AI0) {
if (e == PFE_GAMESCRIPT) {
SetDParam(0, Game::GetInstance()->GetAllocatedMemory());
} else {
SetDParam(0, Company::Get(e - PFE_AI0)->ai_instance->GetAllocatedMemory());
}
DrawString(r.left, r.right, y, STR_FRAMERATE_BYTES_GOOD, TC_FROMSTRING, SA_RIGHT);
y += FONT_HEIGHT_NORMAL;
drawable--;
if (drawable == 0) break;
} else {
/* skip non-script */
y += FONT_HEIGHT_NORMAL;
drawable--;
if (drawable == 0) break;
}
}
}

void DrawWidget(const Rect &r, int widget) const override
{
switch (widget) {
Expand Down Expand Up @@ -618,6 +668,9 @@ struct FramerateWindow : Window {
/* Render averages of all recorded values */
DrawElementTimesColumn(r, STR_FRAMERATE_AVERAGE, this->times_longterm);
break;
case WID_FRW_ALLOCSIZE:
DrawElementAllocationsColumn(r);
break;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/lang/english.txt
Expand Up @@ -2742,13 +2742,17 @@ STR_FRAMERATE_SPEED_FACTOR :{BLACK}Current
STR_FRAMERATE_SPEED_FACTOR_TOOLTIP :{BLACK}How fast the game is currently running, compared to the expected speed at normal simulation rate.
STR_FRAMERATE_CURRENT :{WHITE}Current
STR_FRAMERATE_AVERAGE :{WHITE}Average
STR_FRAMERATE_MEMORYUSE :{WHITE}Memory
STR_FRAMERATE_DATA_POINTS :{BLACK}Data based on {COMMA} measurements
STR_FRAMERATE_MS_GOOD :{LTBLUE}{DECIMAL} ms
STR_FRAMERATE_MS_WARN :{YELLOW}{DECIMAL} ms
STR_FRAMERATE_MS_BAD :{RED}{DECIMAL} ms
STR_FRAMERATE_FPS_GOOD :{LTBLUE}{DECIMAL} frames/s
STR_FRAMERATE_FPS_WARN :{YELLOW}{DECIMAL} frames/s
STR_FRAMERATE_FPS_BAD :{RED}{DECIMAL} frames/s
STR_FRAMERATE_BYTES_GOOD :{LTBLUE}{BYTES}
STR_FRAMERATE_BYTES_WARN :{YELLOW}{BYTES}
STR_FRAMERATE_BYTES_BAD :{RED}{BYTES}
STR_FRAMERATE_GRAPH_MILLISECONDS :{TINY_FONT}{COMMA} ms
STR_FRAMERATE_GRAPH_SECONDS :{TINY_FONT}{COMMA} s
############ Leave those lines in this order!!
Expand Down
2 changes: 2 additions & 0 deletions src/script/api/game/game_window.hpp.sq
Expand Up @@ -468,6 +468,8 @@ void SQGSWindow_Register(Squirrel *engine)
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FRW_TIMES_NAMES, "WID_FRW_TIMES_NAMES");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FRW_TIMES_CURRENT, "WID_FRW_TIMES_CURRENT");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FRW_TIMES_AVERAGE, "WID_FRW_TIMES_AVERAGE");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FRW_ALLOCSIZE, "WID_FRW_ALLOCSIZE");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FRW_SEL_MEMORY, "WID_FRW_SEL_MEMORY");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FRW_SCROLLBAR, "WID_FRW_SCROLLBAR");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FGW_CAPTION, "WID_FGW_CAPTION");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FGW_GRAPH, "WID_FGW_GRAPH");
Expand Down
2 changes: 2 additions & 0 deletions src/script/api/script_window.hpp
Expand Up @@ -1309,6 +1309,8 @@ class ScriptWindow : public ScriptObject {
WID_FRW_TIMES_NAMES = ::WID_FRW_TIMES_NAMES,
WID_FRW_TIMES_CURRENT = ::WID_FRW_TIMES_CURRENT,
WID_FRW_TIMES_AVERAGE = ::WID_FRW_TIMES_AVERAGE,
WID_FRW_ALLOCSIZE = ::WID_FRW_ALLOCSIZE,
WID_FRW_SEL_MEMORY = ::WID_FRW_SEL_MEMORY,
WID_FRW_SCROLLBAR = ::WID_FRW_SCROLLBAR,
};

Expand Down
2 changes: 2 additions & 0 deletions src/widgets/framerate_widget.h
Expand Up @@ -22,6 +22,8 @@ enum FramerateWindowWidgets {
WID_FRW_TIMES_NAMES,
WID_FRW_TIMES_CURRENT,
WID_FRW_TIMES_AVERAGE,
WID_FRW_ALLOCSIZE,
WID_FRW_SEL_MEMORY,
WID_FRW_SCROLLBAR,
};

Expand Down

0 comments on commit 43e18d7

Please sign in to comment.