mpp_trace.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright 2021 Rockchip Electronics Co. LTD
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define MODULE_TAG "mpp_trace"
  17. #include <fcntl.h>
  18. #include <stdarg.h>
  19. #include "mpp_common.h"
  20. #include "mpp_trace.h"
  21. #define ATRACE_MESSAGE_LENGTH 256
  22. class MppTraceService
  23. {
  24. private:
  25. // avoid any unwanted function
  26. MppTraceService();
  27. ~MppTraceService();
  28. MppTraceService(const MppTraceService &);
  29. MppTraceService &operator=(const MppTraceService &);
  30. void trace_write(const char *fmt, ...);
  31. RK_S32 mTraceFd;
  32. public:
  33. static MppTraceService *get_inst() {
  34. static MppTraceService inst;
  35. return &inst;
  36. }
  37. void trace_begin(const char* name);
  38. void trace_end(const char* name);
  39. void trace_async_begin(const char* name, RK_S32 cookie);
  40. void trace_async_end(const char* name, RK_S32 cookie);
  41. void trace_int32(const char* name, RK_S32 val);
  42. void trace_int64(const char* name, RK_S64 val);
  43. };
  44. MppTraceService::MppTraceService()
  45. {
  46. static const char *ftrace_paths[] = {
  47. "/sys/kernel/debug/tracing/trace_marker",
  48. "/debug/tracing/trace_marker",
  49. "/debugfs/tracing/trace_marker",
  50. };
  51. RK_U32 i;
  52. for (i = 0; i < MPP_ARRAY_ELEMS(ftrace_paths); i++) {
  53. if (!access(ftrace_paths[i], F_OK)) {
  54. mTraceFd = open(ftrace_paths[i], O_WRONLY | O_CLOEXEC);
  55. if (mTraceFd >= 0)
  56. break;
  57. }
  58. }
  59. }
  60. MppTraceService::~MppTraceService()
  61. {
  62. if (mTraceFd >= 0) {
  63. close(mTraceFd);
  64. mTraceFd = -1;
  65. }
  66. }
  67. void MppTraceService::trace_write(const char *fmt, ...)
  68. {
  69. char buf[ATRACE_MESSAGE_LENGTH];
  70. va_list ap;
  71. RK_S32 len;
  72. va_start(ap, fmt);
  73. len = vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
  74. va_end(ap);
  75. (void)!write(mTraceFd, buf, len);
  76. }
  77. void MppTraceService::trace_begin(const char* name)
  78. {
  79. if (mTraceFd < 0)
  80. return;
  81. trace_write("B|%d|%s", getpid(), name);
  82. }
  83. void MppTraceService::trace_end(const char* name)
  84. {
  85. if (mTraceFd < 0)
  86. return;
  87. trace_write("E|%d|%s", getpid(), name);
  88. }
  89. void MppTraceService::trace_async_begin(const char* name, RK_S32 cookie)
  90. {
  91. if (mTraceFd < 0)
  92. return;
  93. trace_write("S|%d|%s|%d", getpid(), name, cookie);
  94. }
  95. void MppTraceService::trace_async_end(const char* name, RK_S32 cookie)
  96. {
  97. if (mTraceFd < 0)
  98. return;
  99. trace_write("F|%d|%s|%d", getpid(), name, cookie);
  100. }
  101. void MppTraceService::trace_int32(const char* name, RK_S32 value)
  102. {
  103. if (mTraceFd < 0)
  104. return;
  105. trace_write("C|%d|%s|%d", getpid(), name, value);
  106. }
  107. void MppTraceService::trace_int64(const char* name, RK_S64 value)
  108. {
  109. if (mTraceFd < 0)
  110. return;
  111. trace_write("C|%d|%s|%lld", getpid(), name, value);
  112. }
  113. void mpp_trace_begin(const char* name)
  114. {
  115. MppTraceService::get_inst()->trace_begin(name);
  116. }
  117. void mpp_trace_end(const char* name)
  118. {
  119. MppTraceService::get_inst()->trace_end(name);
  120. }
  121. void mpp_trace_async_begin(const char* name, RK_S32 cookie)
  122. {
  123. MppTraceService::get_inst()->trace_async_begin(name, cookie);
  124. }
  125. void mpp_trace_async_end(const char* name, RK_S32 cookie)
  126. {
  127. MppTraceService::get_inst()->trace_async_end(name, cookie);
  128. }
  129. void mpp_trace_int32(const char* name, RK_S32 value)
  130. {
  131. MppTraceService::get_inst()->trace_int32(name, value);
  132. }
  133. void mpp_trace_int64(const char* name, RK_S64 value)
  134. {
  135. MppTraceService::get_inst()->trace_int64(name, value);
  136. }