1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/debug/t_schedhook.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,279 @@
1.4 +// Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32test\debug\t_schedhook.cpp
1.18 +// Overview:
1.19 +// Test the device hook functionality.
1.20 +// API Information:
1.21 +// RBusLogicalChannel, DLogicalDevice
1.22 +// Details:
1.23 +// - Load the specified logical driver dll and check the return value
1.24 +// is as expected. Create the logical channel for the current thread
1.25 +// and check the return value as KErrNone.
1.26 +// - Install the scheduler hook and check it is installed successfully.
1.27 +// - Enable scheduler callback, setup test threads, test the scheduler hook
1.28 +// before thread resume and after resume is as expected.
1.29 +// - Perform some context switching and check rescheduler count is as expected.
1.30 +// - Disable scheduler callback, perform some context switching and check
1.31 +// thread rescheduler count is as expected.
1.32 +// - Re-enable scheduler callback, perform some context switching and check
1.33 +// thread rescheduler count is as expected.
1.34 +// - Remove schedule hook and check it is successfully removed. perform some
1.35 +// context switching and check thread rescheduler count is as expected.
1.36 +// - Check exception during context switching, user mode interrupt, WFAR,
1.37 +// exec call are as expected.
1.38 +// - Uninstall scheduler hook and check it is as expected.
1.39 +// Platforms/Drives/Compatibility:
1.40 +// Hardware (Automatic).
1.41 +// Assumptions/Requirement/Pre-requisites:
1.42 +// Failures and causes:
1.43 +// Base Port information:
1.44 +//
1.45 +//
1.46 +
1.47 +#include "context.h"
1.48 +#define __KERNEL_MODE__
1.49 +#include "nk_priv.h"
1.50 +#include <nk_plat.h>
1.51 +#undef __KERNEL_MODE__
1.52 +
1.53 +RTest test(_L("T_SCHEDHOOK"));
1.54 +
1.55 +RSchedhookTest ldd;
1.56 +
1.57 +RThread Thread1;
1.58 +TRequestStatus Request1;
1.59 +
1.60 +
1.61 +
1.62 +RThread Thread2;
1.63 +TRequestStatus Request2;
1.64 +
1.65 +TInt Thread2Main(TAny*)
1.66 + {
1.67 + for(;;)
1.68 + {
1.69 + Request2 = KRequestPending;
1.70 + TRequestStatus* request = &Request1;
1.71 + Thread1.RequestComplete(request,KErrNone);
1.72 + User::WaitForRequest(Request2);
1.73 + }
1.74 + }
1.75 +
1.76 +
1.77 +
1.78 +RThread ThreadException;
1.79 +TArmRegSet ThreadExceptionData;
1.80 +RThread ThreadWFAR;
1.81 +TArmRegSet ThreadWFARData;
1.82 +RThread ThreadUserInt;
1.83 +TArmRegSet ThreadUserIntData;
1.84 +RThread ThreadExecCall;
1.85 +TArmRegSet ThreadExecCallData;
1.86 +
1.87 +
1.88 +
1.89 +TInt GetThreadContext(RThread aThread,TArmRegSet& aContext)
1.90 + {
1.91 + TPtr8 context((TUint8*)&aContext,sizeof(TArmRegSet),sizeof(TArmRegSet));
1.92 + return ldd.GetThreadContext(aThread.Id(),context);
1.93 + }
1.94 +
1.95 +
1.96 +
1.97 +void DumpContext(TArmRegSet& aContext,TInt aType)
1.98 + {
1.99 + test.Printf(_L(" Context type %d\n"),aType);
1.100 + test.Printf(_L(" r0 =%08x r1 =%08x r2 =%08x r3 =%08x\n"),aContext.iR0,aContext.iR1,aContext.iR2,aContext.iR3);
1.101 + test.Printf(_L(" r4 =%08x r5 =%08x r6 =%08x r7 =%08x\n"),aContext.iR4,aContext.iR5,aContext.iR6,aContext.iR7);
1.102 + test.Printf(_L(" r8 =%08x r9 =%08x r10=%08x r11=%08x\n"),aContext.iR8,aContext.iR9,aContext.iR10,aContext.iR11);
1.103 + test.Printf(_L(" r12=%08x r13=%08x r14=%08x r15=%08x\n"),aContext.iR12,aContext.iR13,aContext.iR14,aContext.iR15);
1.104 + test.Printf(_L(" cpsr=%08x"),aContext.iFlags);
1.105 + }
1.106 +
1.107 +
1.108 +
1.109 +void TestContext()
1.110 + {
1.111 + TInt r;
1.112 + TArmRegSet context;
1.113 +
1.114 + test.Start(_L("Insert scheduler hook"));
1.115 + r = ldd.InsertHooks();
1.116 + test(r==KErrNone);
1.117 +
1.118 + test.Next(_L("Enable scheduler callback"));
1.119 + r = ldd.EnableCallback();
1.120 + test(r==KErrNone);
1.121 +
1.122 + test.Next(_L("Test exception context"));
1.123 + r = ThreadException.Create(KNullDesC,ThreadContextHwExc,KDefaultStackSize,&User::Allocator(),&ThreadExceptionData);
1.124 + test(r==KErrNone);
1.125 + ThreadException.SetPriority(EPriorityMore);
1.126 + r = ldd.SetTestThread(ThreadException.Id()); // So ldd handles the exception for this thread
1.127 + test(r==KErrNone);
1.128 + ThreadException.Resume();
1.129 + User::After(250000); // Let thread run
1.130 + r = GetThreadContext(ThreadException,context);
1.131 + DumpContext(context,r);
1.132 + test(r==NThread::EContextException);
1.133 + test(CheckContextHwExc(&context,&ThreadExceptionData));
1.134 +
1.135 + test.Next(_L("Test user mode interupt context"));
1.136 + r = ThreadUserInt.Create(KNullDesC,ThreadContextUserInt,KDefaultStackSize,&User::Allocator(),&ThreadUserIntData);
1.137 + test(r==KErrNone);
1.138 + ThreadUserInt.SetPriority(EPriorityLess);
1.139 + ThreadUserInt.Resume();
1.140 + User::After(250000); // Let thread run
1.141 + r = GetThreadContext(ThreadUserInt,context);
1.142 + DumpContext(context,r);
1.143 + test(r==NThread::EContextUserInterrupt);
1.144 + test(CheckContextUserInt(&context,&ThreadUserIntData));
1.145 +
1.146 + test.Next(_L("Test WFAR context"));
1.147 + r = ThreadWFAR.Create(KNullDesC,ThreadContextWFAR,KDefaultStackSize,&User::Allocator(),&ThreadWFARData);
1.148 + test(r==KErrNone);
1.149 + ThreadWFAR.SetPriority(EPriorityMore);
1.150 + ThreadWFAR.Resume();
1.151 + User::After(250000); // Let thread run
1.152 + r = GetThreadContext(ThreadWFAR,context);
1.153 + DumpContext(context,r);
1.154 + test(r==NThread::EContextWFAR);
1.155 + test(CheckContextWFAR(&context,&ThreadWFARData));
1.156 +
1.157 + test.Next(_L("Test exec call context"));
1.158 + r = ThreadExecCall.Create(KNullDesC,ThreadContextExecCall,KDefaultStackSize,&User::Allocator(),&ThreadExecCallData);
1.159 + test(r==KErrNone);
1.160 + ThreadExecCall.SetPriority(EPriorityMore);
1.161 + ThreadExecCall.Resume();
1.162 + User::After(250000); // Let thread run
1.163 + r = GetThreadContext(ThreadExecCall,context);
1.164 + DumpContext(context,r);
1.165 + test(r==NThread::EContextExec);
1.166 + test(CheckContextExecCall(&context,&ThreadExecCallData));
1.167 +
1.168 + test.End();
1.169 + }
1.170 +
1.171 +
1.172 +TInt DoContextSwitches(TInt aCount)
1.173 + {
1.174 + TInt r = ldd.SetTestThread(Thread2.Id()); // Zero test count
1.175 + test(r==KErrNone);
1.176 + while(aCount)
1.177 + {
1.178 + Request1 = KRequestPending;
1.179 + TRequestStatus* request = &Request2;
1.180 + Thread2.RequestComplete(request,KErrNone);
1.181 + User::WaitForRequest(Request1);
1.182 + --aCount;
1.183 + }
1.184 + r = ldd.GetTestCount();
1.185 + test(r>=0);
1.186 + return r;
1.187 + }
1.188 +
1.189 +
1.190 +
1.191 +GLDEF_C TInt E32Main()
1.192 + {
1.193 + test.Title();
1.194 + TInt r;
1.195 +
1.196 + test.Start(_L("Loading LDD"));
1.197 + r = User::LoadLogicalDevice(_L("D_SCHEDHOOK"));
1.198 + test(r==KErrNone || r==KErrAlreadyExists);
1.199 +
1.200 + test.Next(_L("Open channel to LDD"));
1.201 + r = ldd.Open();
1.202 + test(r==KErrNone);
1.203 +
1.204 + test.Next(_L("Installing scheduler hooks"));
1.205 + r = ldd.Install();
1.206 + if (r==KErrNotSupported)
1.207 + {
1.208 + test.Next(_L("Scheduler hooks not supported on this platform, skipping test"));
1.209 + ldd.Close();
1.210 + test.End();
1.211 + return KErrNone;
1.212 + }
1.213 + test(r==KErrNone);
1.214 +
1.215 + test.Next(_L("Enable scheduler callback"));
1.216 + r = ldd.EnableCallback();
1.217 + test(r==KErrNone);
1.218 +
1.219 + test.Next(_L("Setting up test thread"));
1.220 + r= Thread1.Open(RThread().Id());
1.221 + test(r==KErrNone);
1.222 + r = Thread2.Create(KNullDesC,Thread2Main,KDefaultStackSize,&User::Allocator(),NULL);
1.223 + test(r==KErrNone);
1.224 + r = ldd.SetTestThread(Thread2.Id());
1.225 + test(r==KErrNone);
1.226 +
1.227 + test.Next(_L("Test scheduler hook (wait)"));
1.228 + User::After(1000000); // 1 second
1.229 + TInt count = ldd.GetTestCount();
1.230 + test.Printf(_L("count=%d\n"),count);
1.231 + test(count==0);
1.232 +
1.233 + test.Next(_L("Test scheduler hook (resume)"));
1.234 + Request1 = KRequestPending;
1.235 + Thread2.Resume();
1.236 + User::WaitForRequest(Request1);
1.237 + count = ldd.GetTestCount();
1.238 + test.Printf(_L("count=%d\n"),count);
1.239 + test(count>0);
1.240 +
1.241 + test.Next(_L("Test scheduler hook (context switching)"));
1.242 + count = DoContextSwitches(1000);
1.243 + test.Printf(_L("count=%d\n"),count);
1.244 + test(count>=1000);
1.245 +
1.246 + test.Next(_L("Disable scheduler callback"));
1.247 + r = ldd.DisableCallback();
1.248 + test(r==KErrNone);
1.249 + count = DoContextSwitches(1000);
1.250 + test.Printf(_L("count=%d\n"),count);
1.251 + test(count==0);
1.252 +
1.253 + test.Next(_L("Re-enable scheduler callback"));
1.254 + r = ldd.EnableCallback();
1.255 + test(r==KErrNone);
1.256 + count = DoContextSwitches(1000);
1.257 + test.Printf(_L("count=%d\n"),count);
1.258 + test(count>=1000);
1.259 +
1.260 + test.Next(_L("Removing scheduler hook"));
1.261 + r = ldd.RemoveHooks();
1.262 + test(r==KErrNone);
1.263 + count=DoContextSwitches(1000);
1.264 + r = ldd.GetTestCount();
1.265 + test.Printf(_L("count=%d\n"),r);
1.266 + test(r==0);
1.267 +
1.268 + test.Next(_L("Test thread context"));
1.269 + TestContext();
1.270 +
1.271 + test.Next(_L("Uninstalling scheduler hook"));
1.272 + r = ldd.Uninstall();
1.273 + test(r==KErrNone);
1.274 +
1.275 + test.Next(_L("Closing ldd"));
1.276 + ldd.Close();
1.277 +
1.278 + test.End();
1.279 +
1.280 + return(0);
1.281 + }
1.282 +