• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 
22 #include <atomic>
23 #include <unordered_map>
24 #include <string>
25 #include <vector>
26 #include <array>
27 
28 namespace ark::taskmanager {
29 
30 enum class TaskTimeStatsType : uint8_t {
31     /**
32      * NO_STATISTICS type is used when we don't want to collect and output any data. It has lowest overhead and
33      * should be used as default one.
34      */
35     NO_STATISTICS,
36     /// LIGHT_STATISTICS type collects data only for mean and max times. It has low overhead.
37     LIGHT_STATISTICS,
38 };
39 
40 PANDA_PUBLIC_API TaskTimeStatsType StringToTaskTimeStats(std::string_view str);
41 PANDA_PUBLIC_API std::ostream &operator<<(std::ostream &os, TaskTimeStatsType type);
42 
43 /// @brief TaskTimeStatsBase is interface of TaskTimeStats classes.
44 class TaskTimeStatsBase {
45     NO_COPY_SEMANTIC(TaskTimeStatsBase);
46     NO_MOVE_SEMANTIC(TaskTimeStatsBase);
47 
48 public:
49     TaskTimeStatsBase() = default;
50     virtual ~TaskTimeStatsBase() = default;
51     /**
52      * @brief Method registers WorkerThread in TaskTimeStats structure. You should not register helper thread
53      * that can execute tasks.
54      */
55     virtual void RegisterWorkerThread();
56     /**
57      * @brief Method saves information about life time and execution time of task with specified TaskProperties.
58      * @param prop: TaskProperties of task, information about which should be saved
59      * @param lifeTime: time in microseconds between adding task in queue and end of its execution
60      * @param executionTime: time in microseconds it took to complete the task. It should be less or equal to lifeTime.
61      */
62     virtual void CollectLifeAndExecutionTimes(TaskProperties prop, uint64_t lifeTime, uint64_t executionTime) = 0;
63     /**
64      * @brief Method returns vector of strings with statistics. Each string represents element of statistics. For
65      * example one string can represent statistics for one unique TaskProperties.
66      */
67     virtual std::vector<std::string> GetTaskStatistics() = 0;
68 
69 protected:
70     using ContainerId = size_t;
71     static constexpr size_t DEFAULT_CONTAINER_ID = 0U;
72     static thread_local ContainerId containerId_;  // for no WorkerThread should be equal to DEFAULT_CONTAINER_ID
73 
74 private:
75     std::atomic_size_t countOfRegisteredWorkers_ {0U};
76 };
77 
78 namespace internal {
79 
80 /**
81  * @brief LightTaskTimeTimeStats is TaskTimeStats class that collect info to get mean and max
82  * times for each TaskProperties
83  */
84 class LightTaskTimeTimeStats final : public TaskTimeStatsBase {
85     NO_COPY_SEMANTIC(LightTaskTimeTimeStats);
86     NO_MOVE_SEMANTIC(LightTaskTimeTimeStats);
87 
88     struct MeanTimeStats;
89 
90     static constexpr size_t STATISTICS_CONTAINER_SIZE = TaskProperties::Hash::MaxValue() + 1U;
91 
92     using StatisticsContainer = std::array<MeanTimeStats, STATISTICS_CONTAINER_SIZE>;
93     using StatisticsContainerPerThread = std::vector<StatisticsContainer>;
94 
95     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
96     struct MeanTimeStats {
97         uint64_t sumLifeTime = 0;
98         uint64_t sumExecutionTime = 0;
99         uint64_t maxLifeTime = 0;
100         uint64_t maxExecutionTime = 0;
101         size_t countOfTasks = 0;
102     };
103     // NOLINTEND(misc-non-private-member-variables-in-classes)
104 
105 public:
106     PANDA_PUBLIC_API explicit LightTaskTimeTimeStats(size_t countOfWorkers);
107     PANDA_PUBLIC_API ~LightTaskTimeTimeStats() override = default;
108 
109     void CollectLifeAndExecutionTimes(TaskProperties prop, uint64_t lifeTime, uint64_t executionTime) override;
110 
111     std::vector<std::string> GetTaskStatistics() override;
112 
113 private:
114     std::string GetStatisticsForProperties(TaskProperties prop);
115     size_t GetCountOfTasksWithProperties(TaskProperties prop);
116     std::vector<TaskProperties> GetAllTaskProperties();
117 
118     StatisticsContainerPerThread statisticsContainerPerThread_;
119 };
120 
121 }  // namespace internal
122 
123 }  // namespace ark::taskmanager
124 
125 #endif  // LIBPANDABASE_TASKMANAGER_UTILS_TASK_LIFETIME_STATISTICS_H