• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef LIBPANDABASE_TASKMANAGER_UTILS_TASK_LIFETIME_STATISTICS_H
17 #define LIBPANDABASE_TASKMANAGER_UTILS_TASK_LIFETIME_STATISTICS_H
18 
19 #include "libpandabase/macros.h"
20 #include "libpandabase/taskmanager/task.h"
21 #include "libpandabase/taskmanager/task_queue_interface.h"
22 
23 #include <atomic>
24 #include <unordered_map>
25 #include <string>
26 #include <vector>
27 #include <array>
28 
29 namespace ark::taskmanager {
30 
31 enum class TaskTimeStatsType : uint8_t {
32     /**
33      * NO_STATISTICS type is used when we don't want to collect and output any data. It has lowest overhead and
34      * should be used as default one.
35      */
36     NO_STATISTICS,
37     /// LIGHT_STATISTICS type collects data only for mean and max times. It has low overhead.
38     LIGHT_STATISTICS,
39 };
40 
41 PANDA_PUBLIC_API TaskTimeStatsType StringToTaskTimeStats(std::string_view str);
42 PANDA_PUBLIC_API std::ostream &operator<<(std::ostream &os, TaskTimeStatsType type);
43 
44 /// @brief TaskTimeStatsBase is interface of TaskTimeStats classes.
45 class TaskTimeStatsBase {
46     NO_COPY_SEMANTIC(TaskTimeStatsBase);
47     NO_MOVE_SEMANTIC(TaskTimeStatsBase);
48 
49 public:
50     TaskTimeStatsBase() = default;
51     virtual ~TaskTimeStatsBase() = default;
52     /**
53      * @brief Method registers WorkerThread in TaskTimeStats structure. You should not register helper thread
54      * that can execute tasks.
55      */
56     virtual void RegisterWorkerThread();
57     /**
58      * @brief Method saves information about life time and execution time of task with specified TaskProperties.
59      * @param queue: parent queue of task, information about which should be saved
60      * @param lifeTime: time in microseconds between adding task in queue and end of its execution
61      * @param executionTime: time in microseconds it took to complete the task. It should be less or equal to lifeTime.
62      */
63     virtual void CollectLifeAndExecutionTimes(QueueId id, uint64_t lifeTime, uint64_t executionTime) = 0;
64     /**
65      * @brief Method returns vector of strings with statistics. Each string represents element of statistics. For
66      * example one string can represent statistics for one unique TaskProperties.
67      */
68     virtual std::vector<std::string> GetTaskStatistics() = 0;
69 
70 protected:
71     using ContainerId = size_t;
72     static constexpr size_t DEFAULT_CONTAINER_ID = 0U;
73     static thread_local ContainerId containerId_;  // for no WorkerThread should be equal to DEFAULT_CONTAINER_ID
74 
75 private:
76     std::atomic_size_t countOfRegisteredWorkers_ {0U};
77 };
78 
79 namespace internal {
80 
81 /**
82  * @brief LightTaskTimeTimeStats is TaskTimeStats class that collect info to get mean and max
83  * times for each TaskProperties
84  */
85 class LightTaskTimeTimeStats final : public TaskTimeStatsBase {
86     NO_COPY_SEMANTIC(LightTaskTimeTimeStats);
87     NO_MOVE_SEMANTIC(LightTaskTimeTimeStats);
88 
89     struct MeanTimeStats;
90 
91     static constexpr size_t STATISTICS_CONTAINER_SIZE = MAX_ID_COUNT;
92 
93     using StatisticsContainer = std::array<MeanTimeStats, STATISTICS_CONTAINER_SIZE>;
94     using StatisticsContainerPerThread = std::vector<StatisticsContainer>;
95 
96     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
97     struct MeanTimeStats {
98         uint64_t sumLifeTime = 0;
99         uint64_t sumExecutionTime = 0;
100         uint64_t maxLifeTime = 0;
101         uint64_t maxExecutionTime = 0;
102         size_t countOfTasks = 0;
103     };
104     // NOLINTEND(misc-non-private-member-variables-in-classes)
105 
106 public:
107     PANDA_PUBLIC_API explicit LightTaskTimeTimeStats(size_t countOfWorkers);
108     PANDA_PUBLIC_API ~LightTaskTimeTimeStats() override = default;
109 
110     void CollectLifeAndExecutionTimes(QueueId id, uint64_t lifeTime, uint64_t executionTime) override;
111 
112     std::vector<std::string> GetTaskStatistics() override;
113 
114 private:
115     std::string GetStatisticsForProperties(QueueId id);
116     size_t GetCountOfTasksWithProperties(QueueId id);
117 
118     StatisticsContainerPerThread statisticsContainerPerThread_;
119 };
120 
121 }  // namespace internal
122 
123 }  // namespace ark::taskmanager
124 
125 #endif  // LIBPANDABASE_TASKMANAGER_UTILS_TASK_LIFETIME_STATISTICS_H
126