TBool GetCpuTimeIsSupported() { RThread thread; TTimeIntervalMicroSeconds time; TInt err = thread.GetCpuTime(time); test(err == KErrNone || err == KErrNotSupported); return err == KErrNone; }
// Gets the CPU count of all threads in process aproc //and returns the hour minute second in the references passed //If CPU count cant be obtained returns -1 int getCpuCount(TFileName& apname,TDes& aResult) { TInt64 proctime=0; int res; RThread thrd; TTimeIntervalMicroSeconds cputime(0); TBool notime; TFileName aprocname(apname); aprocname.Append('*'); TFindThread tfinder(aprocname); notime=false; while(1) { res = tfinder.Next(aprocname); if(res!=KErrNone) { break; } res=thrd.Open(tfinder); if(res!=KErrNone) { notime=true; continue; } res=thrd.GetCpuTime(cputime); if(res!=KErrNone) { // GetCpuTime() not supported. //kernel doesn't support this } proctime+=cputime.Int64(); thrd.Close(); } TTime time(proctime); TDateTime date=time.DateTime(); if(date.Day()==0) { aResult.AppendFormat(_L(" %02d:%02d:%02d "),date.Hour(),date.Minute(),date.Second()); } else { aResult.AppendFormat(_L(" %d-%02d:%02d:%02d "),date.Day(),date.Hour(),date.Minute(),date.Second()); } if(notime) { aResult.Append(_L("?")); } if(!notime) return 0; else return -1; }
EXPORT_C clock_t clock () { int retval=-1; RThread proc; TTimeIntervalMicroSeconds iMicSecsFromEpoc; TInt err=proc.GetCpuTime(iMicSecsFromEpoc); if(err) { return retval; } return I64INT(iMicSecsFromEpoc.Int64()); }
// Returns the time the current thread has spent executing, in milliseconds. static inline unsigned getCPUTime() { #if OS(DARWIN) mach_msg_type_number_t infoCount = THREAD_BASIC_INFO_COUNT; thread_basic_info_data_t info; // Get thread information mach_port_t threadPort = mach_thread_self(); thread_info(threadPort, THREAD_BASIC_INFO, reinterpret_cast<thread_info_t>(&info), &infoCount); mach_port_deallocate(mach_task_self(), threadPort); unsigned time = info.user_time.seconds * 1000 + info.user_time.microseconds / 1000; time += info.system_time.seconds * 1000 + info.system_time.microseconds / 1000; return time; #elif OS(WINDOWS) union { FILETIME fileTime; unsigned long long fileTimeAsLong; } userTime, kernelTime; // GetThreadTimes won't accept NULL arguments so we pass these even though // they're not used. FILETIME creationTime, exitTime; GetThreadTimes(GetCurrentThread(), &creationTime, &exitTime, &kernelTime.fileTime, &userTime.fileTime); return userTime.fileTimeAsLong / 10000 + kernelTime.fileTimeAsLong / 10000; #elif OS(SYMBIAN) RThread current; TTimeIntervalMicroSeconds cpuTime; TInt err = current.GetCpuTime(cpuTime); ASSERT_WITH_MESSAGE(err == KErrNone, "GetCpuTime failed with %d", err); return cpuTime.Int64() / 1000; #elif PLATFORM(BREWMP) // This function returns a continuously and linearly increasing millisecond // timer from the time the device was powered on. // There is only one thread in BREW, so this is enough. return GETUPTIMEMS(); #else // FIXME: We should return the time the current thread has spent executing. // use a relative time from first call in order to avoid an overflow static double firstTime = currentTime(); //+EAWebKitChange //10/17/2011 - Added explicit cast. return static_cast<unsigned> ((currentTime() - firstTime) * 1000); //-EAWebKitChange #endif }
void EnsureSystemIdle() { const TInt KMaxWait = 60 * 1000000; const TInt KSampleTime = 1 * 1000000; const TInt KWaitTime = 5 * 1000000; test.Printf(_L("Waiting for system to become idle\n")); TInt totalTime = 0; TBool idle; do { RThread thread; test_KErrNone(thread.Create(_L("EnsureSystemIdleThread"), EnsureSystemIdleThread, 1024, NULL, NULL)); thread.SetPriority(EPriorityLess); TRequestStatus status; thread.Rendezvous(status); thread.Resume(); User::WaitForRequest(status); test_KErrNone(status.Int()); User::After(KSampleTime); thread.Suspend(); TTimeIntervalMicroSeconds time; test_KErrNone(thread.GetCpuTime(time)); TReal error = (100.0 * Abs(time.Int64() - KSampleTime)) / KSampleTime; test.Printf(_L(" time == %ld, error == %f%%\n"), time.Int64(), error); idle = error < 2.0; thread.Kill(KErrNone); thread.Logon(status); User::WaitForRequest(status); test_KErrNone(status.Int()); CLOSE_AND_WAIT(thread); if (!idle) User::After(KWaitTime); // Allow system to finish whatever it's doing totalTime += KSampleTime + KWaitTime; test(totalTime < KMaxWait); } while(!idle); }
// Returns the time the current thread has spent executing, in milliseconds. static inline unsigned getCPUTime() { #if OS(DARWIN) mach_msg_type_number_t infoCount = THREAD_BASIC_INFO_COUNT; thread_basic_info_data_t info; // Get thread information mach_port_t threadPort = mach_thread_self(); thread_info(threadPort, THREAD_BASIC_INFO, reinterpret_cast<thread_info_t>(&info), &infoCount); mach_port_deallocate(mach_task_self(), threadPort); unsigned time = info.user_time.seconds * 1000 + info.user_time.microseconds / 1000; time += info.system_time.seconds * 1000 + info.system_time.microseconds / 1000; return time; #elif OS(WINDOWS) union { FILETIME fileTime; unsigned long long fileTimeAsLong; } userTime, kernelTime; // GetThreadTimes won't accept NULL arguments so we pass these even though // they're not used. FILETIME creationTime, exitTime; GetThreadTimes(GetCurrentThread(), &creationTime, &exitTime, &kernelTime.fileTime, &userTime.fileTime); return userTime.fileTimeAsLong / 10000 + kernelTime.fileTimeAsLong / 10000; #elif OS(SYMBIAN) RThread current; TTimeIntervalMicroSeconds cpuTime; TInt err = current.GetCpuTime(cpuTime); ASSERT_WITH_MESSAGE(err == KErrNone, "GetCpuTime failed with %d", err); return cpuTime.Int64() / 1000; #else // FIXME: We should return the time the current thread has spent executing. return currentTime() * 1000; #endif }
TInt ThreadFunction2(TAny* aParam) { TTimeIntervalMicroSeconds& time = *(TTimeIntervalMicroSeconds*)aParam; RThread thread; return thread.GetCpuTime(time); }
//! @SYMTestCaseID t_cputime_1 //! @SYMTestType CT //! @SYMTestCaseDesc Thread CPU time tests //! @SYMREQ CR RFID-66JJKX //! @SYMTestActions Tests cpu time when a thread is put through the various states //! @SYMTestExpectedResults Reported cpu time increses only when the thread is running //! @SYMTestPriority High //! @SYMTestStatus Defined void TestThreadCpuTime() { test.Start(_L("CPU thread time unit tests")); TThreadParam threadParam; FailIfError((threadParam.iSem).CreateLocal(0)); threadParam.iCpu = 0; // Later tests will exercise other CPUs RThread thread; RUndertaker u; TInt h; TRequestStatus s; FailIfError(thread.Create(_L("Thread"), ThreadFunction, 1024, NULL, &threadParam)); thread.SetPriority(EPriorityLess); FailIfError(u.Create()); FailIfError(u.Logon(s,h)); test(s==KRequestPending); TTimeIntervalMicroSeconds time, time2; TInt64 us; // Test cpu time is initially zero FailIfError(thread.GetCpuTime(time)); test(time == 0); // Test cpu time is not increased while thread is waiting on semaphore thread.Resume(); User::After(KShortWait); FailIfError(thread.GetCpuTime(time2)); us = time2.Int64(); test.Printf(_L("Time %dus\n"), us); test(us < KTolerance); // wait should happen in less than 1ms // Test cpu time increases when thread allowed to run // We want to allow 2% tolerance for the thread's CPU time, as there could be // something else running on the system during that time which would result lower CPU time than the // actual KShortPeriod or KLongPeriod wait time. // Also User::After(t) might return within the range of <t, t + 1000000/64 + 2*NanoKarnelTickPeriod>. // Given all that - we expect that the the cpu time should be within the range of: // <t - 0.02*t, t + 15625 + 2*NanoKernelTickPeriod> // or <0.98*t, t + 15625 + 2*NanoKernelTickPeriod> TInt user_after_tolerance = 0; HAL::Get(HAL::ENanoTickPeriod, user_after_tolerance); user_after_tolerance += user_after_tolerance + 15625; (threadParam.iSem).Signal(); User::After(KShortWait); FailIfError(thread.GetCpuTime(time)); us = time.Int64() - time2.Int64(); test.Printf(_L("Time %dus\n"), us); test(100*us >= 98*KShortWait); // left limit test(us - KShortWait <= user_after_tolerance); // right limit FailIfError(thread.GetCpuTime(time)); User::After(KLongWait); FailIfError(thread.GetCpuTime(time2)); us = time2.Int64() - time.Int64(); test.Printf(_L("Time %dus\n"), us); test(100*us >= 98*KLongWait); // left limit test(us - KLongWait <= user_after_tolerance); // right limit // Test not increased while suspended thread.Suspend(); FailIfError(thread.GetCpuTime(time)); User::After(KShortWait); FailIfError(thread.GetCpuTime(time2)); test(time == time2); thread.Resume(); // Test not increased while dead thread.Kill(KErrNone); User::WaitForRequest(s); // wait on undertaker since that completes in supervisor thread FailIfError(thread.GetCpuTime(time)); User::After(KShortWait); FailIfError(thread.GetCpuTime(time2)); test(time == time2); RThread t; t.SetHandle(h); test(t.Id()==thread.Id()); t.Close(); u.Close(); thread.Close(); (threadParam.iSem).Close(); test.End(); }
void EnsureSystemIdle() { // This test assumes 100% cpu resource is available, so it can fail on // windows builds if something else is running in the background. This // function attempts to wait for the system to become idle. #ifdef __WINS__ const TInt KMaxWait = 60 * 1000000; const TInt KSampleTime = 1 * 1000000; const TInt KWaitTime = 5 * 1000000; test.Start(_L("Waiting for system to become idle")); TInt totalTime = 0; TBool idle; do { test(totalTime < KMaxWait); TThreadParam threadParam; FailIfError((threadParam.iSem).CreateLocal(0)); threadParam.iCpu = 1; RThread thread; FailIfError(thread.Create(_L("Thread"), ThreadFunction, 1024, NULL, &threadParam)); thread.SetPriority(EPriorityLess); thread.Resume(); User::After(KShortWait); // Pause to allow thread setup (threadParam.iSem).Signal(); User::After(KSampleTime); thread.Suspend(); TTimeIntervalMicroSeconds time; FailIfError(thread.GetCpuTime(time)); TReal error = (100.0 * Abs(time.Int64() - KSampleTime)) / KSampleTime; test.Printf(_L(" time == %ld, error == %f%%\n"), time, error); idle = error < 2.0; thread.Kill(KErrNone); TRequestStatus status; thread.Logon(status); User::WaitForRequest(status); test(status == KErrNone); CLOSE_AND_WAIT(thread); (threadParam.iSem).Close(); if (!idle) User::After(KWaitTime); // Allow system to finish whatever it's doing totalTime += KShortWait + KSampleTime + KWaitTime; } while(!idle); test.End(); #endif }