|
| 1 | +/* <editor-fold desc="MIT License"> |
| 2 | +
|
| 3 | +Copyright(c) 2026 Robert Osfield |
| 4 | +
|
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 6 | +
|
| 7 | +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 8 | +
|
| 9 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 10 | +
|
| 11 | +</editor-fold> */ |
| 12 | + |
| 13 | +#include <vsg/app/LoadBalancing.h> |
| 14 | +#include <vsg/app/Viewer.h> |
| 15 | +#include <vsg/io/Logger.h> |
| 16 | +#include <vsg/utils/Profiler.h> |
| 17 | + |
| 18 | +using namespace vsg; |
| 19 | + |
| 20 | +LoadBalancing::LoadBalancing() |
| 21 | +{ |
| 22 | +} |
| 23 | + |
| 24 | +LoadBalancing::LoadBalancing(const LoadBalancing& rhs, const CopyOp& copyop) : |
| 25 | + Inherit(rhs, copyop) |
| 26 | +{ |
| 27 | +} |
| 28 | + |
| 29 | +LoadBalancing::~LoadBalancing() |
| 30 | +{ |
| 31 | +} |
| 32 | + |
| 33 | +void LoadBalancing::update(Viewer& viewer) |
| 34 | +{ |
| 35 | + if (previousFrameTime == std::numeric_limits<time_point>::max()) |
| 36 | + { |
| 37 | + previousFrameTime = viewer.getFrameStamp()->time; |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + double frameDelta = (previousFrameTime == std::numeric_limits<time_point>::max()) ? 0.0 : std::chrono::duration<double, std::chrono::seconds::period>(viewer.getFrameStamp()->time - previousFrameTime).count(); |
| 42 | + uint64_t previousFrameCount = viewer.getFrameStamp()->frameCount - 1; |
| 43 | + |
| 44 | + // vsg::info("LoadBalancing::update( ", &viewer, " ) ", viewer.getFrameStamp()->frameCount, ", ", frameDelta); |
| 45 | + |
| 46 | + double frameTargetRatio = frameDelta/targetFrameTime; |
| 47 | + double cpuTargetRatio = 0.0; |
| 48 | + double gpuTargetRatio = 0.0; |
| 49 | + |
| 50 | + if (auto profiler = viewer.instrumentation.cast<Profiler>()) |
| 51 | + { |
| 52 | + auto& log = profiler->log; |
| 53 | + |
| 54 | + size_t targetFrameCount = 16; |
| 55 | + |
| 56 | + double max_cpu_durection = 0.0; |
| 57 | + double max_gpu_durection = 0.0; |
| 58 | + |
| 59 | + auto& frameIndices = log->frameIndices; |
| 60 | + auto last_frame = frameIndices.end(); |
| 61 | + auto first_frame = (frameIndices.size() > targetFrameCount) ? (frameIndices.end() - targetFrameCount) : frameIndices.begin(); |
| 62 | + for(auto itr = first_frame; itr != last_frame; ++itr) |
| 63 | + { |
| 64 | + auto first_index = *itr; |
| 65 | + const auto& first = log->entry(first_index); |
| 66 | + auto last_index = first.reference; |
| 67 | + |
| 68 | + auto cpu_duration = 0.0; |
| 69 | + auto gpu_duration = 0.0; |
| 70 | + |
| 71 | + for(auto i = first_index; i <= last_index;) |
| 72 | + { |
| 73 | + if (log->entry(i).type == ProfileLog::COMMAND_BUFFER) |
| 74 | + { |
| 75 | + const auto& start = log->entry(i); |
| 76 | + const auto& end = log->entry(start.reference); |
| 77 | + if (start.gpuTime != 0 && end.gpuTime != 0) |
| 78 | + { |
| 79 | + gpu_duration = static_cast<double>(end.gpuTime - start.gpuTime) * log->timestampScaleToMilliseconds * 0.001; |
| 80 | + if (gpu_duration > max_gpu_durection) |
| 81 | + { |
| 82 | + max_gpu_durection = gpu_duration; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + cpu_duration = std::abs(std::chrono::duration<double, std::chrono::seconds::period>(end.cpuTime - start.cpuTime).count()); |
| 87 | + |
| 88 | + if (cpu_duration > max_cpu_durection) |
| 89 | + { |
| 90 | + max_cpu_durection = cpu_duration; |
| 91 | + } |
| 92 | + |
| 93 | + i = start.reference+1; |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + ++i; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +#if 0 |
| 104 | + if (first.sourceLocation) vsg::info(" entry(", first_index,") { ", int(first.type), ", ", first.reference, ", func=", first.sourceLocation->function, ", cpu_duration = ", cpu_duration, ", gpu_duration = ", gpu_duration, "} "); |
| 105 | + else vsg::info(" entry(", first_index,") { ", int(first.type), ", ", first.reference, " }"); |
| 106 | + |
| 107 | + if (max_gpu_durection > targetGPUTime) |
| 108 | + { |
| 109 | + vsg::info(" GPU time too high ", max_gpu_durection, "ms is greated than target ", targetGPUTime, "ms"); |
| 110 | + } |
| 111 | +#endif |
| 112 | + } |
| 113 | + |
| 114 | + cpuTargetRatio = max_cpu_durection/targetCPUTime; |
| 115 | + gpuTargetRatio = max_gpu_durection/targetGPUTime; |
| 116 | + } |
| 117 | + |
| 118 | + double maxTargetRatio = std::max({frameTargetRatio, cpuTargetRatio, gpuTargetRatio}); |
| 119 | + |
| 120 | + if (maxTargetRatio > 2.0) |
| 121 | + { |
| 122 | + vsg::info("Frame break on frameCount = ", previousFrameCount, " : maxTargetRatio = ", maxTargetRatio, " { frame = ", frameTargetRatio, ", cpu = ", cpuTargetRatio, ", gpu = ", gpuTargetRatio, " }"); |
| 123 | + } |
| 124 | + else if (maxTargetRatio > 1.2) |
| 125 | + { |
| 126 | + vsg::info("Frame targets exceeded on frameCount = ", previousFrameCount, " : maxTargetRatio = ", maxTargetRatio, " { frame = ", frameTargetRatio, ", cpu = ", cpuTargetRatio, ", gpu = ", gpuTargetRatio, " }"); |
| 127 | + } |
| 128 | + |
| 129 | + previousFrameTime = viewer.getFrameStamp()->time; |
| 130 | +} |
| 131 | + |
| 132 | + |
0 commit comments