perf_os_patch_rt_thread.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /****************************************************************************
  2. * Copyright 2022 Gorgon Meducer (Email:embedded_zhuoran@hotmail.com) *
  3. * *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); *
  5. * you may not use this file except in compliance with the License. *
  6. * You may obtain a copy of the License at *
  7. * *
  8. * http://www.apache.org/licenses/LICENSE-2.0 *
  9. * *
  10. * Unless required by applicable law or agreed to in writing, software *
  11. * distributed under the License is distributed on an "AS IS" BASIS, *
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
  13. * See the License for the specific language governing permissions and *
  14. * limitations under the License. *
  15. * *
  16. ****************************************************************************/
  17. /*============================ INCLUDES ======================================*/
  18. #include <rtthread.h>
  19. #include "perf_counter.h"
  20. #include "cmsis_compiler.h"
  21. /*============================ MACROS ========================================*/
  22. #undef __WRAP_FUNC
  23. #undef WRAP_FUNC
  24. #if defined(__IS_COMPILER_ARM_COMPILER__) && __IS_COMPILER_ARM_COMPILER__
  25. # define __WRAP_FUNC(__NAME) $Sub$$##__NAME
  26. # define __ORIG_FUNC(__NAME) $Super$$##__NAME
  27. #elif (defined(__IS_COMPILER_LLVM__) && __IS_COMPILER_LLVM__) \
  28. || (defined(__IS_COMPILER_GCC__) && __IS_COMPILER_GCC__)
  29. # define __WRAP_FUNC(__NAME) __wrap_##__NAME
  30. # define __ORIG_FUNC(__NAME) __real_##__NAME
  31. #endif
  32. #define WRAP_FUNC(__NAME) __WRAP_FUNC(__NAME)
  33. #define ORIG_FUNC(__NAME) __ORIG_FUNC(__NAME)
  34. struct __task_cycle_info_t {
  35. task_cycle_info_t tInfo;
  36. int64_t lLastTimeStamp;
  37. task_cycle_info_agent_t tList;
  38. uint32_t wMagicWord;
  39. } ;
  40. #ifndef RT_USING_HOOK
  41. #error "In order to use perf_counter:RT-Thread-Patch, please define RT_USING_HOOK \
  42. in rtconfig.h. If you don't want to use this patch, please un-select it in RTE."
  43. #endif
  44. /*============================ TYPES =========================================*/
  45. /*============================ GLOBAL VARIABLES ==============================*/
  46. /*============================ LOCAL VARIABLES ===============================*/
  47. /*============================ PROTOTYPES ====================================*/
  48. extern void __on_context_switch_in(uint32_t *pwStack);
  49. extern void __on_context_switch_out(uint32_t *pwStack);
  50. extern struct rt_thread *rt_current_thread;
  51. /*============================ IMPLEMENTATION ================================*/
  52. void __rt_thread_scheduler_hook(struct rt_thread *from, struct rt_thread *to)
  53. {
  54. if (NULL != from) {
  55. __on_context_switch_out(from->stack_addr);
  56. }
  57. __on_context_switch_in(to->stack_addr);
  58. }
  59. task_cycle_info_t * get_rtos_task_cycle_info(void)
  60. {
  61. return &(((struct __task_cycle_info_t *)rt_current_thread->stack_addr)->tInfo);
  62. }
  63. void __perf_os_patch_init(void)
  64. {
  65. rt_tick_sethook(user_code_insert_to_systick_handler);
  66. #if !defined(PKG_USING_PERF_COUNTER) || (defined(PKG_PERF_COUNTER_USING_THREAD_STATISTIC))
  67. rt_scheduler_sethook(__rt_thread_scheduler_hook);
  68. #endif
  69. }
  70. #ifdef PKG_USING_PERF_COUNTER
  71. void __ensure_systick_wrapper(void)
  72. {
  73. }
  74. #define DBG_TAG "perf_counter"
  75. #define DBG_LVL DBG_INFO
  76. #include <rtdbg.h>
  77. static int _perf_counter_init(void)
  78. {
  79. extern uint32_t SystemCoreClock;
  80. init_cycle_counter(true);
  81. LOG_I("perf_counter init, SystemCoreClock:%d", SystemCoreClock);
  82. return 0;
  83. }
  84. INIT_PREV_EXPORT(_perf_counter_init);
  85. #endif /* PKG_USING_PERF_COUNTER */