summary refs log tree commit diff
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2021-01-08 15:19:41 +0000
committerErik Johnston <erik@matrix.org>2021-01-08 15:19:41 +0000
commite3411dc604de306ab893db3956153c4aaa903bc2 (patch)
treef4f2cbbe8fb37ec37d4e95e8dc1cdb941eef5842
parentFix "Starting metrics collection from sentinel context" errors (#9053) (diff)
downloadsynapse-e3411dc604de306ab893db3956153c4aaa903bc2.tar.xz
Track CPU usage of DB transactions
-rw-r--r--synapse/storage/database.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/synapse/storage/database.py b/synapse/storage/database.py

index b70ca3087b..1824c3aa3d 100644 --- a/synapse/storage/database.py +++ b/synapse/storage/database.py
@@ -43,6 +43,7 @@ from synapse.config.database import DatabaseConnectionConfig from synapse.logging.context import ( LoggingContext, current_context, + get_thread_resource_usage, make_deferred_yieldable, ) from synapse.metrics.background_process_metrics import run_as_background_process @@ -65,6 +66,9 @@ sql_scheduling_timer = Histogram("synapse_storage_schedule_time", "sec") sql_query_timer = Histogram("synapse_storage_query_time", "sec", ["verb"]) sql_txn_timer = Histogram("synapse_storage_transaction_time", "sec", ["desc"]) +sql_txn_ru_utime = Histogram("synapse_storage_transaction_ru_utime", "sec", ["desc"]) +sql_txn_ru_stime = Histogram("synapse_storage_transaction_ru_stime", "sec", ["desc"]) + # Unique indexes which have been added in background updates. Maps from table name # to the name of the background update which added the unique index to that table. @@ -496,6 +500,8 @@ class DatabasePool: transaction_logger.debug("[TXN START] {%s}", name) + start_usage = get_thread_resource_usage() + try: i = 0 N = 5 @@ -581,6 +587,17 @@ class DatabasePool: current_context().add_database_transaction(duration) + end_usage = get_thread_resource_usage() + + # Not all systems support tracking per thread CPU usage. + if start_usage and end_usage: + sql_txn_ru_utime.labels(desc).observe( + end_usage.ru_utime - start_usage.ru_utime + ) + sql_txn_ru_stime.labels(desc).observe( + end_usage.ru_stime - start_usage.ru_stime + ) + transaction_logger.debug("[TXN END] {%s} %f sec", name, duration) self._current_txn_total_time += duration