basic_timers.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  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
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <grpc/support/port_platform.h>
  34. #ifdef GRPC_BASIC_PROFILER
  35. #include "src/core/profiling/timers.h"
  36. #include "src/core/profiling/timers_preciseclock.h"
  37. #include <grpc/support/alloc.h>
  38. #include <grpc/support/log.h>
  39. #include <grpc/support/time.h>
  40. #include <grpc/support/sync.h>
  41. #include <grpc/support/thd.h>
  42. #include <stdio.h>
  43. typedef enum {
  44. BEGIN = '{',
  45. END = '}',
  46. MARK = '.',
  47. IMPORTANT = '!'
  48. } marker_type;
  49. typedef struct grpc_timer_entry {
  50. grpc_precise_clock tm;
  51. int tag;
  52. const char* tagstr;
  53. marker_type type;
  54. void* id;
  55. const char* file;
  56. int line;
  57. } grpc_timer_entry;
  58. #define MAX_COUNT (1024 * 1024 / sizeof(grpc_timer_entry))
  59. static __thread grpc_timer_entry log[MAX_COUNT];
  60. static __thread int count;
  61. static void log_report() {
  62. int i;
  63. for (i = 0; i < count; i++) {
  64. grpc_timer_entry* entry = &(log[i]);
  65. printf("GRPC_LAT_PROF " GRPC_PRECISE_CLOCK_FORMAT
  66. " %p %c %d(%s) %p %s %d\n",
  67. GRPC_PRECISE_CLOCK_PRINTF_ARGS(&entry->tm),
  68. (void*)(gpr_intptr)gpr_thd_currentid(), entry->type, entry->tag,
  69. entry->tagstr, entry->id, entry->file, entry->line);
  70. }
  71. /* Now clear out the log */
  72. count = 0;
  73. }
  74. static void grpc_timers_log_add(int tag, const char* tagstr, marker_type type,
  75. void* id, const char* file, int line) {
  76. grpc_timer_entry* entry;
  77. /* TODO (vpai) : Improve concurrency */
  78. if (count == MAX_COUNT) {
  79. log_report();
  80. }
  81. entry = &log[count++];
  82. grpc_precise_clock_now(&entry->tm);
  83. entry->tag = tag;
  84. entry->tagstr = tagstr;
  85. entry->type = type;
  86. entry->id = id;
  87. entry->file = file;
  88. entry->line = line;
  89. }
  90. /* Latency profiler API implementation. */
  91. void grpc_timer_add_mark(int tag, const char* tagstr, void* id,
  92. const char* file, int line) {
  93. if (tag < GRPC_PTAG_IGNORE_THRESHOLD) {
  94. grpc_timers_log_add(tag, tagstr, MARK, id, file, line);
  95. }
  96. }
  97. void grpc_timer_add_important_mark(int tag, const char* tagstr, void* id,
  98. const char* file, int line) {
  99. if (tag < GRPC_PTAG_IGNORE_THRESHOLD) {
  100. grpc_timers_log_add(tag, tagstr, IMPORTANT, id, file, line);
  101. }
  102. }
  103. void grpc_timer_begin(int tag, const char* tagstr, void* id, const char* file,
  104. int line) {
  105. if (tag < GRPC_PTAG_IGNORE_THRESHOLD) {
  106. grpc_timers_log_add(tag, tagstr, BEGIN, id, file, line);
  107. }
  108. }
  109. void grpc_timer_end(int tag, const char* tagstr, void* id, const char* file,
  110. int line) {
  111. if (tag < GRPC_PTAG_IGNORE_THRESHOLD) {
  112. grpc_timers_log_add(tag, tagstr, END, id, file, line);
  113. }
  114. }
  115. /* Basic profiler specific API functions. */
  116. void grpc_timers_global_init(void) {}
  117. void grpc_timers_global_destroy(void) {}
  118. #else /* !GRPC_BASIC_PROFILER */
  119. void grpc_timers_global_init(void) {}
  120. void grpc_timers_global_destroy(void) {}
  121. #endif /* GRPC_BASIC_PROFILER */