|
| 1 | +// Copyright 2004-present Facebook. All Rights Reserved. |
| 2 | + |
| 3 | +#include <unordered_map> |
| 4 | +#include <mutex> |
| 5 | +#include <memory> |
| 6 | +#include <deque> |
| 7 | +#include <boost/optional.hpp> |
| 8 | +#include <boost/lexical_cast.hpp> |
| 9 | +#include <boost/property_tree/ptree.hpp> |
| 10 | +#include <boost/property_tree/json_parser.hpp> |
| 11 | + |
| 12 | + |
| 13 | +#include "query_tag_perf_counter.h" |
| 14 | +#include "sql_class.h" |
| 15 | +#include "sql_show.h" |
| 16 | + |
| 17 | +namespace qutils { |
| 18 | + |
| 19 | +namespace pt = boost::property_tree; |
| 20 | + |
| 21 | +static int64_t timespec_diff(const timespec& start, const timespec& stop); |
| 22 | + |
| 23 | +static std::mutex stats_mutex; |
| 24 | +static std::unordered_map<std::string, uint64_t> stats; |
| 25 | + |
| 26 | +query_tag_perf_counter::query_tag_perf_counter(THD* _thd) |
| 27 | + : started(false), thd(_thd) |
| 28 | +{ |
| 29 | + if(!thd->query_type.empty() && thd->num_queries > 0) |
| 30 | + { |
| 31 | + this->started = |
| 32 | + clock_gettime(CLOCK_THREAD_CPUTIME_ID, &this->starttime) == 0; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +query_tag_perf_counter::~query_tag_perf_counter() |
| 37 | +{ |
| 38 | + if(!this->started) |
| 39 | + return; |
| 40 | + struct timespec endtime; |
| 41 | + if(clock_gettime(CLOCK_THREAD_CPUTIME_ID, &endtime) != 0) |
| 42 | + return; |
| 43 | + |
| 44 | + int64_t cputime = timespec_diff(this->starttime, endtime); |
| 45 | + if(cputime < 0) |
| 46 | + return; // skip if overflow |
| 47 | + |
| 48 | + assert(thd->num_queries > 0); |
| 49 | + int64_t cputime_per_query = cputime / thd->num_queries; |
| 50 | + |
| 51 | + std::lock_guard<std::mutex> lock(stats_mutex); |
| 52 | + stats[thd->query_type] += cputime_per_query; |
| 53 | +} |
| 54 | + |
| 55 | +int fill_query_tag_perf_counter(THD* thd, TABLE_LIST* tables, Item* cond) |
| 56 | +{ |
| 57 | + std::lock_guard<std::mutex> lock(stats_mutex); |
| 58 | + |
| 59 | + DBUG_ENTER("fill_query_tag_perf_counter"); |
| 60 | + |
| 61 | + TABLE* table= tables->table; |
| 62 | + for (const auto& row : stats) |
| 63 | + { |
| 64 | + |
| 65 | + restore_record(table, s->default_values); |
| 66 | + Field **field = table->field; |
| 67 | + |
| 68 | + const std::string& query_type = row.first; |
| 69 | + uint64_t cpu_time = row.second; |
| 70 | + |
| 71 | + field[0]->store(query_type.c_str(), |
| 72 | + query_type.length(), system_charset_info); |
| 73 | + field[1]->store(cpu_time, TRUE); |
| 74 | + |
| 75 | + if (schema_table_store_record(thd, table)) |
| 76 | + { |
| 77 | + DBUG_RETURN(-1); |
| 78 | + } |
| 79 | + } |
| 80 | + DBUG_RETURN(0); |
| 81 | +} |
| 82 | + |
| 83 | +const uint query_type_field_length = 254; |
| 84 | + |
| 85 | +ST_FIELD_INFO query_tag_perf_fields_info[]= |
| 86 | +{ |
| 87 | + {"QUERY_TYPE", query_type_field_length, MYSQL_TYPE_STRING, |
| 88 | + 0, 0, 0, SKIP_OPEN_TABLE}, |
| 89 | + {"CPU", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, |
| 90 | + 0, 0, 0, SKIP_OPEN_TABLE}, |
| 91 | + {0, 0, MYSQL_TYPE_STRING, 0, 0, 0, SKIP_OPEN_TABLE} |
| 92 | +}; |
| 93 | + |
| 94 | +static int64_t timespec_diff(const timespec& start, const timespec& stop) |
| 95 | +{ |
| 96 | + const int64_t sec = stop.tv_sec - start.tv_sec; |
| 97 | + const int64_t nsec = stop.tv_nsec - start.tv_nsec; |
| 98 | + const int64_t diff = sec * 1000000000LL + nsec; |
| 99 | + return diff; |
| 100 | +} |
| 101 | +} // namespace qutils |
| 102 | + |
0 commit comments