Skip to content

fix code style #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: fix-code-remaining-demos
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions demos/background_subtraction_demo/cpp_gapi/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,12 @@ int main(int argc, char* argv[]) {
slog::info << "Metrics report:" << slog::endl;
slog::info << "\tFPS: " << std::fixed << std::setprecision(1) << metrics.getTotal().fps << slog::endl;
slog::info << presenter.reportMeans() << slog::endl;
} catch (const std::exception& error) {
}
catch (const std::exception& error) {
slog::err << error.what() << slog::endl;
return 1;
} catch (...) {
}
catch (...) {
slog::err << "Unknown/internal exception happened." << slog::endl;
return 1;
}
Expand Down
6 changes: 4 additions & 2 deletions demos/classification_benchmark_demo/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,12 @@ int main(int argc, char* argv[]) {
pipeline.getPostprocessMetrics().getTotal().latency,
renderMetrics.getTotal().latency);
slog::info << presenter.reportMeans() << slog::endl;
} catch (const std::exception& error) {
}
catch (const std::exception& error) {
slog::err << error.what() << slog::endl;
return 1;
} catch (...) {
}
catch (...) {
slog::err << "Unknown/internal exception happened." << slog::endl;
return 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ void HpeAssociativeEmbedding::prepareInputsOutputs(std::shared_ptr<ov::Model>& m
heatmapsTensorName = findTensorByName("heatmaps", outputsNames);
try {
nmsHeatmapsTensorName = findTensorByName("nms_heatmaps", outputsNames);
} catch (const std::runtime_error&) { nmsHeatmapsTensorName = heatmapsTensorName; }
}
catch (const std::runtime_error&) {
nmsHeatmapsTensorName = heatmapsTensorName;
}

changeInputSize(model);
}
Expand Down
3 changes: 2 additions & 1 deletion demos/common/cpp/models/src/jpeg_restoration_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
*/

#include "models/jpeg_restoration_model.h"

#include <stddef.h>

#include <algorithm>
Expand All @@ -33,7 +35,6 @@
#include "models/image_model.h"
#include "models/input_data.h"
#include "models/internal_model_data.h"
#include "models/jpeg_restoration_model.h"
#include "models/results.h"

