bw_monitor.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. // SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause
  2. /*
  3. * Copyright (c) 2019, Chips&Media
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "vpuapifunc.h"
  27. #include "main_helper.h"
  28. #include "bw_monitor.h"
  29. #include "debug.h"
  30. #define MBYTE (1024*1024)
  31. static char picTypeString[10][4] = {"I", "P", "B", "", "", "IDR", "", "", "", ""};//picType enum in vpuapi.h
  32. typedef void* BWData;
  33. typedef struct _tag_bw_data_struct {
  34. Uint32 vcpu_bw_rd;
  35. Uint32 vcpu_bw_wr;
  36. Uint32 sdma_bw_rd;
  37. Uint32 sdma_bw_wr;
  38. Uint32 vcpu_dma_bw_wr;
  39. Uint32 vcpu_dma_bw_rd;
  40. Uint32 vcpu_nbdma_bw_wr;
  41. Uint32 vcpu_nbdma_bw_rd;
  42. Uint32 prescan_wr;
  43. Uint32 prescan_rd;
  44. Uint32 arid_pri_total;
  45. Uint32 awid_pri_total;
  46. Uint32 arid_sec_total;
  47. Uint32 awid_sec_total;
  48. Uint32 arid_mclk_total;
  49. Uint32 awid_mclk_total;
  50. Uint32 total_write;
  51. Uint32 total_read;
  52. Uint32 total_bandwidth;
  53. Uint32 subblock_bw_pri_rd[16];
  54. Uint32 subblock_bw_pri_wr[16];
  55. Uint32 subblock_bw_sec_rd[16];
  56. Uint32 subblock_bw_sec_wr[16];
  57. Uint32 subblock_bw_mclk_rd[16];
  58. Uint32 subblock_bw_mclk_wr[16];
  59. } BWGdiData;
  60. typedef struct _tag_BWMonitorops {
  61. BWCtx (*allocate)(Uint32);
  62. void (*release)(BWCtx);
  63. void (*reset)(BWCtx);
  64. BWData (*get_data)(BWCtx, Uint32 numCores);
  65. void (*print)(BWCtx, BWData, Uint32);
  66. } BWMonitorOps_t;
  67. #define BW_CONTEXT_COMMON_VARS \
  68. BWMonitorOps_t* ops; \
  69. Uint32 coreIndex; \
  70. Uint32 instanceIndex; \
  71. Uint32 productId; \
  72. CodecInst* instance; \
  73. Uint32 numFrames; \
  74. Uint64 totalPriRead; \
  75. Uint64 totalPriWrite; \
  76. Uint64 totalSecRead; \
  77. Uint64 totalSecWrite; \
  78. Uint64 totalProcRead; \
  79. Uint64 totalProcWrite; \
  80. Uint64 total; \
  81. BOOL enableReportPerFrame; \
  82. osal_file_t fpBWTotal; \
  83. BWData* data; \
  84. char strLogDir[256];
  85. typedef struct _tag_bw_common_context_struct {
  86. BW_CONTEXT_COMMON_VARS
  87. } BWCommonCtx;
  88. /************************************************************************/
  89. /* DUMMY */
  90. /************************************************************************/
  91. typedef struct _tag_dummy_bw_context_struct {
  92. BW_CONTEXT_COMMON_VARS
  93. } dummy_bw_ctx_t;
  94. static BWCtx
  95. dummy_bw_monitor_allocate(
  96. Uint32 coreIndex
  97. )
  98. {
  99. dummy_bw_ctx_t* context = (dummy_bw_ctx_t*)osal_malloc(sizeof(dummy_bw_ctx_t));
  100. UNREFERENCED_PARAMETER(coreIndex);
  101. return (BWCtx)context;
  102. }
  103. static void
  104. dummy_bw_monitor_release(
  105. BWCtx ctx
  106. )
  107. {
  108. UNREFERENCED_PARAMETER(ctx);
  109. return;
  110. }
  111. static void
  112. dummy_bw_monitor_reset(
  113. BWCtx ctx
  114. )
  115. {
  116. UNREFERENCED_PARAMETER(ctx);
  117. return;
  118. }
  119. static BWData
  120. dummy_bw_monitor_get_data(
  121. BWCtx ctx,
  122. Uint32 numCores
  123. )
  124. {
  125. BWGdiData* data;
  126. data = (BWGdiData*)osal_malloc(sizeof(BWGdiData));
  127. osal_memset((void*)data, 0x00, sizeof(BWGdiData));
  128. return (BWData)data;
  129. }
  130. static void
  131. dummy_bw_monitor_print(
  132. BWCtx context,
  133. BWData data,
  134. Uint32 picType
  135. )
  136. {
  137. return ;
  138. }
  139. static BWMonitorOps_t s_dummy_ops = {
  140. dummy_bw_monitor_allocate,
  141. dummy_bw_monitor_release,
  142. dummy_bw_monitor_reset,
  143. dummy_bw_monitor_get_data,
  144. dummy_bw_monitor_print
  145. };
  146. /************************************************************************/
  147. /* WAVE5 BACKBONE INTERFACE */
  148. /************************************************************************/
  149. typedef struct _tag_backbone_bw_context_struct {
  150. BW_CONTEXT_COMMON_VARS
  151. Uint32 arid_prp_total;
  152. Uint32 awid_prp_total;
  153. Uint32 arid_fbd_y_total;
  154. Uint32 awid_fbc_y_total;
  155. Uint32 arid_fbd_c_total;
  156. Uint32 awid_fbc_c_total;
  157. Uint32 arid_pri_total;
  158. Uint32 awid_pri_total;
  159. Uint32 arid_sec_total;
  160. Uint32 awid_sec_total;
  161. Uint32 arid_proc_total;
  162. Uint32 awid_proc_total;
  163. } BackBoneBwCtx;
  164. typedef struct _tag_backbone_bw_data_struct {
  165. Uint32 arid_prp;
  166. Uint32 awid_prp;
  167. Uint32 arid_fbd_y;
  168. Uint32 awid_fbc_y;
  169. Uint32 arid_fbd_c;
  170. Uint32 awid_fbc_c;
  171. Uint32 arid_pri;
  172. Uint32 awid_pri;
  173. Uint32 arid_sec;
  174. Uint32 awid_sec;
  175. Uint32 arid_proc;
  176. Uint32 awid_proc;
  177. } BWBackboneData;
  178. static BWCtx
  179. backbone_bw_monitor_allocate(
  180. Uint32 coreIndex
  181. )
  182. {
  183. BackBoneBwCtx* context;
  184. context = (BackBoneBwCtx*)osal_malloc(sizeof(BackBoneBwCtx));
  185. osal_memset((void*)context, 0x00, sizeof(BackBoneBwCtx));
  186. return context;
  187. }
  188. static void
  189. backbone_bw_monitor_release(
  190. BWCtx context
  191. )
  192. {
  193. BackBoneBwCtx* ctx = (BackBoneBwCtx*)context;
  194. Uint64 avgPriRead, avgPriWrite;
  195. Uint64 avgSecRead, avgSecWrite;
  196. Uint32 avgFBCWrite;
  197. Uint32 avgFBDRead;
  198. Uint64 avgProcRead, avgProcWrite;
  199. Uint64 avgPRPRead, avgPRPWrite;
  200. Uint64 avgWrite;
  201. Uint64 avgRead;
  202. Uint64 avg;
  203. if (ctx == NULL)
  204. return;
  205. avgPriRead = ctx->arid_pri_total / ctx->numFrames;
  206. avgPriWrite = ctx->awid_pri_total / ctx->numFrames;
  207. avgSecRead = ctx->arid_sec_total / ctx->numFrames;
  208. avgSecWrite = ctx->awid_sec_total / ctx->numFrames;
  209. avgProcRead = ctx->arid_proc_total / ctx->numFrames;
  210. avgProcWrite = ctx->awid_proc_total / ctx->numFrames;
  211. avgFBCWrite = (ctx->awid_fbc_y_total+ctx->awid_fbc_c_total) / ctx->numFrames;
  212. avgFBDRead = (ctx->arid_fbd_y_total+ctx->arid_fbd_c_total) / ctx->numFrames;
  213. avgPRPRead = ctx->arid_prp_total / ctx->numFrames;
  214. avgPRPWrite = ctx->awid_prp_total / ctx->numFrames;
  215. avgWrite = (ctx->awid_pri_total+ctx->awid_sec_total+ctx->awid_proc_total+ctx->awid_fbc_y_total+ctx->awid_fbc_c_total+ctx->awid_prp_total) / ctx->numFrames;
  216. avgRead = (ctx->arid_pri_total+ctx->arid_sec_total+ctx->arid_proc_total+ctx->arid_fbd_y_total+ctx->arid_fbd_c_total+ctx->arid_prp_total) / ctx->numFrames;
  217. avg = ctx->total / ctx->numFrames;
  218. osal_fprintf(ctx->fpBWTotal, "=======================================================================================================================================================\n");
  219. osal_fprintf(ctx->fpBWTotal, "AVER. %10d %10d %10d %10d %10d %10d %10d %10d %10d %10d %10d %10d %10d\n",
  220. avgPriWrite, avgSecWrite, avgFBCWrite, avgPRPWrite, avgProcWrite, avgWrite,
  221. avgPriRead, avgSecRead, avgFBDRead, avgPRPRead, avgProcRead, avgRead,
  222. avg);
  223. osal_free(context);
  224. }
  225. static void
  226. backbone_bw_monitor_reset(
  227. BWCtx context
  228. )
  229. {
  230. BackBoneBwCtx* ctx = (BackBoneBwCtx*)context;
  231. if (ctx == NULL)
  232. return;
  233. }
  234. static void
  235. backbone_bw_monitor_print(
  236. BWCtx context,
  237. BWData data,
  238. Uint32 picType
  239. )
  240. {
  241. BWBackboneData* bdata = (BWBackboneData*)data;
  242. BWCommonCtx* common = (BWCommonCtx*)context;
  243. Uint32 total_wr_bw;
  244. Uint32 total_rd_bw;
  245. total_wr_bw = bdata->awid_pri + bdata->awid_sec + bdata->awid_fbc_y + bdata->awid_fbc_c + bdata->awid_prp + bdata->awid_proc;
  246. total_rd_bw = bdata->arid_pri + bdata->arid_sec + bdata->arid_fbd_y + bdata->arid_fbd_c + bdata->arid_prp + bdata->arid_proc;
  247. if (common->numFrames == 1) {
  248. osal_fprintf(common->fpBWTotal, " No. WRITE(B) READ(B) TOTAL\n");
  249. osal_fprintf(common->fpBWTotal, " ---------------------------------------------------------------- ----------------------------------------------------------------- ----------\n");
  250. osal_fprintf(common->fpBWTotal, " PRI SEC FBC PRP PROC TOTAL PRI SEC FBD PRP PROC TOTAL\n");
  251. osal_fprintf(common->fpBWTotal, "+======================================================================================================================================================\n");
  252. }
  253. osal_fprintf(common->fpBWTotal, "%5d %s %10d %10d %10d %10d %10d %10d %10d %10d %10d %10d %10d %10d %10d\n", common->numFrames-1, picTypeString[picType],
  254. bdata->awid_pri, bdata->awid_sec, (bdata->awid_fbc_y+bdata->awid_fbc_c), bdata->awid_prp, bdata->awid_proc, total_wr_bw,
  255. bdata->arid_pri, bdata->arid_sec, (bdata->arid_fbd_y+bdata->arid_fbd_c), bdata->arid_prp, bdata->arid_proc, total_rd_bw,
  256. (total_wr_bw + total_rd_bw));
  257. osal_fflush(common->fpBWTotal);
  258. }
  259. static BWData
  260. backbone_bw_monitor_get_data(
  261. BWCtx context,
  262. Uint32 numCores
  263. )
  264. {
  265. BackBoneBwCtx* ctx = (BackBoneBwCtx*)context;
  266. BWBackboneData* idata;
  267. VPUBWData bwdata = {0, };
  268. RetCode ret = RETCODE_FAILURE;
  269. Uint32 prevTotal;
  270. if (ctx->instance->isDecoder == TRUE) {
  271. ret = VPU_DecGiveCommand(ctx->instance, GET_BANDWIDTH_REPORT, (void*)&bwdata);
  272. }
  273. else {
  274. ret = VPU_EncGiveCommand(ctx->instance, GET_BANDWIDTH_REPORT, (void*)&bwdata);
  275. }
  276. if (ret != RETCODE_SUCCESS) {
  277. VLOG(ERR, "%s:%d Failed to VPU_EncGiveCommand(ENC_GET_BW_REPORT), ret(%d)\n", __FUNCTION__, __LINE__, ret);
  278. return NULL;
  279. }
  280. idata = (BWBackboneData*)osal_malloc(sizeof(BWBackboneData));
  281. prevTotal = ctx->total;
  282. ctx->total += idata->arid_prp = bwdata.prpBwRead;
  283. ctx->total += idata->awid_prp = bwdata.prpBwWrite;
  284. ctx->total += idata->arid_fbd_y = bwdata.fbdYRead;
  285. ctx->total += idata->awid_fbc_y = bwdata.fbcYWrite;
  286. ctx->total += idata->arid_fbd_c = bwdata.fbdCRead;
  287. ctx->total += idata->awid_fbc_c = bwdata.fbcCWrite;
  288. ctx->total += idata->arid_pri = bwdata.priBwRead;
  289. ctx->total += idata->awid_pri = bwdata.priBwWrite;
  290. ctx->total += idata->arid_sec = bwdata.secBwRead;
  291. ctx->total += idata->awid_sec = bwdata.secBwWrite;
  292. ctx->total += idata->arid_proc = bwdata.procBwRead;
  293. ctx->total += idata->awid_proc = bwdata.procBwWrite;
  294. if (prevTotal == ctx->total) {
  295. // VPU didn't decode a frame.
  296. return NULL;
  297. }
  298. ctx->arid_prp_total += idata->arid_prp ;
  299. ctx->awid_prp_total += idata->awid_prp ;
  300. ctx->arid_fbd_y_total += idata->arid_fbd_y;
  301. ctx->arid_fbd_c_total += idata->arid_fbd_c;
  302. ctx->awid_fbc_y_total += idata->awid_fbc_y;
  303. ctx->awid_fbc_c_total += idata->awid_fbc_c;
  304. ctx->arid_pri_total += idata->arid_pri ;
  305. ctx->awid_pri_total += idata->awid_pri ;
  306. ctx->arid_sec_total += idata->arid_sec ;
  307. ctx->awid_sec_total += idata->awid_sec ;
  308. ctx->arid_proc_total += idata->arid_proc ;
  309. ctx->awid_proc_total += idata->awid_proc ;
  310. return (BWData)idata;
  311. }
  312. static BWMonitorOps_t s_wave_backbone_ops = {
  313. backbone_bw_monitor_allocate,
  314. backbone_bw_monitor_release,
  315. backbone_bw_monitor_reset,
  316. backbone_bw_monitor_get_data,
  317. backbone_bw_monitor_print
  318. };
  319. /************************************************************************/
  320. /* */
  321. /************************************************************************/
  322. BWCtx
  323. BWMonitorSetup(
  324. CodecInst* instance,
  325. BOOL perFrame,
  326. char* strLogDir
  327. )
  328. {
  329. Uint32 coreIndex;
  330. Uint32 productId;
  331. Uint32 instIndex;
  332. BWCommonCtx* common;
  333. BWMonitorOps_t* bwOps;
  334. osal_file_t fp = NULL;
  335. char path[256];
  336. coreIndex = instance->coreIdx;
  337. productId = instance->productId;
  338. instIndex = instance->instIndex;
  339. switch (productId) {
  340. case PRODUCT_ID_521:
  341. bwOps = &s_wave_backbone_ops;
  342. break;
  343. default:
  344. bwOps = &s_dummy_ops;
  345. break;
  346. }
  347. if (strLogDir) {
  348. sprintf(path, "./report/bw/%s/", strLogDir);
  349. MkDir(path);
  350. sprintf(path, "./report/bw/%s/report_bandwidth_%d_%d.txt", strLogDir, coreIndex, instIndex);
  351. }
  352. else {
  353. sprintf(path, "./report/bw/report_bandwidth_%d_%d.txt", coreIndex, instIndex);
  354. MkDir("./report/bw/");
  355. }
  356. if ((fp=osal_fopen(path, "w")) == NULL) {
  357. VLOG(ERR, "Failed to open %s\n", path);
  358. }
  359. common = (BWCommonCtx*)bwOps->allocate(coreIndex);
  360. common->ops = bwOps;
  361. common->coreIndex = coreIndex;
  362. common->instanceIndex = instIndex;
  363. common->productId = instance->productId;
  364. common->instance = instance;
  365. common->fpBWTotal = fp;
  366. common->enableReportPerFrame = perFrame;
  367. if (strLogDir) {
  368. sprintf(common->strLogDir, "%s", strLogDir);
  369. }
  370. else {
  371. osal_memset(common->strLogDir, 0x00, sizeof(common->strLogDir)*sizeof(char));
  372. }
  373. return common;
  374. }
  375. void
  376. BWMonitorReset(
  377. BWCtx context
  378. )
  379. {
  380. BWCommonCtx* common = (BWCommonCtx*)context;
  381. if (common == NULL)
  382. return;
  383. common->ops->reset(context);
  384. }
  385. void
  386. BWMonitorRelease(
  387. BWCtx context
  388. )
  389. {
  390. BWCommonCtx* common = (BWCommonCtx*)context;
  391. if (common == NULL)
  392. return;
  393. common->ops->release(context);
  394. if (common->fpBWTotal)
  395. osal_fclose(common->fpBWTotal);
  396. }
  397. void
  398. BWMonitorUpdate(
  399. BWCtx context,
  400. Uint32 numCores
  401. )
  402. {
  403. BWCommonCtx* common = (BWCommonCtx*)context;
  404. if (common == NULL || common->fpBWTotal == NULL) {
  405. return;
  406. }
  407. if ((common->data=common->ops->get_data(context, numCores)) == NULL) {
  408. return;
  409. }
  410. common->numFrames++;
  411. }
  412. void
  413. BWMonitorUpdatePrint(
  414. BWCtx context,
  415. Uint32 picType
  416. )
  417. {
  418. BWCommonCtx* common = (BWCommonCtx*)context;
  419. if (common == NULL || common->fpBWTotal == NULL ) {
  420. return;
  421. }
  422. if (common->data)
  423. common->ops->print(context, common->data, picType);
  424. osal_free(common->data);
  425. }