Skip to content

Commit 50b4b45

Browse files
Expose Redis/RedisAI main thread cpu usage via modules info (#530)
* [add] Expose Redis/RedisAI main thread cpu usage via modules info * [fix] Fixed BG thread CPU percentage conversion (we were converting from nsec to ms and it's supposed to be from nsec to micros ) * [fix] Fixed test_info_modules reply check
1 parent 665b358 commit 50b4b45

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/redisai.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2+
#ifdef __linux__
3+
#define _GNU_SOURCE
4+
#endif
5+
16
#define REDISMODULE_MAIN
27
#include "redismodule.h"
38
#include "tensor.h"
@@ -32,6 +37,12 @@
3237
#define REDISAI_GIT_SHA "unknown"
3338
#endif
3439

40+
#ifdef __linux__
41+
#ifndef RUSAGE_THREAD
42+
#define RUSAGE_THREAD 1
43+
#endif
44+
#endif
45+
3546
int redisMajorVersion;
3647
int redisMinorVersion;
3748
int redisPatchVersion;
@@ -986,10 +997,31 @@ void RAI_moduleInfoFunc(RedisModuleInfoCtx *ctx, int for_crash_report) {
986997
RedisModule_InfoAddFieldLongLong(ctx, "inter_op_parallelism", getBackendsInterOpParallelism());
987998
RedisModule_InfoAddFieldLongLong(ctx, "intra_op_parallelism", getBackendsIntraOpParallelism());
988999
struct rusage self_ru, c_ru;
1000+
9891001
// Return resource usage statistics for the calling process,
9901002
// which is the sum of resources used by all threads in the
9911003
// process
9921004
getrusage(RUSAGE_SELF, &self_ru);
1005+
1006+
// Return resource usage statistics for the calling thread
1007+
// which in this case is Redis/RedisAI main thread
1008+
// RUSAGE_THREAD is Linux-specific.
1009+
sds main_thread_used_cpu_sys = sdsempty();
1010+
sds main_thread_used_cpu_user = sdsempty();
1011+
#if (defined(__linux__) && defined(RUSAGE_THREAD))
1012+
struct rusage main_thread_ru;
1013+
getrusage(RUSAGE_THREAD, &main_thread_ru);
1014+
main_thread_used_cpu_sys =
1015+
sdscatprintf(main_thread_used_cpu_sys, "%ld.%06ld", (long)main_thread_ru.ru_stime.tv_sec,
1016+
(long)self_ru.ru_stime.tv_usec);
1017+
main_thread_used_cpu_user =
1018+
sdscatprintf(main_thread_used_cpu_user, "%ld.%06ld", (long)main_thread_ru.ru_utime.tv_sec,
1019+
(long)self_ru.ru_utime.tv_usec);
1020+
#else
1021+
sdscatprintf(main_thread_used_cpu_sys, "N/A");
1022+
sdscatprintf(main_thread_used_cpu_user, "N/A");
1023+
#endif
1024+
9931025
// Return resource usage statistics for all of its
9941026
// terminated child processes
9951027
getrusage(RUSAGE_CHILDREN, &c_ru);
@@ -1006,6 +1038,8 @@ void RAI_moduleInfoFunc(RedisModuleInfoCtx *ctx, int for_crash_report) {
10061038
RedisModule_InfoAddFieldCString(ctx, "self_used_cpu_user", self_used_cpu_user);
10071039
RedisModule_InfoAddFieldCString(ctx, "children_used_cpu_sys", children_used_cpu_sys);
10081040
RedisModule_InfoAddFieldCString(ctx, "children_used_cpu_user", children_used_cpu_user);
1041+
RedisModule_InfoAddFieldCString(ctx, "main_thread_used_cpu_sys", main_thread_used_cpu_sys);
1042+
RedisModule_InfoAddFieldCString(ctx, "main_thread_used_cpu_user", main_thread_used_cpu_user);
10091043

10101044
AI_dictIterator *iter = AI_dictGetSafeIterator(run_queues);
10111045
AI_dictEntry *entry = AI_dictNext(iter);
@@ -1018,7 +1052,7 @@ void RAI_moduleInfoFunc(RedisModuleInfoCtx *ctx, int for_crash_report) {
10181052
struct timespec ts;
10191053
clockid_t cid;
10201054
sds queue_used_cpu_total = sdscatprintf(
1021-
sdsempty(), "queue_%s_bthread_#%d_used_cpu_total", queue_name, i + 1);
1055+
sdsempty(), "queue_%s_bthread_n%d_used_cpu_total", queue_name, i + 1);
10221056
sds bthread_used_cpu_total = sdsempty();
10231057
#if (!defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) || defined(_DARWIN_C_SOURCE) || \
10241058
defined(__cplusplus)
@@ -1034,7 +1068,7 @@ void RAI_moduleInfoFunc(RedisModuleInfoCtx *ctx, int for_crash_report) {
10341068
} else {
10351069
bthread_used_cpu_total =
10361070
sdscatprintf(bthread_used_cpu_total, "%ld.%06ld", (long)ts.tv_sec,
1037-
(long)(ts.tv_nsec / 1000000));
1071+
(long)(ts.tv_nsec / 1000));
10381072
}
10391073
}
10401074
RedisModule_InfoAddFieldCString(ctx, queue_used_cpu_total, bthread_used_cpu_total);

tests/flow/tests_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def test_info_modules(env):
301301
env.assertEqual( 'ai_self_used_cpu_user' in ret, True )
302302
env.assertEqual( 'ai_children_used_cpu_sys' in ret, True )
303303
env.assertEqual( 'ai_children_used_cpu_user' in ret, True )
304-
env.assertEqual( 'ai_queue_CPU_bthread_#1_used_cpu_total' in ret, True )
304+
env.assertEqual( 'ai_queue_CPU_bthread_n1_used_cpu_total' in ret, True )
305305

306306
def test_lua_multi(env):
307307
con = env.getConnection()

0 commit comments

Comments
 (0)