歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> MySQL源代碼:為MySQL增加響應時間status值

MySQL源代碼:為MySQL增加響應時間status值

日期:2017/3/1 10:23:03   编辑:Linux編程

實現的思路很簡單,借助了percona server5.5的information_schema表:query_response_time

過程

1.增加變量,記錄上次查詢rt時,統計的query數,以及總時間(last_count, last_total)


2.采集sql執行時間時(collect函數),累加當前query數,執行總時間(cur_count, cur_total)

3.計算一段時間內rt: (cur_total - last_total)/(cur_count - last_count)

4.設置last_total = cur_total, last_count =cur_count;

5.循環到2)


增加一個新status值,命名有點挫...

root@(none) 02:17:21>show status like 'rt_from_last_query%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| rt_from_last_query | 192 |
+--------------------+-------+
1 row in set (0.00 sec)


注意這個patch理論上會有額外的開銷,因為多計算了兩個值(cur_count,cur_total)。


以下是patch,基於percona 5.5.18

[cpp] view plaincopyprint?

  1. Index: sql/mysqld.cc
  2. ===================================================================
  3. --- sql/mysqld.cc (revision 1006)
  4. +++ sql/mysqld.cc (working copy)
  5. @@ -6307,6 +6307,17 @@
  6. return 0;
  7. }
  8. +#ifdef HAVE_RESPONSE_TIME_DISTRIBUTION
  9. +static int show_rt(THD *thd, SHOW_VAR *var, char *buff)
  10. +{
  11. + var->type = SHOW_LONGLONG;
  12. + ulonglong rt = query_response_time_rt();
  13. + var->value = buff;
  14. + *((long long *)buff)= (long long)rt;
  15. + return 0;
  16. +}
  17. +#endif
  18. +
  19. #ifdef ENABLED_PROFILING
  20. static int show_flushstatustime(THD *thd, SHOW_VAR *var, char *buff)
  21. {
  22. @@ -6811,6 +6822,9 @@
  23. #ifdef ENABLED_PROFILING
  24. {"Uptime_since_flush_status",(char*) &show_flushstatustime, SHOW_FUNC},
  25. #endif
  26. +#ifdef HAVE_RESPONSE_TIME_DISTRIBUTION
  27. + {"rt_from_last_query", (char*) &show_rt, SHOW_FUNC},
  28. +#endif
  29. {NullS, NullS, SHOW_LONG}
  30. };
  31. Index: sql/query_response_time.h
  32. ===================================================================
  33. --- sql/query_response_time.h (revision 1006)
  34. +++ sql/query_response_time.h (working copy)
  35. @@ -59,6 +59,7 @@
  36. extern void query_response_time_flush ();
  37. extern void query_response_time_collect(ulonglong query_time);
  38. extern int query_response_time_fill (THD* thd, TABLE_LIST *tables, COND *cond);
  39. +extern ulonglong query_response_time_rt();
  40. #endif // HAVE_RESPONSE_TIME_DISTRIBUTION
  41. #endif // QUERY_RESPONSE_TIME_H
  42. Index: sql/query_response_time.cc
  43. ===================================================================
  44. --- sql/query_response_time.cc (revision 1006)
  45. +++ sql/query_response_time.cc (working copy)
  46. @@ -170,16 +170,44 @@
  47. my_atomic_rwlock_rdunlock(&time_collector_lock);
  48. return result;
  49. }
  50. + uint64 rt()
  51. + {
  52. + my_atomic_rwlock_rdlock(&time_collector_lock);
  53. + uint32 diff_count = my_atomic_load32((int32*)(&cur_count)) -
  54. + my_atomic_load32((int32*)(&last_count));
  55. + uint64 diff_total = my_atomic_load64((int64*)(&cur_total)) -
  56. + my_atomic_load64((int64*)(&last_total));
  57. +
  58. + my_atomic_store64((int64*)(&last_total), my_atomic_load64((int64*)(&cur_total)));
  59. + my_atomic_store32((int32*)(&last_count), my_atomic_load32((int32*)(&cur_count)));
  60. + my_atomic_rwlock_rdunlock(&time_collector_lock);
  61. +
  62. + uint64 rt = 0;
  63. + if (diff_count != 0)
  64. + rt = diff_total/((uint64)diff_count);
  65. +
  66. + return rt;
  67. + }
  68. +
  69. public:
  70. void flush()
  71. {
  72. my_atomic_rwlock_wrlock(&time_collector_lock);
  73. memset((void*)&m_count,0,sizeof(m_count));
  74. memset((void*)&m_total,0,sizeof(m_total));
  75. - my_atomic_rwlock_wrunlock(&time_collector_lock);
  76. + memset((void*)&cur_count,0,sizeof(cur_count));
  77. + memset((void*)&cur_total,0,sizeof(cur_total));
  78. + memset((void*)&last_count,0,sizeof(last_count));
  79. + memset((void*)&last_total,0,sizeof(last_total));
  80. + my_atomic_rwlock_wrunlock(&time_collector_lock);
  81. }
  82. void collect(uint64 time)
  83. {
  84. + my_atomic_rwlock_wrlock(&time_collector_lock);
  85. + my_atomic_add32((int32*)(&cur_count), 1);
  86. + my_atomic_add64((int64*)(&cur_total), time);
  87. + my_atomic_rwlock_wrunlock(&time_collector_lock);
  88. +
  89. int i= 0;
  90. for(int count= m_utility->bound_count(); count > i; ++i)
  91. {
  92. @@ -201,6 +229,10 @@
  93. my_atomic_rwlock_t time_collector_lock;
  94. uint32 m_count[OVERALL_POWER_COUNT + 1];
  95. uint64 m_total[OVERALL_POWER_COUNT + 1];
  96. + uint32 last_count;
  97. + uint64 last_total;
  98. + uint32 cur_count;
  99. + uint64 cur_total;
  100. };
  101. class collector
  102. @@ -268,6 +300,10 @@
  103. {
  104. return m_time.total(index);
  105. }
  106. + ulonglong rt()
  107. + {
  108. + return m_time.rt();
  109. + }
  110. private:
  111. utility m_utility;
  112. time_collector m_time;
  113. @@ -299,4 +335,10 @@
  114. {
  115. return query_response_time::g_collector.fill(thd,tables,cond);
  116. }
  117. +
  118. +ulonglong query_response_time_rt()
  119. +{
  120. + return query_response_time::g_collector.rt();
  121. +}
  122. +
  123. #endif // HAVE_RESPONSE_TIME_DISTRIBUTION
Copyright © Linux教程網 All Rights Reserved