1 /* 2 * Copyright (c) 2023 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 PANDA_LIBPANDABASE_TASKMANAGER_TASK_STATISTICS_TASK_STATISTICS_H 17 #define PANDA_LIBPANDABASE_TASKMANAGER_TASK_STATISTICS_TASK_STATISTICS_H 18 19 #include "libpandabase/taskmanager/task.h" 20 #include <ostream> 21 #include <vector> 22 23 namespace panda::taskmanager { 24 25 enum class TaskStatisticsImplType : uint8_t { SIMPLE, FINE_GRAINED, LOCK_FREE }; 26 PANDA_PUBLIC_API std::ostream &operator<<(std::ostream &os, TaskStatisticsImplType type); 27 28 PANDA_PUBLIC_API TaskStatisticsImplType TaskStatisticsImplTypeFromString(std::string_view string); 29 30 enum class TaskStatus : size_t { 31 /// Status of tasks that were added in TaskQueue 32 ADDED, 33 34 /// Status of tasks that were executed on WorkerThread 35 EXECUTED, 36 37 /// Status of tasks that were popped from TaskQueue by calling method TaskScheduler::GetTaskFromQueue 38 POPPED 39 }; 40 41 constexpr auto ALL_TASK_STATUSES = std::array {TaskStatus::ADDED, TaskStatus::EXECUTED, TaskStatus::POPPED}; 42 43 /** 44 * TaskStatistics is a structure that counts the number of tasks that have received a certain 45 * status. All statuses that are taken into account are listed in the TaskStatus enum. 46 */ 47 class TaskStatistics { 48 public: 49 TaskStatistics(); 50 virtual ~TaskStatistics() = default; 51 NO_COPY_SEMANTIC(TaskStatistics); 52 NO_MOVE_SEMANTIC(TaskStatistics); 53 54 /** 55 * @brief Method increments status counter for tasks with specified TaskProperties. 56 * @param status - status of task which counter you want to increment. 57 * @param properties - TaskProperties of tasks which counter will be incremented. 58 * @param count - value by which the counter will be increased. 59 */ 60 virtual void IncrementCount(TaskStatus status, TaskProperties properties, size_t count) = 0; 61 62 /** 63 * @brief Method returns status counter for tasks with specified TaskProperties 64 * @param status - status of task which counter you want to return. 65 * @param properties - TaskProperties of tasks which counter will be return. 66 * @return status counter. 67 */ 68 virtual size_t GetCount(TaskStatus status, TaskProperties properties) const = 0; 69 70 /** 71 * @brief Method returns count of tasks that are in system. Task in system means that task was added but wasn't 72 * executed or popped. 73 * @return count of tasks in system for all properties. 74 */ 75 virtual size_t GetCountOfTaskInSystem() const = 0; 76 77 /** 78 * @brief Method returns count of tasks that are in system for specified TaskProperties. Task in system means 79 * that task was added but wasn't executed or popped. 80 * @param properties - TaskProperties of tasks which counter will be return. 81 * @return count of tasks in system for specified properties. 82 */ 83 virtual size_t GetCountOfTasksInSystemWithTaskProperties(TaskProperties properties) const = 0; 84 85 /** 86 * @brief Method resets internal counters for all stats and all properties. It should be used only if it's only 87 * if there is a guarantee that other threads do not change states of all counters 88 */ 89 virtual void ResetAllCounters() = 0; 90 91 /** 92 * @brief Method resets internal counters for all stats but with specified properties. It should be used only if 93 * there is a guarantee that other threads do not change state of counters that you want to reset. 94 * @param properties - TaskProperties of tasks which counters will be reset. 95 */ 96 virtual void ResetCountersWithTaskProperties(TaskProperties properties) = 0; 97 98 protected: 99 std::vector<TaskProperties> allTaskProperties_; // NOLINT(misc-non-private-member-variables-in-classes) 100 }; 101 102 } // namespace panda::taskmanager 103 104 #endif // PANDA_LIBPANDABASE_TASKMANAGER_TASK_STATISTICS_TASK_STATISTICS_H 105