|
79 | 79 | #include "ir_serializer.hpp"
|
80 | 80 | #include "openvino/core/model.hpp"
|
81 | 81 |
|
| 82 | +#if defined _WIN32 |
| 83 | +#include <windows.h> |
| 84 | +#include <psapi.h> |
| 85 | +#else |
| 86 | +#include <fstream> |
| 87 | +#include <regex> |
| 88 | +#include <sstream> |
| 89 | +#endif |
| 90 | + |
82 | 91 | namespace {
|
83 | 92 |
|
| 93 | +#if defined _WIN32 |
| 94 | + |
| 95 | +int64_t get_peak_memory_usage() { |
| 96 | + PROCESS_MEMORY_COUNTERS mem_counters; |
| 97 | + if (!GetProcessMemoryInfo(GetCurrentProcess(), &mem_counters, sizeof(mem_counters))) { |
| 98 | + throw std::runtime_error("Can't get system memory values"); |
| 99 | + } |
| 100 | + |
| 101 | + // Linux tracks memory usage in pages and then converts them to kB. |
| 102 | + // Thus, there is always some room for inaccuracy as pages are not guaranteed to be fully used. |
| 103 | + // In Windows, the situation is different: the system returns the memory usage in bytes, not in pages. |
| 104 | + // To align the output between the two operating systems as closely as possible, we have two options: |
| 105 | + // 1. Use rounding to the nearest integer. |
| 106 | + // 2. Try to estimate the number of pages used in Windows. However, |
| 107 | + // this approach is likely to be inaccurate as well, so option 1 was chosen. |
| 108 | + static constexpr double bytes_in_kilobyte = 1024.0; |
| 109 | + |
| 110 | + // please note then we calculate difference |
| 111 | + // to get peak memory increment value, so we return int64, not size_t |
| 112 | + return static_cast<int64_t>(std::round(mem_counters.PeakWorkingSetSize / bytes_in_kilobyte)); |
| 113 | +} |
| 114 | + |
| 115 | +#else |
| 116 | + |
| 117 | +int64_t get_peak_memory_usage() { |
| 118 | + size_t peak_mem_usage_kB = 0; |
| 119 | + |
| 120 | + std::ifstream status_file("/proc/self/status"); |
| 121 | + std::string line; |
| 122 | + std::regex vm_peak_regex("VmPeak:"); |
| 123 | + std::smatch vm_match; |
| 124 | + bool mem_values_found = false; |
| 125 | + while (std::getline(status_file, line)) { |
| 126 | + if (std::regex_search(line, vm_match, vm_peak_regex)) { |
| 127 | + std::istringstream iss(vm_match.suffix()); |
| 128 | + iss >> peak_mem_usage_kB; |
| 129 | + mem_values_found = true; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if (!mem_values_found) { |
| 134 | + throw std::runtime_error("Can't get system memory values"); |
| 135 | + } |
| 136 | + |
| 137 | + // please note then we calculate difference |
| 138 | + // to get peak memory increment value, so we return int64, not size_t |
| 139 | + return static_cast<int64_t>(peak_mem_usage_kB); |
| 140 | +} |
| 141 | + |
| 142 | +#endif |
| 143 | + |
84 | 144 | constexpr std::string_view INPUTS_PRECISIONS_KEY = "--inputs_precisions";
|
85 | 145 | constexpr std::string_view INPUTS_LAYOUTS_KEY = "--inputs_layouts";
|
86 | 146 | constexpr std::string_view OUTPUTS_PRECISIONS_KEY = "--outputs_precisions";
|
@@ -444,6 +504,7 @@ std::vector<std::shared_ptr<IGraph>> DriverCompilerAdapter::compileWS(const std:
|
444 | 504 | size_t callNumber = 0;
|
445 | 505 | bool compilationDone = false;
|
446 | 506 |
|
| 507 | + auto compile_model_mem_start = get_peak_memory_usage(); |
447 | 508 | while (!compilationDone) {
|
448 | 509 | _logger.debug("compileWS iteration %d", callNumber);
|
449 | 510 |
|
@@ -482,6 +543,12 @@ std::vector<std::shared_ptr<IGraph>> DriverCompilerAdapter::compileWS(const std:
|
482 | 543 | mainGraphHandle = graphHandle;
|
483 | 544 | }
|
484 | 545 | }
|
| 546 | + auto compile_model_mem_end = get_peak_memory_usage(); |
| 547 | + |
| 548 | + std::cout << "Start of compilation memory usage: Peak " << compile_model_mem_start << " KB" << std::endl; |
| 549 | + std::cout << "End of compilation memory usage: Peak " << compile_model_mem_end << " KB" << std::endl; |
| 550 | + std::cout << "Compilation memory usage: Peak " << compile_model_mem_end - compile_model_mem_start << " KB" |
| 551 | + << std::endl; |
485 | 552 |
|
486 | 553 | std::vector<std::shared_ptr<IGraph>> driverGraphs;
|
487 | 554 | for (size_t handleIndex = 0; handleIndex < initGraphHandles.size(); ++handleIndex) {
|
|
0 commit comments