Skip to content
Draft
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ frontend/frontend.o: frontend/frontend.cpp frontend/frontend.h lib/addoninfo.h l
cli/cmdlineparser.o: cli/cmdlineparser.cpp cli/cmdlinelogger.h cli/cmdlineparser.h cli/filelister.h externals/tinyxml2/tinyxml2.h lib/addoninfo.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/pathmatch.h lib/platform.h lib/regex.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h lib/xml.h
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/cmdlineparser.cpp

cli/cppcheckexecutor.o: cli/cppcheckexecutor.cpp cli/cmdlinelogger.h cli/cmdlineparser.h cli/cppcheckexecutor.h cli/executor.h cli/processexecutor.h cli/sehwrapper.h cli/signalhandler.h cli/singleexecutor.h cli/threadexecutor.h externals/picojson/picojson.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/checkers.h lib/checkersreport.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/json.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/sarifreport.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h
cli/cppcheckexecutor.o: cli/cppcheckexecutor.cpp cli/cmdlinelogger.h cli/cmdlineparser.h cli/cppcheckexecutor.h cli/executor.h cli/processexecutor.h cli/sehwrapper.h cli/signalhandler.h cli/singleexecutor.h cli/threadexecutor.h externals/picojson/picojson.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/checkers.h lib/checkersreport.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/json.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/sarifreport.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/cppcheckexecutor.cpp

cli/executor.o: cli/executor.cpp cli/executor.h lib/addoninfo.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h
Expand Down
5 changes: 5 additions & 0 deletions cli/cppcheckexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "settings.h"
#include "singleexecutor.h"
#include "suppressions.h"
#include "timer.h"
#include "utils.h"

#if defined(HAS_THREADING_MODEL_THREAD)
Expand Down Expand Up @@ -259,6 +260,7 @@ namespace {

int CppCheckExecutor::check(int argc, const char* const argv[])
{
Timer realTimeClock("", SHOWTIME_MODES::SHOWTIME_SUMMARY);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you made it a RAII. If the timer is moved down below the CmdLineParser then we don't need to cancel the timer. That could maybe loose a millisecond.. but this is acceptable imho. I don't care if the total time says 1.978s or 1.979s ..

Settings settings;
CmdLineLoggerStd logger;
Suppressions supprs;
Expand All @@ -277,6 +279,9 @@ int CppCheckExecutor::check(int argc, const char* const argv[])

const int ret = check_wrapper(settings, supprs);

if (settings.showtime == SHOWTIME_MODES::SHOWTIME_NONE)
realTimeClock.cancelRealTimeMeasurement();

return ret;
}

Expand Down
11 changes: 7 additions & 4 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,9 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
if (Settings::terminated())
return mLogger->exitcode();

const Timer fileTotalTimer(mSettings.showtime == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL, file.spath());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the old interface so we don't have to cancel it..

Timer WholeFileTimer{file.spath()};
if (mSettings.showtime != SHOWTIME_MODES::SHOWTIME_FILE_TOTAL)
WholeFileTimer.cancelRealTimeMeasurement();

if (!mSettings.quiet) {
std::string fixedpath = Path::toNativeSeparators(file.spath());
Expand Down Expand Up @@ -1311,8 +1313,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
// TODO: clear earlier?
mLogger->clear();

if (mSettings.showtime == SHOWTIME_MODES::SHOWTIME_FILE || mSettings.showtime == SHOWTIME_MODES::SHOWTIME_TOP5_FILE)
printTimerResults(mSettings.showtime);
printTimerResults(mSettings.showtime);

return mLogger->exitcode();
}
Expand Down Expand Up @@ -1505,7 +1506,9 @@ void CppCheck::executeAddons(const std::string& dumpFile, const FileWithDetails&
{
if (!dumpFile.empty()) {
std::vector<std::string> f{dumpFile};
executeAddons(f, file.spath());
Timer::run("CppCheck::executeAddons", mSettings.showtime, &s_timerResults, [&]() {
executeAddons(f, file.spath());
});
}
}

Expand Down
43 changes: 34 additions & 9 deletions lib/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ void TimerResults::showResults(SHOWTIME_MODES mode) const
}
++ordinal;
}

const double secOverall = overallData.seconds();
std::cout << "Overall time: " << secOverall << "s" << std::endl;
}

