* Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved.
* ubs-engine is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include "ubse_request_id_util.h"
namespace ubse::utils {
const std::chrono::steady_clock::time_point UbseRequestIdUtil::PROGRAM_START_TIME = std::chrono::steady_clock::now();
UbseRequestIdUtil::UbseRequestIdUtil(UbseRequestType requestType) : requestType_(requestType), counter_(0) {}
uint64_t UbseRequestIdUtil::GenerateRequestId(uint8_t slotId)
{
uint32_t currentTs = GetCurrentTimestamp();
uint32_t lastTs = lastTimestamp_.load(std::memory_order_relaxed);
uint16_t currentCounter;
if (currentTs == lastTs) {
currentCounter = ++counter_;
if (currentCounter == 0) {
while (GetCurrentTimestamp() == currentTs) {
std::this_thread::yield();
}
currentTs = GetCurrentTimestamp();
counter_.store(1, std::memory_order_relaxed);
lastTimestamp_.store(currentTs, std::memory_order_relaxed);
currentCounter = 1;
}
} else {
lastTimestamp_.store(currentTs, std::memory_order_relaxed);
counter_.store(1, std::memory_order_relaxed);
currentCounter = 1;
}
return (static_cast<uint64_t>(requestType_) << REQUEST_TYPE_SHIFT) |
(static_cast<uint64_t>(slotId) << SLOT_ID_SHIFT) | (static_cast<uint64_t>(currentTs) << TIME_STAMP_SHIFT) |
currentCounter;
}
UbseRequestType UbseRequestIdUtil::ParseRequestType(uint64_t requestId)
{
uint32_t requestTypeMask = (1 << REQUEST_TYPE_BITS) - 1;
return static_cast<UbseRequestType>((requestId >> REQUEST_TYPE_SHIFT) & requestTypeMask);
}
uint32_t UbseRequestIdUtil::GetCurrentTimestamp()
{
auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - PROGRAM_START_TIME);
return static_cast<uint32_t>(elapsed.count());
}
}