1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*!********************************************************************
Audacity: A Digital Audio Editor
BlockHasher.cpp
Dmitry Vedenko
**********************************************************************/
#include "BlockHasher.h"
#include <algorithm>
#include <atomic>
#include <future>
#include <iomanip>
#include <sstream>
#include <utility>
#include "MemoryX.h"
#include "SampleBlock.h"
#include "crypto/SHA256.h"
namespace audacity::cloud::audiocom::sync
{
class BlockHasher::Workers final
{
public:
using SampleData = std::vector<std::remove_pointer_t<samplePtr>>;
explicit Workers(
BlockHashCache& cache, const std::vector<LockedBlock> blocks,
std::function<void()> onComplete)
: mThreadsCount { std::max(1u, std::thread::hardware_concurrency() / 2) }
, mCache { cache }
, mOnComplete { std::move(onComplete) }
{
mResults.reserve(mThreadsCount);
const auto blocksCount = blocks.size();
// Try to add no more that 1 extra block per thread
const size_t blockPerThread = blocks.size() / mThreadsCount + 1;
for (size_t i = 0; i < mThreadsCount; ++i)
{
const size_t startIndex = i;
if (startIndex >= blocks.size())
break;
std::vector<LockedBlock> threadBlocks;
threadBlocks.reserve(blockPerThread);
for (size_t j = startIndex; j < blocksCount; j += mThreadsCount)
threadBlocks.emplace_back(blocks[j]);
mResults.emplace_back(std::async(
std::launch::async,
[this, threadBlocks = std::move(threadBlocks)]()
{
Result result;
SampleData sampleData;
for (const auto& block : threadBlocks)
result.emplace(block.Id, ComputeHash(sampleData, block));
return result;
}));
}
mWaiter = std::async(
std::launch::async,
[this]
{
for (auto& fut : mResults)
fut.wait();
NotifyReady();
});
}
bool IsReady() const
{
return std::all_of(
mResults.begin(), mResults.end(),
[](const auto& result)
{
return result.wait_for(std::chrono::seconds(0)) ==
std::future_status::ready;
});
}
std::pair<std::string, bool>
ComputeHash(SampleData& sampleData, const LockedBlock& block) const
{
assert(block.Id >= 0);
if(block.Id < 0)
return {{}, false};
std::string hash;
if (mCache.GetHash(block.Id, hash))
return { hash, false };
const auto sampleFormat = block.Format;
const auto sampleCount = block.Block->GetSampleCount();
const auto dataSize = sampleCount * SAMPLE_SIZE(sampleFormat);
sampleData.resize(dataSize);
const size_t samplesRead = block.Block->GetSamples(
sampleData.data(), sampleFormat, 0, sampleCount, false);
if (samplesRead != sampleCount)
return { {}, false };
hash = crypto::sha256(sampleData);
std::ostringstream oss;
oss << std::hex << std::uppercase << std::setw(8) << std::setfill('0') << block.Id;
std::string blockIdHex = oss.str();
hash.replace(0, 8, blockIdHex);
return { hash, true };
}
void NotifyReady()
{
if (mOnComplete)
mOnComplete();
}
std::vector<std::pair<int64_t, std::string>> TakeResult()
{
std::vector<std::pair<int64_t, std::string>> result;
for (auto& fut : mResults)
{
const auto& threadResult = fut.get();
for (const auto& [id, hash] : threadResult)
{
result.emplace_back(std::make_pair(id, hash.first));
if (hash.second)
mCache.UpdateHash(id, hash.first);
}
}
mResults.clear();
return result;
}
private:
const size_t mThreadsCount;
BlockHashCache& mCache;
using Result = std::unordered_map<int64_t, std::pair<std::string, bool>>;
std::vector<std::future<Result>> mResults;
std::future<void> mWaiter;
std::function<void()> mOnComplete;
};
BlockHasher::BlockHasher() = default;
BlockHasher::~BlockHasher() = default;
bool BlockHasher::ComputeHashes(
BlockHashCache& cache, std::vector<LockedBlock> blocks,
std::function<void()> onComplete)
{
if (mWorkers != nullptr && !mWorkers->IsReady())
return false;
if (blocks.empty())
{
if (onComplete)
onComplete();
return true;
}
mWorkers = std::make_unique<Workers>(
cache, std::move(blocks), std::move(onComplete));
return true;
}
bool BlockHasher::IsReady() const
{
return mWorkers != nullptr && mWorkers->IsReady();
}
std::vector<std::pair<int64_t, std::string>> BlockHasher::TakeResult()
{
if (mWorkers == nullptr)
return {};
return mWorkers->TakeResult();
}
} // namespace audacity::cloud::audiocom::sync
|