os/kernelhwsrv/kerneltest/e32test/demandpaging/t_threadcreate.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32test\demandpaging\t_threadcreate.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#define __E32TEST_EXTENSION__
sl@0
    19
#include <e32test.h>
sl@0
    20
#include <dptest.h>
sl@0
    21
#include <e32hal.h>
sl@0
    22
#include <u32exec.h>
sl@0
    23
#include <e32svr.h>
sl@0
    24
#include <e32panic.h>
sl@0
    25
#include "u32std.h"
sl@0
    26
sl@0
    27
#include "t_dpcmn.h"
sl@0
    28
sl@0
    29
enum
sl@0
    30
	{
sl@0
    31
	EUnspecified, 
sl@0
    32
	EPaged, 
sl@0
    33
	EUnpaged,
sl@0
    34
	};
sl@0
    35
sl@0
    36
_LIT(KGlobalThreadName, "gThreadGlobal");
sl@0
    37
sl@0
    38
RSemaphore gSem1;
sl@0
    39
RSemaphore gSem2;
sl@0
    40
TBool gStackPaged;
sl@0
    41
TUint8* gStackPtr = NULL;
sl@0
    42
sl@0
    43
struct SThreadPagedInfo
sl@0
    44
	{
sl@0
    45
	TBool iHeapPaged;
sl@0
    46
	TBool iStackPaged;
sl@0
    47
	};
sl@0
    48
sl@0
    49
TUint8 ReadByte(volatile TUint8* aPtr)
sl@0
    50
	{
sl@0
    51
	return *aPtr;
sl@0
    52
	}
sl@0
    53
sl@0
    54
TInt TestThreadFunction(TAny* aPtr)
sl@0
    55
	{
sl@0
    56
	for (TInt i = 0; i<2; i++)
sl@0
    57
		{
sl@0
    58
		if (i == 1)
sl@0
    59
			{
sl@0
    60
			User::SetRealtimeState(User::ERealtimeStateOn);
sl@0
    61
			RDebug::Printf("aPtr %x",aPtr);
sl@0
    62
			ReadByte((TUint8*)aPtr);
sl@0
    63
			}
sl@0
    64
		}
sl@0
    65
	return KErrNone;
sl@0
    66
	}
sl@0
    67
sl@0
    68
//
sl@0
    69
// IsStackPaged
sl@0
    70
//
sl@0
    71
// Determine whether the stack is paged by flushing the cache and attempting
sl@0
    72
// to read a byte that has been paged out
sl@0
    73
//
sl@0
    74
//
sl@0
    75
TInt IsStackPaged(const TUint8* aPtr)
sl@0
    76
	{
sl@0
    77
	RThread thread;
sl@0
    78
	TInt r;
sl@0
    79
	r = thread.Create(KNullDesC, TestThreadFunction, 0x1000, NULL, (TAny*)aPtr);
sl@0
    80
	if (r != KErrNone)
sl@0
    81
		{
sl@0
    82
		return r;
sl@0
    83
		}
sl@0
    84
sl@0
    85
	TRequestStatus status;
sl@0
    86
	thread.Logon(status);
sl@0
    87
	if(status.Int() != KRequestPending)
sl@0
    88
		{
sl@0
    89
		return KErrGeneral;
sl@0
    90
		}
sl@0
    91
	thread.Resume();
sl@0
    92
	User::WaitForRequest(status);
sl@0
    93
	if (thread.ExitType() == EExitPanic &&
sl@0
    94
		thread.ExitCategory() == _L("KERN-EXEC") &&
sl@0
    95
		thread.ExitReason() == EIllegalFunctionForRealtimeThread)
sl@0
    96
		{
sl@0
    97
		gStackPaged = ETrue;
sl@0
    98
		}
sl@0
    99
	else
sl@0
   100
		{ 
sl@0
   101
		r = thread.ExitReason();
sl@0
   102
		if(r != KErrNone)
sl@0
   103
			return r;
sl@0
   104
sl@0
   105
		if (EExitKill != thread.ExitType())
sl@0
   106
			return KErrGeneral;
sl@0
   107
		gStackPaged = EFalse;
sl@0
   108
		}	
sl@0
   109
	thread.Close();
sl@0
   110
	if (!gStackPaged)
sl@0
   111
		{
sl@0
   112
		RDebug::Printf("    %08x present", aPtr);
sl@0
   113
		}
sl@0
   114
	else
sl@0
   115
		{
sl@0
   116
		RDebug::Printf("    %08x not present", aPtr);
sl@0
   117
		}
sl@0
   118
	return r;
sl@0
   119
	}