void TimerResults::addResults(const std::string& str, std::clock_t clocks)
Expand All @@ -108,11 +105,13 @@ Timer::Timer(std::string str, SHOWTIME_MODES showtimeMode, TimerResultsIntf* tim
, mStart(std::clock())
, mShowTimeMode(showtimeMode)
, mStopped(showtimeMode == SHOWTIME_MODES::SHOWTIME_NONE || showtimeMode == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL)
, mStartTimePoint(Clock::now())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why we need to have both mStart and mStartTimePoint. Why not remove the std::clock() handling and always use Clock::now() instead?

{}

Timer::Timer(bool fileTotal, std::string filename)
: mStr(std::move(filename))
, mStopped(!fileTotal)
Timer::Timer(std::string str)
: mStr(std::move(str))
, mShowTimeMode(SHOWTIME_MODES::SHOWTIME_FILE_TOTAL)
, mStartTimePoint(Clock::now())
{}

Timer::~Timer()
Expand All @@ -130,15 +129,41 @@ void Timer::stop()
const double sec = static_cast<double>(diff) / CLOCKS_PER_SEC;
std::lock_guard<std::mutex> l(stdCoutLock);
std::cout << mStr << ": " << sec << "s" << std::endl;
} else if (mShowTimeMode == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL) {
const double sec = static_cast<double>(diff) / CLOCKS_PER_SEC;
} else if (mShowTimeMode == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL && mStartTimePoint != TimePoint{}) {
std::lock_guard<std::mutex> l(stdCoutLock);
std::cout << "Check time: " << mStr << ": " << sec << "s" << std::endl;
std::cout << "Check time: " << mStr << ": " << getRealTimePassed() << std::endl;
} else {
if (mTimerResults)
mTimerResults->addResults(mStr, diff);
else if (mStr.empty() && mStartTimePoint != TimePoint{}) { // Get real time
std::lock_guard<std::mutex> l(stdCoutLock);
std::cout << "Overall time: " << getRealTimePassed() << std::endl;
}
}
}

mStopped = true;
}

std::string Timer::getRealTimePassed()
{
auto diff = std::chrono::duration_cast<std::chrono::microseconds>(Clock::now() - mStartTimePoint);

// Extract hours
auto hours = std::chrono::duration_cast<std::chrono::hours>(diff);
diff -= hours; // Subtract the extracted hours

// Extract minutes
auto minutes = std::chrono::duration_cast<std::chrono::minutes>(diff);
diff -= minutes; // Subtract the extracted minutes

// Extract seconds
auto seconds = static_cast<double>(diff.count()) / std::chrono::microseconds::period::den;

std::string ellapsedTime;
if (hours.count() > 0)
ellapsedTime += std::to_string(hours.count()) + "h ";
if (minutes.count() > 0)
ellapsedTime += std::to_string(minutes.count()) + "m ";
return (ellapsedTime + std::to_string(seconds) + "s ");
}
15 changes: 14 additions & 1 deletion lib/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "config.h"

