OpenTTD Source: src/linkgraph/linkgraphschedule.cpp Source File

OpenTTD Source 14.0-RC3

linkgraphschedule.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #include "../stdafx.h"
11 #include "linkgraphschedule.h"
12 #include "init.h"
13 #include "demands.h"
14 #include "mcf.h"
15 #include "flowmapper.h"
16 #include "../framerate_type.h"
17 #include "../command_func.h"
18 #include "../network/network.h"
19 #include "../misc_cmd.h"
20
21 #include "../safeguards.h"
22
28 /* static */ LinkGraphSchedule LinkGraphSchedule::instance;
29
33 void LinkGraphSchedule::SpawnNext()
34 {
35 if (this->schedule.empty()) return;
36 LinkGraph *next = this->schedule.front();
37 LinkGraph *first = next;
38 while (next->Size() < 2) {
39 this->schedule.splice(this->schedule.end(), this->schedule, this->schedule.begin());
40 next = this->schedule.front();
41 if (next == first) return;
42 }
43 assert(next == LinkGraph::Get(next->index));
44 this->schedule.pop_front();
45 if (LinkGraphJob::CanAllocateItem()) {
46 LinkGraphJob *job = new LinkGraphJob(*next);
47 job->SpawnThread();
48 this->running.push_back(job);
49 } else {
50 NOT_REACHED();
51 }
52 }
53
58 bool LinkGraphSchedule::IsJoinWithUnfinishedJobDue() const
59 {
60 if (this->running.empty()) return false;
61 const LinkGraphJob *next = this->running.front();
62 return next->IsScheduledToBeJoined() && !next->IsJobCompleted();
63 }
64
68 void LinkGraphSchedule::JoinNext()
69 {
70 if (this->running.empty()) return;
71 LinkGraphJob *next = this->running.front();
72 if (!next->IsScheduledToBeJoined()) return;
73 this->running.pop_front();
74 LinkGraphID id = next->LinkGraphIndex();
75 delete next; // implicitly joins the thread
76 if (LinkGraph::IsValidID(id)) {
77 LinkGraph *lg = LinkGraph::Get(id);
78 this->Unqueue(lg); // Unqueue to avoid double-queueing recycled IDs.
79 this->Queue(lg);
80 }
81 }
82
87 /* static */ void LinkGraphSchedule::Run(LinkGraphJob *job)
88 {
89 for (uint i = 0; i < lengthof(instance.handlers); ++i) {
90 if (job->IsJobAborted()) return;
91 instance.handlers[i]->Run(*job);
92 }
93
94 /*
95 * Readers of this variable in another thread may see an out of date value.
96 * However this is OK as this will only happen just as a job is completing,
97 * and the real synchronisation is provided by the thread join operation.
98 * In the worst case the main thread will be paused for longer than
99 * strictly necessary before joining.
100 * This is just a hint variable to avoid performing the join excessively
101 * early and blocking the main thread.
102 */
103
104 job->job_completed.store(true, std::memory_order_release);
105 }
106
111 void LinkGraphSchedule::SpawnAll()
112 {
113 for (auto &it : this->running) {
114 it->SpawnThread();
115 }
116 }
117
121 /* static */ void LinkGraphSchedule::Clear()
122 {
123 for (auto &it : instance.running) {
124 it->AbortJob();
125 }
126 instance.running.clear();
127 instance.schedule.clear();
128 }
129
135 void LinkGraphSchedule::ShiftDates(TimerGameEconomy::Date interval)
136 {
137 for (LinkGraph *lg : LinkGraph::Iterate()) lg->ShiftDates(interval);
138 for (LinkGraphJob *lgj : LinkGraphJob::Iterate()) lgj->ShiftJoinDate(interval);
139 }
140
144 LinkGraphSchedule::LinkGraphSchedule()
145 {
146 this->handlers[0] = new InitHandler;
147 this->handlers[1] = new DemandHandler;
148 this->handlers[2] = new MCFHandler<MCF1stPass>;
149 this->handlers[3] = new FlowMapper(false);
150 this->handlers[4] = new MCFHandler<MCF2ndPass>;
151 this->handlers[5] = new FlowMapper(true);
152 }
153
157 LinkGraphSchedule::~LinkGraphSchedule()
158 {
159 this->Clear();
160 for (uint i = 0; i < lengthof(this->handlers); ++i) {
161 delete this->handlers[i];
162 }
163 }
164
172 void StateGameLoop_LinkGraphPauseControl()
173 {
174 if (_pause_mode & PM_PAUSED_LINK_GRAPH) {
175 /* We are paused waiting on a job, check the job every tick. */
176 if (!LinkGraphSchedule::instance.IsJoinWithUnfinishedJobDue()) {
177 Command<CMD_PAUSE>::Post(PM_PAUSED_LINK_GRAPH, false);
178 }
179 } else if (_pause_mode == PM_UNPAUSED &&
180 TimerGameEconomy::date_fract == LinkGraphSchedule::SPAWN_JOIN_TICK - 2 &&
181 TimerGameEconomy::date.base() % (_settings_game.linkgraph.recalc_interval / EconomyTime::SECONDS_PER_DAY) == (_settings_game.linkgraph.recalc_interval / EconomyTime::SECONDS_PER_DAY) / 2 &&
182 LinkGraphSchedule::instance.IsJoinWithUnfinishedJobDue()) {
183 /* Perform check two TimerGameEconomy::date_fract ticks before we would join, to make
184 * sure it also works in multiplayer. */
185 Command<CMD_PAUSE>::Post(PM_PAUSED_LINK_GRAPH, true);
186 }
187 }
188
194 void AfterLoad_LinkGraphPauseControl()
195 {
196 if (LinkGraphSchedule::instance.IsJoinWithUnfinishedJobDue()) {
197 _pause_mode |= PM_PAUSED_LINK_GRAPH;
198 }
199 }
200
205 void OnTick_LinkGraph()
206 {
207 if (TimerGameEconomy::date_fract != LinkGraphSchedule::SPAWN_JOIN_TICK) return;
208 TimerGameEconomy::Date offset = TimerGameEconomy::date.base() % (_settings_game.linkgraph.recalc_interval / EconomyTime::SECONDS_PER_DAY);
209 if (offset == 0) {
210 LinkGraphSchedule::instance.SpawnNext();
211 } else if (offset == (_settings_game.linkgraph.recalc_interval / EconomyTime::SECONDS_PER_DAY) / 2) {
212 if (!_networking || _network_server) {
213 PerformanceMeasurer::SetInactive(PFE_GL_LINKGRAPH);
214 LinkGraphSchedule::instance.JoinNext();
215 } else {
216 PerformanceMeasurer framerate(PFE_GL_LINKGRAPH);
217 LinkGraphSchedule::instance.JoinNext();
218 }
219 }
220 }
221
222
LinkGraphSchedule::Clear
static void Clear()
Clear all link graphs and jobs from the schedule.
Definition: linkgraphschedule.cpp:121
mcf.h
Pool::PoolItem<&_link_graph_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:339
LinkGraphSchedule::JoinNext
void JoinNext()
Join the next finished job, if available.
Definition: linkgraphschedule.cpp:68
LinkGraph
A connected component of a link graph.
Definition: linkgraph.h:37
DemandHandler
Stateless, thread safe demand handler.
Definition: demands.h:28
LinkGraphSchedule::SPAWN_JOIN_TICK
static const uint SPAWN_JOIN_TICK
Tick when jobs are spawned or joined every day.
Definition: linkgraphschedule.h:51
LinkGraphJob
Class for calculation jobs to be run on link graphs.
Definition: linkgraphjob.h:29
LinkGraphSchedule::running
JobList running
Currently running jobs.
Definition: linkgraphschedule.h:47
_network_server
bool _network_server
network-server is active
Definition: network.cpp:60
LinkGraphSchedule
Definition: linkgraphschedule.h:36
LinkGraphJob::IsJobCompleted
bool IsJobCompleted() const
Check if job has actually finished.
Definition: linkgraphjob.h:193
LinkGraphSchedule::instance
static LinkGraphSchedule instance
Static instance of LinkGraphSchedule.
Definition: linkgraphschedule.h:52
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:238
demands.h
PerformanceMeasurer
RAII class for measuring simple elements of performance.
Definition: framerate_type.h:92
LinkGraphJob::job_completed
std::atomic< bool > job_completed
Is the job still running. This is accessed by multiple threads and reads may be stale.
Definition: linkgraphjob.h:168
TimerGameEconomy::date_fract
static DateFract date_fract
Fractional part of the day.
Definition: timer_game_economy.h:38
PFE_GL_LINKGRAPH
@ PFE_GL_LINKGRAPH
Time spent waiting for link graph background jobs.
Definition: framerate_type.h:56
AfterLoad_LinkGraphPauseControl
void AfterLoad_LinkGraphPauseControl()
Pause the game on load if we would do a join with the next link graph job, but it is still running,...
Definition: linkgraphschedule.cpp:194
LinkGraph::Size
NodeID Size() const
Get the current size of the component.
Definition: linkgraph.h:230
LinkGraphSchedule::SpawnAll
void SpawnAll()
Start all threads in the running list.
Definition: linkgraphschedule.cpp:111
PerformanceMeasurer::SetInactive
static void SetInactive(PerformanceElement elem)
Mark a performance element as not currently in use.
Definition: framerate_gui.cpp:286
LinkGraphJob::LinkGraphIndex
LinkGraphID LinkGraphIndex() const
Get the ID of the underlying link graph.
Definition: linkgraphjob.h:263
OnTick_LinkGraph
void OnTick_LinkGraph()
Spawn or join a link graph job or compress a link graph if any link graph is due to do so.
Definition: linkgraphschedule.cpp:205
LinkGraphSettings::recalc_interval
uint16_t recalc_interval
time (in days) between subsequent checks for link graphs to be calculated.
Definition: settings_type.h:568
PM_UNPAUSED
@ PM_UNPAUSED
A normal unpaused game.
Definition: openttd.h:69
LinkGraphSchedule::Queue
void Queue(LinkGraph *lg)
Queue a link graph for execution.
Definition: linkgraphschedule.h:67
LinkGraphSchedule::ShiftDates
void ShiftDates(TimerGameEconomy::Date interval)
Shift all dates (join dates and edge annotations) of link graphs and link graph jobs by the number of...
Definition: linkgraphschedule.cpp:135
LinkGraphJob::SpawnThread
void SpawnThread()
Spawn a thread if possible and run the link graph job in the thread.
Definition: linkgraphjob.cpp:61
ComponentHandler::Run
virtual void Run(LinkGraphJob &job) const =0
Run the handler.
LinkGraphJob::IsScheduledToBeJoined
bool IsScheduledToBeJoined() const
Check if job is supposed to be finished.
Definition: linkgraphjob.h:214
init.h
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:49
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:55
LinkGraphSchedule::SpawnNext
void SpawnNext()
Start the next job in the schedule.
Definition: linkgraphschedule.cpp:33
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:59
LinkGraphSchedule::LinkGraphSchedule
LinkGraphSchedule()
Create a link graph schedule and initialize its handlers.
Definition: linkgraphschedule.cpp:144
GameSettings::linkgraph
LinkGraphSettings linkgraph
settings for link graph calculations
Definition: settings_type.h:628
LinkGraphSchedule::schedule
GraphList schedule
Queue for new jobs.
Definition: linkgraphschedule.h:46
linkgraphschedule.h
FlowMapper
Map the path trees generated by the MCF solver into flows.
Definition: flowmapper.h:22
Pool::PoolItem<&_link_graph_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:388
LinkGraphJob::IsJobAborted
bool IsJobAborted() const
Check if job has been aborted.
Definition: linkgraphjob.h:200
LinkGraphSchedule::IsJoinWithUnfinishedJobDue
bool IsJoinWithUnfinishedJobDue() const
Check if the next job is supposed to be finished, but has not yet completed.
Definition: linkgraphschedule.cpp:58
MCFHandler
Link graph handler for MCF.
Definition: mcf.h:76
Pool::PoolItem<&_link_graph_job_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:309
PM_PAUSED_LINK_GRAPH
@ PM_PAUSED_LINK_GRAPH
A game paused due to the link graph schedule lagging.
Definition: openttd.h:76
CommandHelper
Definition: command_func.h:93
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:300
LinkGraphSchedule::Run
static void Run(LinkGraphJob *job)
Run all handlers for the given Job.
Definition: linkgraphschedule.cpp:87
TimerGameConst< struct Economy >::SECONDS_PER_DAY
static constexpr int SECONDS_PER_DAY
approximate seconds per day, not for precise calculations
Definition: timer_game_common.h:153
LinkGraphSchedule::handlers
ComponentHandler * handlers[6]
Handlers to be run for each job.
Definition: linkgraphschedule.h:45
LinkGraphSchedule::Unqueue
void Unqueue(LinkGraph *lg)
Remove a link graph from the execution queue.
Definition: linkgraphschedule.h:77
Pool::PoolItem<&_link_graph_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:328
flowmapper.h
LinkGraphSchedule::~LinkGraphSchedule
~LinkGraphSchedule()
Delete a link graph schedule and its handlers.
Definition: linkgraphschedule.cpp:157
InitHandler
Stateless, thread safe initialization handler.
Definition: init.h:12
StateGameLoop_LinkGraphPauseControl
void StateGameLoop_LinkGraphPauseControl()
Pause the game if in 2 TimerGameEconomy::date_fract ticks, we would do a join with the next link grap...
Definition: linkgraphschedule.cpp:172
TimerGameEconomy::date
static Date date
Current date in days (day counter).
Definition: timer_game_economy.h:37