关键词搜索

源码搜索 ×
×

漫话Redis源码之四十四

发布2022-01-02浏览588次

详情内容

这里主要是跟时钟相关的一些函数,其实不是特别需要仔细阅读。

  1. #include "monotonic.h"
  2. #include <stddef.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <time.h>
  6. #undef NDEBUG
  7. #include <assert.h>
  8. /* The function pointer for clock retrieval. */
  9. monotime (*getMonotonicUs)(void) = NULL;
  10. static char monotonic_info_string[32];
  11. /* Using the processor clock (aka TSC on x86) can provide improved performance
  12. * throughout Redis wherever the monotonic clock is used. The processor clock
  13. * is significantly faster than calling 'clock_getting' (POSIX). While this is
  14. * generally safe on modern systems, this link provides additional information
  15. * about use of the x86 TSC: http://oliveryang.nethttps://files.jxasp.com/image/2015/09/pitfalls-of-TSC-usage
  16. *
  17. * To use the processor clock, either uncomment this line, or build with
  18. * CFLAGS="-DUSE_PROCESSOR_CLOCK"
  19. #define USE_PROCESSOR_CLOCK
  20. */
  21. #if defined(USE_PROCESSOR_CLOCK) && defined(__x86_64__) && defined(__linux__)
  22. #include <regex.h>
  23. #include <x86intrin.h>
  24. static long mono_ticksPerMicrosecond = 0;
  25. static monotime getMonotonicUs_x86() {
  26. return __rdtsc() / mono_ticksPerMicrosecond;
  27. }
  28. static void monotonicInit_x86linux() {
  29. const int bufflen = 256;
  30. char buf[bufflen];
  31. regex_t cpuGhzRegex, constTscRegex;
  32. const size_t nmatch = 2;
  33. regmatch_t pmatch[nmatch];
  34. int constantTsc = 0;
  35. int rc;
  36. /* Determine the number of TSC ticks in a micro-second. This is
  37. * a constant value matching the standard speed of the processor.
  38. * On modern processors, this speed remains constant even though
  39. * the actual clock speed varies dynamically for each core. */
  40. rc = regcomp(&cpuGhzRegex, "^model name\\s+:.*@ ([0-9.]+)GHz", REG_EXTENDED);
  41. assert(rc == 0);
  42. /* Also check that the constant_tsc flag is present. (It should be
  43. * unless this is a really old CPU. */
  44. rc = regcomp(&constTscRegex, "^flags\\s+:.* constant_tsc", REG_EXTENDED);
  45. assert(rc == 0);
  46. FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
  47. if (cpuinfo != NULL) {
  48. while (fgets(buf, bufflen, cpuinfo) != NULL) {
  49. if (regexec(&cpuGhzRegex, buf, nmatch, pmatch, 0) == 0) {
  50. buf[pmatch[1].rm_eo] = '\0';
  51. double ghz = atof(&buf[pmatch[1].rm_so]);
  52. mono_ticksPerMicrosecond = (long)(ghz * 1000);
  53. break;
  54. }
  55. }
  56. while (fgets(buf, bufflen, cpuinfo) != NULL) {
  57. if (regexec(&constTscRegex, buf, nmatch, pmatch, 0) == 0) {
  58. constantTsc = 1;
  59. break;
  60. }
  61. }
  62. fclose(cpuinfo);
  63. }
  64. regfree(&cpuGhzRegex);
  65. regfree(&constTscRegex);
  66. if (mono_ticksPerMicrosecond == 0) {
  67. fprintf(stderr, "monotonic: x86 linux, unable to determine clock rate");
  68. return;
  69. }
  70. if (!constantTsc) {
  71. fprintf(stderr, "monotonic: x86 linux, 'constant_tsc' flag not present");
  72. return;
  73. }
  74. snprintf(monotonic_info_string, sizeof(monotonic_info_string),
  75. "X86 TSC @ %ld ticks/us", mono_ticksPerMicrosecond);
  76. getMonotonicUs = getMonotonicUs_x86;
  77. }
  78. #endif
  79. #if defined(USE_PROCESSOR_CLOCK) && defined(__aarch64__)
  80. static long mono_ticksPerMicrosecond = 0;
  81. /* Read the clock value. */
  82. static inline uint64_t __cntvct() {
  83. uint64_t virtual_timer_value;
  84. __asm__ volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
  85. return virtual_timer_value;
  86. }
  87. /* Read the Count-timer Frequency. */
  88. static inline uint32_t cntfrq_hz() {
  89. uint64_t virtual_freq_value;
  90. __asm__ volatile("mrs %0, cntfrq_el0" : "=r"(virtual_freq_value));
  91. return (uint32_t)virtual_freq_value; /* top 32 bits are reserved */
  92. }
  93. static monotime getMonotonicUs_aarch64() {
  94. return __cntvct() / mono_ticksPerMicrosecond;
  95. }
  96. static void monotonicInit_aarch64() {
  97. mono_ticksPerMicrosecond = (long)cntfrq_hz() / 1000L / 1000L;
  98. if (mono_ticksPerMicrosecond == 0) {
  99. fprintf(stderr, "monotonic: aarch64, unable to determine clock rate");
  100. return;
  101. }
  102. snprintf(monotonic_info_string, sizeof(monotonic_info_string),
  103. "ARM CNTVCT @ %ld ticks/us", mono_ticksPerMicrosecond);
  104. getMonotonicUs = getMonotonicUs_aarch64;
  105. }
  106. #endif
  107. static monotime getMonotonicUs_posix() {
  108. /* clock_gettime() is specified in POSIX.1b (1993). Even so, some systems
  109. * did not support this until much later. CLOCK_MONOTONIC is technically
  110. * optional and may not be supported - but it appears to be universal.
  111. * If this is not supported, provide a system-specific alternate version. */
  112. struct timespec ts;
  113. clock_gettime(CLOCK_MONOTONIC, &ts);
  114. return ((uint64_t)ts.tv_sec) * 1000000 + ts.tv_nsec / 1000;
  115. }
  116. static void monotonicInit_posix() {
  117. /* Ensure that CLOCK_MONOTONIC is supported. This should be supported
  118. * on any reasonably current OS. If the assertion below fails, provide
  119. * an appropriate alternate implementation. */
  120. struct timespec ts;
  121. int rc = clock_gettime(CLOCK_MONOTONIC, &ts);
  122. assert(rc == 0);
  123. snprintf(monotonic_info_string, sizeof(monotonic_info_string),
  124. "POSIX clock_gettime");
  125. getMonotonicUs = getMonotonicUs_posix;
  126. }
  127. const char * monotonicInit() {
  128. #if defined(USE_PROCESSOR_CLOCK) && defined(__x86_64__) && defined(__linux__)
  129. if (getMonotonicUs == NULL) monotonicInit_x86linux();
  130. #endif
  131. #if defined(USE_PROCESSOR_CLOCK) && defined(__aarch64__)
  132. if (getMonotonicUs == NULL) monotonicInit_aarch64();
  133. #endif
  134. if (getMonotonicUs == NULL) monotonicInit_posix();
  135. return monotonic_info_string;
  136. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载