void QueryThreadsCycleData(DWORD processID, std::unordered_map<DWORD, ULONG64>& previousCycleTimes,
DWORD& threadWithLowestDelta, ULONG64& lowestCycleDelta,
std::unordered_map<DWORD, bool>& suspendedThreads, bool& isValorantDetected,
std::unordered_map<DWORD, bool>& existingThreads, std::unordered_map<DWORD, ULONG64>& newThreads,
ULONG64& highestTotalCycles, ULONG64& lowestCycleDeltaSaved, std::unordered_map<DWORD, ULONG64>& newThreadsSaved) {
THREADENTRY32 te32;
HANDLE hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hThreadSnap == INVALID_HANDLE_VALUE) {
std::cerr << "CreateToolhelp32Snapshot failed: " << GetLastError() << "n";
return;
}
te32.dwSize = sizeof(THREADENTRY32);
if (!Thread32First(hThreadSnap, &te32)) {
std::cerr << "Thread32First failed: " << GetLastError() << "n";
CloseHandle(hThreadSnap);
return;
}
lowestCycleDelta = ULLONG_MAX;
std::cout << "Thread CPU Cycles Data for Process ID: " << processID << std::endl;
do {
if (te32.th32OwnerProcessID == processID) {
HANDLE hThread = OpenThread(THREAD_QUERY_INFORMATION | THREAD_SUSPEND_RESUME, FALSE, te32.th32ThreadID);
if (hThread == NULL) {
std::cerr << "OpenThread failed: " << GetLastError() << "n";
continue;
}
ULONG64 currentCycleTime = 0;
if (QueryThreadCycleTime(hThread, ¤tCycleTime)) {
ULONG64 cycleDelta = 0;
if (previousCycleTimes.find(te32.th32ThreadID) != previousCycleTimes.end()) {
ULONG64 previousCycleTime = previousCycleTimes[te32.th32ThreadID];
cycleDelta = (currentCycleTime > previousCycleTime) ? (currentCycleTime - previousCycleTime) : 0;
}
previousCycleTimes[te32.th32ThreadID] = currentCycleTime;
std::cout << "Thread ID: " << te32.th32ThreadID
<< " - Total Cycles: " << currentCycleTime
<< " - Cycle Delta: " << cycleDelta << " cycles" << std::endl;
if (cycleDelta >= 15000 && cycleDelta <= 100000) {
if (cycleDelta < lowestCycleDelta) {
lowestCycleDelta = cycleDelta;
threadWithLowestDelta = te32.th32ThreadID;
}
}
if (currentCycleTime > highestTotalCycles) {
highestTotalCycles = currentCycleTime;
}
if (isValorantDetected) {
if (existingThreads.find(te32.th32ThreadID) == existingThreads.end()) {
newThreads[te32.th32ThreadID] = currentCycleTime;
std::cout << "New thread detected: Thread ID " << te32.th32ThreadID << std::endl;
}
}
else {
existingThreads[te32.th32ThreadID] = true;
}
}
else {
std::cerr << "QueryThreadCycleTime failed for thread ID: " << te32.th32ThreadID
<< ", error: " << GetLastError() << std::endl;
}
CloseHandle(hThread);
}
} while (Thread32Next(hThreadSnap, &te32));
CloseHandle(hThreadSnap);
if (isValorantDetected && !newThreads.empty()) {
DWORD threadToSuspend = 0;
ULONG64 maxCycles = 0;
for (const auto& entry : newThreads) {
if (entry.second > maxCycles) {
maxCycles = entry.second;
threadToSuspend = entry.first;
}
}
if (threadToSuspend != 0) {
std::this_thread::sleep_for(std::chrono::seconds(1));
HANDLE hThreadSuspend = OpenThread(THREAD_SUSPEND_RESUME, FALSE, threadToSuspend);
if (hThreadSuspend) {
SuspendThread(hThreadSuspend);
CloseHandle(hThreadSuspend);
std::cout << "Suspended new thread with highest total cycles: Thread ID " << threadToSuspend << std::endl;
std::cout << "Debug: Thread with highest total cycles has been suspended." << std::endl;
if (threadWithLowestDelta != 0) {
HANDLE hThreadLowestDelta = OpenThread(THREAD_SUSPEND_RESUME, FALSE, threadWithLowestDelta);
if (hThreadLowestDelta) {
SuspendThread(hThreadLowestDelta);
CloseHandle(hThreadLowestDelta);
std::cout << "Suspended thread with lowest cycle delta: Thread ID " << threadWithLowestDelta << std::endl;
std::cout << "Debug: Thread with lowest cycle delta has been suspended." << std::endl;
}
}
}
}
}
if (threadWithLowestDelta != 0 && suspendedThreads.empty()) {
for (const auto& thread : previousCycleTimes) {
if (thread.first != threadWithLowestDelta) {
HANDLE hThreadSuspend = OpenThread(THREAD_SUSPEND_RESUME, FALSE, thread.first);
if (hThreadSuspend) {
SuspendThread(hThreadSuspend);
CloseHandle(hThreadSuspend);
suspendedThreads[thread.first] = true;
}
}
}
}
if (threadWithLowestDelta != 0 && lowestCycleDelta != ULLONG_MAX) {
std::cout << "Lowest Cycle Delta Thread ID: " << threadWithLowestDelta << std::endl;
std::cout << "Cycle Delta: " << lowestCycleDelta << std::endl;
}
else {
std::cout << "No thread with cycle delta within the specified range found." << std::endl;
}
std::cout << "Highest Total Cycles: " << highestTotalCycles << std::endl;
// Save the results to a file
std::ofstream outFile("thread_data.txt");
if (outFile.is_open()) {
outFile << "Lowest Cycle Delta: " << lowestCycleDelta << std::endl;
outFile << "Highest Total Cycles: " << highestTotalCycles << std::endl;
if (isValorantDetected && !newThreads.empty()) {
outFile << "New Threads:" << std::endl;
for (const auto& entry : newThreads) {
outFile << "Thread ID: " << entry.first << " - Total Cycles: " << entry.second << std::endl;
}
}
else {
outFile << "No new threads detected." << std::endl;
}
outFile.close();
}
else {
std::cerr << "Failed to open file for writing." << std::endl;
}
}
this is my function, but somehow it’s unable to find the total lowest cycles delta and displays as 0:
As you can see in the output, every lowest cycle delta is displayed as 0. I want to find the lowest cycle delta for the thread, then suspend every thread except the lowest cycle delta. however, currently, as seen in the code every cycle delta is now 0, after the while loop was removed.
does anyone know how to resolve this issue without having to run a loop!?