timers_preciseclock.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #ifndef GRPC_CORE_PROFILING_TIMERS_PRECISECLOCK_H
  34. #define GRPC_CORE_PROFILING_TIMERS_PRECISECLOCK_H
  35. #include <grpc/support/sync.h>
  36. #include <grpc/support/time.h>
  37. #include <stdio.h>
  38. #ifdef GRPC_TIMERS_RDTSC
  39. typedef long long int grpc_precise_clock;
  40. #if defined(__i386__)
  41. static void grpc_precise_clock_now(grpc_precise_clock *clk) {
  42. grpc_precise_clock ret;
  43. __asm__ volatile("rdtsc" : "=A"(ret));
  44. *clk = ret;
  45. }
  46. // ----------------------------------------------------------------
  47. #elif defined(__x86_64__) || defined(__amd64__)
  48. static void grpc_precise_clock_now(grpc_precise_clock *clk) {
  49. unsigned long long low, high;
  50. __asm__ volatile("rdtsc" : "=a"(low), "=d"(high));
  51. *clk = (high << 32) | low;
  52. }
  53. #endif
  54. static gpr_once precise_clock_init = GPR_ONCE_INIT;
  55. static double cycles_per_second = 0.0;
  56. static void grpc_precise_clock_init() {
  57. time_t start = time(NULL);
  58. grpc_precise_clock start_time;
  59. grpc_precise_clock end_time;
  60. while (time(NULL) == start)
  61. ;
  62. grpc_precise_clock_now(&start_time);
  63. while (time(NULL) == start + 1)
  64. ;
  65. grpc_precise_clock_now(&end_time);
  66. cycles_per_second = end_time - start_time;
  67. }
  68. static double grpc_precise_clock_scaling_factor() {
  69. gpr_once_init(&precise_clock_init, grpc_precise_clock_init);
  70. return 1e6 / cycles_per_second;
  71. }
  72. #define GRPC_PRECISE_CLOCK_FORMAT "%f"
  73. #define GRPC_PRECISE_CLOCK_PRINTF_ARGS(clk) \
  74. (*(clk)*grpc_precise_clock_scaling_factor())
  75. #else
  76. typedef struct grpc_precise_clock grpc_precise_clock;
  77. struct grpc_precise_clock {
  78. gpr_timespec clock;
  79. };
  80. static void grpc_precise_clock_now(grpc_precise_clock* clk) {
  81. clk->clock = gpr_now();
  82. }
  83. #define GRPC_PRECISE_CLOCK_FORMAT "%ld.%09d"
  84. #define GRPC_PRECISE_CLOCK_PRINTF_ARGS(clk) \
  85. (clk)->clock.tv_sec, (clk)->clock.tv_nsec
  86. static void grpc_precise_clock_print(const grpc_precise_clock* clk, FILE* fp) {
  87. fprintf(fp, "%ld.%09d", clk->clock.tv_sec, clk->clock.tv_nsec);
  88. }
  89. #endif /* GRPC_TIMERS_RDTSC */
  90. #endif /* GRPC_CORE_PROFILING_TIMERS_PRECISECLOCK_H */