* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* 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 FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#include <iostream>
#include <vector>
#include <unistd.h>
#include <string>
#include <pthread.h>
#include "securec.h"
#include "utils.h"
#include "acl/acl.h"
#include "acl/acl_prof.h"
namespace {
void getModelInfo(void *data, uint32_t len)
{
uint32_t opNumber = 0;
uint32_t dataLen = 0;
aclprofGetOpNum(data, len, &opNumber);
for (uint32_t i = 0; i < opNumber; i++) {
uint32_t modelId = aclprofGetModelId(data, len, i);
size_t opTypeLen = 0;
aclprofGetOpTypeLen(data, len, i, &opTypeLen);
char opType[opTypeLen];
aclprofGetOpType(data, len, i, opType, opTypeLen);
size_t opNameLen = 0;
aclprofGetOpNameLen(data, len, i, &opNameLen);
char opName[opNameLen];
aclprofGetOpName(data, len, i, opName, opNameLen);
uint64_t opStart = aclprofGetOpStart(data, len, i);
uint64_t opEnd = aclprofGetOpEnd(data, len, i);
uint64_t opDuration = aclprofGetOpDuration(data, len, i);
}
}
void *profDataRead(void *fd)
{
uint64_t N = 10;
uint64_t bufferSize = 0;
aclprofGetOpDescSize(&bufferSize);
uint64_t readbufLen = bufferSize * N;
char *readbuf = new char[readbufLen];
ssize_t dataLen = read(*(int*)fd, readbuf, readbufLen);
while (dataLen > 0) {
if (dataLen == static_cast<ssize_t>(5) && strncmp(readbuf, "Stop", 4) == 0) {
break;
}
getModelInfo(readbuf, static_cast<uint32_t>(dataLen));
if (dataLen > static_cast<ssize_t>(readbufLen)) {
dataLen = static_cast<ssize_t>(readbufLen);
}
memset_s(readbuf, readbufLen, 0, readbufLen);
dataLen = read(*(int*)fd, readbuf, readbufLen);
}
delete []readbuf;
}
}
int main(int argc, char *argv[])
{
uint32_t deviceIdList[1] = {0};
aclrtStream stream = nullptr;
aclInit(nullptr);
aclrtSetDevice(deviceIdList[0]);
aclrtCreateStream(&stream);
int subFd[2];
CHECK_ERROR(pipe(subFd));
aclprofSubscribeConfig *config = aclprofCreateSubscribeConfig(1, ACL_AICORE_NONE, &subFd[1]);
uint32_t fake_modelId = 1;
aclprofModelSubscribe(fake_modelId, config);
pthread_t subTid = 0;
CHECK_ERROR(pthread_create(&subTid, nullptr, profDataRead, &subFd[0]));
INFO_LOG("Running ...... \n");
if (write(subFd[1], "Hello", static_cast<size_t>(6)) != static_cast<size_t>(6)) {
ERROR_LOG("Write Hello error");
}
(void)sleep(static_cast<uint8_t>(3));
INFO_LOG("Try to Stop ...... \n");
if (write(subFd[1], "Stop", static_cast<size_t>(5)) != static_cast<size_t>(5)) {
ERROR_LOG("Write Stop error");
}
aclprofModelUnSubscribe(fake_modelId);
CHECK_ERROR(pthread_join(subTid, nullptr));
INFO_LOG("Stopped ...... \n");
CHECK_ERROR(close(subFd[0]));
aclprofDestroySubscribeConfig(config);
aclrtDestroyStream(stream);
aclrtResetDeviceForce(deviceIdList[0]);
aclFinalize();
return 0;
}