sl@0
   120
sl@0
   121
/**
sl@0
   122
Thread that just returns the data paging attributes of the thread.
sl@0
   123
*/
sl@0
   124
TInt ThreadFunc(TAny* aThreadInfo)
sl@0
   125
	{
sl@0
   126
	SThreadPagedInfo& info = *(SThreadPagedInfo*)aThreadInfo;
sl@0
   127
	RHeap& heap = User::Heap();
sl@0
   128
	RChunk chunk;
sl@0
   129
	chunk.SetHandle(heap.ChunkHandle());
sl@0
   130
	info.iHeapPaged = chunk.IsPaged();
sl@0
   131
	gStackPtr = (TUint8*)&chunk;
sl@0
   132
	RDebug::Printf("&chunk %x",&chunk);
sl@0
   133
	gSem1.Signal();
sl@0
   134
	gSem2.Wait();
sl@0
   135
	info.iStackPaged = gStackPaged;
sl@0
   136
	return KErrNone;
sl@0
   137
	}
sl@0
   138
sl@0
   139
TInt DummyFunction(TAny*)
sl@0
   140
	{
sl@0
   141
	return KErrNone;
sl@0
   142
	}
sl@0
   143
sl@0
   144
TInt PanicThreadCreate(TAny* aCreateInfo)
sl@0
   145
	{
sl@0
   146
	RThread thread;
sl@0
   147
	TThreadCreateInfo createInfo((*(TThreadCreateInfo*) aCreateInfo));
sl@0
   148
	thread.Create(createInfo);
sl@0
   149
	return KErrGeneral; // Should never reach here
sl@0
   150
	}
sl@0
   151
sl@0
   152
//
sl@0
   153
// CheckHeapStackPaged
sl@0
   154
//
sl@0
   155
// Using the TThreadCreateInfo used to create the cheap, determine 
sl@0
   156
// whether the stack and the heap are paged or not
sl@0
   157
//
sl@0
   158
//
sl@0
   159
TInt CheckHeapStackPaged(TThreadCreateInfo& aCreateInfo, TInt aPaged, SThreadPagedInfo& aPagedInfo, TBool aUseProcessHeap = EFalse)
sl@0
   160
	{
sl@0
   161
	RThread thread;
sl@0
   162
	TBool paged;
sl@0
   163
	switch (aPaged)
sl@0
   164
		{
sl@0
   165
		case EUnspecified:
sl@0
   166
			test.Printf(_L("Testing gProcessPaged\n"));
sl@0
   167
			paged = gProcessPaged;
sl@0
   168
			break;
sl@0
   169
sl@0
   170
		case EPaged:
sl@0
   171
			test.Printf(_L("Testing Paged\n"));
sl@0
   172
			aCreateInfo.SetPaging(TThreadCreateInfo::EPaged);
sl@0
   173
			paged = ETrue;
sl@0
   174
			break;
sl@0
   175
sl@0
   176
		case EUnpaged:
sl@0
   177
			test.Printf(_L("Testing Unpaged\n"));
sl@0
   178
			aCreateInfo.SetPaging(TThreadCreateInfo::EUnpaged);
sl@0
   179
			paged = EFalse;
sl@0
   180
			break;
sl@0
   181
		}
sl@0
   182
sl@0
   183
sl@0
   184
	test_KErrNone(thread.Create(aCreateInfo));
sl@0
   185
	
sl@0
   186
	// Disable JIT debugging.
sl@0
   187
	TBool justInTime=User::JustInTime();
sl@0
   188
	User::SetJustInTime(EFalse);
sl@0
   189
sl@0
   190
	TRequestStatus status;
sl@0
   191
	thread.Logon(status); 
sl@0
   192
	
sl@0
   193
	thread.Resume();
sl@0
   194
	
sl@0
   195
	gSem1.Wait();		
sl@0
   196
	DPTest::FlushCache();
sl@0
   197
	TInt r = IsStackPaged(gStackPtr);
sl@0
   198
	test_KErrNone(r);
sl@0
   199
	gSem2.Signal();
sl@0
   200
sl@0
   201
	User::WaitForRequest(status);
sl@0
   202
	test (EExitKill == thread.ExitType());
sl@0
   203
	test(KErrNone == status.Int());
sl@0
   204
	
sl@0
   205
	test(KErrNone == thread.ExitReason());
sl@0
   206
sl@0
   207
	if (thread.ExitType() == EExitPanic)
sl@0
   208
		{
sl@0
   209
		test(thread.ExitCategory()==_L("USER"));
sl@0
   210
		}
sl@0
   211
			
sl@0
   212
	CLOSE_AND_WAIT(thread);
sl@0
   213
sl@0
   214
	// Put JIT debugging back to previous status.
sl@0
   215
	User::SetJustInTime(justInTime);
sl@0
   216
sl@0
   217
	UpdatePaged(paged);
sl@0
   218
	if (aUseProcessHeap)
sl@0
   219
		{// If using existing thread heap, heap will take the process paging status
sl@0
   220
		test_Equal(gProcessPaged, aPagedInfo.iHeapPaged);
sl@0
   221
		}
sl@0
   222
	else
sl@0
   223
		{
sl@0
   224
		test_Equal(paged, aPagedInfo.iHeapPaged);
sl@0
   225
		}
sl@0
   226
	test_Equal(paged, aPagedInfo.iStackPaged);
sl@0
   227
	return KErrNone;
sl@0
   228
	}
