HRESULT MeterModule::StartThreadTimeMeter() { ErrorIf(0 != this->startThreadCycleTime); ErrorIf(0 == QueryThreadCycleTime(this->thread, &this->startThreadCycleTime)); return S_OK; Error: return E_FAIL; }
HRESULT MeterModule::StopThreadTimeMeter() { ULONG64 nowCycles; ErrorIf(0 == this->startThreadCycleTime); ErrorIf(0 == QueryThreadCycleTime(this->thread, &nowCycles)); this->threadCycleTime += nowCycles - this->startThreadCycleTime; this->startThreadCycleTime = 0; return S_OK; Error: return E_FAIL; }
int Ten18::Util::EnumerateNativeThreads(bool traceFullInfo, const char* msg) { Ten18_UNUSED(traceFullInfo); DebugOut(msg); int numThreads = 0; auto snapshot = MakeScoped(CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0), [] (HANDLE snapshot) { Ten18_HOPE_FOR.True = CloseHandle(snapshot); }); Ten18_EXPECT.Not(INVALID_HANDLE_VALUE) = static_cast<HANDLE>(snapshot); THREADENTRY32 te = {}; te.dwSize = sizeof(te); const auto minSizeOfInterest = (std::min)((FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + sizeof(te.th32OwnerProcessID)), (FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + sizeof(te.th32ThreadID))); if (Thread32First(snapshot, &te)) { do { if (te.dwSize >= minSizeOfInterest) if (te.th32OwnerProcessID == GetCurrentProcessId()) { ++numThreads; auto thread = MakeScoped(OpenThread(THREAD_QUERY_INFORMATION, FALSE, te.th32ThreadID), [] (HANDLE t) { Ten18_HOPE_FOR.NotZero = CloseHandle(t); }); Ten18_EXPECT.NotNull = static_cast<HANDLE>(thread); ULONG64 cycleTime = 0; Ten18_EXPECT.True = QueryThreadCycleTime(thread, &cycleTime); FILETIME create, exit, kernel, user; Ten18_EXPECT.True = GetThreadTimes(thread, &create, &exit, &kernel, &user); Format("\tNative Thread %0 - Create: %1, Exit: %2, Kernel: %3, User: %4, Cycles: %5", te.th32ThreadID, create.dwLowDateTime, exit.dwLowDateTime, kernel.dwLowDateTime, user.dwLowDateTime, static_cast<int>(cycleTime)).DebugOut(); } te.dwSize = sizeof(te); } while (Thread32Next(snapshot, &te)); } Format("Number of Native Threads: %0", numThreads).DebugOut(); return numThreads; }
HRESULT MeterModule::GetCurrentThreadCycleTime(bool getInProgressStats, ULONG64 *applicationTime, ULONG64 *totalTime) { ErrorIf(NULL == applicationTime); ErrorIf(NULL == totalTime); ErrorIf(0 == QueryThreadCycleTime(this->thread, totalTime)); if (this->startThreadCycleTime == 0 || !getInProgressStats) { *applicationTime = this->threadCycleTime; } else { *applicationTime = this->threadCycleTime + *totalTime - this->startThreadCycleTime; } return S_OK; Error: return E_FAIL; }
/* Lists threads of the running process plus * Some information about them. */ BOOL GetThreadsList(INT NumberOfProcess,DWORD dwTID){ // Initializing Number of threads. INT NumberOfThreads=0; THREADENTRY32 th32; HANDLE hThreadSnap = INVALID_HANDLE_VALUE; HANDLE hThread=INVALID_HANDLE_VALUE; // Taking a sanpshot from threads of the process. hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,0); // Setting size of the structure before using it. th32.dwSize=sizeof(THREADENTRY32); // obtaining the first process and checking for error. if(!Thread32First(hThreadSnap,&th32)){ // If there was an error ( because each process has at least one thread ). PeInfo[NumberOfProcess].ThreadCount = NumberOfThreads; // Clean the snapshot object. CloseHandle(hThreadSnap); return FALSE; } do{ if(th32.th32OwnerProcessID == dwTID){ // Opens an existing thread object. hThread = OpenThread(THREAD_QUERY_INFORMATION ,FALSE,th32.th32ThreadID); // Obtaining thread PIDs. PeInfo[NumberOfProcess].Threads[NumberOfThreads].dwTID = th32.th32ThreadID; // Obtaining Thread Priority. PeInfo[NumberOfProcess].Threads[NumberOfThreads].ThreadPriority = GetThreadPriority(hThread); // The number of CPU clock cycles used by the thread. This value includes cycles spent in both user mode and kernel mode. QueryThreadCycleTime(hThread,&PeInfo[NumberOfProcess].Threads[NumberOfThreads].CycleTime); // Closes the handle. CloseHandle(hThread); // Adding NumberOfThreads. ++NumberOfThreads; } }while(Thread32Next(hThreadSnap,&th32)); // Closing the handle object. CloseHandle(hThreadSnap); // assigning number of threads to the variable of that process. PeInfo[NumberOfProcess].ThreadCount = NumberOfThreads; return TRUE; }
int __cdecl main(int argc, char *argv[]) { int ret = FAIL; //Test is failing unreliably, so for now we always return pass. if (TRUE){ ret = PASS; goto EXIT; } LONG64 Actual, Expected, Delta = 850000000; Actual = 0; Expected = 0; const LONG64 MSEC_TO_NSEC = 1000000; /* * Initialize the PAL and return FAILURE if this fails */ if(0 != (PAL_Initialize(argc, argv))) { return FAIL; } HANDLE cThread = GetCurrentThread(); int i; /* Take 2000 tiny measurements */ for (i = 0; i < 2000; i++){ ULONG64 FirstCount, SecondCount; LONG64 Init; Sleep(1); /* Grab a FirstCount, then loop for a bit to make the clock increase */ if (!QueryThreadCycleTime(cThread, (PULONG64)&FirstCount)) { Fail("ERROR: QueryThreadCycleTime returned failure.\n"); } LONG64 x; /* Init is in milliseconds, so we will convert later */ Init = (LONG64)GetTickCount(); x = Init + 3; volatile int counter; do { for (counter = 0; counter < 100000; counter++) { // spin to consume CPU time } } while (x > GetTickCount()); Expected += (GetTickCount() - Init) * MSEC_TO_NSEC; /* Get a second count */ if (!QueryThreadCycleTime(cThread, (PULONG64)&SecondCount)) { Fail("ERROR: QueryThreadCycleTime returned failure.\n"); } LONG64 trial = (LONG64)SecondCount - (LONG64)FirstCount; if (trial < 0){ printf("Negative value %llu measured", trial); } Actual += (trial); } if(labs(Expected - Actual) > Delta) { Fail("ERROR: The measured time (%llu millisecs) was not within Delta %llu " "of the expected time (%llu millisecs).\n", (Actual / MSEC_TO_NSEC), (Delta / MSEC_TO_NSEC), (Expected / MSEC_TO_NSEC)); } //printf("%llu, %llu\n", Expected, Actual); PAL_Terminate(); ret = PASS; EXIT: return ret; }
bool CycleTimer::GetThreadCyclesS(unsigned __int64* cycles) { BOOL res = FALSE; res = QueryThreadCycleTime(GetCurrentThread(), cycles); return res != FALSE; }
void TracingCallTree::UpdateCycleTime(TracingCallTreeElem * prevActiveElem, TracingCallTreeElem* nextActiveElem){ BOOL suc = QueryThreadCycleTime(_osThreadHandle,&nextActiveElem->LastCycleTime); CheckError2(suc); prevActiveElem->CycleTime += nextActiveElem->LastCycleTime - prevActiveElem->LastCycleTime; }
JNIEXPORT jlong JNICALL Java_edu_brown_cs_systems_clockcycles_CPUCycles_getNative(JNIEnv *env, jclass thisObj) { QueryThreadCycleTime(GetCurrentThread(), &cycles); return cycles; }