JPEGRestorationModel::JPEGRestorationModel(const std::string& modelFileName,
Expand Down
2 changes: 2 additions & 0 deletions demos/common/cpp/monitors/include/monitors/cpu_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <deque>
#include <iosfwd>
#include <memory>
#include <vector>

Expand All @@ -24,5 +25,6 @@ class CpuMonitor {
std::vector<double> cpuLoadSum;
std::deque<std::vector<double>> cpuLoadHistory;
class PerformanceCounter;

std::unique_ptr<PerformanceCounter> performanceCounter;
};
8 changes: 6 additions & 2 deletions demos/common/cpp/monitors/include/monitors/memory_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#pragma once

#include <deque>
#include <iosfwd>
#include <memory>
#include <utility>

class MemoryMonitor {
public:
Expand All @@ -15,12 +17,13 @@ class MemoryMonitor {
std::size_t getHistorySize() const;
void collectData();
std::deque<std::pair<double, double>> getLastHistory() const;
double getMeanMem() const; // in GiB
double getMeanMem() const; // in GiB
double getMeanSwap() const;
double getMaxMem() const;
double getMaxSwap() const;
double getMemTotal() const;
double getMaxMemTotal() const; // a system may have hotpluggable memory
double getMaxMemTotal() const; // a system may have hotpluggable memory

private:
unsigned samplesNumber;
std::size_t historySize;
Expand All @@ -30,5 +33,6 @@ class MemoryMonitor {
double maxMemTotal;
std::deque<std::pair<double, double>> memSwapUsageHistory;
class PerformanceCounter;

std::unique_ptr<PerformanceCounter> performanceCounter;
};
22 changes: 12 additions & 10 deletions demos/common/cpp/monitors/include/monitors/presenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,37 @@
#pragma once

#include <chrono>
#include <map>
#include <ostream>
#include <set>
#include <string>
#include <vector>

#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>

#include "cpu_monitor.h"
#include "memory_monitor.h"

enum class MonitorType{CpuAverage, DistributionCpu, Memory};
enum class MonitorType { CpuAverage, DistributionCpu, Memory };

class Presenter {
public:
explicit Presenter(std::set<MonitorType> enabledMonitors = {},
int yPos = 20,
cv::Size graphSize = {150, 60},
std::size_t historySize = 20);
int yPos = 20,
cv::Size graphSize = {150, 60},
std::size_t historySize = 20);
explicit Presenter(const std::string& keys,
int yPos = 20,
cv::Size graphSize = {150, 60},
std::size_t historySize = 20);
int yPos = 20,
cv::Size graphSize = {150, 60},
std::size_t historySize = 20);
void addRemoveMonitor(MonitorType monitor);
void handleKey(int key); // handles C, D, M, H keys
void handleKey(int key); // handles C, D, M, H keys
void drawGraphs(const cv::Mat& frame);
std::vector<std::string> reportMeans() const;

const int yPos;
const cv::Size graphSize;
const int graphPadding;

private:
std::chrono::steady_clock::time_point prevTimeStamp;
std::size_t historySize;
Expand Down
74 changes: 40 additions & 34 deletions demos/common/cpp/monitors/src/cpu_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@
#include "monitors/cpu_monitor.h"

#include <algorithm>
#ifdef _WIN32
#include "query_wrapper.h"
#include <stdexcept>
#include <string>
#include <system_error>
#include <PdhMsg.h>
#include <Windows.h>
#ifdef _WIN32
// Windows.h must come first
// clang-format off
# include <Windows.h>
# include <PdhMsg.h>
// clang-format on
# include <string>
# include <system_error>

# include "query_wrapper.h"

namespace {
const std::size_t nCores = []() {
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
}();
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
}();
}

class CpuMonitor::PerformanceCounter {
Expand All @@ -30,7 +36,7 @@ class CpuMonitor::PerformanceCounter {
if (ERROR_SUCCESS != status) {
throw std::system_error(status, std::system_category(), "PdhAddCounterW() failed");
}
status = PdhSetCounterScaleFactor(coreTimeCounters[i], -2); // scale counter to [0, 1]
status = PdhSetCounterScaleFactor(coreTimeCounters[i], -2); // scale counter to [0, 1]
if (ERROR_SUCCESS != status) {
throw std::system_error(status, std::system_category(), "PdhSetCounterScaleFactor() failed");
}
Expand All @@ -51,12 +57,13 @@ class CpuMonitor::PerformanceCounter {
PDH_FMT_COUNTERVALUE displayValue;
std::vector<double> cpuLoad(coreTimeCounters.size());
for (std::size_t i = 0; i < coreTimeCounters.size(); ++i) {
status = PdhGetFormattedCounterValue(coreTimeCounters[i], PDH_FMT_DOUBLE, NULL,
&displayValue);
status = PdhGetFormattedCounterValue(coreTimeCounters[i], PDH_FMT_DOUBLE, NULL, &displayValue);
switch (status) {
case ERROR_SUCCESS: break;
case ERROR_SUCCESS:
break;
// PdhGetFormattedCounterValue() can sometimes return PDH_CALC_NEGATIVE_DENOMINATOR for some reason
case PDH_CALC_NEGATIVE_DENOMINATOR: return {};
case PDH_CALC_NEGATIVE_DENOMINATOR:
return {};
default:
throw std::system_error(status, std::system_category(), "PdhGetFormattedCounterValue() failed");
}
Expand All @@ -75,11 +82,11 @@ class CpuMonitor::PerformanceCounter {
};

#elif __linux__
#include <chrono>
#include <regex>
#include <utility>
#include <fstream>
#include <unistd.h>
# include <unistd.h>
# include <chrono>
# include <fstream>
# include <regex>
# include <utility>

namespace {
const long clockTicks = sysconf(_SC_CLK_TCK);
Expand All @@ -92,17 +99,16 @@ std::vector<unsigned long> getIdleCpuStat() {
std::string line;
std::smatch match;
std::regex coreJiffies("^cpu(\\d+)\\s+"
"(\\d+)\\s+"
"(\\d+)\\s+"
"(\\d+)\\s+"
"(\\d+)\\s+" // idle
"(\\d+)"); // iowait
"(\\d+)\\s+"
"(\\d+)\\s+"
"(\\d+)\\s+"
"(\\d+)\\s+" // idle
"(\\d+)"); // iowait

while (std::getline(procStat, line)) {
if (std::regex_search(line, match, coreJiffies)) {
// it doesn't handle overflow of sum and overflows of /proc/stat values
unsigned long idleInfo = stoul(match[5]) + stoul(match[6]),
coreId = stoul(match[1]);
unsigned long idleInfo = stoul(match[5]) + stoul(match[6]), coreId = stoul(match[1]);
if (nCores <= coreId) {
throw std::runtime_error("The number of cores has changed");
}
Expand All @@ -111,7 +117,7 @@ std::vector<unsigned long> getIdleCpuStat() {
}
return idleCpuStat;
}
}
} // namespace

class CpuMonitor::PerformanceCounter {
public:
Expand All @@ -127,15 +133,16 @@ class CpuMonitor::PerformanceCounter {
for (std::size_t i = 0; i < idleCpuStat.size(); ++i) {
double idleDiff = idleCpuStat[i] - prevIdleCpuStat[i];
typedef std::chrono::duration<double, std::chrono::seconds::period> Sec;
cpuLoad[i] = 1.0
- idleDiff / clockTicks / std::chrono::duration_cast<Sec>(timePoint - prevTimePoint).count();
cpuLoad[i] =
1.0 - idleDiff / clockTicks / std::chrono::duration_cast<Sec>(timePoint - prevTimePoint).count();
}
prevIdleCpuStat = std::move(idleCpuStat);
prevTimePoint = timePoint;
return cpuLoad;
}
return {};
}

private:
std::vector<unsigned long> prevIdleCpuStat;
std::chrono::steady_clock::time_point prevTimePoint;
Expand All @@ -149,14 +156,13 @@ const std::size_t nCores{0};

class CpuMonitor::PerformanceCounter {
public:
std::vector<double> getCpuLoad() {return {};};
std::vector<double> getCpuLoad() {
return {};
}
};
#endif

CpuMonitor::CpuMonitor() :
samplesNumber{0},
historySize{0},
cpuLoadSum(nCores, 0) {}
CpuMonitor::CpuMonitor() : samplesNumber{0}, historySize{0}, cpuLoadSum(nCores, 0) {}

// PerformanceCounter is incomplete in header and destructor can't be defined implicitly
CpuMonitor::~CpuMonitor() = default;
Expand Down
Loading