rt_ai_runtime.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-04-01 liqiwen the first version
  9. */
  10. #include <rt_ai_runtime.h>
  11. #include <rt_ai_common.h>
  12. #include <rt_ai_log.h>
  13. #include <rt_ai_core.h>
  14. void run_entry(rt_ai_t ai)
  15. {
  16. #if ENABLE_PERFORMANCE
  17. rt_ai_record_t run_cost = rt_ai_record_find("run_time_cost");
  18. run_cost->record = rt_ai_get_timestamp();
  19. #endif
  20. return;
  21. }
  22. void run_done(void *arg)
  23. {
  24. rt_ai_t ai = RT_AI_T(arg);
  25. #if ENABLE_PERFORMANCE
  26. rt_ai_timestamp_t cost = STATISTIC_TIME_INTERVAL("run_time_cost");
  27. AI_LOG("run_time_cost:%d\n", cost);
  28. #endif
  29. // todo
  30. if (ai->done_callback_user != RT_AI_NULL)
  31. {
  32. ai->done_callback_user(ai->arg);
  33. }
  34. return;
  35. }
  36. rt_ai_record_t rt_ai_record_find(const char *name)
  37. {
  38. RT_AI_ASSERT_RETURN_NULL(name);
  39. rt_ai_core_t core = RT_AI_NULL;
  40. core = rt_ai_core_find(name,RT_AI_CLASS_RECORD);
  41. if(core == RT_AI_NULL){
  42. AI_LOG_I("rt_ai_core_find return RT_AI_NULL in %s,%d",__FILE__,__LINE__);
  43. return RT_AI_NULL;
  44. }
  45. return (rt_ai_record_t) core;
  46. }
  47. rt_ai_record_t rt_ai_record_register(rt_ai_record_t record, const char *name)
  48. {
  49. RT_AI_ASSERT_RETURN_NULL(record != RT_AI_NULL);
  50. if (record == RT_AI_NULL)
  51. {
  52. return RT_AI_NULL;
  53. }
  54. if(rt_ai_record_find(name)){
  55. AI_LOG_E("record %s exist:%s,%d",name,__FILE__,__LINE__);
  56. }
  57. if(rt_ai_core_register(&(record->parent),RT_AI_CLASS_RECORD,name) == RT_AI_NULL){
  58. AI_LOG_I("rt_ai_core_register return RT_AI_NULL in %s,%d",__FILE__,__LINE__);
  59. return RT_AI_NULL;
  60. }
  61. return record;
  62. }
  63. rt_ai_record_t rt_ai_record_unregister(const char *name)
  64. {
  65. rt_ai_record_t record = RT_AI_NULL;
  66. record = rt_ai_record_find(name);
  67. if (!record)
  68. {
  69. AI_LOG_I("rt_ai_record_find return RT_AI_NULL in %s,%d",__FILE__,__LINE__);
  70. return RT_AI_NULL;
  71. }
  72. rt_ai_core_detach(&(record->parent));
  73. return record;
  74. }
  75. rt_ai_record_t rt_ai_record_create(const char *name, rt_ai_uint32_t value)
  76. {
  77. rt_ai_record_t record = rt_ai_new_obj(struct rt_ai_record);
  78. record->record = value;
  79. if (rt_ai_record_register(record, name) == RT_AI_NULL)
  80. {
  81. AI_LOG_I("rt_ai_record_register return RT_AI_NULL in %s,%d",__FILE__,__LINE__);
  82. return RT_AI_NULL;
  83. }
  84. return record;
  85. }
  86. rt_ai_record_t rt_ai_record_delete(const char *name)
  87. {
  88. rt_ai_record_t record = RT_AI_NULL;
  89. record = rt_ai_record_unregister(name);
  90. if (!record)
  91. {
  92. AI_LOG_I("rt_ai_record_unregister return RT_AI_NULL in %s,%d",__FILE__,__LINE__);
  93. return RT_AI_NULL;
  94. }
  95. if(!RT_AI_IS_STATICIAL(record->parent.type)){
  96. AI_LOG("NOT STATICAL\n");
  97. rt_ai_del(record);
  98. }
  99. return record;
  100. }
  101. rt_ai_timestamp_t statistic_time_interval(const char *name)
  102. {
  103. rt_ai_record_t record = rt_ai_record_find(name);
  104. if (record == RT_AI_NULL)
  105. {
  106. return 0;
  107. }
  108. rt_ai_timestamp_t now = rt_ai_get_timestamp();
  109. return now - record->record;
  110. }
  111. int _rt_ai_runtime_init(){
  112. #if ENABLE_PERFORMANCE
  113. static struct rt_ai_record run_time_cost;
  114. run_time_cost.parent.type = RT_AI_STATICIAL_TYPE(0);
  115. rt_ai_record_register(&run_time_cost,"run_time_cost");
  116. #endif
  117. return 0;
  118. }
  119. INIT_PREV_EXPORT(_rt_ai_runtime_init);