Browse Source

Add system::getNanoseconds(). Use it instead of std::chrono.

tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
5a45c26da6
4 changed files with 25 additions and 12 deletions
  1. +4
    -0
      include/system.hpp
  2. +4
    -6
      src/engine/Engine.cpp
  3. +6
    -6
      src/logger.cpp
  4. +11
    -0
      src/system.cpp

+ 4
- 0
include/system.hpp View File

@@ -36,6 +36,10 @@ int getLogicalCoreCount();
void setThreadName(const std::string& name);
/** Returns the caller's human-readable stack trace with "\n"-separated lines. */
std::string getStackTrace();
/** Gets the current number of nanoseconds since the epoch.
Currently uses std::chrono::high_resolution_clock.
*/
long getNanoseconds();
/** Opens a URL, also happens to work with PDFs and folders.
Shell injection is possible, so make sure the URL is trusted or hard coded.
May block, so open in a new thread.


+ 4
- 6
src/engine/Engine.cpp View File

@@ -7,7 +7,6 @@
#include <plugin.hpp>

#include <algorithm>
#include <chrono>
#include <thread>
#include <condition_variable>
#include <mutex>
@@ -222,10 +221,9 @@ static void Engine_stepModulesWorker(Engine* that, int threadId) {
Module* module = internal->modules[i];

// Start CPU timer
using time_point = std::chrono::time_point<std::chrono::high_resolution_clock>;
time_point beginTime;
long startTime;
if (cpuMeter) {
beginTime = std::chrono::high_resolution_clock::now();
startTime = system::getNanoseconds();
}

// Step module
@@ -236,8 +234,8 @@ static void Engine_stepModulesWorker(Engine* that, int threadId) {

// Stop CPU timer
if (cpuMeter) {
time_point endTime = std::chrono::high_resolution_clock::now();
float duration = std::chrono::duration<float>(endTime - beginTime).count();
long endTime = system::getNanoseconds();
float duration = (endTime - startTime) / 1e9;

// Smooth CPU time
const float cpuTau = 2.f /* seconds */;


+ 6
- 6
src/logger.cpp View File

@@ -1,7 +1,7 @@
#include <common.hpp>
#include <asset.hpp>
#include <settings.hpp>
#include <chrono>
#include <system.hpp>
#include <mutex>


@@ -10,12 +10,12 @@ namespace logger {


static FILE* outputFile = NULL;
static std::chrono::high_resolution_clock::time_point startTime;
static long startTime = 0;
static std::mutex logMutex;


void init() {
startTime = std::chrono::high_resolution_clock::now();
startTime = system::getNanoseconds();
if (settings::devMode) {
outputFile = stderr;
return;
@@ -50,11 +50,11 @@ static const int levelColors[] = {
static void logVa(Level level, const char* filename, int line, const char* format, va_list args) {
std::lock_guard<std::mutex> lock(logMutex);

auto nowTime = std::chrono::high_resolution_clock::now();
int duration = std::chrono::duration_cast<std::chrono::milliseconds>(nowTime - startTime).count();
long nowTime = system::getNanoseconds();
double duration = (nowTime - startTime) / 1e9;
if (outputFile == stderr)
fprintf(outputFile, "\x1B[%dm", levelColors[level]);
fprintf(outputFile, "[%.03f %s %s:%d] ", duration / 1000.0, levelLabels[level], filename, line);
fprintf(outputFile, "[%.03f %s %s:%d] ", duration, levelLabels[level], filename, line);
if (outputFile == stderr)
fprintf(outputFile, "\x1B[0m");
vfprintf(outputFile, format, args);


+ 11
- 0
src/system.cpp View File

@@ -3,6 +3,7 @@

#include <thread>
#include <regex>
#include <chrono>
#include <dirent.h>
#include <sys/stat.h>
#include <cxxabi.h> // for __cxxabiv1::__cxa_demangle
@@ -214,6 +215,16 @@ std::string getStackTrace() {
}


long getNanoseconds() {
using clock = std::chrono::high_resolution_clock;
using time_point = std::chrono::time_point<clock>;
time_point now = clock::now();
using duration = std::chrono::duration<long, std::nano>;
duration d = now.time_since_epoch();
return d.count();
}


void openBrowser(const std::string& url) {
#if defined ARCH_LIN
std::string command = "xdg-open \"" + url + "\"";


Loading…
Cancel
Save