MSSQLWIKI

Karthick P.K on SQL Server

AWE allocator API’s (How SQL Server AWE works)

Posted by Karthick P.K on November 11, 2010

#include <windows.h> 
#include <string> 
#include <winbase.h> 
#include <iostream> 
using namespace std;
#include <psapi.h>
#pragma comment(lib,"psapi.lib")


BOOL LoggedSetLockPagesPrivilege ( HANDLE hProcess,BOOL bEnable)
{
  struct {
    DWORD Count;
    LUID_AND_ATTRIBUTES Privilege [1];
  } Info;

  HANDLE Token;
  BOOL Result;

  // Open the token.

  Result = OpenProcessToken ( hProcess,
                              TOKEN_ADJUST_PRIVILEGES,
                              & Token);

  if( Result != TRUE ) 
  {
    printf( "Cannot open process token.\n" );
    return FALSE;
  }

  // Enable or disable?

  Info.Count = 1;
  if( bEnable ) 
  {
    Info.Privilege[0].Attributes = SE_PRIVILEGE_ENABLED;
  } 
  else 
  {
    Info.Privilege[0].Attributes = 0;
  }

  // Get the LUID.

  Result = LookupPrivilegeValue ( NULL,
                                  SE_LOCK_MEMORY_NAME,
                                  &(Info.Privilege[0].Luid));

  if( Result != TRUE ) 
  {
    printf( "Cannot get privilege for %s.\n", SE_LOCK_MEMORY_NAME );
    return FALSE;
  }

  // Adjust the privilege.

  Result = AdjustTokenPrivileges ( Token, FALSE,
                                   (PTOKEN_PRIVILEGES) &Info,
                                   0, NULL, NULL);

  // Check the result.

  if( Result != TRUE ) 
  {
    printf ("Cannot adjust token privileges (%u)\n", GetLastError() );
    return FALSE;
  } 
  else 
  {
    if( GetLastError() != ERROR_SUCCESS ) 
    {
      printf ("Cannot enable the SE_LOCK_MEMORY_NAME privilege; ");
      printf ("please check the local policy.\n");
      return FALSE;
    }
  }

  CloseHandle( Token );

  return TRUE;
};






