1
+
2
+ #ifdef __linux__
3
+ #define _GNU_SOURCE
4
+ #endif
5
+
1
6
#define REDISMODULE_MAIN
2
7
#include "redismodule.h"
3
8
#include "tensor.h"
32
37
#define REDISAI_GIT_SHA "unknown"
33
38
#endif
34
39
40
+ #ifdef __linux__
41
+ #ifndef RUSAGE_THREAD
42
+ #define RUSAGE_THREAD 1
43
+ #endif
44
+ #endif
45
+
35
46
int redisMajorVersion ;
36
47
int redisMinorVersion ;
37
48
int redisPatchVersion ;
@@ -986,10 +997,31 @@ void RAI_moduleInfoFunc(RedisModuleInfoCtx *ctx, int for_crash_report) {
986
997
RedisModule_InfoAddFieldLongLong (ctx , "inter_op_parallelism" , getBackendsInterOpParallelism ());
987
998
RedisModule_InfoAddFieldLongLong (ctx , "intra_op_parallelism" , getBackendsIntraOpParallelism ());
988
999
struct rusage self_ru , c_ru ;
1000
+
989
1001
// Return resource usage statistics for the calling process,
990
1002
// which is the sum of resources used by all threads in the
991
1003
// process
992
1004
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
+
993
1025
// Return resource usage statistics for all of its
994
1026
// terminated child processes
995
1027
getrusage (RUSAGE_CHILDREN , & c_ru );
@@ -1006,6 +1038,8 @@ void RAI_moduleInfoFunc(RedisModuleInfoCtx *ctx, int for_crash_report) {
1006
1038
RedisModule_InfoAddFieldCString (ctx , "self_used_cpu_user" , self_used_cpu_user );
1007
1039
RedisModule_InfoAddFieldCString (ctx , "children_used_cpu_sys" , children_used_cpu_sys );
1008
1040
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 );
1009
1043
1010
1044
AI_dictIterator * iter = AI_dictGetSafeIterator (run_queues );
1011
1045
AI_dictEntry * entry = AI_dictNext (iter );
@@ -1018,7 +1052,7 @@ void RAI_moduleInfoFunc(RedisModuleInfoCtx *ctx, int for_crash_report) {
1018
1052
struct timespec ts ;
1019
1053
clockid_t cid ;
1020
1054
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 );
1022
1056
sds bthread_used_cpu_total = sdsempty ();
1023
1057
#if (!defined(_POSIX_C_SOURCE ) && !defined(_XOPEN_SOURCE )) || defined(_DARWIN_C_SOURCE ) || \
1024
1058
defined(__cplusplus )
@@ -1034,7 +1068,7 @@ void RAI_moduleInfoFunc(RedisModuleInfoCtx *ctx, int for_crash_report) {
1034
1068
} else {
1035
1069
bthread_used_cpu_total =
1036
1070
sdscatprintf (bthread_used_cpu_total , "%ld.%06ld" , (long )ts .tv_sec ,
1037
- (long )(ts .tv_nsec / 1000000 ));
1071
+ (long )(ts .tv_nsec / 1000 ));
1038
1072
}
1039
1073
}
1040
1074
RedisModule_InfoAddFieldCString (ctx , queue_used_cpu_total , bthread_used_cpu_total );
0 commit comments