profiling.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2017. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. *
  31. * The following software/firmware and/or related documentation ("MediaTek Software")
  32. * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
  33. * applicable license agreements with MediaTek Inc.
  34. */
  35. #include <pal_typedefs.h>
  36. #include <pal_log.h>
  37. #include <profiling.h>
  38. #define PROFILING_DEFAULT_EN (0)
  39. #define MOD "PROFILING"
  40. #if PROFILING_DEFAULT_EN
  41. static uint32_t g_profiling_enable = 1;
  42. #else
  43. static uint32_t g_profiling_enable = 0;
  44. #endif
  45. unsigned int g_profile_level = 0;
  46. void set_profiling_enable(uint32_t enable)
  47. {
  48. g_profiling_enable = enable;
  49. return;
  50. }
  51. void profiling_reset(struct profile_info *ctx)
  52. {
  53. uint32_t i;
  54. ctx->start_time = 0;
  55. ctx->running = 0;
  56. ctx->cur_time_slot_idx = 0;
  57. for (i = 0; i < NUM_TIME_SLOT; i++) {
  58. ctx->time[i].name = NULL;
  59. ctx->time[i].time = 0;
  60. }
  61. return;
  62. }
  63. void profiling_start(struct profile_info *ctx, char *prompt_str,
  64. char *time_slot_name)
  65. {
  66. if (0 == g_profiling_enable)
  67. return;
  68. if (NULL == prompt_str)
  69. prompt_str = "NULL";
  70. pal_log_debug("[%s] %s start\n", MOD, prompt_str);
  71. if (NULL != time_slot_name && (ctx->cur_time_slot_idx < NUM_TIME_SLOT))
  72. ctx->time[ctx->cur_time_slot_idx].name = time_slot_name;
  73. ctx->running = 1;
  74. ctx->start_time = pal_get_timer(0);
  75. return;
  76. }
  77. void profiling_stop(struct profile_info *ctx, char *prompt_str,
  78. uint32_t advance_time_slot)
  79. {
  80. if (0 == g_profiling_enable)
  81. return;
  82. if (0 == ctx->running)
  83. return;
  84. /* behavior similar to we stop then start timer immediately, but we don't stop timer actually */
  85. if (ctx->cur_time_slot_idx < NUM_TIME_SLOT) {
  86. ctx->time[ctx->cur_time_slot_idx].time += pal_get_timer(ctx->start_time);
  87. if (advance_time_slot)
  88. ctx->cur_time_slot_idx++;
  89. } else
  90. pal_log_err("profiling time slot depleted\n");
  91. if (NULL == prompt_str)
  92. prompt_str = "NULL";
  93. pal_log_debug("[%s] %s stop\n", MOD, prompt_str);
  94. ctx->running = 0;
  95. return;
  96. }
  97. void profiling_end(struct profile_info *ctx, char *prompt_str)
  98. {
  99. uint32_t i = 0;
  100. uint32_t total_time = 0;
  101. if (0 == g_profiling_enable)
  102. return;
  103. profiling_stop(ctx, prompt_str, 0);
  104. if (NULL == prompt_str)
  105. prompt_str = "NULL";
  106. pal_log_debug("[%s] %s end\n", MOD, prompt_str);
  107. for (i = 0; i < NUM_TIME_SLOT; i++) {
  108. char *time_slot_name = NULL;
  109. if (0 == ctx->time[i].time)
  110. break;
  111. time_slot_name = ctx->time[i].name != NULL ? ctx->time[i].name : "NULL";
  112. pal_log_info("[%s][%s][%s][0x%x] time = %dms\n", MOD, prompt_str,
  113. time_slot_name, i, ctx->time[i].time);
  114. total_time += ctx->time[i].time;
  115. }
  116. pal_log_info("[%s][%s] total time = %dms\n", MOD, prompt_str, total_time);
  117. return;
  118. }