feat: expand the usb buf size.

Signed-off-by: TaowerfulMAX <liurantao@huawei.com>
This commit is contained in:
TaowerfulMAX 2024-07-31 20:10:37 +08:00
parent 0775a3986f
commit 6b8365d504
9 changed files with 97 additions and 14 deletions

View File

@ -166,6 +166,14 @@ namespace Base {
{
return MAX_USBFFS_BULK;
}
inline int GetMaxBufSizeStable()
{
return MAX_SIZE_IOBUF_STABLE;
}
inline int GetUsbffsBulkSizeStable()
{
return MAX_USBFFS_BULK_STABLE;
}
int CloseFd(int &fd);
void InitProcess(void);

View File

@ -359,7 +359,8 @@ void HdcChannelBase::AllocCallback(uv_handle_t *handle, size_t sizeWanted, uv_bu
HChannel context = (HChannel)handle->data;
Base::ReallocBuf(&context->ioBuf, &context->bufSize, Base::GetMaxBufSize() * BUF_EXTEND_SIZE);
buf->base = (char *)context->ioBuf + context->availTailIndex;
buf->len = context->bufSize - context->availTailIndex;
int size = context->bufSize - context->availTailIndex;
buf->len = std::min(size, static_cast<int>(sizeWanted));
}
uint32_t HdcChannelBase::GetChannelPseudoUid()

View File

@ -44,6 +44,7 @@ constexpr uint8_t COUNT = 10;
constexpr uint8_t STREAM_MAIN = 0; // work at main thread
constexpr uint8_t STREAM_WORK = 1; // work at work thread
constexpr uint8_t STREAM_SIZE = 2;
constexpr uint8_t FEATURE_FLAG_MAX_SIZE = 8;
constexpr uint16_t TIME_BUF_SIZE = 32;
constexpr uint16_t BUF_SIZE_MICRO = 16;
constexpr uint16_t BUF_SIZE_TINY = 64;
@ -59,8 +60,10 @@ constexpr uint16_t UV_DEFAULT_INTERVAL = 250; // ms
constexpr uint16_t VER_PROTOCOL = 0x01;
constexpr uint16_t MAX_PACKET_SIZE_HISPEED = 512;
constexpr uint16_t DEVICE_CHECK_INTERVAL = 3000; // ms
constexpr uint16_t MAX_SIZE_IOBUF = 61440;
constexpr uint16_t MAX_USBFFS_BULK = 62464;
constexpr uint32_t MAX_SIZE_IOBUF = 511 * 1024; // 511KB, large file transfer speed up
constexpr uint32_t MAX_USBFFS_BULK = 512 * 1024; // 512KB, large file transfer speed up
constexpr uint32_t MAX_SIZE_IOBUF_STABLE = 60 * 1024; // 60KB, compatible with previous version
constexpr uint32_t MAX_USBFFS_BULK_STABLE = 61 * 1024; // 61KB, compatible with provious version
constexpr double DECODE_SCALE = 0.75;
constexpr uint16_t LAST_EQUAL_NUM = 2;
constexpr uint16_t UV_START_TIMEOUT = 10;
@ -91,8 +94,8 @@ constexpr uint16_t TCP_CONNECT_RETRY_TIME_MS = 500;
// |----------------------------------------------------------------|
// | major |reserve| minor |reserve|version| fix | reserve |
// |----------------------------------------------------------------|
// 0x30000400 is 3.0.0e
constexpr uint32_t HDC_VERSION_NUMBER = 0x30000400;
// 0x30100000 is 3.1.0a
constexpr uint32_t HDC_VERSION_NUMBER = 0x30100000;
constexpr uint32_t HDC_BUF_MAX_BYTES = INT_MAX;
#ifdef HDC_HOST
constexpr uint32_t HOST_SOCKETPAIR_SIZE = 1024 * 1024;
@ -117,6 +120,8 @@ const string EMPTY_ECHO = "[Empty]";
const string MESSAGE_INFO = "[Info]";
const string MESSAGE_FAIL = "[Fail]";
const string MESSAGE_SUCCESS = "[Success]";
const char HUGE_BUF_TAG = 'H'; // support huge buffer
const size_t BANNER_FEATURE_TAG_OFFSET = 11;
// input command
const string CMDSTR_SOFTWARE_VERSION = "version";
const string CMDSTR_SOFTWARE_HELP = "help";

View File

@ -302,6 +302,7 @@ struct TaskInformation {
bool channelTask;
void *channelClass;
uint8_t debugRelease; // 0:allApp 1:debugApp 2:releaseApp
bool isStableBuf;
};
using HTaskInfo = TaskInformation *;
@ -309,7 +310,7 @@ using HTaskInfo = TaskInformation *;
#ifdef HDC_HOST
struct HostUSBEndpoint {
HostUSBEndpoint(uint16_t epBufSize)
HostUSBEndpoint(uint32_t epBufSize)
{
endpoint = 0;
sizeEpBuf = epBufSize; // MAX_USBFFS_BULK
@ -330,7 +331,7 @@ struct HostUSBEndpoint {
bool isComplete;
bool isShutdown;
bool bulkInOut; // true is bulkIn
uint16_t sizeEpBuf;
uint32_t sizeEpBuf;
std::mutex mutexIo;
std::mutex mutexCb;
condition_variable cv;
@ -351,7 +352,8 @@ struct HdcUSB {
std::string usbMountPoint;
HostUSBEndpoint hostBulkIn;
HostUSBEndpoint hostBulkOut;
HdcUSB() : hostBulkIn(63488), hostBulkOut(62464) {} // 62464 MAX_USBFFS_BULK + 1024 = 63488
HdcUSB() : hostBulkIn(513 * 1024), hostBulkOut(512 * 1024) {} // 513: 512 + 1, 1024: 1KB
// 512 * 1024 + 1024 = 513 * 1024, MAX_USBFFS_BULK: 512 * 1024
#else
// usb accessory FunctionFS
@ -535,6 +537,7 @@ struct HdcChannel {
RemoteType remote = RemoteType::REMOTE_NONE;
bool fromClient = false;
bool connectLocalDevice = false;
bool isStableBuf = false;
};
using HChannel = struct HdcChannel *;

View File

@ -21,6 +21,7 @@ HdcFile::HdcFile(HTaskInfo hTaskInfo)
{
commandBegin = CMD_FILE_BEGIN;
commandData = CMD_FILE_DATA;
isStableBuf = hTaskInfo->isStableBuf;
}
HdcFile::~HdcFile()

View File

@ -31,6 +31,7 @@ HdcTransferBase::HdcTransferBase(HTaskInfo hTaskInfo)
ResetCtx(&ctxNow, true);
commandBegin = 0;
commandData = 0;
isStableBuf = false;
}
HdcTransferBase::~HdcTransferBase()
@ -77,7 +78,9 @@ int HdcTransferBase::SimpleFileIO(CtxFile *context, uint64_t index, uint8_t *sen
}
bool ret = false;
while (true) {
size_t bufMaxSize = static_cast<size_t>(Base::GetUsbffsBulkSize() - payloadPrefixReserve);
size_t bufMaxSize = context->isStableBufSize ?
static_cast<size_t>(Base::GetUsbffsBulkSizeStable() - payloadPrefixReserve) :
static_cast<size_t>(Base::GetUsbffsBulkSize() - payloadPrefixReserve);
if (bytes < 0 || static_cast<size_t>(bytes) > bufMaxSize) {
WRITE_LOG(LOG_DEBUG, "SimpleFileIO param check failed");
break;
@ -232,8 +235,9 @@ void HdcTransferBase::OnFileIO(uv_fs_t *req)
break;
}
if (context->indexIO < context->fileSize) {
thisClass->SimpleFileIO(context, context->indexIO, nullptr,
Base::GetMaxBufSize() * thisClass->maxTransferBufFactor);
thisClass->SimpleFileIO(context, context->indexIO, nullptr, context->isStableBufSize ?
(Base::GetMaxBufSizeStable() * thisClass->maxTransferBufFactor) :
(Base::GetMaxBufSize() * thisClass->maxTransferBufFactor));
} else {
context->ioFinish = true;
}
@ -349,7 +353,13 @@ void HdcTransferBase::OnFileOpen(uv_fs_t *req)
}
#endif
}
thisClass->SendToAnother(thisClass->commandBegin, nullptr, 0);
union FeatureFlagsUnion f{};
if (!thisClass->AddFeatures(f)) {
WRITE_LOG(LOG_FATAL, "AddFeatureFlag failed");
thisClass->SendToAnother(thisClass->commandBegin, nullptr, 0);
} else {
thisClass->SendToAnother(thisClass->commandBegin, f.raw, sizeof(f));
}
}
}
@ -670,7 +680,14 @@ bool HdcTransferBase::CommandDispatch(const uint16_t command, uint8_t *payload,
while (true) {
if (command == commandBegin) {
CtxFile *context = &ctxNow;
int ioRet = SimpleFileIO(context, context->indexIO, nullptr, Base::GetMaxBufSize() * maxTransferBufFactor);
if (!CheckFeatures(context, payload, payloadSize)) {
WRITE_LOG(LOG_FATAL, "CommandDispatch CheckFeatures command:%u", command);
ret = false;
break;
}
int ioRet = SimpleFileIO(context, context->indexIO, nullptr, (context->isStableBufSize) ?
Base::GetMaxBufSizeStable() * maxTransferBufFactor :
Base::GetMaxBufSize() * maxTransferBufFactor);
if (ioRet < 0) {
WRITE_LOG(LOG_FATAL, "CommandDispatch SimpleFileIO ioRet:%d", ioRet);
ret = false;
@ -705,4 +722,31 @@ void HdcTransferBase::ExtractRelativePath(string &cwd, string &path)
path = cwd + path;
}
}
bool HdcTransferBase::AddFeatures(FeatureFlagsUnion &feature)
{
feature.bits.hugeBuf = !isStableBuf;
return true;
}
bool HdcTransferBase::CheckFeatures(CtxFile *context, uint8_t *payload, const int payloadSize)
{
if (payloadSize == FEATURE_FLAG_MAX_SIZE) {
union FeatureFlagsUnion feature{};
if (memcpy_s(&feature, sizeof(feature), payload, payloadSize)) {
WRITE_LOG(LOG_FATAL, "CheckFeatures memcpy_s failed");
return false;
}
WRITE_LOG(LOG_DEBUG, "isStableBuf:%d, hugeBuf:%d", isStableBuf, feature.bits.hugeBuf);
context->isStableBufSize = isStableBuf ? true : (!feature.bits.hugeBuf);
return true;
} else if (payloadSize == 0) {
WRITE_LOG(LOG_DEBUG, "FileBegin CheckFeatures payloadSize:%d, use default feature.", payloadSize);
context->isStableBufSize = true;
return true;
} else {
WRITE_LOG(LOG_FATAL, "CheckFeatures payloadSize:%d", payloadSize);
return false;
}
}
} // namespace Hdc

View File

@ -50,6 +50,19 @@ public:
uint32_t compressSize;
uint32_t uncompressSize;
};
union FeatureFlagsUnion {
struct {
uint8_t hugeBuf : 1; // bit 1: enable huge buffer 512K
uint8_t compressLz4 : 1; // bit 2: enable compress default is lz4
uint8_t reserveBits1 : 6; // bit 3-8: reserved
uint8_t reserveBits2 : 8; // bit 9-16: reserved
uint16_t reserveBits3 : 16; // bit 17-32: reserved
uint32_t reserveBits4 : 32; // bit 33-64: reserved
} bits;
uint8_t raw[FEATURE_FLAG_MAX_SIZE];
};
// FEATURE_FLAG_MAX_SIZE * 8 bits = 8 * 8 bits = 64 bits
// is used for HdcTransferBase. just base class use, not public
HdcTransferBase(HTaskInfo hTaskInfo);
virtual ~HdcTransferBase();
virtual void StopTask()
@ -77,6 +90,7 @@ protected:
bool closeNotify;
bool ioFinish;
bool closeReqSubmitted;
bool isStableBufSize; // USB IO buffer size set stable value, false: 512K, true: 61K
void *thisClass;
uint32_t lastErrno;
uv_loop_t *loop;
@ -114,10 +128,13 @@ protected:
bool CheckFilename(string &localPath, string &optName, string &errStr);
void SetFileTime(CtxFile *context);
void ExtractRelativePath(string &cwd, string &path);
bool AddFeatures(FeatureFlagsUnion &feature);
bool CheckFeatures(CtxFile *context, uint8_t *payload, const int payloadSize);
CtxFile ctxNow;
uint16_t commandBegin;
uint16_t commandData;
bool isStableBuf;
const string CMD_OPTION_CLIENTCWD = "-cwd";
CircleBuffer cirbuf;

View File

@ -568,6 +568,8 @@ int HdcClient::PreHandshake(HChannel hChannel, const uint8_t *buf)
WRITE_LOG(LOG_DEBUG, "Channel Hello failed");
return ERR_BUF_CHECK;
}
hChannel->isStableBuf = (hShake->banner[BANNER_FEATURE_TAG_OFFSET] != HUGE_BUF_TAG);
WRITE_LOG(LOG_DEBUG, "Channel PreHandshake isStableBuf:%d", hChannel->isStableBuf);
// sync remote session id to local
uint32_t unOld = hChannel->channelId;
hChannel->channelId = ntohl(hShake->channelId);
@ -770,6 +772,7 @@ HTaskInfo HdcClient::GetRemoteTaskInfo(HChannel hChannel)
hTaskInfo->serverOrDaemon = true;
hTaskInfo->channelTask = true;
hTaskInfo->channelClass = this;
hTaskInfo->isStableBuf = hChannel->isStableBuf;
return hTaskInfo;
};
} // namespace Hdc

