1484543d1Sopenharmony_ci/*
2484543d1Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3484543d1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4484543d1Sopenharmony_ci * you may not use this file except in compliance with the License.
5484543d1Sopenharmony_ci * You may obtain a copy of the License at
6484543d1Sopenharmony_ci *
7484543d1Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8484543d1Sopenharmony_ci *
9484543d1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10484543d1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11484543d1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12484543d1Sopenharmony_ci * See the License for the specific language governing permissions and
13484543d1Sopenharmony_ci * limitations under the License.
14484543d1Sopenharmony_ci */
15484543d1Sopenharmony_ci
16484543d1Sopenharmony_ci#ifndef FFRT_WORKER_MONITOR_H
17484543d1Sopenharmony_ci#define FFRT_WORKER_MONITOR_H
18484543d1Sopenharmony_ci
19484543d1Sopenharmony_ci#include <mutex>
20484543d1Sopenharmony_ci#include <map>
21484543d1Sopenharmony_ci#include "eu/worker_thread.h"
22484543d1Sopenharmony_ci#include "tm/cpu_task.h"
23484543d1Sopenharmony_ci
24484543d1Sopenharmony_cinamespace ffrt {
25484543d1Sopenharmony_cistruct TaskTimeoutInfo {
26484543d1Sopenharmony_ci    CPUEUTask* task_ = nullptr;
27484543d1Sopenharmony_ci    int recordLevel_ = 0;
28484543d1Sopenharmony_ci    int sampledTimes_ = 0;
29484543d1Sopenharmony_ci    int executionTime_ = 0;
30484543d1Sopenharmony_ci
31484543d1Sopenharmony_ci    TaskTimeoutInfo() {}
32484543d1Sopenharmony_ci    explicit TaskTimeoutInfo(CPUEUTask* task) : task_(task) {}
33484543d1Sopenharmony_ci};
34484543d1Sopenharmony_ci
35484543d1Sopenharmony_cistruct CoWorkerInfo {
36484543d1Sopenharmony_ci    size_t qosLevel_;
37484543d1Sopenharmony_ci    int coWorkerCount_;
38484543d1Sopenharmony_ci    int executionNum_;
39484543d1Sopenharmony_ci    int sleepingWorkerNum_;
40484543d1Sopenharmony_ci
41484543d1Sopenharmony_ci    CoWorkerInfo(size_t qosLevel, int coWorkerCount, int executionNum, int sleepingWorkerNum)
42484543d1Sopenharmony_ci        : qosLevel_(qosLevel), coWorkerCount_(coWorkerCount),
43484543d1Sopenharmony_ci        executionNum_(executionNum), sleepingWorkerNum_(sleepingWorkerNum) {}
44484543d1Sopenharmony_ci};
45484543d1Sopenharmony_ci
46484543d1Sopenharmony_cistruct WorkerInfo {
47484543d1Sopenharmony_ci    int tid_;
48484543d1Sopenharmony_ci    uint64_t gid_;
49484543d1Sopenharmony_ci    uintptr_t workerTaskType_;
50484543d1Sopenharmony_ci    std::string label_;
51484543d1Sopenharmony_ci
52484543d1Sopenharmony_ci    WorkerInfo(int workerId, uint64_t taskId, uintptr_t workerTaskType, std::string workerTaskLabel)
53484543d1Sopenharmony_ci        : tid_(workerId), gid_(taskId), workerTaskType_(workerTaskType), label_(workerTaskLabel) {}
54484543d1Sopenharmony_ci};
55484543d1Sopenharmony_ci
56484543d1Sopenharmony_cistruct TimeoutFunctionInfo {
57484543d1Sopenharmony_ci    CoWorkerInfo coWorkerInfo_;
58484543d1Sopenharmony_ci    WorkerInfo workerInfo_;
59484543d1Sopenharmony_ci    int executionTime_;
60484543d1Sopenharmony_ci
61484543d1Sopenharmony_ci    TimeoutFunctionInfo(const CoWorkerInfo& coWorkerInfo, const WorkerInfo& workerInfo, int executionTime)
62484543d1Sopenharmony_ci        : coWorkerInfo_(coWorkerInfo), workerInfo_(workerInfo), executionTime_(executionTime)
63484543d1Sopenharmony_ci    {
64484543d1Sopenharmony_ci        if (workerInfo_.workerTaskType_ != ffrt_normal_task && workerInfo_.workerTaskType_ != ffrt_queue_task) {
65484543d1Sopenharmony_ci            workerInfo_.gid_ = UINT64_MAX; //该task type 没有 gid
66484543d1Sopenharmony_ci            workerInfo_.label_ = "Unsupport_Task_type"; //该task type 没有 label
67484543d1Sopenharmony_ci        }
68484543d1Sopenharmony_ci    }
69484543d1Sopenharmony_ci};
70484543d1Sopenharmony_ci
71484543d1Sopenharmony_ciclass WorkerMonitor {
72484543d1Sopenharmony_cipublic:
73484543d1Sopenharmony_ci    static WorkerMonitor &GetInstance();
74484543d1Sopenharmony_ci    void SubmitTask();
75484543d1Sopenharmony_ci
76484543d1Sopenharmony_ciprivate:
77484543d1Sopenharmony_ci    WorkerMonitor();
78484543d1Sopenharmony_ci    ~WorkerMonitor();
79484543d1Sopenharmony_ci    WorkerMonitor(const WorkerMonitor &) = delete;
80484543d1Sopenharmony_ci    WorkerMonitor(WorkerMonitor &&) = delete;
81484543d1Sopenharmony_ci    WorkerMonitor &operator=(const WorkerMonitor &) = delete;
82484543d1Sopenharmony_ci    WorkerMonitor &operator=(WorkerMonitor &&) = delete;
83484543d1Sopenharmony_ci    void SubmitSamplingTask();
84484543d1Sopenharmony_ci    void SubmitMemReleaseTask();
85484543d1Sopenharmony_ci    void CheckWorkerStatus();
86484543d1Sopenharmony_ci    void RecordTimeoutFunctionInfo(const CoWorkerInfo& coWorkerInfo, WorkerThread* worker,
87484543d1Sopenharmony_ci        CPUEUTask* workerTask, std::vector<TimeoutFunctionInfo>& timeoutFunctions);
88484543d1Sopenharmony_ci    void RecordSymbolAndBacktrace(const TimeoutFunctionInfo& timeoutFunction);
89484543d1Sopenharmony_ci    void RecordIpcInfo(const std::string& dumpInfo, int tid);
90484543d1Sopenharmony_ci
91484543d1Sopenharmony_ciprivate:
92484543d1Sopenharmony_ci    std::mutex mutex_;
93484543d1Sopenharmony_ci    std::mutex submitTaskMutex_;
94484543d1Sopenharmony_ci    bool skipSampling_ = false;
95484543d1Sopenharmony_ci    WaitUntilEntry watchdogWaitEntry_;
96484543d1Sopenharmony_ci    WaitUntilEntry memReleaseWaitEntry_;
97484543d1Sopenharmony_ci    std::map<WorkerThread*, TaskTimeoutInfo> workerStatus_;
98484543d1Sopenharmony_ci    bool samplingTaskExit_ = true;
99484543d1Sopenharmony_ci    bool memReleaseTaskExit_ = true;
100484543d1Sopenharmony_ci};
101484543d1Sopenharmony_ci}
102484543d1Sopenharmony_ci#endif