Skip to content

Commit d47ef2b

Browse files
authored
Merge branch 'master' into Create_test_module_for_LLAPI
2 parents e480596 + 50b4b45 commit d47ef2b

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

src/dag.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,11 @@ void DAG_ReplyAndUnblock(RedisAI_OnFinishCtx *ctx, void *private_data) {
13131313
int RedisAI_ProcessDagRunCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc,
13141314
int dagMode) {
13151315

1316+
int flags = RedisModule_GetContextFlags(ctx);
1317+
bool blocking_not_allowed = (flags & (REDISMODULE_CTX_FLAGS_MULTI | REDISMODULE_CTX_FLAGS_LUA));
1318+
if (blocking_not_allowed)
1319+
return RedisModule_ReplyWithError(
1320+
ctx, "ERR Cannot run RedisAI command within a transaction or a LUA script");
13161321
RedisAI_RunInfo *rinfo = NULL;
13171322
if (RAI_InitRunInfo(&rinfo) == REDISMODULE_ERR) {
13181323
RedisModule_ReplyWithError(

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: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,24 @@ 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 )
305+
306+
def test_lua_multi(env):
307+
con = env.getConnection()
308+
ret = con.execute_command('MULTI')
309+
env.assertEqual(ret, b'OK')
310+
ret = con.execute_command('AI.MODELRUN', "no_model{1}", "INPUTS", "no_input{1}", "OUTPUTS", "no_output{1}")
311+
env.assertEqual(ret, b'QUEUED')
312+
try:
313+
ret = con.execute_command('EXEC')
314+
except Exception as e:
315+
exception = e
316+
env.assertEqual(type(exception), redis.exceptions.ResponseError)
317+
env.assertEqual("ERR Cannot run RedisAI command within a transaction or a LUA script", exception.__str__())
318+
try:
319+
ret = con.execute_command('EVAL', "return redis.pcall('AI.MODELRUN', 'no_model{1}', 'INPUTS', 'NO_INPUT{1}',"
320+
" 'OUTPUTS', 'NO_OUTPUT{1}')", 0)
321+
except Exception as e:
322+
exception = e
323+
env.assertEqual(type(exception), redis.exceptions.ResponseError)
324+
env.assertEqual("Cannot run RedisAI command within a transaction or a LUA script", exception.__str__())

0 commit comments

Comments
 (0)