Search This Blog

Thursday, January 12, 2012

Global Allocation Map & Shared Global Allocation Map

Global Allocation Map (GAM)

GAM pages record what extents have been allocated. Each GAM covers 64,000 extents, or almost 4 GB of data. The GAM has one bit for each extent in the interval it covers. If the bit is 1, the extent is free; if the bit is 0, the extent is allocated.

Shared Global Allocation Map (SGAM)

SGAM pages record which extents are currently being used as mixed extents and also have at least one unused page. Each SGAM covers 64,000 extents, or almost 4 GB of data. The SGAM has one bit for each extent in the interval it covers. If the bit is 1, the extent is being used as a mixed extent and has a free page. If the bit is 0, the extent is not used as a mixed extent, or it is a mixed extent and all its pages are being used.

Each extent has the following bit patterns set in the GAM and SGAM, based on its current use.



Current use of extent
GAM bit setting
SGAM bit setting
Free, not being used
1
0
Uniform extent, or full mixed extent
0
0
Mixed extent with free pages
0
1


To allocate a uniform extent, the Database Engine searches the GAM for a 1 bit and sets it to 0. To find a mixed extent with free pages, the Database Engine searches the SGAM for a 1 bit. To allocate a mixed extent, the Database Engine searches the GAM for a 1 bit, sets it to 0, and then also sets the corresponding bit in the SGAM to 1. To de-allocate an extent, the Database Engine makes sure that the GAM bit is set to 1 and the SGAM bit is set to 0. The algorithms that are actually used internally by the Database Engine are more sophisticated than what is described in this topic, because the Database Engine distributes data evenly in a database. However, even the real algorithms are simplified by not having to manage chains of extent allocation information.

Let me take a small example here to explain you GAM and then you can work on SGAM. I will use the page database.

USe Page

GO

DBCC TRACEON(3604)

DBCC PAGE (Page, 1, 2, 1)


And the partial dump of the output is

Memory Dump @0x000000000E9DA0BE


0000000000000000: 0000381f 000080ff ffffffff ffffffff †..8....ÿÿÿÿÿÿÿÿÿ

0000000000000010: ffffffff ffffffff ffffffff ffffffff †ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

0000000000000020: ffffffff ffffffff ffffffff ffffffff †ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

0000000000000030: ffffffff ffffffff ffffffff ffffffff †ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

0000000000000040: ffffffff ffffffff ffffffff ffffffff †ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

0000000000000050: ffffffff ffffffff ffffffff ffffffff †ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

0000000000000060: ffffffff ffffffff ffffffff ffffffff †ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

0000000000000070: ffffffff ffffffff ffffffff ffffffff †ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

000080ff => these four bytes can be expanded as below.

First byte = 00 = 00000000 = each bit covers 8 pages (1 extent) = 0 to 63 pages

Second byte = 00 = 00000000 = 64 to 127 pages

Third byte = 80 = 10000000 = 7 * 8 = 128 to 183 pages

Let’s check PFS page

USe Page

GO

DBCC TRACEON(3604)

DBCC PAGE (Page, 1, 1, 3)

PFS: Page Alloc Status @0x000000000F92C000


(1:0) - (1:3) = ALLOCATED 100_PCT_FULL

(1:4) - (1:5) = NOT ALLOCATED 0_PCT_FULL

(1:6) - (1:7) = ALLOCATED 100_PCT_FULL

(1:8) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:9) - = ALLOCATED 100_PCT_FULL Mixed Ext

(1:10) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:11) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:12) - = ALLOCATED 100_PCT_FULL IAM Page Mixed Ext

(1:13) - (1:16) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:17) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:18) - (1:19) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:20) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:21) - (1:25) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:26) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:27) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:28) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:29) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:30) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:31) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:32) - = ALLOCATED 50_PCT_FULL Mixed Ext

(1:33) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:34) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:35) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:36) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:37) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:38) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:39) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:40) - (1:41) = ALLOCATED 0_PCT_FULL

(1:42) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:43) - = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:44) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:45) - (1:46) = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:47) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:48) - (1:53) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:54) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:55) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:56) - (1:63) = ALLOCATED 0_PCT_FULL

(1:64) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:65) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:66) - (1:71) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:72) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:73) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:74) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:75) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:76) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:77) - (1:78) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:79) - = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:80) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:81) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:82) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:83) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:84) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:85) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:86) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:87) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:88) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:89) - = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:90) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:91) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:92) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:93) - = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:94) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:95) - = ALLOCATED 0_PCT_FULL Has Ghost Mixed Ext

(1:96) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:97) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:98) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:99) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:100) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:101) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:102) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:103) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:104) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:105) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:106) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:107) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:108) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:109) - = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:110) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:111) - (1:113) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:114) - (1:115) = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:116) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:117) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:118) - = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:119) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:120) - = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:121) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:122) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:123) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:124) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:125) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:126) - = NOT ALLOCATED 0_PCT_FULL Has Ghost Mixed Ext

(1:127) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:128) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:129) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:130) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:131) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:132) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:133) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:134) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:135) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:136) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:137) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:138) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:139) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:140) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:141) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:142) - (1:144) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:145) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:146) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:147) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:148) - = NOT ALLOCATED 50_PCT_FULL Mixed Ext

(1:149) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:150) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:151) - = NOT ALLOCATED 100_PCT_FULL Mixed Ext

(1:152) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:153) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:154) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:155) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:156) - (1:158) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:159) - = ALLOCATED 100_PCT_FULL Mixed Ext

(1:160) - = ALLOCATED 50_PCT_FULL Mixed Ext

(1:161) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:162) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:163) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:164) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:165) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:166) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:167) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:168) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:169) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:170) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:171) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:172) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:173) - (1:175) = NOT ALLOCATED 0_PCT_FULL

(1:176) - (1:181) = ALLOCATED 0_PCT_FULL

(1:182) - (1:383) = NOT ALLOCATED 0_PCT_FULL

As you can see all extents after page 183 are not allocated. Similarly you check for page 3 (SGAM).