/* ---------------------------------------------------------------------------- * Copyright (c) Huawei Technologies Co., Ltd. 2017-2020. All rights reserved. * Description: LiteOS Performance Monitor Module Implementation * Author: Huawei LiteOS Team * Create: 2017-01-01 * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * 3. Neither the name of the copyright holder nor the names of its contributors may be used * to endorse or promote products derived from this software without specific prior written * permission. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * --------------------------------------------------------------------------- */ /** * @defgroup los_perf Perf * @ingroup kernel */ #ifndef _LOS_PERF_H #define _LOS_PERF_H #include "los_typedef.h" #ifdef __cplusplus #if __cplusplus extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ /** * @ingroup los_perf * Perf max sample filter task number. */ #define PERF_MAX_FILTER_TSKS 32 /** * @ingroup los_perf * Perf max sample event counter's number. */ #define PERF_MAX_EVENT 7 /** * @ingroup los_perf * Perf max backtrace depth. */ #define PERF_MAX_CALLCHAIN_DEPTH 10 /** * @ingroup los_perf * Perf sample data buffer's water mark 1/N. */ #define PERF_BUFFER_WATERMARK_ONE_N 2 /** * @ingroup los_perf * Perf state. */ enum PerfStatus { PERF_UNINIT, /**< perf isn't inited */ PERF_STARTED, /**< perf is started */ PERF_STOPED, /**< perf is stopped */ }; /*枚举类型 PerfStatus 包含了三个枚举常量: PERF_UNINIT:表示性能监控未初始化的状态。 PERF_STARTED:表示性能监控已经开始的状态。 PERF_STOPED:表示性能监控已经停止的状态。 通过使用这个枚举类型,可以方便地表示和判断性能监控的当前状态。*/ /** * @ingroup los_perf * Define the type of the perf sample data buffer water mark hook function. * */ typedef VOID (*PERF_BUF_NOTIFY_HOOK)(VOID); /** * @ingroup los_perf * Define the type of the perf sample data buffer flush hook function. * */ typedef VOID (*PERF_BUF_FLUSH_HOOK)(VOID *addr, UINT32 size); /** * @ingroup los_perf * Perf error code: Bad status. * * Value: 0x02002000 * * Solution: Follow the perf state machine. */ #define LOS_ERRNO_PERF_STATUS_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_PERF, 0x00) /** * @ingroup los_perf * Perf error code: Hardware pmu init failed. * * Value: 0x02002001 * * Solution: Check the pmu hwi irq. */ #define LOS_ERRNO_PERF_HW_INIT_ERROR LOS_ERRNO_OS_ERROR(LOS_MOD_PERF, 0x01) /** * @ingroup los_perf * Perf error code: Hrtimer init failed for hrtimer timed pmu init. * * Value: 0x02002002 * * Solution: Check the Hrtimer init. */ #define LOS_ERRNO_PERF_TIMED_INIT_ERROR LOS_ERRNO_OS_ERROR(LOS_MOD_PERF, 0x02) /** * @ingroup los_perf * Perf error code: Software pmu init failed. * * Value: 0x02002003 * * Solution: Check the Perf software events init. */ #define LOS_ERRNO_PERF_SW_INIT_ERROR LOS_ERRNO_OS_ERROR(LOS_MOD_PERF, 0x03) /** * @ingroup los_perf * Perf error code: Perf buffer init failed. * * Value: 0x02002004 * * Solution: Check the buffer init size. */ #define LOS_ERRNO_PERF_BUF_ERROR LOS_ERRNO_OS_ERROR(LOS_MOD_PERF, 0x04) /** * @ingroup los_perf * Perf error code: Perf pmu type error. * * Value: 0x02002005 * * Solution: Check whether the corresponding pmu is enabled in the menuconfig. */ #define LOS_ERRNO_PERF_INVALID_PMU LOS_ERRNO_OS_ERROR(LOS_MOD_PERF, 0x05) /** * @ingroup los_perf * Perf error code: Perf pmu config error. * * Value: 0x02002006 * * Solution: Check the config attr of event id and event period. */ #define LOS_ERRNO_PERF_PMU_CONFIG_ERROR LOS_ERRNO_OS_ERROR(LOS_MOD_PERF, 0x06) /** * @ingroup los_perf * Perf error code: Perf pmu config attr is null. * * Value: 0x02002007 * * Solution: Check if the input params of attr is null. */ #define LOS_ERRNO_PERF_CONFIG_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_PERF, 0x07) /** * @ingroup los_perf * Perf types */ enum PerfEventType { PERF_EVENT_TYPE_HW, /**< boards common hw events */ PERF_EVENT_TYPE_TIMED, /**< hrtimer timed events */ PERF_EVENT_TYPE_SW, /**< software trace events */ PERF_EVENT_TYPE_RAW, /**< boards special hw events, see enum PmuEventType in corresponding arch headfile */ PERF_EVENT_TYPE_MAX }; /*枚举类型 PerfEventType 包含了四个枚举常量: PERF_EVENT_TYPE_HW:表示硬件事件类型,用于表示板载通用硬件事件。 PERF_EVENT_TYPE_TIMED:表示定时事件类型,用于表示高精度定时器定时事件。 PERF_EVENT_TYPE_SW:表示软件事件类型,用于表示软件跟踪事件。 PERF_EVENT_TYPE_RAW:表示原始事件类型,用于表示特殊板载硬件事件,在对应的架构头文件中有关于枚举 PmuEventType 的定义。 通过使用这个枚举类型,可以方便地表示和区分不同类型的性能事件,便于在代码中进行处理和管理。*/ /** * @ingroup los_perf * Common hardware pmu events */ enum PmuHwId { PERF_COUNT_HW_CPU_CYCLES = 0, /**< cpu cycle event */ PERF_COUNT_HW_INSTRUCTIONS, /**< instruction event */ PERF_COUNT_HW_DCACHE_REFERENCES, /**< dcache access event */ PERF_COUNT_HW_DCACHE_MISSES, /**< dcache miss event */ PERF_COUNT_HW_ICACHE_REFERENCES, /**< icache access event */ PERF_COUNT_HW_ICACHE_MISSES, /**< icache miss event */ PERF_COUNT_HW_BRANCH_INSTRUCTIONS, /**< software change of pc event */ PERF_COUNT_HW_BRANCH_MISSES, /**< branch miss event */ PERF_COUNT_HW_MAX, }; /*枚举类型 PmuHwId 包含了多个枚举常量,每个常量代表一个具体的硬件事件: PERF_COUNT_HW_CPU_CYCLES:表示 CPU 循环周期事件。 PERF_COUNT_HW_INSTRUCTIONS:表示指令执行事件。 PERF_COUNT_HW_DCACHE_REFERENCES:表示数据缓存访问事件。 PERF_COUNT_HW_DCACHE_MISSES:表示数据缓存缺失事件。 PERF_COUNT_HW_ICACHE_REFERENCES:表示指令缓存访问事件。 PERF_COUNT_HW_ICACHE_MISSES:表示指令缓存缺失事件。 PERF_COUNT_HW_BRANCH_INSTRUCTIONS:表示分支指令事件。 PERF_COUNT_HW_BRANCH_MISSES:表示分支缺失事件。 通过使用这个枚举类型的常量,可以指定要监控的硬件事件,方便地在代码中进行配置和统计。*/ /** * @ingroup los_perf * Common hrtimer timed events */ enum PmuTimedId { PERF_COUNT_CPU_CLOCK = 0, /**< hrtimer timed event */ }; /** * @ingroup los_perf * Common software pmu events */ enum PmuSwId { PERF_COUNT_SW_TASK_SWITCH = 1, /**< task switch event */ PERF_COUNT_SW_IRQ_RESPONSE, /**< irq response event */ PERF_COUNT_SW_MEM_ALLOC, /**< memory alloc event */ PERF_COUNT_SW_MUX_PEND, /**< mutex pend event */ PERF_COUNT_SW_MAX, }; /*枚举类型 PmuSwId 包含了多个枚举常量,每个常量代表一个具体的软件事件: PERF_COUNT_SW_TASK_SWITCH:表示任务切换事件。 PERF_COUNT_SW_IRQ_RESPONSE:表示中断响应事件。 PERF_COUNT_SW_MEM_ALLOC:表示内存分配事件。 PERF_COUNT_SW_MUX_PEND:表示互斥锁挂起事件。 通过使用这个枚举类型的常量,可以指定要监控的软件事件,方便地在代码中进行配置和统计。*/ /** * @ingroup los_perf * perf sample data types * Config it through PerfConfigAttr->sampleType. */ enum PerfSampleType { PERF_RECORD_CPU = 1U << 0, /**< record current cpuid */ PERF_RECORD_TID = 1U << 1, /**< record current task id */ PERF_RECORD_TYPE = 1U << 2, /**< record event type */ PERF_RECORD_PERIOD = 1U << 3, /**< record event period */ PERF_RECORD_TIMESTAMP = 1U << 4, /**< record timestamp */ PERF_RECORD_IP = 1U << 5, /**< record instruction pointer */ PERF_RECORD_CALLCHAIN = 1U << 6, /**< record backtrace */ }; /*枚举类型 PerfSampleType 包含了多个枚举常量,每个常量代表一个具体的采样数据类型: PERF_RECORD_CPU:记录当前 CPU 的 ID。 PERF_RECORD_TID:记录当前任务的 ID。 PERF_RECORD_TYPE:记录事件类型。 PERF_RECORD_PERIOD:记录事件周期。 PERF_RECORD_TIMESTAMP:记录时间戳。 PERF_RECORD_IP:记录指令指针。 PERF_RECORD_CALLCHAIN:记录回溯信息。 通过使用这个枚举类型的常量,可以方便地表示和区分不同类型的性能采样数据,便于在代码中进行处理和管理。*/ /** * @ingroup los_perf * perf configuration sub event information * * This structure is used to config specific events attributes. */ typedef struct { UINT32 type; /**< enum PerfEventType */ struct { UINT32 eventId; /**< the specific event corresponds to the PerfEventType */ UINT32 period; /**< event period, for every "period"th occurrence of the event a sample will be recorded */ } events[PERF_MAX_EVENT]; /**< perf event list */ UINT32 eventsNr; /**< total perf event number */ BOOL predivided; /**< whether to prescaler (once every 64 counts), which only take effect on cpu cycle hardware event */ } PerfEventConfig; /*type:表示性能事件类型,是一个 UINT32 类型的值,对应于 PerfEventType 枚举。 events:一个数组,用于存储具体的事件信息。每个元素都包含两个成员变量: eventId:表示特定事件的标识符,是一个 UINT32 类型的值。 period:表示事件的周期,即每隔多少次事件发生时记录一次样本,是一个 UINT32 类型的值。 eventsNr:表示总共的性能事件数量,是一个 UINT32 类型的值。 predivided:表示是否进行预分频(每64个计数器进行一次),仅对CPU循环硬件事件有效。它是一个 BOOL 类型的值,用于表示是否进行预分频操作。*/ /** * @ingroup los_perf * perf configuration main information * * This structure is used to set perf sampling attributes, including events, tasks and other information. */ typedef struct { PerfEventConfig eventsCfg; /**< perf event config */ UINT32 taskIds[PERF_MAX_FILTER_TSKS]; /**< perf task filter list (whitelist) */ UINT32 taskIdsNr; /**< task numbers of task filter whiltelist, if set 0 perf will sample all tasks */ UINT32 sampleType; /**< type of data to sample defined in PerfSampleType */ BOOL needSample; /**< whether to sample data */ BOOL taskFilterEnable; /**< whether to filter tasks */ } PerfConfigAttr; extern VOID OsPerfHook(UINT32 event); #if defined(LOSCFG_KERNEL_PERF) && defined(LOSCFG_PERF_SW_PMU) #define LOS_PERF(EVENT) do { \ OsPerfHook(EVENT); \ } while (0) #else #define LOS_PERF(EVENT) #endif /*PerfConfigAttr 结构体包含以下成员变量: eventsCfg:表示性能事件的配置信息,是一个 PerfEventConfig 结构体类型的值。 taskIds:表示任务 ID 的过滤列表,是一个 UINT32 类型的数组,最多可以存储 PERF_MAX_FILTER_TSKS 个任务 ID。 taskIdsNr:表示任务 ID 的数量,是一个 UINT32 类型的值。 sampleType:表示需要采样的数据类型,是一个 UINT32 类型的值,对应于 PerfSampleType 枚举。 needSample:表示是否需要采样数据,是一个 BOOL 类型的值。 taskFilterEnable:表示是否启用任务过滤器,是一个 BOOL 类型的值。 OsPerfHook 函数用于处理性能事件的回调函数,接收一个 UINT32 类型的参数 event,表示触发的性能事件类型。 LOS_PERF 宏定义用于触发性能事件,如果操作系统配置了 LOSCFG_KERNEL_PERF 和 LOSCFG_PERF_SW_PMU 宏,则会调用 OsPerfHook 函数,否则该宏为空操作。*/ /** * @ingroup los_perf * @brief Init perf. * * @par Description: *