void main()
    {

    long int s=0;
    
    printf("\nEnter the size in MB  for address range that can be used to map Address Windowing Extensions (AWE) pages:");
    scanf("%d",&s);
    LPVOID lpaddress=NULL; 
    SIZE_T size=s*1024*1024;  //size in bytes
    printf ("\n%d",size);
    LPVOID ADD;
    int i;
    char *ADDw;
    BOOL bResult= FALSE;
    BOOL bResult2= FALSE;
    BOOL bResult3= FALSE;
    BOOL bResult4= FALSE;
    BOOL bResult5= FALSE;
    
    ULONG_PTR sizemap= (size)/4096;
    ULONG_PTR sizemap2= (size)/4096;
    ULONG_PTR sizemap3= (size)/4096;
    ULONG_PTR sizemap4= (size)/4096;
    ULONG_PTR sizemap5= (size)/4096;
    
                if( ! LoggedSetLockPagesPrivilege( GetCurrentProcess(), TRUE ) )  
                /*. The SeLockMemoryPrivilege privilege must be enabled in the caller's token or 
                the function will fail with ERROR_PRIVILEGE_NOT_HELD*/
                  {
                    printf("\n No Previledge");
                    printf("\n %u", GetLastError() );
                      return;
                  }


    ULONG_PTR * aRAMPages = new ULONG_PTR[sizemap];
    ULONG_PTR * aRAMPages2 = new ULONG_PTR[sizemap2];
    ULONG_PTR * aRAMPages3 = new ULONG_PTR[sizemap3];
    ULONG_PTR * aRAMPages4 = new ULONG_PTR[sizemap4];
    ULONG_PTR * aRAMPages5 = new ULONG_PTR[sizemap5];


    ADD=VirtualAlloc(lpaddress,size,MEM_RESERVE | MEM_PHYSICAL,PAGE_READWRITE);

    
    if (ADD==0)

    {
    printf ("allocation failled");
    printf("\n %u", GetLastError() );
        return;
    }



    bResult=AllocateUserPhysicalPages(GetCurrentProcess(),&sizemap,aRAMPages);

            if( bResult != TRUE ) 
            {
            printf("\n %uError in AllocateUserPhysicalPages", GetLastError() );
            return;
            }

    bResult2=AllocateUserPhysicalPages(GetCurrentProcess(),&sizemap2,aRAMPages2);

            if( bResult != TRUE ) 
            {
            printf("\n %uError in AllocateUserPhysicalPages2", GetLastError() );
            return;
            }
    bResult3=AllocateUserPhysicalPages(GetCurrentProcess(),&sizemap3,aRAMPages3);

            if( bResult != TRUE ) 
            {
            printf("\n %uError in AllocateUserPhysicalPages2", GetLastError() );
            return;
            }

    bResult4=AllocateUserPhysicalPages(GetCurrentProcess(),&sizemap4,aRAMPages4);

            if( bResult != TRUE ) 
            {
            printf("\n %uError in AllocateUserPhysicalPages2", GetLastError() );
            return;
            }

    bResult5=AllocateUserPhysicalPages(GetCurrentProcess(),&sizemap5,aRAMPages5);

            if( bResult != TRUE ) 
            {
            printf("\n %uError in AllocateUserPhysicalPages2", GetLastError() );
            return;
            }


printf("\n We have allocated 5 different ranges of physical memory pages that could be used to map and unmapp within Address Windowing Extensions (AWE) region of a specified process");


printf("\n Mapping the first range and filling with : MAP1");

   bResult=MapUserPhysicalPages(ADD,sizemap,aRAMPages);

            if( bResult != TRUE ) 
            {
            printf("\n %uError in MapUserPhysicalPages", GetLastError() );
            return;
            }
            

        ADDw =(LPSTR) ADD;
        for(i=1;i<=(size-5);i=i+5)

        {
            ADDw[i] = 'M';
            ADDw[i+1] = 'A';
            ADDw[i+2] = 'P';
            ADDw[i+3] = '1';
            ADDw[i+4] = ':';

        }
 
    printf("\n Mapping the second range and filling with : MAP2");
    bResult2=MapUserPhysicalPages(ADD,sizemap2,aRAMPages2);
                if( bResult != TRUE ) 
                {
                printf("\n %uError in MapUserPhysicalPages", GetLastError() );
                return;
                }

            for(i=1;i<=(size-5);i=i+5)
            {
            ADDw[i] = 'M';
            ADDw[i+1] = 'A';
            ADDw[i+2] = 'P';
            ADDw[i+3] = '2';
            ADDw[i+4] = ':';
            }
       
    printf("\n Mapping the third range and filling with : MAP3");            
       bResult3=MapUserPhysicalPages(ADD,sizemap3,aRAMPages3);

                if( bResult != TRUE ) 
                {
                printf("\n %uError in MapUserPhysicalPages", GetLastError() );
                return;
                }

            for(i=1;i<=(size-5);i=i+5)
            {
            ADDw[i] = 'M';
            ADDw[i+1] = 'A';
            ADDw[i+2] = 'P';
            ADDw[i+3] = '3';
            ADDw[i+4] = ':';
            }
    
       printf("\n Mapped the fourth range and filling with : MAP4");    
            
        bResult4=MapUserPhysicalPages(ADD,sizemap4,aRAMPages4);
        
                if( bResult != TRUE ) 
                {
                printf("\n %uError in MapUserPhysicalPages", GetLastError() );
                return;
                }

            for(i=1;i<=(size-5);i=i+5)
            {
            ADDw[i] = 'M';
            ADDw[i+1] = 'A';
            ADDw[i+2] = 'P';
            ADDw[i+3] = '4';
            ADDw[i+4] = ':';
            }
    
        printf("\n Mapped the fifth range and filled with : MAP5");

        bResult5=MapUserPhysicalPages(ADD,sizemap5,aRAMPages5);

                if( bResult != TRUE ) 
                {
                printf("\n %uError in MapUserPhysicalPages", GetLastError() );
                return;
                }

            for(i=1;i<=(size-5);i=i+5)
            {
            ADDw[i] = 'M';
            ADDw[i+1] = 'A';
            ADDw[i+2] = 'P';
            ADDw[i+3] = '5';
            ADDw[i+4] = ':';
            }

         


    printf("\n Mapping the first range and printing First 128 charecters");

    bResult=MapUserPhysicalPages(ADD,sizemap,aRAMPages);
            
            if( bResult != TRUE ) 
            {
            printf("\n %uError in MapUserPhysicalPages", GetLastError() );
            return;
            }


            

                for(int i=1;i<=128;i++)
                {
                printf("%c",ADDw[i]);
                }

printf("\n Mapping the second range and printing First 128 charecters");            
     bResult2=MapUserPhysicalPages(ADD,sizemap2,aRAMPages2);

                if( bResult != TRUE ) 
                {
                printf("\n %uError in MapUserPhysicalPages", GetLastError() );
                return;
                }
                for(int i=1;i<=128;i++)
                {
                printf("%c",ADDw[i]);
                }

printf("\n Mapping the third range and printing First 128 charecters");
bResult3=MapUserPhysicalPages(ADD,sizemap3,aRAMPages3);

                if( bResult != TRUE ) 
                {
                printf("\n %uError in MapUserPhysicalPages", GetLastError() );
                return;
                }
                for(int i=1;i<=128;i++)
                {
                printf("%c",ADDw[i]);
                }

printf("\n Mapping the fourth range and printing First 128 charecters");
bResult4=MapUserPhysicalPages(ADD,sizemap4,aRAMPages4);

                if( bResult != TRUE ) 
                {
                printf("\n %uError in MapUserPhysicalPages", GetLastError() );
                return;
                }
                for(int i=1;i<=128;i++)
                {
                printf("%c",ADDw[i]);
                }

printf("\n Mapping the five range and printing First 128 charecters");
bResult5=MapUserPhysicalPages(ADD,sizemap5,aRAMPages5);

                if( bResult != TRUE ) 
                {
                printf("\n %uError in MapUserPhysicalPages", GetLastError() );
                return;
                }
                for(int i=1;i<=128;i++)
                {
                printf("%c",ADDw[i]);
                }

system("pause");

}

 
 
 
Thanks
Karthick P.K
About these ads

