stats.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 0x00000000
  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. kfree(core_stats->time);
  148. kfree(core_stats->events);
  149. kfree(core_stats);
  150. }
  151. }
  152. void mod_stats_dump(struct mod_stats *mod_stats)
  153. {
  154. struct dc *dc = NULL;
  155. struct dal_logger *logger = NULL;
  156. struct core_stats *core_stats = NULL;
  157. struct stats_time_cache *time = NULL;
  158. struct stats_event_cache *events = NULL;
  159. unsigned int time_index = 1;
  160. unsigned int event_index = 0;
  161. unsigned int index = 0;
  162. struct log_entry log_entry;
  163. if (mod_stats == NULL)
  164. return;
  165. core_stats = MOD_STATS_TO_CORE(mod_stats);
  166. dc = core_stats->dc;
  167. logger = dc->ctx->logger;
  168. time = core_stats->time;
  169. events = core_stats->events;
  170. DISPLAY_STATS_BEGIN(log_entry);
  171. DISPLAY_STATS("==Display Caps==\n");
  172. DISPLAY_STATS("==Display Stats==\n");
  173. DISPLAY_STATS("%10s %10s %10s %10s %10s"
  174. " %11s %11s %17s %10s %14s"
  175. " %10s %10s %10s %10s %10s"
  176. " %10s %10s %10s %10s\n",
  177. "render", "avgRender",
  178. "minWindow", "midPoint", "maxWindow",
  179. "vsyncToFlip", "flipToVsync", "vsyncsBetweenFlip",
  180. "numFrame", "insertDuration",
  181. "vTotalMin", "vTotalMax", "eventTrigs",
  182. "vSyncTime1", "vSyncTime2", "vSyncTime3",
  183. "vSyncTime4", "vSyncTime5", "flags");
  184. for (int i = 0; i < core_stats->entry_id; i++) {
  185. if (event_index < core_stats->event_index &&
  186. i == events[event_index].entry_id) {
  187. DISPLAY_STATS("==Event==%s\n", events[event_index].event_string);
  188. event_index++;
  189. } else if (time_index < core_stats->index &&
  190. i == time[time_index].entry_id) {
  191. DISPLAY_STATS("%10u %10u %10u %10u %10u"
  192. " %11u %11u %17u %10u %14u"
  193. " %10u %10u %10u %10u %10u"
  194. " %10u %10u %10u %10u\n",
  195. time[time_index].render_time_in_us,
  196. time[time_index].avg_render_time_in_us_last_ten,
  197. time[time_index].min_window,
  198. time[time_index].lfc_mid_point_in_us,
  199. time[time_index].max_window,
  200. time[time_index].vsync_to_flip_time_in_us,
  201. time[time_index].flip_to_vsync_time_in_us,
  202. time[time_index].num_vsync_between_flips,
  203. time[time_index].num_frames_inserted,
  204. time[time_index].inserted_duration_in_us,
  205. time[time_index].v_total_min,
  206. time[time_index].v_total_max,
  207. time[time_index].event_triggers,
  208. time[time_index].v_sync_time_in_us[0],
  209. time[time_index].v_sync_time_in_us[1],
  210. time[time_index].v_sync_time_in_us[2],
  211. time[time_index].v_sync_time_in_us[3],
  212. time[time_index].v_sync_time_in_us[4],
  213. time[time_index].flags);
  214. time_index++;
  215. }
  216. }
  217. DISPLAY_STATS_END(log_entry);
  218. }
  219. void mod_stats_reset_data(struct mod_stats *mod_stats)
  220. {
  221. struct core_stats *core_stats = NULL;
  222. struct stats_time_cache *time = NULL;
  223. unsigned int index = 0;
  224. if (mod_stats == NULL)
  225. return;
  226. core_stats = MOD_STATS_TO_CORE(mod_stats);
  227. memset(core_stats->time, 0,
  228. sizeof(struct stats_time_cache) * core_stats->entries);
  229. memset(core_stats->events, 0,
  230. sizeof(struct stats_event_cache) * core_stats->event_entries);
  231. core_stats->index = 1;
  232. core_stats->event_index = 0;
  233. // Keeps track of ordering within the different stats structures
  234. core_stats->entry_id = 0;
  235. }
  236. void mod_stats_update_event(struct mod_stats *mod_stats,
  237. char *event_string,
  238. unsigned int length)
  239. {
  240. struct core_stats *core_stats = NULL;
  241. struct stats_event_cache *events = NULL;
  242. unsigned int index = 0;
  243. unsigned int copy_length = 0;
  244. if (mod_stats == NULL)
  245. return;
  246. core_stats = MOD_STATS_TO_CORE(mod_stats);
  247. if (core_stats->event_index >= core_stats->event_entries)
  248. return;
  249. events = core_stats->events;
  250. index = core_stats->event_index;
  251. copy_length = length;
  252. if (length > MOD_STATS_EVENT_STRING_MAX)
  253. copy_length = MOD_STATS_EVENT_STRING_MAX;
  254. memcpy(&events[index].event_string, event_string, copy_length);
  255. events[index].event_string[copy_length - 1] = '\0';
  256. events[index].entry_id = core_stats->entry_id;
  257. core_stats->event_index++;
  258. core_stats->entry_id++;
  259. }
  260. void mod_stats_update_flip(struct mod_stats *mod_stats,
  261. unsigned long timestamp_in_ns)
  262. {
  263. struct core_stats *core_stats = NULL;
  264. struct stats_time_cache *time = NULL;
  265. unsigned int index = 0;
  266. if (mod_stats == NULL)
  267. return;
  268. core_stats = MOD_STATS_TO_CORE(mod_stats);
  269. if (core_stats->index >= core_stats->entries)
  270. return;
  271. time = core_stats->time;
  272. index = core_stats->index;
  273. time[index].flip_timestamp_in_ns = timestamp_in_ns;
  274. time[index].render_time_in_us =
  275. (timestamp_in_ns - time[index - 1].flip_timestamp_in_ns) / 1000;
  276. if (index >= 10) {
  277. for (unsigned int i = 0; i < 10; i++)
  278. time[index].avg_render_time_in_us_last_ten +=
  279. time[index - i].render_time_in_us;
  280. time[index].avg_render_time_in_us_last_ten /= 10;
  281. }
  282. if (time[index].num_vsync_between_flips > 0)
  283. time[index].vsync_to_flip_time_in_us =
  284. (timestamp_in_ns -
  285. time[index].vupdate_timestamp_in_ns) / 1000;
  286. else
  287. time[index].vsync_to_flip_time_in_us =
  288. (timestamp_in_ns -
  289. time[index - 1].vupdate_timestamp_in_ns) / 1000;
  290. time[index].entry_id = core_stats->entry_id;
  291. core_stats->index++;
  292. core_stats->entry_id++;
  293. }
  294. void mod_stats_update_vupdate(struct mod_stats *mod_stats,
  295. unsigned long timestamp_in_ns)
  296. {
  297. struct core_stats *core_stats = NULL;
  298. struct stats_time_cache *time = NULL;
  299. unsigned int index = 0;
  300. unsigned int num_vsyncs = 0;
  301. unsigned int prev_vsync_in_ns = 0;
  302. if (mod_stats == NULL)
  303. return;
  304. core_stats = MOD_STATS_TO_CORE(mod_stats);
  305. if (core_stats->index >= core_stats->entries)
  306. return;
  307. time = core_stats->time;
  308. index = core_stats->index;
  309. num_vsyncs = time[index].num_vsync_between_flips;
  310. if (num_vsyncs < MOD_STATS_NUM_VSYNCS) {
  311. if (num_vsyncs == 0) {
  312. prev_vsync_in_ns =
  313. time[index - 1].vupdate_timestamp_in_ns;
  314. time[index].flip_to_vsync_time_in_us =
  315. (timestamp_in_ns -
  316. time[index - 1].flip_timestamp_in_ns) /
  317. 1000;
  318. } else {
  319. prev_vsync_in_ns =
  320. time[index].vupdate_timestamp_in_ns;
  321. }
  322. time[index].v_sync_time_in_us[num_vsyncs] =
  323. (timestamp_in_ns - prev_vsync_in_ns) / 1000;
  324. }
  325. time[index].vupdate_timestamp_in_ns = timestamp_in_ns;
  326. time[index].num_vsync_between_flips++;
  327. }
  328. void mod_stats_update_freesync(struct mod_stats *mod_stats,
  329. unsigned int v_total_min,
  330. unsigned int v_total_max,
  331. unsigned int event_triggers,
  332. unsigned int window_min,
  333. unsigned int window_max,
  334. unsigned int lfc_mid_point_in_us,
  335. unsigned int inserted_frames,
  336. unsigned int inserted_duration_in_us)
  337. {
  338. struct core_stats *core_stats = NULL;
  339. struct stats_time_cache *time = NULL;
  340. unsigned int index = 0;
  341. if (mod_stats == NULL)
  342. return;
  343. core_stats = MOD_STATS_TO_CORE(mod_stats);
  344. if (core_stats->index >= core_stats->entries)
  345. return;
  346. time = core_stats->time;
  347. index = core_stats->index;
  348. time[index].v_total_min = v_total_min;
  349. time[index].v_total_max = v_total_max;
  350. time[index].event_triggers = event_triggers;
  351. time[index].min_window = window_min;
  352. time[index].max_window = window_max;
  353. time[index].lfc_mid_point_in_us = lfc_mid_point_in_us;
  354. time[index].num_frames_inserted = inserted_frames;
  355. time[index].inserted_duration_in_us = inserted_duration_in_us;
  356. }