author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 17 Sep 2010 08:37:04 +0300 | |
changeset 266 | 0008ccd16016 |
parent 201 | 43365a9b78a3 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). |
2 |
// All rights reserved. |
|
3 |
// This component and the accompanying materials are made available |
|
4 |
// under the terms of the License "Eclipse Public License v1.0" |
|
5 |
// which accompanies this distribution, and is available |
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 |
// |
|
8 |
// Initial Contributors: |
|
9 |
// Nokia Corporation - initial contribution. |
|
10 |
// |
|
11 |
// Contributors: |
|
12 |
// |
|
13 |
// Description: |
|
14 |
// e32\memmodel\epoc\mmubase\defragbase.cpp |
|
15 |
// |
|
16 |
// |
|
17 |
||
18 |
/** |
|
19 |
@file |
|
20 |
@internalComponent |
|
21 |
*/ |
|
22 |
#include <kernel/kern_priv.h> |
|
23 |
#include <defrag.h> |
|
24 |
#include <ramalloc.h> |
|
25 |
||
26 |
#ifndef __MEMMODEL_FLEXIBLE__ |
|
27 |
#include <mmubase.inl> |
|
28 |
#else |
|
29 |
#include "mdefrag.inl" |
|
30 |
#endif //__MEMMODEL_FLEXIBLE__ |
|
31 |
||
32 |
// Maximum number of times to attempt to defrag a particular zone. |
|
33 |
const TUint KDefragMaxRetries = 5; |
|
34 |
||
35 |
const TInt KDefragIdlingThreadPriority = 27; |
|
36 |
||
37 |
Defrag* Defrag::TheDefrag = NULL; |
|
38 |
||
39 |
_LIT(KDefragDfcThreadName, "DefragDFC"); |
|
40 |
||
41 |
void Defrag::Panic(TPanic aPanic) |
|
42 |
{ |
|
43 |
Kern::Fault("DEFRAG",aPanic); |
|
44 |
} |
|
45 |
||
46 |
||
47 |
Defrag::Defrag() |
|
48 |
{ |
|
49 |
} |
|
50 |
||
51 |
||
52 |
void Defrag::Init3(DRamAllocator* aRamAllocator) |
|
53 |
{ |
|
54 |
TheDefrag = this; |
|
55 |
||
56 |
TInt r = Kern::DfcQInit(&iTaskQ, KDefragIdlingThreadPriority, &KDefragDfcThreadName); |
|
57 |
if (r!=KErrNone) |
|
58 |
Panic(EDfcQInitFailed); |
|
59 |
||
60 |
iRamAllocator = aRamAllocator; |
|
61 |
} |
|
62 |
||
63 |
||
64 |
/** |
|
65 |
Move the movable pages in this zone into higher priority zones. |
|
66 |
||
67 |
@param aZone The zone to clear movable pages from |
|
68 |
@param aBestEffort Set to ETrue to always keep clearing pages even if all can't |
|
69 |
be moved or other threads allocate into the zone. |
|
70 |
@param aRequest The request object containing the defrag parameters. |
|
71 |
@pre RamAlloc mutex held. |
|
72 |
@post RamAlloc mutex held. |
|
73 |
*/ |
|
74 |
TInt Defrag::ClearMovableFromZone(SZone& aZone, TBool aBestEffort, TRamDefragRequest* aRequest) |
|
75 |
{ |
|
76 |
__KTRACE_OPT(KMMU, Kern::Printf("ClearMovableFromZone: ID %x ", aZone.iId)); |
|
77 |
||
78 |
TUint offset = 0; |
|
79 |
TPhysAddr newAddr; |
|
80 |
TBool zoneActive = EFalse; |
|
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
81 |
const TUint moveDisFlags = (aBestEffort)? 0 : M::EMoveDisBlockRest | M::EMoveDisMoveDirty; |
0 | 82 |
TInt ret = iRamAllocator->NextAllocatedPage(&aZone, offset, EPageMovable); |
83 |
||
84 |
// if not best effort mode keep moving pages unless someone allocates into |
|
85 |
// the zone or an unmovable page is hit. |
|
86 |
// If in best effort mode then attempt to move all allocated pages in |
|
87 |
// the zone regardless. |
|
88 |
while ( aZone.iAllocPages[EPageMovable] != 0 && ret == KErrNone) |
|
89 |
{ |
|
90 |
if (aRequest->PollForCancel()) |
|
91 |
{ |
|
92 |
__KTRACE_OPT(KMMU, Kern::Printf("ClearMovableFromZone: cancelled")); |
|
93 |
return KErrCancel; |
|
94 |
} |
|
95 |
||
96 |
TPhysAddr addr = (offset << M::PageShift()) + aZone.iPhysBase; |
|
97 |
||
98 |
if (!aBestEffort && |
|
99 |
(aZone.iAllocPages[EPageMovable] > iRamAllocator->GenDefragFreePages(EPageMovable) || |
|
100 |
zoneActive)) |
|
101 |
{ |
|
102 |
__KTRACE_OPT(KMMU, Kern::Printf("ClearMovableFromZone: memory too low or zone active addr %x", addr)); |
|
103 |
return KErrNoMemory; |
|
104 |
} |
|
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
105 |
TInt moved = M::MovePage(addr, newAddr, aZone.iId, moveDisFlags); |
0 | 106 |
if (moved != KErrNone) |
107 |
{// Couldn't move the page so stop as we can't clear the zone |
|
108 |
if (!aBestEffort) |
|
109 |
{ |
|
110 |
__KTRACE_OPT(KMMU, Kern::Printf("ClearMovableFromZone exit: move fail zone %x addr %x", aZone.iId, addr)); |
|
111 |
return moved; |
|
112 |
} |
|
113 |
} |
|
114 |
// Flash RAM alloc mutex to allow other allocations |
|
115 |
iRamAllocator->ZoneMark(aZone); |
|
116 |
M::RamAllocUnlock(); |
|
117 |
M::RamAllocLock(); |
|
118 |
zoneActive = !iRamAllocator->ZoneUnmark(aZone); |
|
119 |
offset++; |
|
120 |
ret = iRamAllocator->NextAllocatedPage(&aZone, offset, EPageMovable); |
|
121 |
} |
|
122 |
__KTRACE_OPT(KMMU, Kern::Printf("ClearMovableFromZone: ret %d off %x", ret, offset)); |
|
123 |
return KErrNone; |
|
124 |
} |
|
125 |
||
126 |
||
127 |
/** |
|
128 |
Discard as many RAM cache pages from the specified zone as possible in one pass. |
|
129 |
The method will return when no more pages could be discarded. |
|
130 |
||
131 |
@param aZone The zone to clear of cache pages. |
|
132 |
@param aBestEffort Set to ETrue continue clearing the zone even if more allocations |
|
133 |
are made into the zone or if it isn't possible to clear all the |
|
134 |
cache pages from the zone. Otherwise set to EFalse. |
|
135 |
@param aRequest The request object containing the defrag parameters. |
|
136 |
@param aMaxDiscard A pointer to the maximum number of discardable pages to discard. |
|
137 |
Set to NULL if there is no maximum. |
|
138 |
@pre RamAlloc mutex held. |
|
139 |
@post RamAlloc mutex held. |
|
140 |
*/ |
|
141 |
TInt Defrag::ClearDiscardableFromZone(SZone& aZone, TBool aBestEffort, TRamDefragRequest* aRequest, TUint* aMaxDiscard) |
|
142 |
{ |
|
143 |
__KTRACE_OPT(KMMU, Kern::Printf("ClearDiscardableFromZone: ID %x ", aZone.iId)); |
|
144 |
||
145 |
TUint offset = 0; |
|
146 |
TBool zoneActive = EFalse; |
|
147 |
TInt ret = iRamAllocator->NextAllocatedPage(&aZone, offset, EPageDiscard); |
|
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
148 |
const TUint moveDisFlags = (aBestEffort)? 0 : M::EMoveDisBlockRest | M::EMoveDisMoveDirty; |
0 | 149 |
|
150 |
||
151 |
while ( aZone.iAllocPages[EPageDiscard] != 0 && ret == KErrNone && |
|
152 |
(!aMaxDiscard || *aMaxDiscard)) |
|
153 |
{ |
|
154 |
if (aRequest->PollForCancel()) |
|
155 |
{ |
|
156 |
__KTRACE_OPT(KMMU, Kern::Printf("ClearDiscardableFromZone: cancelled")); |
|
157 |
return KErrCancel; |
|
158 |
} |
|
159 |
||
160 |
TPhysAddr addr = aZone.iPhysBase + (offset << M::PageShift()); |
|
161 |
||
162 |
// When running is best effort mode keep clearing pages whatever. If not in |
|
163 |
// best effort stop the defrag if can't remove all the discardable pages |
|
164 |
// without reducing the cache beyond its minimum size or someone is |
|
165 |
// allocating into this zone or if it is active. |
|
166 |
if (!aBestEffort && |
|
167 |
(iRamAllocator->GenDefragFreePages(EPageDiscard) + M::NumberOfFreeDpPages() < aZone.iAllocPages[EPageDiscard] || |
|
168 |
zoneActive)) |
|
169 |
{ |
|
170 |
__KTRACE_OPT(KMMU, Kern::Printf("ClearDiscardableFromZone: memory too low or zone active addr %x", addr)); |
|
171 |
return KErrNoMemory; |
|
172 |
} |
|
173 |
||
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
174 |
TInt discardRet = M::DiscardPage(addr, aZone.iId, moveDisFlags); |
0 | 175 |
if (discardRet == KErrNone) |
176 |
{// Page was discarded successfully. |
|
177 |
if (aMaxDiscard) |
|
178 |
(*aMaxDiscard)--; |
|
179 |
} |
|
180 |
else |
|
181 |
{ |
|
182 |
if (!aBestEffort) |
|
183 |
{// Page couldn't be discarded and this is a general defrag so stop. |
|
184 |
__KTRACE_OPT(KMMU, Kern::Printf("ClearDiscardableFromZone: page discard fail addr %x r %d", addr, discardRet)); |
|
185 |
return discardRet; |
|
186 |
} |
|
187 |
} |
|
188 |
||
189 |
// Give someone else ago on the RAM alloc mutex. |
|
190 |
iRamAllocator->ZoneMark(aZone); |
|
191 |
M::RamAllocUnlock(); |
|
192 |
M::RamAllocLock(); |
|
193 |
zoneActive = !iRamAllocator->ZoneUnmark(aZone); |
|
194 |
offset++; |
|
195 |
ret = iRamAllocator->NextAllocatedPage(&aZone, offset, EPageDiscard); |
|
196 |
} |
|
197 |
__KTRACE_OPT(KMMU, Kern::Printf("ClearDiscardableFromZone: ret %d off %x", ret, offset)); |
|
198 |
return KErrNone; |
|
199 |
} |
|
200 |
||
201 |
||
202 |
/** |
|
203 |
Attempt to remove as many pages as possible from the zone. |
|
204 |
||
205 |
If there are not enough free zones to move pages to or there are fixed pages in the zone it |
|
206 |
will not be possible to completely clear the zone, in this case this method will return |
|
207 |
KErrNoMemory. |
|
208 |
||
209 |
@param aZone The zone to be cleared |
|
210 |
@param aMaxRetries The maximum number of passes to run through the zone |
|
211 |
@param aRequest The request object containing the defrag parameters. |
|
212 |
@return KErrNone if zone cleared, KErrNoMemory if some pages still allocated in |
|
213 |
the zone (see above), KErrCancel if the defrag operation was cancelled. |
|
214 |
||
215 |
@pre RamAlloc mutex held. |
|
216 |
@post RamAlloc mutex held. |
|
217 |
*/ |
|
218 |
TInt Defrag::ClearZone(SZone& aZone, TUint aMaxRetries, TRamDefragRequest* aRequest) |
|
219 |
{ |
|
220 |
__KTRACE_OPT(KMMU, Kern::Printf("ClearZone ID%x retry %d", aZone.iId, aMaxRetries)); |
|
221 |
||
222 |
// Attempt to clear all pages from the zone. |
|
223 |
// Keep retrying until no more progress is being made or the retry limit |
|
224 |
// has been reached |
|
225 |
TUint retryCount = 0; |
|
226 |
for (; aZone.iPhysPages != aZone.iFreePages && |
|
227 |
retryCount < aMaxRetries; |
|
228 |
retryCount++) |
|
229 |
{ |
|
230 |
TUint prevFreePages = aZone.iFreePages; |
|
231 |
||
232 |
// Discard all discardable pages in the zone |
|
233 |
if (ClearDiscardableFromZone(aZone, ETrue, aRequest) == KErrCancel) |
|
234 |
{// Defrag has been cancelled |
|
235 |
return KErrCancel; |
|
236 |
} |
|
237 |
||
238 |
// Remove all the movable pages in the zone |
|
239 |
if (ClearMovableFromZone(aZone, ETrue, aRequest) == KErrCancel) |
|
240 |
{// Defrag has been cancelled |
|
241 |
return KErrCancel; |
|
242 |
} |
|
243 |
||
244 |
if (prevFreePages >= aZone.iFreePages) |
|
245 |
{// i.e. the number of free pages didn't increase so give up |
|
246 |
break; |
|
247 |
} |
|
248 |
} |
|
249 |
if (aZone.iPhysPages != aZone.iFreePages) |
|
250 |
{// Zone couldn't be completely cleared |
|
251 |
return KErrNoMemory; |
|
252 |
} |
|
253 |
return KErrNone; |
|
254 |
} |
|
255 |
||
256 |
||
257 |
/** |
|
258 |
Perform a general defragmentation of the RAM zones. Attempt to clear as many |
|
259 |
of the lowest preference zones of allocated pages as possible while still respecting |
|
260 |
the thresholds and still allowing other allocations to occur. |
|
261 |
||
262 |
@param aRequest A TRamDefragRequest object with the iMaxPages member set to |
|
263 |
the maximum number of pages to clear during the defrag operation. |
|
264 |
||
265 |
@return KErrNone when done, KErrCancel when the defrag was cancelled. |
|
266 |
*/ |
|
267 |
TInt Defrag::GeneralDefrag(TRamDefragRequest* aRequest) |
|
268 |
{ |
|
269 |
TInt ret = KErrNone; |
|
270 |
TUint defragCount = 0; |
|
271 |
TUint stage = EGenDefragStage1; |
|
272 |
||
273 |
// Acquire RAM alloc mutex |
|
274 |
M::RamAllocLock(); |
|
275 |
||
276 |
// Determine which stage the general defrag should begin at. |
|
277 |
TUint requiredToDiscard = 0; |
|
278 |
SZone* zone = iRamAllocator->GeneralDefragStart0((TGenDefragStage&)stage, requiredToDiscard); |
|
279 |
||
280 |
// First stage is to clear any discardable pages that are required for |
|
281 |
// movable pages to be allocated into. |
|
282 |
if (aRequest->iMaxPages && aRequest->iMaxPages < requiredToDiscard) |
|
283 |
{// Can't discard the required amount of pages without hitting the |
|
284 |
// max pages limit so no point continuing. |
|
285 |
__KTRACE_OPT(KMMU, Kern::Printf("GeneralDefrag exit - zone %x max %x requiredToDiscard %x", |
|
286 |
zone->iId, aRequest->iMaxPages, requiredToDiscard)); |
|
287 |
goto exit; |
|
288 |
} |
|
289 |
while (zone != NULL && requiredToDiscard) |
|
290 |
{ |
|
291 |
TUint prevRequired = requiredToDiscard; |
|
292 |
if (ClearDiscardableFromZone(*zone, EFalse, aRequest, &requiredToDiscard) == KErrCancel) |
|
293 |
{// Defrag cancelled |
|
294 |
ret = KErrCancel; |
|
295 |
goto exit; |
|
296 |
} |
|
297 |
defragCount += prevRequired - requiredToDiscard; |
|
298 |
zone = iRamAllocator->GeneralDefragNextZone0(); |
|
299 |
} |
|
300 |
for (; stage < EGenDefragStageEnd; stage++) |
|
301 |
{ |
|
302 |
||
303 |
SZone* zone = NULL; |
|
304 |
// Initialise the allocator for the current general defrag stage. |
|
305 |
if (stage == EGenDefragStage1) |
|
306 |
zone = iRamAllocator->GeneralDefragStart1(); |
|
307 |
else |
|
308 |
zone = iRamAllocator->GeneralDefragStart2(); |
|
309 |
||
310 |
while (zone != NULL) |
|
311 |
{ |
|
312 |
if (zone->iAllocPages[EPageMovable] > iRamAllocator->GenDefragFreePages(EPageMovable) || |
|
313 |
(aRequest->iMaxPages && |
|
314 |
zone->iAllocPages[EPageMovable] + zone->iAllocPages[EPageDiscard] > aRequest->iMaxPages - defragCount)) |
|
315 |
{// Not enough free pages in the more preferable RAM zone(s) or would hit the iMaxPages limit. |
|
316 |
__KTRACE_OPT(KMMU, Kern::Printf("GeneralDefrag exit - zone %x max %x defrag %x", |
|
317 |
zone->iId, aRequest->iMaxPages, |
|
318 |
zone->iAllocPages[EPageMovable] + zone->iAllocPages[EPageDiscard] + defragCount)); |
|
319 |
break; |
|
320 |
} |
|
321 |
||
322 |
// Discard all discardable pages in the zone. |
|
323 |
defragCount += zone->iAllocPages[EPageDiscard]; |
|
324 |
if (ClearDiscardableFromZone(*zone, EFalse, aRequest) == KErrCancel) |
|
325 |
{// Defrag cancelled |
|
326 |
ret = KErrCancel; |
|
327 |
goto exit; |
|
328 |
} |
|
329 |
if (zone->iAllocPages[EPageDiscard]) |
|
330 |
{// Couldn't discard all the discardable pages so no point continuing. |
|
331 |
__KTRACE_OPT(KMMU, Kern::Printf("GeneralDefrag exit - zone%x Discardable %x", zone->iId, zone->iAllocPages[EPageDiscard])); |
|
332 |
break; |
|
333 |
} |
|
334 |
||
335 |
// Should only have movable pages left in the zone now so shift them |
|
336 |
// to the higher preference zones. |
|
337 |
defragCount += zone->iAllocPages[EPageMovable]; |
|
338 |
if (ClearMovableFromZone(*zone, EFalse, aRequest) == KErrCancel) |
|
339 |
{// Defrag cancelled |
|
340 |
ret = KErrCancel; |
|
341 |
goto exit; |
|
342 |
} |
|
343 |
if (zone->iAllocPages[EPageMovable]) |
|
344 |
{// Couldn't move all the movable pages so no point continuing. |
|
345 |
__KTRACE_OPT(KMMU, Kern::Printf("GeneralDefrag exit - zone%x Movable %x", zone->iId, zone->iAllocPages[EPageMovable])); |
|
346 |
break; |
|
347 |
} |
|
348 |
// Get the next RAM zone to be defraged. |
|
349 |
if (stage == EGenDefragStage1) |
|
350 |
zone = iRamAllocator->GeneralDefragNextZone1(); |
|
351 |
else |
|
352 |
zone = iRamAllocator->GeneralDefragNextZone2(); |
|
353 |
} |
|
354 |
} |
|
355 |
exit: |
|
356 |
iRamAllocator->GeneralDefragEnd(); |
|
357 |
M::RamAllocUnlock(); |
|
358 |
return ret; |
|
359 |
} |
|
360 |
||
361 |
||
362 |
/** |
|
363 |
Claim a RAM zone by removing all the pages from it and then allocating it as fixed. |
|
364 |
||
365 |
This method may return the following error codes: |
|
366 |
- KErrCancel: The call was cancelled, |
|
367 |
- KErrArgument: The specified zone could not be found, |
|
368 |
- KErrNoMemory: ClaimRamZone failed; there may be no free zones to move pages to, there may be fixed pages in the zone which can not be moved. |
|
369 |
||
370 |
@param aRequest A TRamDefragRequest object with the iId member set to the ID of |
|
371 |
the zone to empty. On success the iPhysAddr member will contain the physical |
|
372 |
base address of the zone that has been claimed. |
|
373 |
||
374 |
@return KErrNone if successful or one of the errors described above. |
|
375 |
*/ |
|
376 |
TInt Defrag::ClaimRamZone(TRamDefragRequest* aRequest) |
|
377 |
{ |
|
378 |
TInt ret = KErrNoMemory; |
|
379 |
||
380 |
// Acquire RAM alloc mutex |
|
381 |
M::RamAllocLock(); |
|
382 |
||
383 |
SZone* zone = iRamAllocator->ZoneFromId(aRequest->iId); |
|
384 |
if (zone == NULL) |
|
385 |
{// can't find zone |
|
386 |
M::RamAllocUnlock(); |
|
387 |
__KTRACE_OPT(KMMU, Kern::Printf("ClaimZone exit - no zone %x", aRequest->iId)); |
|
388 |
return KErrArgument; |
|
389 |
} |
|
390 |
||
391 |
// Mark the zone as restricted for future allocations |
|
392 |
iRamAllocator->ZoneClaimStart(*zone); |
|
393 |
||
394 |
if (zone->iAllocPages[EPageUnknown] != 0) |
|
395 |
{// Can't ever empty this zone so stop |
|
396 |
__KTRACE_OPT(KMMU, Kern::Printf("ClaimZone exit - zone%x unk%x", zone->iId, zone->iAllocPages[EPageUnknown])); |
|
397 |
goto exit; |
|
398 |
} |
|
399 |
||
400 |
// Attempt to clear all pages from the zone. |
|
401 |
ret = ClearZone(*zone, KDefragMaxRetries, aRequest); |
|
402 |
||
403 |
if (ret == KErrNone) |
|
404 |
{// The zone is empty so claim it |
|
405 |
__KTRACE_OPT(KMMU, Kern::Printf("ClaimZone success - zone%x", zone->iId)); |
|
406 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
407 |
BTrace4(BTrace::ERamAllocator, BTrace::ERamAllocClaimZone, zone->iId); |
|
408 |
#endif |
|
409 |
||
102
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
410 |
#if defined(BTRACE_KERNEL_MEMORY) && !defined(__MEMMODEL_FLEXIBLE__) |
0 | 411 |
TUint size = zone->iPhysPages << M::PageShift(); |
412 |
BTrace8(BTrace::EKernelMemory, BTrace::EKernelMemoryDrvPhysAlloc, size, zone->iPhysBase); |
|
413 |
Epoc::DriverAllocdPhysRam += size; |
|
414 |
#endif |
|
415 |
||
416 |
iRamAllocator->MarkPagesAllocated(zone->iPhysBase, zone->iPhysPages, EPageFixed); |
|
417 |
*(aRequest->iPhysAddr) = zone->iPhysBase; |
|
418 |
ret = KErrNone; |
|
419 |
M::RamZoneClaimed(zone); |
|
420 |
} |
|
421 |
exit: |
|
422 |
// Release RAM alloc mutex and allow the zone to be allocated into |
|
423 |
iRamAllocator->ZoneClaimEnd(*zone); |
|
424 |
M::RamAllocUnlock(); |
|
425 |
return ret; |
|
426 |
} |
|
427 |
||
428 |
||
429 |
/** |
|
430 |
Empty a RAM zone by removing as many pages as possible from it. |
|
431 |
||
432 |
This method may return the following errors: |
|
433 |
- KErrCancel: The defrag was cancelled, |
|
434 |
- KErrArgument: The specified zone couldn't be found, |
|
435 |
- KErrNoMemory: The zone could not be completely cleared, there may be not enough free zones to move pages to, there may be are fixed pages in the zone. |
|
436 |
||
437 |
@param aRequest A TRamDefragRequest object with the iId member set to the ID |
|
438 |
of the zone to empty. |
|
439 |
||
440 |
@return KErrNone: Zone emptied or one of the above error codes. |
|
441 |
*/ |
|
442 |
TInt Defrag::EmptyRamZone(TRamDefragRequest* aRequest) |
|
443 |
{ |
|
444 |
TInt ret = KErrNone; |
|
445 |
||
446 |
// Acquire RAM alloc mutex |
|
447 |
M::RamAllocLock(); |
|
448 |
||
449 |
SZone* zone = iRamAllocator->ZoneFromId(aRequest->iId); |
|
450 |
if (zone == NULL) |
|
451 |
{// can't find zone |
|
452 |
ret = KErrArgument; |
|
453 |
goto exit; |
|
454 |
} |
|
455 |
||
456 |
// Attempt to clear all the pages from the zone |
|
457 |
ret = ClearZone(*zone, KDefragMaxRetries, aRequest); |
|
458 |
||
459 |
exit: |
|
460 |
// Release RAM alloc mutex |
|
461 |
M::RamAllocUnlock(); |
|
462 |
return ret; |
|
463 |
} |
|
464 |
||
465 |
||
466 |
void Defrag::DefragTask(TAny* aArg) |
|
467 |
{ |
|
468 |
Defrag& d = *Defrag::TheDefrag; |
|
469 |
TRamDefragRequest* task = (TRamDefragRequest*)aArg; |
|
470 |
||
471 |
TInt r = Kern::SetThreadPriority(task->iThreadPriority, NULL); |
|
472 |
if (r!=KErrNone) |
|
473 |
{ |
|
474 |
task->Complete(r); |
|
475 |
return; |
|
476 |
} |
|
477 |
||
478 |
d.iDefragPriority = task->iThreadPriority; |
|
479 |
||
480 |
||
481 |
if (task->PollForCancel()) |
|
482 |
{ |
|
483 |
__KTRACE_OPT(KMMU, Kern::Printf("DefragTask: cancelled")); |
|
484 |
r = KErrCancel; |
|
485 |
goto exit; |
|
486 |
} |
|
487 |
||
488 |
switch (task->iOp) |
|
489 |
{ |
|
490 |
case Epoc::ERamDefrag_DefragRam: |
|
491 |
r = d.GeneralDefrag(task); |
|
492 |
break; |
|
493 |
case Epoc::ERamDefrag_EmptyRamZone: |
|
494 |
r = d.EmptyRamZone(task); |
|
495 |
break; |
|
496 |
case Epoc::ERamDefrag_ClaimRamZone: |
|
497 |
r = d.ClaimRamZone(task); |
|
498 |
break; |
|
499 |
default: |
|
500 |
r = KErrNotSupported; |
|
501 |
break; |
|
502 |
} |
|
503 |
||
504 |
exit: |
|
505 |
task->Complete(r); |
|
506 |
Kern::SetThreadPriority(KDefragIdlingThreadPriority, NULL); |
|
507 |
} |
|
508 |
||
509 |
/** |
|
510 |
Constructor for TRamDefragRequest. |
|
511 |
||
512 |
@publishedPartner |
|
513 |
@released |
|
514 |
*/ |
|
515 |
EXPORT_C TRamDefragRequest::TRamDefragRequest() |
|
516 |
: TAsyncRequest(Defrag::DefragTask, &Defrag::TheDefrag->iTaskQ, 0) |
|
517 |
{ |
|
518 |
} |
|
519 |
||
520 |
||
521 |
/** |
|
522 |
Performs a general defragmentation of RAM. Attempts to free/move as many |
|
523 |
pages from the lowest preference RAM zones as possible. |
|
524 |
||
525 |
@param aPriority The thread priority for the defragmentation. |
|
526 |
TRamDefragRequest::KInheritPriority to use the priority of the caller. |
|
527 |
@param aMaxPages The maximum number of pages to move or discard during defragmentation. |
|
528 |
Zero implies no limit. |
|
529 |
||
530 |
@return KErrNone if successful, or KErrArgument if the parameters given are invalid. |
|
531 |
||
532 |
@publishedPartner |
|
533 |
@released |
|
534 |
*/ |
|
535 |
EXPORT_C TInt TRamDefragRequest::DefragRam(TInt aPriority, TInt aMaxPages) |
|
536 |
{ |
|
537 |
if (aMaxPages < 0 || aPriority < -1 || aPriority >= KNumPriorities) |
|
538 |
return KErrArgument; |
|
539 |
||
540 |
iOp = Epoc::ERamDefrag_DefragRam; |
|
541 |
iMaxPages = aMaxPages; |
|
542 |
SetupPriority(aPriority); |
|
543 |
return SendReceive(); |
|
544 |
} |
|
545 |
||
546 |
||
547 |
/** |
|
548 |
Performs a general defragmentation of RAM. Attempts to free/move as many |
|
549 |
pages from the lowest preference RAM zones as possible. |
|
550 |
The function returns immediately. |
|
551 |
When the operation is complete (or cancelled), aSem is signalled. |
|
552 |
||
553 |
@param aSem The fast semaphore to signal on completion of the operation. |
|
554 |
@param aPriority The thread priority for the defragmentation. |
|
555 |
TRamDefragRequest::KInheritPriority to use the priority of the caller. |
|
556 |
@param aMaxPages The maximum number of pages to move or discard during defragmentation. |
|
557 |
Zero implies no limit. |
|
558 |
||
559 |
@return KErrNone if successful, or a system-wide error code. |
|
560 |
||
561 |
@publishedPartner |
|
562 |
@released |
|
563 |
*/ |
|
564 |
EXPORT_C TInt TRamDefragRequest::DefragRam(NFastSemaphore* aSem, TInt aPriority, TInt aMaxPages) |
|
565 |
{ |
|
566 |
if (aMaxPages < 0 || !aSem || aPriority < -1 || aPriority >= KNumPriorities) |
|
567 |
return KErrArgument; |
|
568 |
||
569 |
iOp = Epoc::ERamDefrag_DefragRam; |
|
570 |
iMaxPages = aMaxPages; |
|
571 |
SetupPriority(aPriority); |
|
572 |
Send(aSem); |
|
573 |
return KErrNone; |
|
574 |
} |
|
575 |
||
576 |
||
577 |
/** |
|
578 |
Performs a general defragmentation of RAM. Attempts to free or move as many |
|
579 |
pages from the lowest preference RAM zones as possible. |
|
580 |
The function returns immediately. |
|
581 |
When the operation is complete (or cancelled), aDfc is enqueued. |
|
582 |
||
583 |
@param aDfc The DFC to enqueue on completion of the operation. |
|
584 |
@param aPriority The thread priority for the defragmentation. |
|
585 |
TRamDefragRequest::KInheritPriority to use the priority of the caller. |
|
586 |
@param aMaxPages The maximum number of pages to move or discard during defragmentation. |
|
587 |
Zero implies no limit. |
|
588 |
||
589 |
@return KErrNone if successful, or a system-wide error code. |
|
590 |
||
591 |
@see TDfc |
|
592 |
||
593 |
@publishedPartner |
|
594 |
@released |
|
595 |
*/ |
|
596 |
EXPORT_C TInt TRamDefragRequest::DefragRam(TDfc* aDfc, TInt aPriority, TInt aMaxPages) |
|
597 |
{ |
|
598 |
if (aMaxPages < 0 || !aDfc || aPriority < -1 || aPriority >= KNumPriorities) |
|
599 |
return KErrArgument; |
|
600 |
||
601 |
iOp = Epoc::ERamDefrag_DefragRam; |
|
602 |
iMaxPages = aMaxPages; |
|
603 |
SetupPriority(aPriority); |
|
604 |
Send(aDfc); |
|
605 |
return KErrNone; |
|
606 |
} |
|
607 |
||
608 |
||
609 |
/** |
|
610 |
Removes as many pages from the specified RAM zone as possible. |
|
611 |
||
612 |
This method may return the following errors: |
|
613 |
- KErrCancel: The defrag was cancelled, |
|
614 |
- KErrArgument: The specified zone couldn't be found, or the parameters are invalid, |
|
615 |
- KErrNoMemory: The zone could not be completely cleared, there may be not enough free zones to move pages to, there may be fixed pages in the zone. |
|
616 |
||
617 |
@param aId The ID of the RAM zone to empty. |
|
618 |
@param aPriority The thread priority for the defragmentation. |
|
619 |
TRamDefragRequest::KInheritPriority to use the priority of the caller. |
|
620 |
||
621 |
@return KErrNone if successful, see above for errors returned by this method. |
|
622 |
||
623 |
@publishedPartner |
|
624 |
@released |
|
625 |
*/ |
|
626 |
EXPORT_C TInt TRamDefragRequest::EmptyRamZone(TUint aId, TInt aPriority) |
|
627 |
{ |
|
628 |
if (aPriority < -1 || aPriority >= KNumPriorities) |
|
629 |
return KErrArgument; |
|
630 |
||
631 |
iOp = Epoc::ERamDefrag_EmptyRamZone; |
|
632 |
iId = aId; |
|
633 |
SetupPriority(aPriority); |
|
634 |
return SendReceive(); |
|
635 |
} |
|
636 |
||
637 |
||
638 |
/** |
|
639 |
Removes as many pages from the specified RAM zone as possible. The function returns immediately. |
|
640 |
When the operation is complete (or cancelled) aSem is signalled. The result of the request can |
|
641 |
be found by calling TRamDefragRequest::Result(); the following may be returned: |
|
642 |
- KErrCancel: The defrag was cancelled, |
|
643 |
- KErrArgument: The specified zone couldn't be found, |
|
644 |
- KErrNoMemory: The zone could not be completely cleared, there may be not enough free zones to move pages to, there may be fixed pages in the zone. |
|
645 |
||
646 |
@param aId The ID of the RAM zone to empty. |
|
647 |
@param aSem The fast semaphore to signal on completion of the operation. |
|
648 |
@param aPriority The thread priority for the defragmentation. |
|
649 |
TRamDefragRequest::KInheritPriority to use the priority of the caller. |
|
650 |
||
651 |
@return KErrNone if request sent or KErrArgument on invalid parameters |
|
652 |
||
653 |
@see NFastSemaphore |
|
654 |
||
655 |
@publishedPartner |
|
656 |
@released |
|
657 |
*/ |
|
658 |
EXPORT_C TInt TRamDefragRequest::EmptyRamZone(TUint aId, NFastSemaphore* aSem, TInt aPriority) |
|
659 |
{ |
|
660 |
if (!aSem || aPriority < -1 || aPriority >= KNumPriorities) |
|
661 |
return KErrArgument; |
|
662 |
||
663 |
iOp = Epoc::ERamDefrag_EmptyRamZone; |
|
664 |
iId = aId; |
|
665 |
SetupPriority(aPriority); |
|
666 |
Send(aSem); |
|
667 |
return KErrNone; |
|
668 |
} |
|
669 |
||
670 |
||
671 |
/** |
|
672 |
Removes as many pages from the specified RAM zone as possible. The function returns immediately. |
|
673 |
When the operation is complete (or cancelled) aDfc is enqueued. The result of the request can be |
|
674 |
found by calling TRamDefragRequest::Result(); the following may be returned: |
|
675 |
- KErrCancel: The defrag was cancelled, |
|
676 |
- KErrArgument: The specified zone couldn't be found, |
|
677 |
- KErrNoMemory: The zone could not be completely cleared, there may be not enough free zones to move pages to, there may be fixed pages in the zone. |
|
678 |
||
679 |
@param aId The ID of the RAM zone to empty. |
|
680 |
@param aDfc The DFC to enqueue on completion of the operation. |
|
681 |
@param aPriority The thread priority for the defragmentation. |
|
682 |
TRamDefragRequest::KInheritPriority to use the priority of the caller. |
|
683 |
||
684 |
@return KErrNone if request sent or KErrArgument on invalid parameters |
|
685 |
||
686 |
@see TDfc |
|
687 |
||
688 |
@publishedPartner |
|
689 |
@released |
|
690 |
*/ |
|
691 |
EXPORT_C TInt TRamDefragRequest::EmptyRamZone(TUint aId, TDfc* aDfc, TInt aPriority) |
|
692 |
{ |
|
693 |
if (!aDfc || aPriority < -1 || aPriority >= KNumPriorities) |
|
694 |
return KErrArgument; |
|
695 |
||
696 |
iOp = Epoc::ERamDefrag_EmptyRamZone; |
|
697 |
iId = aId; |
|
698 |
SetupPriority(aPriority); |
|
699 |
Send(aDfc); |
|
700 |
return KErrNone; |
|
701 |
} |
|
702 |
||
703 |
||
704 |
/** |
|
705 |
Attempts to claim the whole of the specified RAM zone. |
|
706 |
||
707 |
This method may return the following error codes: |
|
708 |
- KErrCancel: The call was cancelled, |
|
709 |
- KErrArgument: aPriority was out of scope or the specified zone could not be found, |
|
710 |
- KErrNoMemory: ClaimRamZone failed; may be no free zones to move pages to, there may be fixed pages in the zone which can not be moved. |
|
711 |
||
712 |
@param aId The ID of the RAM zone to claim. |
|
713 |
@param aPhysAddr On success, this holds the base address of the claimed RAM zone |
|
714 |
@param aPriority The thread priority for the defragmentation. |
|
715 |
TRamDefragRequest::KInheritPriority to use the priority of the caller. |
|
716 |
||
717 |
@return KErrNone if successful, or a system-wide error code, see above. |
|
718 |
||
719 |
@see TPhysAddr |
|
720 |
||
721 |
@publishedPartner |
|
722 |
@released |
|
723 |
*/ |
|
724 |
EXPORT_C TInt TRamDefragRequest::ClaimRamZone(TUint aId, TPhysAddr& aPhysAddr, TInt aPriority) |
|
725 |
{ |
|
726 |
if (aPriority < -1 || aPriority >= KNumPriorities) |
|
727 |
return KErrArgument; |
|
728 |
||
729 |
iOp = Epoc::ERamDefrag_ClaimRamZone; |
|
730 |
iId = aId; |
|
731 |
iPhysAddr = &aPhysAddr; |
|
732 |
SetupPriority(aPriority); |
|
733 |
return SendReceive(); |
|
734 |
} |
|
735 |
||
736 |
||
737 |
/** |
|
738 |
Attempts to claim the whole of the specified RAM zone. |
|
739 |
The function returns immediately. When the operation is complete (or cancelled) |
|
740 |
aSem is signalled. The result of the request can be found by calling |
|
741 |
TRamDefragRequest::Result(); the following may be returned: |
|
742 |
- KErrNone: The zone was claimed, |
|
743 |
- KErrCancel: The call was cancelled, |
|
744 |
- KErrArgument: The specified zone could not be found, |
|
745 |
- KErrNoMemory: ClaimRamZone failed; there may be no free zones to move pages to, there may be fixed pages in the zone which can not be moved. |
|
746 |
||
747 |
@param aId The ID of the RAM zone to claim. |
|
748 |
@param aPhysAddr On success, this holds the base address of the claimed RAM zone |
|
749 |
@param aSem The fast semaphore to signal on completion of the operation. |
|
750 |
@param aPriority The thread priority for the defragmentation. |
|
751 |
TRamDefragRequest::KInheritPriority to use the priority of the caller. |
|
752 |
||
753 |
@return KErrNone if the request was sent or KErrArgument if parameters were invalid. |
|
754 |
||
755 |
@see TPhysAddr |
|
756 |
@see NFastSemaphore |
|
757 |
||
758 |
@publishedPartner |
|
759 |
@released |
|
760 |
*/ |
|
761 |
EXPORT_C TInt TRamDefragRequest::ClaimRamZone(TUint aId, TPhysAddr& aPhysAddr, NFastSemaphore* aSem, TInt aPriority) |
|
762 |
{ |
|
763 |
if (!aSem || aPriority < -1 || aPriority >= KNumPriorities) |
|
764 |
return KErrArgument; |
|
765 |
||
766 |
iOp = Epoc::ERamDefrag_ClaimRamZone; |
|
767 |
iId = aId; |
|
768 |
iPhysAddr = &aPhysAddr; |
|
769 |
SetupPriority(aPriority); |
|
770 |
Send(aSem); |
|
771 |
return KErrNone; |
|
772 |
} |
|
773 |
||
774 |
||
775 |
/** |
|
776 |
Attempts to claim the whole of the specified RAM zone. The function returns immediately. |
|
777 |
When the operation is complete (or cancelled) aDfc is enqueued. The result of the request |
|
778 |
can be found by calling TRamDefragRequest::Result(); the following may be returned: |
|
779 |
- KErrNone: The zone was claimed, |
|
780 |
- KErrCancel: The call was cancelled, |
|
781 |
- KErrArgument: The specified zone could not be found, |
|
782 |
- KErrNoMemory: ClaimRamZone failed; there may be no free zones to move pages to, there may be fixed pages in the zone which can not be moved. |
|
783 |
||
784 |
@param aId The ID of the RAM zone to claim. |
|
785 |
@param aPhysAddr On success, this holds the base address of the claimed RAM zone |
|
786 |
@param aDfc The DFC to enqueue on completion of the operation. |
|
787 |
@param aPriority The thread priority for the defragmentation. |
|
788 |
TRamDefragRequest::KInheritPriority to use the priority of the caller. |
|
789 |
||
790 |
@return KErrNone if the request was sent or KErrArgument if parameters were invalid. |
|
791 |
||
792 |
@see TPhysAddr |
|
793 |
@see TDfc |
|
794 |
||
795 |
@publishedPartner |
|
796 |
@released |
|
797 |
*/ |
|
798 |
EXPORT_C TInt TRamDefragRequest::ClaimRamZone(TUint aId, TPhysAddr& aPhysAddr, TDfc* aDfc, TInt aPriority) |
|
799 |
{ |
|
800 |
if (!aDfc || aPriority < -1 || aPriority >= KNumPriorities) |
|
801 |
return KErrArgument; |
|
802 |
||
803 |
iOp = Epoc::ERamDefrag_ClaimRamZone; |
|
804 |
iId = aId; |
|
805 |
iPhysAddr = &aPhysAddr; |
|
806 |
SetupPriority(aPriority); |
|
807 |
Send(aDfc); |
|
808 |
return KErrNone; |
|
809 |
} |
|
810 |
||
811 |
||
812 |
/** |
|
813 |
Retrieves the result of the last request. This value is only valid if notification of |
|
814 |
completion has been received (via DFC callback or by waiting on the semaphore). |
|
815 |
||
816 |
@return KErrNone if the last request was successful, or a system-wide error code. |
|
817 |
||
818 |
@publishedPartner |
|
819 |
@released |
|
820 |
*/ |
|
821 |
EXPORT_C TInt TRamDefragRequest::Result() |
|
822 |
{ |
|
823 |
return iResult; |
|
824 |
} |
|
825 |
||
826 |
||
827 |
/** |
|
828 |
Cancel the request. If the operation has already started, it terminates at the |
|
829 |
next opportunity. This function has no effect if no request has been made or if |
|
830 |
the request has already finished. |
|
831 |
||
832 |
@publishedPartner |
|
833 |
@released |
|
834 |
*/ |
|
835 |
EXPORT_C void TRamDefragRequest::Cancel() |
|
836 |
{ |
|
837 |
TAsyncRequest::Cancel(); |
|
838 |
} |
|
839 |
||
840 |
void TRamDefragRequest::SetupPriority(TInt aPriority) |
|
841 |
{ |
|
842 |
if (aPriority == KInheritPriority) |
|
843 |
iThreadPriority = NCurrentThread()->iPriority; |
|
844 |
else |
|
845 |
iThreadPriority = aPriority; |
|
846 |
||
847 |
const TUint KPriorityDivisor = (TUint) ((KNumPriorities + KNumDfcPriorities - 1) / KNumDfcPriorities); |
|
848 |
SetPriority(iThreadPriority / KPriorityDivisor); |
|
849 |
} |