6 Responses to “AWE allocator API’s (How SQL Server AWE works)”

  1. panic away review

  2. abosalkdu said

    acne no more reviews penisadvantage

  3. Ramkumar said

    Karthik,

    I could understand the code in high level. tried executing this in Visual C++ 2008 (Express edition) and got some errors similar to this. any clue?

    1>Linking…
    1>murugan.obj : error LNK2028: unresolved token (0A0002A4) “extern “C” int __stdcall AdjustTokenPrivileges(void *,int,struct _TOKEN_PRIVILEGES *,unsigned long,struct _TOKEN_PRIVILEGES *,unsigned long *)” (?AdjustTokenPrivileges@@$$J224YGHPAXHPAU_TOKEN_PRIVILEGES@@K1PAK@Z) referenced in function “int __cdecl LoggedSetLockPagesPrivilege(void *,int)” (?LoggedSetLockPagesPrivilege@@$$FYAHPAXH@Z)

  4. Ramkumar said

    i downladed Turbo C++ and later found some header files like windows.h is not available.
    so downloaded Visual C++ 2008 Express and trying your programs one by one. #statusupdate

  5. […] AWE allocator API’s (How SQL Server AWE works) […]

  6. Link exchange is nothing else except it is just placing the
    other person’s blog link on your page at appropriate place and other person will also do same in favor of you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 2,137 other followers

%d bloggers like this: