time_precise.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_SUPPORT_TIME_PRECISE_H_
  34. #define GRPC_CORE_SUPPORT_TIME_PRECISE_H_
  35. #include <grpc/support/sync.h>
  36. #include <grpc/support/time.h>
  37. #include <stdio.h>
  38. #ifdef GRPC_TIMERS_RDTSC
  39. #if defined(__i386__)
  40. static void gpr_get_cycle_counter(long long int *clk) {
  41. long long int ret;
  42. __asm__ volatile("rdtsc" : "=A"(ret));
  43. *clk = ret;
  44. }
  45. // ----------------------------------------------------------------
  46. #elif defined(__x86_64__) || defined(__amd64__)
  47. static void gpr_get_cycle_counter(long long int *clk) {
  48. unsigned long long low, high;
  49. __asm__ volatile("rdtsc" : "=a"(low), "=d"(high));
  50. *clk = (high << 32) | low;
  51. }
  52. #endif
  53. static gpr_once precise_clock_init = GPR_ONCE_INIT;
  54. static long long cycles_per_second = 0;
  55. static void gpr_precise_clock_init() {
  56. time_t start = time(NULL);
  57. gpr_precise_clock start_cycle;
  58. gpr_precise_clock end_cycle;
  59. while (time(NULL) == start)
  60. ;
  61. gpr_get_cycle_counter(&start_cycle);
  62. while (time(NULL) == start + 1)
  63. ;
  64. gpr_get_cycle_counter(&end_cycle);
  65. cycles_per_second = end_cycle - start_cycle;
  66. }
  67. static double grpc_precise_clock_scaling_factor() {
  68. gpr_once_init(&precise_clock_init, grpc_precise_clock_init);
  69. return 1e6 / cycles_per_second;
  70. }
  71. static void gpr_precise_clock_now(gpr_timespec *clk) {
  72. long long int counter;
  73. gpr_get_cycle_counter(&counter);
  74. clk->clock = GPR_CLOCK_REALTIME;
  75. clk->tv_sec = counter / cycles_per_second;
  76. clk->tv_nsec = counter % cycles_per_second;
  77. }
  78. #else /* GRPC_TIMERS_RDTSC */
  79. static void gpr_precise_clock_now(gpr_timespec *clk) {
  80. *clk = gpr_now(GPR_CLOCK_REALTIME);
  81. clk->clock_type = GPR_CLOCK_PRECISE;
  82. }
  83. #endif /* GRPC_TIMERS_RDTSC */
  84. #endif /* GRPC_CORE_SUPPORT_TIME_PRECISE_ */