#include <chrono>
#include <cstdint>
#include <ctime>
#include <functional>
Expand Down Expand Up @@ -73,25 +74,37 @@ class CPPCHECKLIB TimerResults : public TimerResultsIntf {
class CPPCHECKLIB Timer {
public:
Timer(std::string str, SHOWTIME_MODES showtimeMode, TimerResultsIntf* timerResults = nullptr);
Timer(bool fileTotal, std::string filename);
Timer(std::string str);
~Timer();

Timer(const Timer&) = delete;
Timer& operator=(const Timer&) = delete;

using Clock = std::chrono::high_resolution_clock;
using TimePoint = std::chrono::time_point<Clock>;

void stop();

void cancelRealTimeMeasurement() {
mStartTimePoint = TimePoint{};
}

static void run(std::string str, SHOWTIME_MODES showtimeMode, TimerResultsIntf* timerResults, const std::function<void()>& f) {
Timer t(std::move(str), showtimeMode, timerResults);
f();
}

private:

std::string getRealTimePassed();

const std::string mStr;
TimerResultsIntf* mTimerResults{};
std::clock_t mStart = std::clock();
const SHOWTIME_MODES mShowTimeMode = SHOWTIME_MODES::SHOWTIME_FILE_TOTAL;
bool mStopped{};
TimePoint mStartTimePoint{};
};

//---------------------------------------------------------------------------
#endif // timerH
1 change: 0 additions & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@
#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we include it in the timer now

#include <cstdint>
#include <cstdlib>
#include <cstring>
Expand Down
2 changes: 1 addition & 1 deletion test/testprocessexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class TestProcessExecutorBase : public TestFixture {
$.showtime = SHOWTIME_MODES::SHOWTIME_TOP5_SUMMARY));
const std::string output_s = GET_REDIRECT_OUTPUT;
// once: top5 results + overall + empty line
TODO_ASSERT_EQUALS(5 + 1 + 1, 2, cppcheck::count_all_of(output_s, '\n'));
TODO_ASSERT_EQUALS(5 + 1 + 1, 1, cppcheck::count_all_of(output_s, '\n'));
// should only report the top5 once
ASSERT(output_s.find("1 result(s)") == std::string::npos);
TODO_ASSERT(output_s.find("2 result(s)") != std::string::npos);
Expand Down
10 changes: 5 additions & 5 deletions test/testsingleexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ class TestSingleExecutorBase : public TestFixture {
dinit(CheckOptions,
$.showtime = SHOWTIME_MODES::SHOWTIME_TOP5_FILE));
const std::string output_s = GET_REDIRECT_OUTPUT;
// for each file: top5 results + overall + empty line
ASSERT_EQUALS((5 + 1 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n'));
// for each file: top5 results + overall
ASSERT_EQUALS((5 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n'));
}

void showtime_top5_summary() {
Expand All @@ -253,8 +253,8 @@ class TestSingleExecutorBase : public TestFixture {
dinit(CheckOptions,
$.showtime = SHOWTIME_MODES::SHOWTIME_TOP5_SUMMARY));
const std::string output_s = GET_REDIRECT_OUTPUT;
// once: top5 results + overall + empty line
ASSERT_EQUALS(5 + 1 + 1, cppcheck::count_all_of(output_s, '\n'));
// once: top5 results + overall
ASSERT_EQUALS(5 + 1, cppcheck::count_all_of(output_s, '\n'));
// should only report the top5 once
ASSERT(output_s.find("1 result(s)") == std::string::npos);
ASSERT(output_s.find("2 result(s)") != std::string::npos);
Expand All @@ -267,7 +267,7 @@ class TestSingleExecutorBase : public TestFixture {
dinit(CheckOptions,
$.showtime = SHOWTIME_MODES::SHOWTIME_FILE));
const std::string output_s = GET_REDIRECT_OUTPUT;
ASSERT_EQUALS(2, cppcheck::count_all_of(output_s, "Overall time:"));
ASSERT_EQUALS(0, cppcheck::count_all_of(output_s, "Overall time:"));
}

void showtime_summary() {
Expand Down
11 changes: 5 additions & 6 deletions test/testthreadexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,9 @@ class TestThreadExecutorBase : public TestFixture {
"int main() {}",
dinit(CheckOptions,
$.showtime = SHOWTIME_MODES::SHOWTIME_TOP5_FILE));
// for each file: top5 results + overall + empty line
const std::string output_s = GET_REDIRECT_OUTPUT;
// for each file: top5 results + overall + empty line
ASSERT_EQUALS((5 + 1 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n'));
// for each file: top5 results + overall
ASSERT_EQUALS((5 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n'));
}

void showtime_top5_summary() {
Expand All @@ -246,8 +245,8 @@ class TestThreadExecutorBase : public TestFixture {
dinit(CheckOptions,
$.showtime = SHOWTIME_MODES::SHOWTIME_TOP5_SUMMARY));
const std::string output_s = GET_REDIRECT_OUTPUT;
// once: top5 results + overall + empty line
ASSERT_EQUALS(5 + 1 + 1, cppcheck::count_all_of(output_s, '\n'));
// once: top5 results + overall
ASSERT_EQUALS(5 + 1, cppcheck::count_all_of(output_s, '\n'));
// should only report the top5 once
ASSERT(output_s.find("1 result(s)") == std::string::npos);
ASSERT(output_s.find("2 result(s)") != std::string::npos);
Expand All @@ -260,7 +259,7 @@ class TestThreadExecutorBase : public TestFixture {
dinit(CheckOptions,
$.showtime = SHOWTIME_MODES::SHOWTIME_FILE));
const std::string output_s = GET_REDIRECT_OUTPUT;
ASSERT_EQUALS(2, cppcheck::count_all_of(output_s, "Overall time:"));
ASSERT_EQUALS(0, cppcheck::count_all_of(output_s, "Overall time:"));
}

void showtime_summary() {
Expand Down
Loading