stats.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 DAL_STATS_EVENT_ENTRIES_DEFAULT 0x00000100
  36. #define MOD_STATS_NUM_VSYNCS 5
  37. #define MOD_STATS_EVENT_STRING_MAX 512
  38. struct stats_time_cache {
  39. unsigned int entry_id;
  40. unsigned long flip_timestamp_in_ns;
  41. unsigned long vupdate_timestamp_in_ns;
  42. unsigned int render_time_in_us;
  43. unsigned int avg_render_time_in_us_last_ten;
  44. unsigned int v_sync_time_in_us[MOD_STATS_NUM_VSYNCS];
  45. unsigned int num_vsync_between_flips;
  46. unsigned int flip_to_vsync_time_in_us;
  47. unsigned int vsync_to_flip_time_in_us;
  48. unsigned int min_window;
  49. unsigned int max_window;
  50. unsigned int v_total_min;
  51. unsigned int v_total_max;
  52. unsigned int event_triggers;
  53. unsigned int lfc_mid_point_in_us;
  54. unsigned int num_frames_inserted;
  55. unsigned int inserted_duration_in_us;
  56. unsigned int flags;
  57. };
  58. struct stats_event_cache {
  59. unsigned int entry_id;
  60. char event_string[MOD_STATS_EVENT_STRING_MAX];
  61. };
  62. struct core_stats {
  63. struct mod_stats public;
  64. struct dc *dc;
  65. bool enabled;
  66. unsigned int entries;
  67. unsigned int event_entries;
  68. unsigned int entry_id;
  69. struct stats_time_cache *time;
  70. unsigned int index;
  71. struct stats_event_cache *events;
  72. unsigned int event_index;
  73. };
  74. #define MOD_STATS_TO_CORE(mod_stats)\
  75. container_of(mod_stats, struct core_stats, public)
  76. bool mod_stats_init(struct mod_stats *mod_stats)
  77. {
  78. bool result = false;
  79. struct core_stats *core_stats = NULL;
  80. struct dc *dc = NULL;
  81. if (mod_stats == NULL)
  82. return false;
  83. core_stats = MOD_STATS_TO_CORE(mod_stats);
  84. dc = core_stats->dc;
  85. return result;
  86. }
  87. struct mod_stats *mod_stats_create(struct dc *dc)
  88. {
  89. struct core_stats *core_stats = NULL;
  90. struct persistent_data_flag flag;
  91. unsigned int reg_data;
  92. int i = 0;
  93. if (dc == NULL)
  94. goto fail_construct;
  95. core_stats = kzalloc(sizeof(struct core_stats), GFP_KERNEL);
  96. if (core_stats == NULL)
  97. goto fail_construct;
  98. core_stats->dc = dc;
  99. core_stats->enabled = DAL_STATS_ENABLE_REGKEY_DEFAULT;
  100. if (dm_read_persistent_data(dc->ctx, NULL, NULL,
  101. DAL_STATS_ENABLE_REGKEY,
  102. &reg_data, sizeof(unsigned int), &flag))
  103. core_stats->enabled = reg_data;
  104. if (core_stats->enabled) {
  105. core_stats->entries = DAL_STATS_ENTRIES_REGKEY_DEFAULT;
  106. if (dm_read_persistent_data(dc->ctx, NULL, NULL,
  107. DAL_STATS_ENTRIES_REGKEY,
  108. &reg_data, sizeof(unsigned int), &flag)) {
  109. if (reg_data > DAL_STATS_ENTRIES_REGKEY_MAX)
  110. core_stats->entries = DAL_STATS_ENTRIES_REGKEY_MAX;
  111. else
  112. core_stats->entries = reg_data;
  113. }
  114. core_stats->time = kcalloc(core_stats->entries,
  115. sizeof(struct stats_time_cache),
  116. GFP_KERNEL);
  117. if (core_stats->time == NULL)
  118. goto fail_construct_time;
  119. core_stats->event_entries = DAL_STATS_EVENT_ENTRIES_DEFAULT;
  120. core_stats->events = kcalloc(core_stats->event_entries,
  121. sizeof(struct stats_event_cache),
  122. GFP_KERNEL);
  123. if (core_stats->events == NULL)
  124. goto fail_construct_events;
  125. } else {
  126. core_stats->entries = 0;
  127. }
  128. /* Purposely leave index 0 unused so we don't need special logic to
  129. * handle calculation cases that depend on previous flip data.
  130. */
  131. core_stats->index = 1;
  132. core_stats->event_index = 0;
  133. // Keeps track of ordering within the different stats structures
  134. core_stats->entry_id = 0;
  135. return &core_stats->public;
  136. fail_construct_events:
  137. kfree(core_stats->time);
  138. fail_construct_time:
  139. kfree(core_stats);
  140. fail_construct:
  141. return NULL;
  142. }
  143. void mod_stats_destroy(struct mod_stats *mod_stats)
  144. {
  145. if (mod_stats != NULL) {
  146. struct core_stats *core_stats = MOD_STATS_TO_CORE(mod_stats);
  147. if (core_stats->time != NULL)
  148. kfree(core_stats->time);
  149. if (core_stats->events != NULL)
  150. kfree(core_stats->events);
  151. kfree(core_stats);
  152. }
  153. }
  154. void mod_stats_dump(struct mod_stats *mod_stats)
  155. {
  156. struct dc *dc = NULL;
  157. struct dal_logger *logger = NULL;
  158. struct core_stats *core_stats = NULL;
  159. struct stats_time_cache *time = NULL;
  160. struct stats_event_cache *events = NULL;
  161. unsigned int time_index = 1;
  162. unsigned int event_index = 0;
  163. unsigned int index = 0;
  164. struct log_entry log_entry;
  165. if (mod_stats == NULL)
  166. return;
  167. core_stats = MOD_STATS_TO_CORE(mod_stats);
  168. dc = core_stats->dc;
  169. logger = dc->ctx->logger;
  170. time = core_stats->time;
  171. events = core_stats->events;
  172. DISPLAY_STATS_BEGIN(log_entry);
  173. DISPLAY_STATS("==Display Caps==\n");
  174. DISPLAY_STATS("==Display Stats==\n");
  175. DISPLAY_STATS("%10s %10s %10s %10s %10s"
  176. " %11s %11s %17s %10s %14s"
  177. " %10s %10s %10s %10s %10s"
  178. " %10s %10s %10s %10s\n",
  179. "render", "avgRender",
  180. "minWindow", "midPoint", "maxWindow",
  181. "vsyncToFlip", "flipToVsync", "vsyncsBetweenFlip",
  182. "numFrame", "insertDuration",
  183. "vTotalMin", "vTotalMax", "eventTrigs",
  184. "vSyncTime1", "vSyncTime2", "vSyncTime3",
  185. "vSyncTime4", "vSyncTime5", "flags");
  186. for (int i = 0; i < core_stats->entry_id; i++) {
  187. if (event_index < core_stats->event_index &&
  188. i == events[event_index].entry_id) {
  189. DISPLAY_STATS("%s\n", events[event_index].event_string);
  190. event_index++;
  191. } else if (time_index < core_stats->index &&
  192. i == time[time_index].entry_id) {
  193. DISPLAY_STATS("%10u %10u %10u %10u %10u"
  194. " %11u %11u %17u %10u %14u"
  195. " %10u %10u %10u %10u %10u"
  196. " %10u %10u %10u %10u\n",
  197. time[time_index].render_time_in_us,
  198. time[time_index].avg_render_time_in_us_last_ten,
  199. time[time_index].min_window,
  200. time[time_index].lfc_mid_point_in_us,
  201. time[time_index].max_window,
  202. time[time_index].vsync_to_flip_time_in_us,
  203. time[time_index].flip_to_vsync_time_in_us,
  204. time[time_index].num_vsync_between_flips,
  205. time[time_index].num_frames_inserted,
  206. time[time_index].inserted_duration_in_us,
  207. time[time_index].v_total_min,
  208. time[time_index].v_total_max,
  209. time[time_index].event_triggers,
  210. time[time_index].v_sync_time_in_us[0],
  211. time[time_index].v_sync_time_in_us[1],
  212. time[time_index].v_sync_time_in_us[2],
  213. time[time_index].v_sync_time_in_us[3],
  214. time[time_index].v_sync_time_in_us[4],
  215. time[time_index].flags);
  216. time_index++;
  217. }
  218. }
  219. DISPLAY_STATS_END(log_entry);
  220. }
  221. void mod_stats_reset_data(struct mod_stats *mod_stats)
  222. {
  223. struct core_stats *core_stats = NULL;
  224. struct stats_time_cache *time = NULL;
  225. unsigned int index = 0;
  226. if (mod_stats == NULL)
  227. return;
  228. core_stats = MOD_STATS_TO_CORE(mod_stats);
  229. memset(core_stats->time, 0,
  230. sizeof(struct stats_time_cache) * core_stats->entries);
  231. memset(core_stats->events, 0,
  232. sizeof(struct stats_event_cache) * core_stats->event_entries);
  233. core_stats->index = 1;
  234. core_stats->event_index = 0;
  235. // Keeps track of ordering within the different stats structures
  236. core_stats->entry_id = 0;
  237. }
  238. void mod_stats_update_event(struct mod_stats *mod_stats,
  239. char *event_string,
  240. unsigned int length)
  241. {
  242. struct core_stats *core_stats = NULL;
  243. struct stats_event_cache *events = NULL;
  244. unsigned int index = 0;
  245. unsigned int copy_length = 0;
  246. if (mod_stats == NULL)
  247. return;
  248. core_stats = MOD_STATS_TO_CORE(mod_stats);
  249. if (core_stats->event_index >= core_stats->event_entries)
  250. return;
  251. events = core_stats->events;
  252. index = core_stats->event_index;
  253. copy_length = length;
  254. if (length > MOD_STATS_EVENT_STRING_MAX)
  255. copy_length = MOD_STATS_EVENT_STRING_MAX;
  256. memcpy(&events[index].event_string, event_string, copy_length);
  257. events[index].event_string[copy_length - 1] = '\0';
  258. events[index].entry_id = core_stats->entry_id;
  259. core_stats->event_index++;
  260. core_stats->entry_id++;
  261. }
  262. void mod_stats_update_flip(struct mod_stats *mod_stats,
  263. unsigned long timestamp_in_ns)
  264. {
  265. struct core_stats *core_stats = NULL;
  266. struct stats_time_cache *time = NULL;
  267. unsigned int index = 0;
  268. if (mod_stats == NULL)
  269. return;
  270. core_stats = MOD_STATS_TO_CORE(mod_stats);
  271. if (core_stats->index >= core_stats->entries)
  272. return;
  273. time = core_stats->time;
  274. index = core_stats->index;
  275. time[index].flip_timestamp_in_ns = timestamp_in_ns;
  276. time[index].render_time_in_us =
  277. (timestamp_in_ns - time[index - 1].flip_timestamp_in_ns) / 1000;
  278. if (index >= 10) {
  279. for (unsigned int i = 0; i < 10; i++)
  280. time[index].avg_render_time_in_us_last_ten +=
  281. time[index - i].render_time_in_us;
  282. time[index].avg_render_time_in_us_last_ten /= 10;
  283. }
  284. if (time[index].num_vsync_between_flips > 0)
  285. time[index].vsync_to_flip_time_in_us =
  286. (timestamp_in_ns -
  287. time[index].vupdate_timestamp_in_ns) / 1000;
  288. else
  289. time[index].vsync_to_flip_time_in_us =
  290. (timestamp_in_ns -
  291. time[index - 1].vupdate_timestamp_in_ns) / 1000;
  292. time[index].entry_id = core_stats->entry_id;
  293. core_stats->index++;
  294. core_stats->entry_id++;
  295. }
  296. void mod_stats_update_vupdate(struct mod_stats *mod_stats,
  297. unsigned long timestamp_in_ns)
  298. {
  299. struct core_stats *core_stats = NULL;
  300. struct stats_time_cache *time = NULL;
  301. unsigned int index = 0;
  302. unsigned int num_vsyncs = 0;
  303. unsigned int prev_vsync_in_ns = 0;
  304. if (mod_stats == NULL)
  305. return;
  306. core_stats = MOD_STATS_TO_CORE(mod_stats);
  307. if (core_stats->index >= core_stats->entries)
  308. return;
  309. time = core_stats->time;
  310. index = core_stats->index;
  311. num_vsyncs = time[index].num_vsync_between_flips;
  312. if (num_vsyncs < MOD_STATS_NUM_VSYNCS) {
  313. if (num_vsyncs == 0) {
  314. prev_vsync_in_ns =
  315. time[index - 1].vupdate_timestamp_in_ns;
  316. time[index].flip_to_vsync_time_in_us =
  317. (timestamp_in_ns -
  318. time[index - 1].flip_timestamp_in_ns) /
  319. 1000;
  320. } else {
  321. prev_vsync_in_ns =
  322. time[index].vupdate_timestamp_in_ns;
  323. }
  324. time[index].v_sync_time_in_us[num_vsyncs] =
  325. (timestamp_in_ns - prev_vsync_in_ns) / 1000;
  326. }
  327. time[index].vupdate_timestamp_in_ns = timestamp_in_ns;
  328. time[index].num_vsync_between_flips++;
  329. }
  330. void mod_stats_update_freesync(struct mod_stats *mod_stats,
  331. unsigned int v_total_min,
  332. unsigned int v_total_max,
  333. unsigned int event_triggers,
  334. unsigned int window_min,
  335. unsigned int window_max,
  336. unsigned int lfc_mid_point_in_us,
  337. unsigned int inserted_frames,
  338. unsigned int inserted_duration_in_us)
  339. {
  340. struct core_stats *core_stats = NULL;
  341. struct stats_time_cache *time = NULL;
  342. unsigned int index = 0;
  343. if (mod_stats == NULL)
  344. return;
  345. core_stats = MOD_STATS_TO_CORE(mod_stats);
  346. if (core_stats->index >= core_stats->entries)
  347. return;
  348. time = core_stats->time;
  349. index = core_stats->index;
  350. time[index].v_total_min = v_total_min;
  351. time[index].v_total_max = v_total_max;
  352. time[index].event_triggers = event_triggers;
  353. time[index].min_window = window_min;
  354. time[index].max_window = window_max;
  355. time[index].lfc_mid_point_in_us = lfc_mid_point_in_us;
  356. time[index].num_frames_inserted = inserted_frames;
  357. time[index].inserted_duration_in_us = inserted_duration_in_us;
  358. }