Skip to content

Commit

Permalink
Merge branch 'main' into release/1.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
svengcz committed Apr 29, 2023
2 parents 43ce8b6 + d583367 commit 73beceb
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ SET(SOURCE_FILES
config/FileFormat.h
com/Airport.cpp
com/Airport.h
com/PerformanceLock.h
com/Server.cpp
com/Server.h
logging/Logger.cpp
logging/Logger.h
logging/Performance.h
logging/sqlite3.c
logging/sqlite3.h
logging/sqlite3ext.h
Expand Down
41 changes: 34 additions & 7 deletions com/Airport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@ static constexpr std::size_t FlightServer = 2;
Airport::Airport() :
m_airport(),
m_worker(),
m_lock(),
m_pause(false),
m_lock("EMPTY"),
m_flights(),
m_stop(false) { }
m_stop(false),
m_manualUpdatePerformance("EMPTY"),
m_workerAllFlightsPerformance("EMPTY"),
m_workerUpdateFlightsPerformance("EMPTY") { }

Airport::Airport(const std::string& airport) :
m_airport(airport),
m_worker(),
m_lock(),
m_pause(false),
m_lock(airport),
m_flights(),
m_stop(false) {
m_stop(false),
m_manualUpdatePerformance("ManualUpdates" + airport),
m_workerAllFlightsPerformance("AllFlightsRequest" + airport),
m_workerUpdateFlightsPerformance("UpdateFlights" + airport) {
const auto flights = Server::instance().allFlights(this->m_airport);
for (const auto& flight : std::as_const(flights)) {
this->m_flights.insert({ flight.callsign, { flight, types::Flight_t(), flight } });
Expand Down Expand Up @@ -84,6 +92,7 @@ void Airport::flightDisconnected(const std::string& callsign) {

std::lock_guard guard(this->m_lock);

this->m_manualUpdatePerformance.start();
auto it = this->m_flights.find(callsign);
if (it != this->m_flights.end() && it->second[FlightServer].callsign == callsign) {
Json::Value root;
Expand All @@ -95,6 +104,7 @@ void Airport::flightDisconnected(const std::string& callsign) {

it->second[FlightEuroscope].inactive = true;
}
this->m_manualUpdatePerformance.stop();
}

std::string Airport::timestampToIsoString(const std::chrono::utc_clock::time_point& timepoint) {
Expand All @@ -116,6 +126,7 @@ void Airport::updateExot(const std::string& callsign, const std::chrono::utc_clo

std::lock_guard guard(this->m_lock);

this->m_manualUpdatePerformance.start();
auto it = this->m_flights.find(callsign);
if (it != this->m_flights.end() && it->second[FlightServer].callsign == callsign) {
Json::Value root;
Expand All @@ -140,6 +151,8 @@ void Airport::updateExot(const std::string& callsign, const std::chrono::utc_clo
logging::Logger::instance().log("Airport", logging::Logger::Level::Debug, "Updating EXOT: " + callsign + ", " + root["vacdm"]["exot"].asString());
Server::instance().patchFlight(callsign, root);
}

this->m_manualUpdatePerformance.stop();
}

void Airport::updateTobt(const std::string& callsign, const std::chrono::utc_clock::time_point& tobt, bool manualTobt) {
Expand All @@ -148,6 +161,7 @@ void Airport::updateTobt(const std::string& callsign, const std::chrono::utc_clo

std::lock_guard guard(this->m_lock);

this->m_manualUpdatePerformance.start();
auto it = this->m_flights.find(callsign);
if (it != this->m_flights.end() && it->second[FlightServer].callsign == callsign) {
bool resetTsat = (tobt == types::defaultTime && true == manualTobt) || tobt >= it->second[FlightConsolidated].tsat;
Expand Down Expand Up @@ -178,6 +192,7 @@ void Airport::updateTobt(const std::string& callsign, const std::chrono::utc_clo
logging::Logger::instance().log("Airport", logging::Logger::Level::Debug, "Updating TOBT: " + callsign + ", " + root["vacdm"]["tobt"].asString());
Server::instance().patchFlight(callsign, root);
}
this->m_manualUpdatePerformance.stop();
}

void Airport::updateAsat(const std::string& callsign, const std::chrono::utc_clock::time_point& asat) {
Expand All @@ -186,6 +201,7 @@ void Airport::updateAsat(const std::string& callsign, const std::chrono::utc_clo

std::lock_guard guard(this->m_lock);

this->m_manualUpdatePerformance.start();
auto it = this->m_flights.find(callsign);
if (it != this->m_flights.end() && it->second[FlightServer].callsign == callsign) {
Json::Value root;
Expand All @@ -200,6 +216,7 @@ void Airport::updateAsat(const std::string& callsign, const std::chrono::utc_clo
logging::Logger::instance().log("Airport", logging::Logger::Level::Debug, "Updating ASAT: " + callsign + ", " + root["vacdm"]["asat"].asString());
Server::instance().patchFlight(callsign, root);
}
this->m_manualUpdatePerformance.stop();
}

void Airport::updateAobt(const std::string& callsign, const std::chrono::utc_clock::time_point& aobt) {
Expand All @@ -208,6 +225,7 @@ void Airport::updateAobt(const std::string& callsign, const std::chrono::utc_clo

std::lock_guard guard(this->m_lock);

this->m_manualUpdatePerformance.start();
auto it = this->m_flights.find(callsign);
if (it != this->m_flights.end() && it->second[FlightServer].callsign == callsign) {
Json::Value root;
Expand All @@ -222,6 +240,7 @@ void Airport::updateAobt(const std::string& callsign, const std::chrono::utc_clo
logging::Logger::instance().log("Airport", logging::Logger::Level::Debug, "Updating AOBT: " + callsign + ", " + root["vacdm"]["aobt"].asString());
Server::instance().patchFlight(callsign, root);
}
this->m_manualUpdatePerformance.stop();
}

void Airport::updateAtot(const std::string& callsign, const std::chrono::utc_clock::time_point& atot) {
Expand All @@ -230,6 +249,7 @@ void Airport::updateAtot(const std::string& callsign, const std::chrono::utc_clo

std::lock_guard guard(this->m_lock);

this->m_manualUpdatePerformance.start();
auto it = this->m_flights.find(callsign);
if (it != this->m_flights.end() && it->second[FlightServer].callsign == callsign) {
Json::Value root;
Expand All @@ -244,6 +264,7 @@ void Airport::updateAtot(const std::string& callsign, const std::chrono::utc_clo
logging::Logger::instance().log("Airport", logging::Logger::Level::Debug, "Updating ATOT: " + callsign + ", " + root["vacdm"]["atot"].asString());
Server::instance().patchFlight(callsign, root);
}
this->m_manualUpdatePerformance.stop();
}

void Airport::updateAsrt(const std::string& callsign, const std::chrono::utc_clock::time_point& asrt) {
Expand All @@ -252,6 +273,7 @@ void Airport::updateAsrt(const std::string& callsign, const std::chrono::utc_clo

std::lock_guard guard(this->m_lock);

this->m_manualUpdatePerformance.start();
auto it = this->m_flights.find(callsign);
if (it != this->m_flights.end() && it->second[FlightServer].callsign == callsign) {
Json::Value root;
Expand All @@ -266,6 +288,7 @@ void Airport::updateAsrt(const std::string& callsign, const std::chrono::utc_clo
logging::Logger::instance().log("Airport", logging::Logger::Level::Debug, "Updating ASRT: " + callsign + ", " + root["vacdm"]["asrt"].asString());
Server::instance().patchFlight(callsign, root);
}
this->m_manualUpdatePerformance.stop();
}

void Airport::updateAort(const std::string& callsign, const std::chrono::utc_clock::time_point& aort) {
Expand All @@ -274,6 +297,7 @@ void Airport::updateAort(const std::string& callsign, const std::chrono::utc_clo

std::lock_guard guard(this->m_lock);

this->m_manualUpdatePerformance.start();
auto it = this->m_flights.find(callsign);
if (it != this->m_flights.end() && it->second[FlightServer].callsign == callsign) {
Json::Value root;
Expand All @@ -288,6 +312,7 @@ void Airport::updateAort(const std::string& callsign, const std::chrono::utc_clo
logging::Logger::instance().log("Airport", logging::Logger::Level::Debug, "Updating AORT: " + callsign + ", " + root["vacdm"]["aort"].asString());
Server::instance().patchFlight(callsign, root);
}
this->m_manualUpdatePerformance.stop();
}

Airport::SendType Airport::deltaEuroscopeToBackend(const std::array<types::Flight_t, 3>& data, Json::Value& root) {
Expand Down Expand Up @@ -379,7 +404,7 @@ Airport::SendType Airport::deltaEuroscopeToBackend(const std::array<types::Fligh
if (lastDelta == deltaCount)
root.removeMember("clearance");

return Airport::SendType::Patch;
return deltaCount != 0 ? Airport::SendType::Patch : Airport::SendType::None;
}
}

Expand Down Expand Up @@ -438,9 +463,11 @@ void Airport::run() {
if (true == this->m_pause)
continue;

this->m_workerAllFlightsPerformance.start();
logging::Logger::instance().log("Airport", logging::Logger::Level::Debug, "New server update cycle");
auto flights = com::Server::instance().allFlights(this->m_airport);
std::list<std::tuple<std::string, Airport::SendType, Json::Value>> transmissionBuffer;
this->m_workerAllFlightsPerformance.stop();

this->m_lock.lock();
// check which updates are needed and update consolidated views based on the server
Expand Down Expand Up @@ -477,8 +504,6 @@ void Airport::run() {
const auto sendType = Airport::deltaEuroscopeToBackend(it->second, root);
if (Airport::SendType::None != sendType)
transmissionBuffer.push_back({ it->first, sendType, root });
else
logging::Logger::instance().log("vACDM", logging::Logger::Level::Debug, "Don't update " + it->first);

if (true == removeFlight) {
logging::Logger::instance().log("vACDM", logging::Logger::Level::Debug, "Deleting " + it->first);
Expand All @@ -492,12 +517,14 @@ void Airport::run() {
this->m_lock.unlock();

// send the deltas
this->m_workerUpdateFlightsPerformance.start();
for (const auto& transmission : std::as_const(transmissionBuffer)) {
if (std::get<1>(transmission) == Airport::SendType::Post)
Server::instance().postFlight(std::get<2>(transmission));
else
Server::instance().patchFlight(std::get<0>(transmission), std::get<2>(transmission));
}
this->m_workerUpdateFlightsPerformance.stop();
}
}

Expand Down
7 changes: 6 additions & 1 deletion com/Airport.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <types/Flight.h>

#include "PerformanceLock.h"

namespace vacdm {
namespace com {

Expand All @@ -24,9 +26,12 @@ class Airport {
std::string m_airport;
std::thread m_worker;
volatile bool m_pause;
std::mutex m_lock;
PerformanceLock m_lock;
std::map<std::string, std::array<types::Flight_t, 3>> m_flights;
volatile bool m_stop;
logging::Performance m_manualUpdatePerformance;
logging::Performance m_workerAllFlightsPerformance;
logging::Performance m_workerUpdateFlightsPerformance;

static std::string timestampToIsoString(const std::chrono::utc_clock::time_point& timepoint);
static SendType deltaEuroscopeToBackend(const std::array<types::Flight_t, 3>& data, Json::Value& root);
Expand Down
35 changes: 35 additions & 0 deletions com/PerformanceLock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <mutex>

#include <logging/Performance.h>

namespace vacdm {
namespace com {
class PerformanceLock {
private:
vacdm::logging::Performance m_acquireTime;
vacdm::logging::Performance m_criticalSectionTime;

std::mutex m_lock;
public:
PerformanceLock(const std::string& component) :
m_acquireTime("LockAcquire" + component),
m_criticalSectionTime("CriticalSectionAcquire" + component),
m_lock() {}

void lock() {
this->m_acquireTime.start();
this->m_lock.lock();
this->m_acquireTime.stop();

this->m_criticalSectionTime.start();
}

void unlock() {
this->m_lock.unlock();
this->m_criticalSectionTime.stop();
}
};
}
}
67 changes: 67 additions & 0 deletions logging/Performance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma once

#include <algorithm>
#include <chrono>
#include <cstdint>
#include <string>

#include "Logger.h"

#define PERFORMANCE_MEASUREMENT 1

namespace vacdm {
namespace logging {
class Performance {
private:
std::string m_name;
std::uint64_t m_average;
std::int64_t m_minimum;
std::int64_t m_maximum;
std::uint32_t m_cycleCount;
std::chrono::high_resolution_clock::time_point m_startTime;
std::chrono::high_resolution_clock::time_point m_lastLogStamp;

public:
Performance(const std::string& name) :
m_name(name),
m_average(0),
m_minimum(std::numeric_limits<std::uint32_t>::max()),
m_maximum(std::numeric_limits<std::uint32_t>::min()),
m_cycleCount(0),
m_startTime(),
m_lastLogStamp(std::chrono::high_resolution_clock::now()) {}

void start() {
#if PERFORMANCE_MEASUREMENT
this->m_startTime = std::chrono::high_resolution_clock::now();
#endif
}

void stop() {
#if PERFORMANCE_MEASUREMENT
const auto now = std::chrono::high_resolution_clock::now();
const auto delta = std::chrono::duration_cast<std::chrono::microseconds>(now - this->m_startTime).count();

this->m_minimum = std::min(this->m_minimum, delta);
this->m_maximum = std::max(this->m_maximum, delta);
if (this->m_cycleCount == 0)
this->m_average = delta;
else
this->m_average = static_cast<std::uint32_t>((this->m_average + delta) * 0.5);

if (std::chrono::duration_cast<std::chrono::minutes>(now - this->m_lastLogStamp).count() >= 1) {
std::stringstream stream;
stream << "Performance statistics:" << std::endl;
stream << " Average: " << std::to_string(this->m_average) << " us" << std::endl;
stream << " Minimum: " << std::to_string(this->m_minimum) << " us" << std::endl;
stream << " Maximum: " << std::to_string(this->m_maximum) << " us" << std::endl;

Logger::instance().log("Performance-" + this->m_name, Logger::Level::Info, stream.str());

this->m_lastLogStamp = now;
}
#endif
}
};
}
}

0 comments on commit 73beceb

Please sign in to comment.