Skip to content

Commit

Permalink
Allow providing a seed value for std::rand in faultinj config (NVIDIA…
Browse files Browse the repository at this point in the history
…#460)

Add ability to provide a seed value in config. Closes NVIDIA#452  
    
Signed-off-by: Gera Shegalov <gera@apache.org>
  • Loading branch information
gerashegalov authored Aug 5, 2022
1 parent 8cf379f commit 96fc1d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/main/cpp/faultinj/faultinj.cu
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ void CUPTIAPI faultInjectionCallbackHandler(void*,

if (injectionProbability < 100) {
if (injectionProbability <= 0) { return; }
const int rand10000 = rand() % 10000;
const int rand10000 = std::rand() % 10000;
const int skipThreshold = injectionProbability * 10000 / 100;
spdlog::trace("rand1000={} skipThreshold={}", rand10000, skipThreshold);
if (rand10000 >= skipThreshold) { return; }
Expand Down Expand Up @@ -336,7 +336,7 @@ void CUPTIAPI faultInjectionCallbackHandler(void*,
break;
}

default: ;
default:;
}
}

Expand Down Expand Up @@ -366,6 +366,11 @@ void readFaultInjectorConfig(void)
//
const std::string dynamicConfigKey = "dynamic";

// An unsigned int to seed the random number generator to deterministically
// recreate a fault sequence
//
const std::string seedKey = "seed";

// To retrieve a map of driver/runtime fault configs
// "functionName" -> fault config
// "CUPT callback id" -> fault config
Expand All @@ -381,6 +386,11 @@ void readFaultInjectorConfig(void)
globalControl.dynamic =
globalControl.configRoot.get_optional<bool>(dynamicConfigKey).value_or(false);

const unsigned seed =
globalControl.configRoot.get_optional<unsigned>(seedKey).value_or(std::time(0));
spdlog::info("Seeding std::srand with {}", seed);
std::srand(seed);

const spdlog::level::level_enum logLevelEnum = static_cast<spdlog::level::level_enum>(logLevel);
spdlog::info("changed log level to {}", logLevelEnum);
spdlog::set_level(logLevelEnum);
Expand Down
11 changes: 9 additions & 2 deletions src/test/cpp/faultinj/test_faultinj.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"logLevel": 1,
"seed": 12345,
"dynamic": true,
"cudaRuntimeFaults": {
"cudaLaunchKernel_ptsz": {
Expand All @@ -12,8 +13,14 @@
"*": {
"percent": 0,
"injectionType": 2,
"substituteReturnCode": 999,
"interceptionCount": 1
"substituteReturnCode": 2,
"interceptionCount": 1000
},
"cuLaunchKernel_ptsz": {
"percent": 0,
"injectionType": 2,
"substituteReturnCode": 2,
"interceptionCount": 1000
}
}
}

0 comments on commit 96fc1d9

Please sign in to comment.