sl@0
   229
sl@0
   230
//
sl@0
   231
// TestThreadCreate
sl@0
   232
//
sl@0
   233
//----------------------------------------------------------------------------------------------
sl@0
   234
//! @SYMTestCaseID			KBASE-T_THREADHEAPCREATE-xxxx
sl@0
   235
//! @SYMTestType			UT
sl@0
   236
//! @SYMPREQ				PREQ1954
sl@0
   237
//! @SYMTestCaseDesc		TThreadCreateInfo tests
sl@0
   238
//!							Verify the thread heap creation implementation
sl@0
   239
//! @SYMTestActions	
sl@0
   240
//! 1.	Call TThreadCreateInfo::TThreadCreateInfo() with valid parameters. 
sl@0
   241
//! 	Following this call RThread::Create()
sl@0
   242
//! 2.	Call TThreadCreateInfo::TThreadCreateInfo() with an invalid stack size. 
sl@0
   243
//!		Following this call RThread::Create()
sl@0
   244
//! 3.	Call TThreadCreateInfo::SetCreateHeap() with an invalid min heap size. 
sl@0
   245
//!		Following this call RThread::Create()
sl@0
   246
//! 4.	Call TThreadCreateInfo::SetCreateHeap() with an invalid max heap size. 
sl@0
   247
//!		Following this call RThread::Create()
sl@0
   248
//! 5.	Call TThreadCreateInfo::SetCreateHeap() with minHeapSize. > maxHeapSize 
sl@0
   249
//!		Following this call RThread::Create()
sl@0
   250
//! 6.	Call TThreadCreateInfo::SetUseHeap() specifying NULL. Following this call RThread::Create()
sl@0
   251
//! 7.	Call TThreadCreateInfo::SetOwner() with aOwner set to EOwnerProcess. 
sl@0
   252
//!		Following this call RThread::Create()
sl@0
   253
//! 8.	Call TThreadCreateInfo::SetOwner() with aOwner set to EOwnerThread. 
sl@0
   254
//!		Following this call RThread::Create()
sl@0
   255
//! 9.	Call TThreadCreateInfo::SetPaging() with aPaging set to unspecified. 
sl@0
   256
//!		Following this call RThread::Create() and check the paging status of the thread
sl@0
   257
//! 10.	Call TThreadCreateInfo::SetPaging() with aPaging set to EPaged. 
sl@0
   258
//!		Following this call RThread::Create() and check the paging status of the thread
sl@0
   259
//! 11.	Call TThreadCreateInfo::SetPaging() with aPaging set to EUnpaged. 
sl@0
   260
//!		Following this call RThread::Create() and check the paging status of the thread
sl@0
   261
//!
sl@0
   262
//! @SYMTestExpectedResults All tests should pass.
sl@0
   263
//! @SYMTestPriority        High
sl@0
   264
//! @SYMTestStatus          Implemented
sl@0
   265
//----------------------------------------------------------------------------------------------
sl@0
   266
