stats.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright 2016 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: AMD
  23. *
  24. */
  25. #include "mod_stats.h"
  26. #include "dm_services.h"
  27. #include "dc.h"
  28. #include "core_types.h"
  29. #define DAL_STATS_ENABLE_REGKEY "DalStatsEnable"
  30. #define DAL_STATS_ENABLE_REGKEY_DEFAULT 0x00000001
  31. #define DAL_STATS_ENABLE_REGKEY_ENABLED 0x00000001
  32. #define DAL_STATS_ENTRIES_REGKEY "DalStatsEntries"
  33. #define DAL_STATS_ENTRIES_REGKEY_DEFAULT 0x00350000
  34. #define DAL_STATS_ENTRIES_REGKEY_MAX 0x01000000
  35. #define MOD_STATS_NUM_VSYNCS 5
  36. struct stats_time_cache {
  37. unsigned long flip_timestamp_in_ns;
  38. unsigned long vupdate_timestamp_in_ns;
  39. unsigned int render_time_in_us;
  40. unsigned int avg_render_time_in_us_last_ten;
  41. unsigned int v_sync_time_in_us[MOD_STATS_NUM_VSYNCS];
  42. unsigned int num_vsync_between_flips;
  43. unsigned int flip_to_vsync_time_in_us;
  44. unsigned int vsync_to_flip_time_in_us;
  45. unsigned int min_window;
  46. unsigned int max_window;
  47. unsigned int v_total_min;
  48. unsigned int v_total_max;
  49. unsigned int event_triggers;
  50. unsigned int lfc_mid_point_in_us;
  51. unsigned int num_frames_inserted;
  52. unsigned int inserted_duration_in_us;
  53. unsigned int flags;
  54. };
  55. struct core_stats {
  56. struct mod_stats public;
  57. struct dc *dc;
  58. struct stats_time_cache *time;
  59. unsigned int index;
  60. bool enabled;
  61. unsigned int entries;
  62. };
  63. #define MOD_STATS_TO_CORE(mod_stats)\
  64. container_of(mod_stats, struct core_stats, public)
  65. bool mod_stats_init(struct mod_stats *mod_stats)
  66. {
  67. bool result = false;
  68. struct core_stats *core_stats = NULL;
  69. struct dc *dc = NULL;
  70. if (mod_stats == NULL)
  71. return false;
  72. core_stats = MOD_STATS_TO_CORE(mod_stats);
  73. dc = core_stats->dc;
  74. return result;
  75. }
  76. struct mod_stats *mod_stats_create(struct dc *dc)
  77. {
  78. struct core_stats *core_stats = NULL;
  79. struct persistent_data_flag flag;
  80. unsigned int reg_data;
  81. int i = 0;
  82. core_stats = kzalloc(sizeof(struct core_stats), GFP_KERNEL);
  83. if (core_stats == NULL)
  84. goto fail_alloc_context;
  85. if (dc == NULL)
  86. goto fail_construct;
  87. core_stats->dc = dc;
  88. core_stats->enabled = DAL_STATS_ENABLE_REGKEY_DEFAULT;
  89. if (dm_read_persistent_data(dc->ctx, NULL, NULL,
  90. DAL_STATS_ENABLE_REGKEY,
  91. &reg_data, sizeof(unsigned int), &flag))
  92. core_stats->enabled = reg_data;
  93. core_stats->entries = DAL_STATS_ENTRIES_REGKEY_DEFAULT;
  94. if (dm_read_persistent_data(dc->ctx, NULL, NULL,
  95. DAL_STATS_ENTRIES_REGKEY,
  96. &reg_data, sizeof(unsigned int), &flag)) {
  97. if (reg_data > DAL_STATS_ENTRIES_REGKEY_MAX)
  98. core_stats->entries = DAL_STATS_ENTRIES_REGKEY_MAX;
  99. else
  100. core_stats->entries = reg_data;
  101. }
  102. core_stats->time = kzalloc(sizeof(struct stats_time_cache) * core_stats->entries,
  103. GFP_KERNEL);
  104. if (core_stats->time == NULL)
  105. goto fail_construct;
  106. /* Purposely leave index 0 unused so we don't need special logic to
  107. * handle calculation cases that depend on previous flip data.
  108. */
  109. core_stats->index = 1;
  110. return &core_stats->public;
  111. fail_construct:
  112. kfree(core_stats);
  113. fail_alloc_context:
  114. return NULL;
  115. }
  116. void mod_stats_destroy(struct mod_stats *mod_stats)
  117. {
  118. if (mod_stats != NULL) {
  119. struct core_stats *core_stats = MOD_STATS_TO_CORE(mod_stats);
  120. if (core_stats->time != NULL)
  121. kfree(core_stats->time);
  122. kfree(core_stats);
  123. }
  124. }
  125. void mod_stats_dump(struct mod_stats *mod_stats)
  126. {
  127. struct dc *dc = NULL;
  128. struct dal_logger *logger = NULL;
  129. struct core_stats *core_stats = NULL;
  130. struct stats_time_cache *time = NULL;
  131. unsigned int index = 0;
  132. if (mod_stats == NULL)
  133. return;
  134. core_stats = MOD_STATS_TO_CORE(mod_stats);
  135. dc = core_stats->dc;
  136. logger = dc->ctx->logger;
  137. time = core_stats->time;
  138. //LogEntry* pLog = GetLog()->Open(LogMajor_ISR, LogMinor_ISR_FreeSyncSW);
  139. //if (!pLog->IsDummyEntry())
  140. {
  141. dm_logger_write(logger, LOG_PROFILING, "==Display Caps==\n");
  142. dm_logger_write(logger, LOG_PROFILING, "\n");
  143. dm_logger_write(logger, LOG_PROFILING, "\n");
  144. dm_logger_write(logger, LOG_PROFILING, "==Stats==\n");
  145. dm_logger_write(logger, LOG_PROFILING,
  146. "render avgRender minWindow midPoint maxWindow vsyncToFlip flipToVsync #vsyncBetweenFlip #frame insertDuration vTotalMin vTotalMax eventTrigs vSyncTime1 vSyncTime2 vSyncTime3 vSyncTime4 vSyncTime5 flags\n");
  147. for (int i = 0; i < core_stats->index && i < core_stats->entries; i++) {
  148. dm_logger_write(logger, LOG_PROFILING,
  149. "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u\n",
  150. time[i].render_time_in_us,
  151. time[i].avg_render_time_in_us_last_ten,
  152. time[i].min_window,
  153. time[i].lfc_mid_point_in_us,
  154. time[i].max_window,
  155. time[i].vsync_to_flip_time_in_us,
  156. time[i].flip_to_vsync_time_in_us,
  157. time[i].num_vsync_between_flips,
  158. time[i].num_frames_inserted,
  159. time[i].inserted_duration_in_us,
  160. time[i].v_total_min,
  161. time[i].v_total_max,
  162. time[i].event_triggers,
  163. time[i].v_sync_time_in_us[0],
  164. time[i].v_sync_time_in_us[1],
  165. time[i].v_sync_time_in_us[2],
  166. time[i].v_sync_time_in_us[3],
  167. time[i].v_sync_time_in_us[4],
  168. time[i].flags);
  169. }
  170. }
  171. //GetLog()->Close(pLog);
  172. //GetLog()->UnSetLogMask(LogMajor_ISR, LogMinor_ISR_FreeSyncSW);
  173. }
  174. void mod_stats_reset_data(struct mod_stats *mod_stats)
  175. {
  176. struct core_stats *core_stats = NULL;
  177. struct stats_time_cache *time = NULL;
  178. unsigned int index = 0;
  179. if (mod_stats == NULL)
  180. return;
  181. core_stats = MOD_STATS_TO_CORE(mod_stats);
  182. memset(core_stats->time, 0,
  183. sizeof(struct stats_time_cache) * core_stats->entries);
  184. core_stats->index = 0;
  185. }
  186. void mod_stats_update_flip(struct mod_stats *mod_stats,
  187. unsigned long timestamp_in_ns)
  188. {
  189. struct core_stats *core_stats = NULL;
  190. struct stats_time_cache *time = NULL;
  191. unsigned int index = 0;
  192. if (mod_stats == NULL)
  193. return;
  194. core_stats = MOD_STATS_TO_CORE(mod_stats);
  195. if (core_stats->index >= core_stats->entries)
  196. return;
  197. time = core_stats->time;
  198. index = core_stats->index;
  199. time[index].flip_timestamp_in_ns = timestamp_in_ns;
  200. time[index].render_time_in_us =
  201. timestamp_in_ns - time[index - 1].flip_timestamp_in_ns;
  202. if (index >= 10) {
  203. for (unsigned int i = 0; i < 10; i++)
  204. time[index].avg_render_time_in_us_last_ten +=
  205. time[index - i].render_time_in_us;
  206. time[index].avg_render_time_in_us_last_ten /= 10;
  207. }
  208. if (time[index].num_vsync_between_flips > 0)
  209. time[index].vsync_to_flip_time_in_us =
  210. timestamp_in_ns - time[index].vupdate_timestamp_in_ns;
  211. else
  212. time[index].vsync_to_flip_time_in_us =
  213. timestamp_in_ns - time[index - 1].vupdate_timestamp_in_ns;
  214. core_stats->index++;
  215. }
  216. void mod_stats_update_vupdate(struct mod_stats *mod_stats,
  217. unsigned long timestamp_in_ns)
  218. {
  219. struct core_stats *core_stats = NULL;
  220. struct stats_time_cache *time = NULL;
  221. unsigned int index = 0;
  222. if (mod_stats == NULL)
  223. return;
  224. core_stats = MOD_STATS_TO_CORE(mod_stats);
  225. if (core_stats->index >= core_stats->entries)
  226. return;
  227. time = core_stats->time;
  228. index = core_stats->index;
  229. time[index].vupdate_timestamp_in_ns = timestamp_in_ns;
  230. if (time[index].num_vsync_between_flips < MOD_STATS_NUM_VSYNCS)
  231. time[index].v_sync_time_in_us[time[index].num_vsync_between_flips] =
  232. timestamp_in_ns - time[index - 1].vupdate_timestamp_in_ns;
  233. time[index].flip_to_vsync_time_in_us =
  234. timestamp_in_ns - time[index - 1].flip_timestamp_in_ns;
  235. time[index].num_vsync_between_flips++;
  236. }
  237. void mod_stats_update_freesync(struct mod_stats *mod_stats,
  238. unsigned int v_total_min,
  239. unsigned int v_total_max,
  240. unsigned int event_triggers,
  241. unsigned int window_min,
  242. unsigned int window_max,
  243. unsigned int lfc_mid_point_in_us,
  244. unsigned int inserted_frames,
  245. unsigned int inserted_duration_in_us)
  246. {
  247. struct core_stats *core_stats = NULL;
  248. struct stats_time_cache *time = NULL;
  249. unsigned int index = 0;
  250. if (mod_stats == NULL)
  251. return;
  252. core_stats = MOD_STATS_TO_CORE(mod_stats);
  253. if (core_stats->index >= core_stats->entries)
  254. return;
  255. time = core_stats->time;
  256. index = core_stats->index;
  257. time[index].v_total_min = v_total_min;
  258. time[index].v_total_max = v_total_max;
  259. time[index].event_triggers = event_triggers;
  260. time[index].min_window = window_min;
  261. time[index].max_window = window_max;
  262. time[index].lfc_mid_point_in_us = lfc_mid_point_in_us;
  263. time[index].num_frames_inserted = inserted_frames;
  264. time[index].inserted_duration_in_us = inserted_duration_in_us;
  265. }