View File

@ -66,7 +66,7 @@ void HdcServerForClient::AcceptClient(uv_stream_t *server, int status)
uv_recv_buffer_size((uv_handle_t *)&hChannel->hWorkTCP, &bufMaxSize);
auto funcChannelHeaderAlloc = [](uv_handle_t *handle, size_t sizeWanted, uv_buf_t *buf) -> void {
HChannel context = (HChannel)handle->data;
Base::ReallocBuf(&context->ioBuf, &context->bufSize, sizeWanted); // sizeWanted default 6k
Base::ReallocBuf(&context->ioBuf, &context->bufSize, Base::GetMaxBufSize() * BUF_EXTEND_SIZE);
buf->base = (char *)context->ioBuf + context->availTailIndex;
#ifdef HDC_VERSION_CHECK
buf->len = sizeof(struct ChannelHandShake) + DWORD_SERIALIZE_SIZE; // only recv static size
@ -79,6 +79,7 @@ void HdcServerForClient::AcceptClient(uv_stream_t *server, int status)
// channel handshake step1
struct ChannelHandShake handShake = {};
if (EOK == strcpy_s(handShake.banner, sizeof(handShake.banner), HANDSHAKE_MESSAGE.c_str())) {
handShake.banner[BANNER_FEATURE_TAG_OFFSET] = HUGE_BUF_TAG; // set feature tag for huge buf size
handShake.channelId = htonl(hChannel->channelId);
string ver = Base::GetVersion() + HDC_MSG_HASH;
WRITE_LOG(LOG_DEBUG, "Server ver:%s", ver.c_str());