kerneltest/e32test/misc/t_cputime.cpp
changeset 109 b3a1d9898418
parent 90 947f0dc9f7a8
child 201 43365a9b78a3
equal deleted inserted replaced
102:ef2a444a7410 109:b3a1d9898418
   226 	FailIfError(u.Create());
   226 	FailIfError(u.Create());
   227 	FailIfError(u.Logon(s,h));
   227 	FailIfError(u.Logon(s,h));
   228 	test(s==KRequestPending);
   228 	test(s==KRequestPending);
   229 
   229 
   230 	TTimeIntervalMicroSeconds time, time2;
   230 	TTimeIntervalMicroSeconds time, time2;
   231 	TUint us;
   231 	TInt64 us;
   232 	
   232 
   233 	// Test time is initially zero
   233 	// Test cpu time is initially zero
   234 	FailIfError(thread.GetCpuTime(time));
   234 	FailIfError(thread.GetCpuTime(time));
   235 	test(time == 0);
   235 	test(time == 0);
   236 
   236 
   237 	// Test not increased while waiting on semaphore
   237 	// Test cpu time is not increased while thread is waiting on semaphore
   238 	thread.Resume();
   238 	thread.Resume();
   239 	User::After(KShortWait);
   239 	User::After(KShortWait);
   240 	FailIfError(thread.GetCpuTime(time));
   240 	FailIfError(thread.GetCpuTime(time2));
   241 	us = I64LOW(time.Int64());
   241 	us = time2.Int64();
   242 	test.Printf(_L("Time %dus\n"), us);
   242 	test.Printf(_L("Time %dus\n"), us);
   243 	test(time < KTolerance); // wait happens in less than 0.5ms
   243 	test(us < KTolerance); // wait should happen in less than 1ms
   244 
   244 
   245 	// Test increases when thread allowed to run
   245 	// Test cpu time increases when thread allowed to run
       
   246 	// We want to allow 2% tolerance for the thread's CPU time, as there could be
       
   247 	// something else running on the system during that time which would result lower CPU time than the
       
   248 	// actual KShortPeriod or KLongPeriod wait time.
       
   249 	// Also User::After(t) might return within the range of <t, t + 1000000/64 + 2*NanoKarnelTickPeriod>.
       
   250 	// Given all that - we expect that the the cpu time should be within the range of:
       
   251 	// <t - 0.02*t, t + 15625 + 2*NanoKernelTickPeriod>
       
   252 	// or <0.98*t, t + 15625 + 2*NanoKernelTickPeriod>
       
   253 	TInt user_after_tolerance = 0;
       
   254 	HAL::Get(HAL::ENanoTickPeriod, user_after_tolerance);
       
   255 	user_after_tolerance += user_after_tolerance + 15625;
       
   256 
   246 	(threadParam.iSem).Signal();
   257 	(threadParam.iSem).Signal();
   247 	User::After(KShortWait);
   258 	User::After(KShortWait);
   248 	FailIfError(thread.GetCpuTime(time));
   259 	FailIfError(thread.GetCpuTime(time));
   249 	us = I64LOW(time.Int64());
   260 	us = time.Int64() - time2.Int64();
   250 	test.Printf(_L("Time %dus\n"), us);
   261 	test.Printf(_L("Time %dus\n"), us);
   251 	test(time > (KShortWait - KTolerance));
   262 	test(100*us >= 98*KShortWait); // left limit
       
   263 	test(us - KShortWait <= user_after_tolerance); // right limit
       
   264 
   252 	User::After(KLongWait);
   265 	User::After(KLongWait);
   253 	FailIfError(thread.GetCpuTime(time2));
   266 	FailIfError(thread.GetCpuTime(time2));
   254 	us = I64LOW(time2.Int64());
   267 	us = time2.Int64() - time.Int64();
   255 	test.Printf(_L("Time %dus\n"), us);
   268 	test.Printf(_L("Time %dus\n"), us);
   256 	test(time2.Int64() - time.Int64() > (KLongWait - KTolerance));
   269 	test(100*us >= 98*KLongWait); // left limit
       
   270 	test(us - KLongWait <= user_after_tolerance); // right limit
   257 
   271 
   258 	// Test not increased while suspended
   272 	// Test not increased while suspended
   259 	thread.Suspend();
   273 	thread.Suspend();
   260 	FailIfError(thread.GetCpuTime(time));
   274 	FailIfError(thread.GetCpuTime(time));
   261 	User::After(KShortWait);
   275 	User::After(KShortWait);
   262 	FailIfError(thread.GetCpuTime(time2));
   276 	FailIfError(thread.GetCpuTime(time2));
   263 	test(time == time2);
   277 	test(time == time2);
   264 	thread.Resume();
   278 	thread.Resume();
   265 	
   279 
   266 	// Test not increased while dead
   280 	// Test not increased while dead
   267 	thread.Kill(KErrNone);
   281 	thread.Kill(KErrNone);
   268 	User::WaitForRequest(s);	// wait on undertaker since that completes in supervisor thread
   282 	User::WaitForRequest(s);	// wait on undertaker since that completes in supervisor thread
   269 	FailIfError(thread.GetCpuTime(time));
   283 	FailIfError(thread.GetCpuTime(time));
   270 	User::After(KShortWait);
   284 	User::After(KShortWait);