thread_status_impl.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. #include <sstream>
  7. #include "rocksdb/env.h"
  8. #include "rocksdb/thread_status.h"
  9. #include "util/string_util.h"
  10. #include "util/thread_operation.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. #ifdef ROCKSDB_USING_THREAD_STATUS
  13. std::string ThreadStatus::GetThreadTypeName(
  14. ThreadStatus::ThreadType thread_type) {
  15. switch (thread_type) {
  16. case ThreadStatus::ThreadType::HIGH_PRIORITY:
  17. return "High Pri";
  18. case ThreadStatus::ThreadType::LOW_PRIORITY:
  19. return "Low Pri";
  20. case ThreadStatus::ThreadType::USER:
  21. return "User";
  22. case ThreadStatus::ThreadType::BOTTOM_PRIORITY:
  23. return "Bottom Pri";
  24. case ThreadStatus::ThreadType::NUM_THREAD_TYPES:
  25. assert(false);
  26. }
  27. return "Unknown";
  28. }
  29. const std::string& ThreadStatus::GetOperationName(
  30. ThreadStatus::OperationType op_type) {
  31. if (op_type < 0 || op_type >= NUM_OP_TYPES) {
  32. return global_operation_table[OP_UNKNOWN].name;
  33. }
  34. return global_operation_table[op_type].name;
  35. }
  36. const std::string& ThreadStatus::GetOperationStageName(
  37. ThreadStatus::OperationStage stage) {
  38. if (stage < 0 || stage >= NUM_OP_STAGES) {
  39. return global_op_stage_table[STAGE_UNKNOWN].name;
  40. }
  41. return global_op_stage_table[stage].name;
  42. }
  43. const std::string& ThreadStatus::GetStateName(
  44. ThreadStatus::StateType state_type) {
  45. if (state_type < 0 || state_type >= NUM_STATE_TYPES) {
  46. return global_state_table[STATE_UNKNOWN].name;
  47. }
  48. return global_state_table[state_type].name;
  49. }
  50. const std::string ThreadStatus::MicrosToString(uint64_t micros) {
  51. if (micros == 0) {
  52. return "";
  53. }
  54. const int kBufferLen = 100;
  55. char buffer[kBufferLen];
  56. AppendHumanMicros(micros, buffer, kBufferLen, false);
  57. return std::string(buffer);
  58. }
  59. const std::string& ThreadStatus::GetOperationPropertyName(
  60. ThreadStatus::OperationType op_type, int i) {
  61. static const std::string empty_str = "";
  62. switch (op_type) {
  63. case ThreadStatus::OP_COMPACTION:
  64. if (i >= NUM_COMPACTION_PROPERTIES) {
  65. return empty_str;
  66. }
  67. return compaction_operation_properties[i].name;
  68. case ThreadStatus::OP_FLUSH:
  69. if (i >= NUM_FLUSH_PROPERTIES) {
  70. return empty_str;
  71. }
  72. return flush_operation_properties[i].name;
  73. default:
  74. return empty_str;
  75. }
  76. }
  77. std::map<std::string, uint64_t> ThreadStatus::InterpretOperationProperties(
  78. ThreadStatus::OperationType op_type, const uint64_t* op_properties) {
  79. int num_properties;
  80. switch (op_type) {
  81. case OP_COMPACTION:
  82. num_properties = NUM_COMPACTION_PROPERTIES;
  83. break;
  84. case OP_FLUSH:
  85. num_properties = NUM_FLUSH_PROPERTIES;
  86. break;
  87. default:
  88. num_properties = 0;
  89. }
  90. std::map<std::string, uint64_t> property_map;
  91. for (int i = 0; i < num_properties; ++i) {
  92. if (op_type == OP_COMPACTION && i == COMPACTION_INPUT_OUTPUT_LEVEL) {
  93. property_map.insert({"BaseInputLevel", op_properties[i] >> 32});
  94. property_map.insert(
  95. {"OutputLevel", op_properties[i] % (uint64_t(1) << 32U)});
  96. } else if (op_type == OP_COMPACTION && i == COMPACTION_PROP_FLAGS) {
  97. property_map.insert({"IsManual", ((op_properties[i] & 2) >> 1)});
  98. property_map.insert({"IsDeletion", ((op_properties[i] & 4) >> 2)});
  99. property_map.insert({"IsTrivialMove", ((op_properties[i] & 8) >> 3)});
  100. } else {
  101. property_map.insert(
  102. {GetOperationPropertyName(op_type, i), op_properties[i]});
  103. }
  104. }
  105. return property_map;
  106. }
  107. #else
  108. std::string ThreadStatus::GetThreadTypeName(
  109. ThreadStatus::ThreadType /*thread_type*/) {
  110. static std::string dummy_str = "";
  111. return dummy_str;
  112. }
  113. const std::string& ThreadStatus::GetOperationName(
  114. ThreadStatus::OperationType /*op_type*/) {
  115. static std::string dummy_str = "";
  116. return dummy_str;
  117. }
  118. const std::string& ThreadStatus::GetOperationStageName(
  119. ThreadStatus::OperationStage /*stage*/) {
  120. static std::string dummy_str = "";
  121. return dummy_str;
  122. }
  123. const std::string& ThreadStatus::GetStateName(
  124. ThreadStatus::StateType /*state_type*/) {
  125. static std::string dummy_str = "";
  126. return dummy_str;
  127. }
  128. const std::string ThreadStatus::MicrosToString(uint64_t /*op_elapsed_time*/) {
  129. static std::string dummy_str = "";
  130. return dummy_str;
  131. }
  132. const std::string& ThreadStatus::GetOperationPropertyName(
  133. ThreadStatus::OperationType /*op_type*/, int /*i*/) {
  134. static std::string dummy_str = "";
  135. return dummy_str;
  136. }
  137. std::map<std::string, uint64_t> ThreadStatus::InterpretOperationProperties(
  138. ThreadStatus::OperationType /*op_type*/,
  139. const uint64_t* /*op_properties*/) {
  140. return std::map<std::string, uint64_t>();
  141. }
  142. #endif // ROCKSDB_USING_THREAD_STATUS
  143. } // namespace ROCKSDB_NAMESPACE