106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci
1606f6ba60Sopenharmony_ci#include "hidebug/hidebug.h"
1706f6ba60Sopenharmony_ci
1806f6ba60Sopenharmony_ci#include <memory>
1906f6ba60Sopenharmony_ci#include <vector>
2006f6ba60Sopenharmony_ci#include <unistd.h>
2106f6ba60Sopenharmony_ci
2206f6ba60Sopenharmony_ci#include "hidebug/hidebug_type.h"
2306f6ba60Sopenharmony_ci#include "hidebug_native_interface.h"
2406f6ba60Sopenharmony_ci#include "securec.h"
2506f6ba60Sopenharmony_ci
2606f6ba60Sopenharmony_cidouble OH_HiDebug_GetAppCpuUsage()
2706f6ba60Sopenharmony_ci{
2806f6ba60Sopenharmony_ci    double cpuUsage = OHOS::HiviewDFX::HidebugNativeInterface::CreateInstance()->GetCpuUsage();
2906f6ba60Sopenharmony_ci    return cpuUsage;
3006f6ba60Sopenharmony_ci}
3106f6ba60Sopenharmony_ci
3206f6ba60Sopenharmony_cidouble OH_HiDebug_GetSystemCpuUsage()
3306f6ba60Sopenharmony_ci{
3406f6ba60Sopenharmony_ci    auto cpuUsageOptional = OHOS::HiviewDFX::HidebugNativeInterface::CreateInstance()->GetSystemCpuUsage();
3506f6ba60Sopenharmony_ci    if (cpuUsageOptional.has_value()) {
3606f6ba60Sopenharmony_ci        return cpuUsageOptional.value();
3706f6ba60Sopenharmony_ci    }
3806f6ba60Sopenharmony_ci    return 0;
3906f6ba60Sopenharmony_ci}
4006f6ba60Sopenharmony_ci
4106f6ba60Sopenharmony_ciHiDebug_ThreadCpuUsagePtr OH_HiDebug_GetAppThreadCpuUsage()
4206f6ba60Sopenharmony_ci{
4306f6ba60Sopenharmony_ci    auto nativeInterface = OHOS::HiviewDFX::HidebugNativeInterface::CreateInstance();
4406f6ba60Sopenharmony_ci    if (!nativeInterface) {
4506f6ba60Sopenharmony_ci        return nullptr;
4606f6ba60Sopenharmony_ci    }
4706f6ba60Sopenharmony_ci    std::map<uint32_t, double> threadMap = nativeInterface->GetAppThreadCpuUsage();
4806f6ba60Sopenharmony_ci    HiDebug_ThreadCpuUsagePtr head = nullptr;
4906f6ba60Sopenharmony_ci    HiDebug_ThreadCpuUsagePtr prev = nullptr;
5006f6ba60Sopenharmony_ci    for (const auto[threadId, cpuUsage] : threadMap) {
5106f6ba60Sopenharmony_ci        HiDebug_ThreadCpuUsagePtr node = (HiDebug_ThreadCpuUsagePtr) malloc(sizeof(HiDebug_ThreadCpuUsage));
5206f6ba60Sopenharmony_ci        if (node == nullptr) {
5306f6ba60Sopenharmony_ci            continue;
5406f6ba60Sopenharmony_ci        }
5506f6ba60Sopenharmony_ci        node->threadId = threadId;
5606f6ba60Sopenharmony_ci        node->cpuUsage = cpuUsage;
5706f6ba60Sopenharmony_ci        node->next = nullptr;
5806f6ba60Sopenharmony_ci        if (prev == nullptr) {
5906f6ba60Sopenharmony_ci            head = node;
6006f6ba60Sopenharmony_ci        } else {
6106f6ba60Sopenharmony_ci            prev->next = node;
6206f6ba60Sopenharmony_ci        }
6306f6ba60Sopenharmony_ci        prev = node;
6406f6ba60Sopenharmony_ci    }
6506f6ba60Sopenharmony_ci    return head;
6606f6ba60Sopenharmony_ci}
6706f6ba60Sopenharmony_ci
6806f6ba60Sopenharmony_civoid OH_HiDebug_FreeThreadCpuUsage(HiDebug_ThreadCpuUsagePtr *threadCpuUsage)
6906f6ba60Sopenharmony_ci{
7006f6ba60Sopenharmony_ci    if (threadCpuUsage == nullptr || *threadCpuUsage == nullptr) {
7106f6ba60Sopenharmony_ci        return;
7206f6ba60Sopenharmony_ci    }
7306f6ba60Sopenharmony_ci    HiDebug_ThreadCpuUsagePtr node = *threadCpuUsage;
7406f6ba60Sopenharmony_ci    while (node != nullptr) {
7506f6ba60Sopenharmony_ci        HiDebug_ThreadCpuUsagePtr next = node->next;
7606f6ba60Sopenharmony_ci        free(node);
7706f6ba60Sopenharmony_ci        node = next;
7806f6ba60Sopenharmony_ci    }
7906f6ba60Sopenharmony_ci    *threadCpuUsage = nullptr;
8006f6ba60Sopenharmony_ci}
8106f6ba60Sopenharmony_ci
8206f6ba60Sopenharmony_civoid OH_HiDebug_GetAppMemoryLimit(HiDebug_MemoryLimit *memoryLimit)
8306f6ba60Sopenharmony_ci{
8406f6ba60Sopenharmony_ci    if (!memoryLimit) {
8506f6ba60Sopenharmony_ci        return;
8606f6ba60Sopenharmony_ci    }
8706f6ba60Sopenharmony_ci    auto nativeInterface = OHOS::HiviewDFX::HidebugNativeInterface::CreateInstance();
8806f6ba60Sopenharmony_ci    if (!nativeInterface) {
8906f6ba60Sopenharmony_ci        return;
9006f6ba60Sopenharmony_ci    }
9106f6ba60Sopenharmony_ci    auto collectResult = nativeInterface->GetAppMemoryLimit();
9206f6ba60Sopenharmony_ci    if (!collectResult) {
9306f6ba60Sopenharmony_ci        return;
9406f6ba60Sopenharmony_ci    }
9506f6ba60Sopenharmony_ci    memoryLimit->vssLimit = collectResult->vssLimit;
9606f6ba60Sopenharmony_ci    memoryLimit->rssLimit = collectResult->rssLimit;
9706f6ba60Sopenharmony_ci}
9806f6ba60Sopenharmony_ci
9906f6ba60Sopenharmony_civoid OH_HiDebug_GetAppNativeMemInfo(HiDebug_NativeMemInfo *nativeMemInfo)
10006f6ba60Sopenharmony_ci{
10106f6ba60Sopenharmony_ci    auto nativeInterface = OHOS::HiviewDFX::HidebugNativeInterface::CreateInstance();
10206f6ba60Sopenharmony_ci    if (!nativeMemInfo || !nativeInterface) {
10306f6ba60Sopenharmony_ci        return;
10406f6ba60Sopenharmony_ci    }
10506f6ba60Sopenharmony_ci    auto nativeMemoryInfo = nativeInterface->GetAppNativeMemInfo();
10606f6ba60Sopenharmony_ci    if (!nativeMemoryInfo) {
10706f6ba60Sopenharmony_ci        return;
10806f6ba60Sopenharmony_ci    }
10906f6ba60Sopenharmony_ci
11006f6ba60Sopenharmony_ci    nativeMemInfo->pss = static_cast<uint32_t>(nativeMemoryInfo->pss);
11106f6ba60Sopenharmony_ci    nativeMemInfo->vss = static_cast<uint32_t>(nativeMemoryInfo->vss);
11206f6ba60Sopenharmony_ci    nativeMemInfo->rss = static_cast<uint32_t>(nativeMemoryInfo->rss);
11306f6ba60Sopenharmony_ci    nativeMemInfo->sharedDirty = static_cast<uint32_t>(nativeMemoryInfo->sharedDirty);
11406f6ba60Sopenharmony_ci    nativeMemInfo->privateDirty = static_cast<uint32_t>(nativeMemoryInfo->privateDirty);
11506f6ba60Sopenharmony_ci    nativeMemInfo->sharedClean = static_cast<uint32_t>(nativeMemoryInfo->sharedClean);
11606f6ba60Sopenharmony_ci    nativeMemInfo->privateClean = static_cast<uint32_t>(nativeMemoryInfo->privateClean);
11706f6ba60Sopenharmony_ci}
11806f6ba60Sopenharmony_ci
11906f6ba60Sopenharmony_civoid OH_HiDebug_GetSystemMemInfo(HiDebug_SystemMemInfo *systemMemInfo)
12006f6ba60Sopenharmony_ci{
12106f6ba60Sopenharmony_ci    auto nativeInterface = OHOS::HiviewDFX::HidebugNativeInterface::CreateInstance();
12206f6ba60Sopenharmony_ci    if (!systemMemInfo || !nativeInterface) {
12306f6ba60Sopenharmony_ci        return;
12406f6ba60Sopenharmony_ci    }
12506f6ba60Sopenharmony_ci    auto sysMemInfo = nativeInterface->GetSystemMemInfo();
12606f6ba60Sopenharmony_ci    if (!sysMemInfo) {
12706f6ba60Sopenharmony_ci        return;
12806f6ba60Sopenharmony_ci    }
12906f6ba60Sopenharmony_ci
13006f6ba60Sopenharmony_ci    systemMemInfo->totalMem = static_cast<uint32_t>(sysMemInfo->memTotal);
13106f6ba60Sopenharmony_ci    systemMemInfo->freeMem = static_cast<uint32_t>(sysMemInfo->memFree);
13206f6ba60Sopenharmony_ci    systemMemInfo->availableMem = static_cast<uint32_t>(sysMemInfo->memAvailable);
13306f6ba60Sopenharmony_ci}
13406f6ba60Sopenharmony_ci
13506f6ba60Sopenharmony_ciHiDebug_ErrorCode OH_HiDebug_StartAppTraceCapture(HiDebug_TraceFlag flag,
13606f6ba60Sopenharmony_ci    uint64_t tags, uint32_t limitSize, char* fileName, uint32_t length)
13706f6ba60Sopenharmony_ci{
13806f6ba60Sopenharmony_ci    if (fileName == nullptr) {
13906f6ba60Sopenharmony_ci        return HIDEBUG_INVALID_ARGUMENT;
14006f6ba60Sopenharmony_ci    }
14106f6ba60Sopenharmony_ci    auto nativeInterface = OHOS::HiviewDFX::HidebugNativeInterface::CreateInstance();
14206f6ba60Sopenharmony_ci    if (!nativeInterface) {
14306f6ba60Sopenharmony_ci        return HIDEBUG_TRACE_ABNORMAL;
14406f6ba60Sopenharmony_ci    }
14506f6ba60Sopenharmony_ci    std::string file;
14606f6ba60Sopenharmony_ci    auto ret = nativeInterface->StartAppTraceCapture(tags, flag, limitSize, file);
14706f6ba60Sopenharmony_ci    if (ret != HIDEBUG_SUCCESS) {
14806f6ba60Sopenharmony_ci        return ret;
14906f6ba60Sopenharmony_ci    }
15006f6ba60Sopenharmony_ci    if (strcpy_s(fileName, length, file.c_str()) != EOK) {
15106f6ba60Sopenharmony_ci        nativeInterface->StopAppTraceCapture();
15206f6ba60Sopenharmony_ci        return HIDEBUG_INVALID_ARGUMENT;
15306f6ba60Sopenharmony_ci    }
15406f6ba60Sopenharmony_ci    return HIDEBUG_SUCCESS;
15506f6ba60Sopenharmony_ci}
15606f6ba60Sopenharmony_ci
15706f6ba60Sopenharmony_ci
15806f6ba60Sopenharmony_ciHiDebug_ErrorCode OH_HiDebug_StopAppTraceCapture()
15906f6ba60Sopenharmony_ci{
16006f6ba60Sopenharmony_ci    auto nativeInterface = OHOS::HiviewDFX::HidebugNativeInterface::CreateInstance();
16106f6ba60Sopenharmony_ci    if (!nativeInterface) {
16206f6ba60Sopenharmony_ci        return HIDEBUG_TRACE_ABNORMAL;
16306f6ba60Sopenharmony_ci    }
16406f6ba60Sopenharmony_ci    return nativeInterface->StopAppTraceCapture();
16506f6ba60Sopenharmony_ci}
16606f6ba60Sopenharmony_ci
16706f6ba60Sopenharmony_ciHiDebug_ErrorCode OH_HiDebug_GetGraphicsMemory(uint32_t *value)
16806f6ba60Sopenharmony_ci{
16906f6ba60Sopenharmony_ci    if (value == nullptr) {
17006f6ba60Sopenharmony_ci        return HIDEBUG_INVALID_ARGUMENT;
17106f6ba60Sopenharmony_ci    }
17206f6ba60Sopenharmony_ci    auto nativeInterface = OHOS::HiviewDFX::HidebugNativeInterface::CreateInstance();
17306f6ba60Sopenharmony_ci    if (!nativeInterface) {
17406f6ba60Sopenharmony_ci        return HIDEBUG_TRACE_ABNORMAL;
17506f6ba60Sopenharmony_ci    }
17606f6ba60Sopenharmony_ci    std::optional<int32_t> ret = nativeInterface->GetGraphicsMemory();
17706f6ba60Sopenharmony_ci    if (!ret.has_value() || ret < 0) {
17806f6ba60Sopenharmony_ci        return HIDEBUG_TRACE_ABNORMAL;
17906f6ba60Sopenharmony_ci    }
18006f6ba60Sopenharmony_ci    *value = static_cast<uint32_t>(ret.value());
18106f6ba60Sopenharmony_ci    return HIDEBUG_SUCCESS;
18206f6ba60Sopenharmony_ci}