void TestThreadCreate()
sl@0
   267
	{
sl@0
   268
	TInt r;
sl@0
   269
	test.Start(_L("Test RThread::Create() (New Heap)"));
sl@0
   270
		{
sl@0
   271
		RThread thread;
sl@0
   272
		TThreadCreateInfo createInfo(KGlobalThreadName, DummyFunction, KDefaultStackSize, NULL);
sl@0
   273
		createInfo.SetCreateHeap(KMinHeapSize, KMinHeapSize);
sl@0
   274
		r = thread.Create(createInfo);
sl@0
   275
		test_KErrNone(r);
sl@0
   276
		test_KErrNone(TestThreadExit(thread, EExitKill, KErrNone));
sl@0
   277
		thread.Close();
sl@0
   278
		}
sl@0
   279
sl@0
   280
	test.Next(_L("Test RThread::Create() - invalid stack size"));
sl@0
   281
		{
sl@0
   282
		TThreadCreateInfo createInfo(KGlobalThreadName, DummyFunction, -1 , NULL);
sl@0
   283
		createInfo.SetCreateHeap(KMinHeapSize, KMinHeapSize);
sl@0
   284
		
sl@0
   285
		RThread threadPanic;
sl@0
   286
		test_KErrNone(threadPanic.Create(_L("Panic UserHeap"), PanicThreadCreate, KDefaultStackSize, KMinHeapSize, 
sl@0
   287
															KMinHeapSize,  (TAny*) &createInfo));
sl@0
   288
		test_KErrNone(TestThreadExit(threadPanic, EExitPanic, EThrdStackSizeNegative));
sl@0
   289
		}
sl@0
   290
sl@0
   291
	test.Next(_L("Test RThread::Create() - invalid min heap size"));
sl@0
   292
		{
sl@0
   293
		TThreadCreateInfo createInfo(KGlobalThreadName, DummyFunction, KDefaultStackSize , NULL);
sl@0
   294
		createInfo.SetCreateHeap(-1, KMinHeapSize);
sl@0
   295
		
sl@0
   296
		RThread threadPanic;
sl@0
   297
		test_KErrNone(threadPanic.Create(_L("Panic UserHeap"), PanicThreadCreate, KDefaultStackSize, KMinHeapSize, 
sl@0
   298
															KMinHeapSize,  (TAny*) &createInfo));
sl@0
   299
sl@0
   300
		test_KErrNone(TestThreadExit(threadPanic, EExitPanic, EThrdHeapMinTooSmall));
sl@0
   301
		}
sl@0
   302
sl@0
   303
	test.Next(_L("Test RThread::Create() - invalid max heap size"));
sl@0
   304
		{
sl@0
   305
		TThreadCreateInfo createInfo(KGlobalThreadName, DummyFunction, KDefaultStackSize , NULL);
sl@0
   306
		createInfo.SetCreateHeap(KMinHeapSize, -1);
sl@0
   307
		
sl@0
   308
		RThread threadPanic;
sl@0
   309
		test_KErrNone(threadPanic.Create(_L("Panic UserHeap"), PanicThreadCreate, KDefaultStackSize, KMinHeapSize, 
sl@0
   310
															KMinHeapSize,  (TAny*) &createInfo));
sl@0
   311
sl@0
   312
		test_KErrNone(TestThreadExit(threadPanic, EExitPanic, EThrdHeapMaxLessThanMin));
sl@0
   313
		}
sl@0
   314
sl@0
   315
	test.Next(_L("Test RThread::Create() - min heap size > max heap size"));
sl@0
   316
		{
sl@0
   317
		TThreadCreateInfo createInfo(KGlobalThreadName, DummyFunction, KDefaultStackSize , NULL);
sl@0
   318
		createInfo.SetCreateHeap(KMinHeapSize << 1, KMinHeapSize);
sl@0
   319
		
sl@0
   320
		RThread threadPanic;
sl@0
   321
		test_KErrNone(threadPanic.Create(_L("Panic UserHeap"), PanicThreadCreate, KDefaultStackSize, KMinHeapSize, 
sl@0
   322
															KMinHeapSize,  (TAny*) &createInfo));
sl@0
   323
sl@0
   324
		test_KErrNone(TestThreadExit(threadPanic, EExitPanic, EThrdHeapMaxLessThanMin));
sl@0
   325
		}
sl@0
   326
sl@0
   327
	test.Next(_L("Test TThreadCreateInfo::SetUseHeap() "));
sl@0
   328
		{
sl@0
   329
		RThread thread;
sl@0
   330
		TThreadCreateInfo createInfo(KGlobalThreadName, DummyFunction, KDefaultStackSize, NULL);
sl@0
   331
		createInfo.SetUseHeap(NULL);
sl@0
   332
		r = thread.Create(createInfo);
sl@0
   333
		test_KErrNone(r);
sl@0
   334
		test_KErrNone(TestThreadExit(thread, EExitKill, KErrNone));
sl@0
   335
		thread.Close();
sl@0
   336
		}
sl@0
   337
sl@0
   338
	test.Next(_L("Test TThreadCreateInfo::SetOwner(EOwnerProcess) "));
sl@0
   339
		{
sl@0
   340
		RThread thread;
sl@0
   341
		TThreadCreateInfo createInfo(KGlobalThreadName, DummyFunction, KDefaultStackSize, NULL);
sl@0
   342
		createInfo.SetCreateHeap(KMinHeapSize, KMinHeapSize);
sl@0
   343
		createInfo.SetOwner(EOwnerProcess);
sl@0
   344
		r = thread.Create(createInfo);
sl@0
   345
		test_KErrNone(r);
sl@0
   346
		test_KErrNone(TestThreadExit(thread, EExitKill, KErrNone));
sl@0
   347
		thread.Close();
sl@0
   348
		}
sl@0
   349
sl@0
   350
sl@0
   351
	test.Next(_L("Test TThreadCreateInfo::SetOwner(EOwnerThread) "));
sl@0
   352
		{
sl@0
   353
		RThread thread;
sl@0
   354
		TThreadCreateInfo createInfo(KGlobalThreadName, DummyFunction, KDefaultStackSize, NULL);
sl@0
   355
		createInfo.SetCreateHeap(KMinHeapSize, KMinHeapSize);
sl@0
   356
		createInfo.SetOwner(EOwnerThread);
sl@0
   357
		r = thread.Create(createInfo);
sl@0
   358
		test_KErrNone(r);
sl@0
   359
		test_KErrNone(TestThreadExit(thread, EExitKill, KErrNone));
sl@0
   360
		thread.Close();
sl@0
   361
		}
sl@0
   362
sl@0
   363
sl@0
   364
sl@0
   365
	gSem1.CreateLocal(0);
sl@0
   366
	gSem2.CreateLocal(0);
sl@0
   367
	test.Next(_L("Test Thread paging (New Heap)"));
sl@0
   368
		{		
sl@0
   369
		TBool aPaged = gProcessPaged;
sl@0
   370
		SThreadPagedInfo pagedInfo;
sl@0
   371
		test.Printf(_L("Testing gProcessPaged: aPaged = %x\n"), aPaged);
sl@0
   372
		TThreadCreateInfo createInfo(	KGlobalThreadName, ThreadFunc, KDefaultStackSize,
sl@0
   373
										(TAny*)&pagedInfo);
sl@0
   374
		createInfo.SetCreateHeap(KMinHeapSize, KMinHeapSize);
sl@0
   375
		
sl@0
   376
		test_KErrNone(CheckHeapStackPaged(createInfo, EUnspecified, pagedInfo));
sl@0
   377
		test_KErrNone(CheckHeapStackPaged(createInfo, EPaged, pagedInfo));
sl@0
   378
		test_KErrNone(CheckHeapStackPaged(createInfo, EUnpaged, pagedInfo));
sl@0
   379
		}
sl@0
   380
sl@0
   381
sl@0
   382
	test.Next(_L("Test RThread::Create() (Existing Heap)"));
sl@0
   383
		{
sl@0
   384
		SThreadPagedInfo pagedInfo;
sl@0
   385
		TThreadCreateInfo createInfo(	KGlobalThreadName, ThreadFunc, KDefaultStackSize, 
sl@0
   386
										(TAny*)&pagedInfo);
sl@0
   387
		createInfo.SetUseHeap(NULL);
sl@0
   388
		
sl@0
   389
		test_KErrNone(CheckHeapStackPaged(createInfo, EUnspecified, pagedInfo, ETrue));
sl@0
   390
		test_KErrNone(CheckHeapStackPaged(createInfo, EPaged, pagedInfo, ETrue));
sl@0
   391
		test_KErrNone(CheckHeapStackPaged(createInfo, EUnpaged, pagedInfo, ETrue));
sl@0
   392
		}
sl@0
   393
	test.End();
sl@0
   394
	}
sl@0
   395
sl@0
   396
sl@0
   397
sl@0
   398
TInt TestingTThreadCreate()
sl@0
   399
	{
sl@0
   400
	test.Printf(_L("Test TThreadCreateInfo\n"));
sl@0
   401
	TestThreadCreate();
sl@0
   402
sl@0
   403
	return 0;
sl@0
   404
	}