MSSQLWIKI

Karthick P.K on SQL Server

Lock pages in memory

 

#include <windows.h> 

#include <string> 

#include <winbase.h> 

#include <iostream> 

 

using namespace std;

 

DWORD dwLength;

DWORD dwMemoryLoad;

ULONG_PTR dwTotalPhys;

ULONG_PTR dwAvailPhys;

ULONG_PTR dwTotalPageFile;

ULONG_PTR dwAvailPageFile;

ULONG_PTR dwTotalVirtual;

ULONG_PTR dwAvailVirtual;

int *m_pBuf; 

MEMORY_RESOURCE_NOTIFICATION_TYPE Low;

MEMORY_RESOURCE_NOTIFICATION_TYPE High;

HANDLE LMHandle;

HANDLE HMHandle;

HANDLE THandle;

int ResourceState;

int x=0;

BOOL state=1;

char dateStr [9];

char timeStr [9];

 

 

BOOL SetPrivilege(

    HANDLE hToken,          // access token handle

    LPCTSTR lpszPrivilege,  // name of privilege to enable/disable

    BOOL bEnablePrivilege   // to enable or disable privilege

    ) 

{

    TOKEN_PRIVILEGES tp;

    LUID luid;

 

    if ( !LookupPrivilegeValue( 

            NULL,            // lookup privilege on local system

            lpszPrivilege,   // privilege to lookup 

            &luid ) )        // receives LUID of privilege

    {

        printf("LookupPrivilegeValue error: %u\n", GetLastError() ); 

        return FALSE; 

    }

 

    tp.PrivilegeCount = 1;

    tp.Privileges[0].Luid = luid;

    if (bEnablePrivilege)

        tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    else

        tp.Privileges[0].Attributes = 0;

 

    // Enable the privilege or disable all privileges.

 

    if ( !AdjustTokenPrivileges(

           hToken, 

           FALSE, 

           &tp, 

           sizeof(TOKEN_PRIVILEGES), 

           (PTOKEN_PRIVILEGES) NULL, 

           (PDWORD) NULL) )

    { 

          printf("AdjustTokenPrivileges error: %u\n", GetLastError() ); 

          return FALSE; 

    } 

 

    if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)

 

    {

          printf("The token does not have the specified privilege. \n");

          return FALSE;

    } 

 

    return TRUE;

}

 

 

 

void main2()

    

{

 

FILE * pFile;

pFile = fopen ("MemoryStatus.txt","a");

DWORD     PID[1024];

DWORD pBytesReturned=NULL;

BOOL S;

//PID= new DWORD(SIZEOF(pBytesReturned));

 

S= EnumProcesses(PID,sizeof(PID), &pBytesReturned);

BOOL x;

HANDLE TokenHandle;

TokenHandle=NULL;

fprintf(pFile,"Low Memory Notification received on  %s  %s  ", dateStr,timeStr);

x= OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&TokenHandle);

      

   if (x==0)

                               {

                                

                                   fprintf (pFile,"Unable To OpenProcessToken For current Process Error:%d",GetLastError());

                               

                                }

         

 

if(SetPrivilege(TokenHandle, SE_DEBUG_NAME, TRUE))

            {

            fprintf (pFile,"Success");

            }

            else

            {

            fprintf (pFile,"FAILURE");

            }            

 

 

 

for(int i=0;i<=pBytesReturned/ sizeof(DWORD);i++)

{

    HANDLE H;    

    fprintf (pFile,"\nProcess:%d",PID[i]);

    

    H= OpenProcess(PROCESS_QUERY_INFORMATION |PROCESS_VM_READ |PROCESS_ALL_ACCESS,TRUE,PID[i]);

 

 

            if (H==0)

                {

                    fprintf (pFile,"Unable To Get Process Name ");

                    fprintf (pFile,"Error:%d",GetLastError());

                }

            else

    

                {

                    char   Basename[MAX_PATH];

 

                    BOOL A=0;

    

                   A=GetModuleBaseName(H,NULL,(LPSTR) Basename,sizeof(Basename)/sizeof(TCHAR));

                           if (A==0)

                               {

                                fprintf (pFile,"Error:%d",GetLastError());

                                   fprintf (pFile,"Unable To Get ModuleBaseName ");

                               }

                            else

                               {

                                fprintf (pFile,",Process Name:%s",Basename);

                               }

    

                    PROCESS_MEMORY_COUNTERS PMC;

    

                    GetProcessMemoryInfo(H, &PMC,sizeof(PMC));

    

  

                  fprintf (pFile,",PageFaultCount:%d",PMC.PageFaultCount);

                  fprintf (pFile,",PeakWorkingSetSize:%d",PMC.PeakWorkingSetSize);

                  fprintf (pFile,",WorkingSetSize:%d",PMC.WorkingSetSize);

                  fprintf (pFile,",QuotaPeakPagedPoolUsage:%d",PMC.QuotaPeakPagedPoolUsage);

                  fprintf (pFile,",QuotaPagedPoolUsage:%d",PMC.QuotaPagedPoolUsage);

                  fprintf (pFile,",QuotaPeakNonPagedPoolUsage:%d",PMC.QuotaPeakNonPagedPoolUsage);

                  fprintf (pFile,",QuotaNonPagedPoolUsage:%d",PMC.QuotaNonPagedPoolUsage);

                  fprintf (pFile,",PagefileUsage:%d",PMC.PagefileUsage);

                  fprintf (pFile,",PeakPagefileUsage:%d",PMC.PeakPagefileUsage);

    

             }

    

     

 

     

 

}

 

 

  PERFORMANCE_INFORMATION Perfinfo;

  GetPerformanceInfo(&Perfinfo,sizeof(Perfinfo));

  DWORD  cb;

  SIZE_T CommitTotal=Perfinfo.CommitTotal;

  SIZE_T CommitLimit=Perfinfo.CommitLimit;

  SIZE_T CommitPeak=Perfinfo.CommitPeak;

  SIZE_T PhysicalTotal=Perfinfo.PhysicalTotal;

  SIZE_T PhysicalAvailable=Perfinfo.PhysicalAvailable;

  SIZE_T SystemCache=Perfinfo.SystemCache;

  SIZE_T KernelTotal=Perfinfo.KernelTotal;

  SIZE_T KernelPaged=Perfinfo.KernelPaged;

  SIZE_T KernelNonpaged=Perfinfo.KernelNonpaged;

  SIZE_T PageSize=Perfinfo.PageSize;

  DWORD  HandleCount=Perfinfo.HandleCount;

  DWORD  ProcessCount=Perfinfo.ProcessCount;

  DWORD  ThreadCount=Perfinfo.ThreadCount;

 

  fprintf (pFile,"\n\n\nSYSTEM PERFORMANCE INFORMATION");

  fprintf (pFile,"\nCommitTotal=%d",CommitTotal);

  fprintf (pFile,"\nCommitLimit=%d",Perfinfo.CommitLimit);

  fprintf (pFile,"\nCommitPeak=%d",Perfinfo.CommitPeak);

  fprintf (pFile,"\nPhysicalTotal=%d",Perfinfo.PhysicalTotal);

  fprintf (pFile,"\nPhysicalAvailable=%d",Perfinfo.PhysicalAvailable);

  fprintf (pFile,"\nSystemCache=%d",Perfinfo.SystemCache);

  fprintf (pFile,"\nKernelTotal=%d",Perfinfo.KernelTotal);

  fprintf (pFile,"\nKernelPaged=%d",Perfinfo.KernelPaged);

  fprintf (pFile,"\nKernelNonpaged=%d",Perfinfo.KernelNonpaged);

  fprintf (pFile,"\nPageSize=%d",Perfinfo.PageSize);

  fprintf (pFile,"\nHandleCount=%d",Perfinfo.HandleCount);

  fprintf (pFile,"\nProcessCount=%d",Perfinfo.ProcessCount);

  fprintf (pFile,"\nThreadCount=%d",Perfinfo.ThreadCount);

 

 

 

 

 OSVERSIONINFOEX   OSINFO;

 DWORD dwOSVersionInfoSize;

 DWORD dwMajorVersion;

 DWORD dwMinorVersion;

 DWORD dwBuildNumber;

 DWORD dwPlatformId;

 TCHAR szCSDVersion[128];

 WORD  wServicePackMajor;

 WORD  wServicePackMinor;

 WORD  wSuiteMask;

 BYTE  wProductType;

 BYTE  wReserved;

 OSINFO.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

 GetVersionEx((LPOSVERSIONINFOA) &OSINFO);

 fprintf (pFile,"\n\n\nWINDOWS VERSION INFO");

 fprintf (pFile,"\ndwMajorVersion:%d",OSINFO.dwMajorVersion);

 fprintf (pFile,"\ndwMinorVersion:%d",OSINFO.dwMinorVersion);

 fprintf (pFile,"\ndwBuildNumber:%d",OSINFO.dwBuildNumber);

 fprintf (pFile,"\ndwPlatformId:%d",OSINFO.dwPlatformId);

 fprintf (pFile,"\nszCSDVersion[128]:%s",OSINFO.szCSDVersion[128]);

 fprintf (pFile,"\nwServicePackMajor:%d",OSINFO.wServicePackMajor);

 fprintf (pFile,"\nwServicePackMinor:%d",OSINFO.wServicePackMinor);

 fprintf (pFile,"\nwSuiteMask:%d",OSINFO.wSuiteMask);

 fprintf (pFile,"\nwProductType:%d",OSINFO.wProductType);

 fprintf (pFile,"\nwReserved:%d",OSINFO.wReserved);

 

 fclose (pFile);

 

}

 

 

 

 

 

 

    

 

 

DWORD Lowmemorynotification()    

{

 

          

 

    LMHandle = CreateMemoryResourceNotification(Low);

    HMHandle = CreateMemoryResourceNotification(High);

 

 

 

    state=QueryMemoryResourceNotification(LMHandle, &ResourceState);

    

    

 

            if (state==1)

            {

            printf("QueryMemoryResourceNotification Created for Low Memory pressure");

            printf("\nYou will be signaled when there is low MemoryResourceNotification");

            }

 

 

    loop:

    

    x=x+1;

    WaitForSingleObject( LMHandle,INFINITE);

    _strdate( dateStr);

    _strtime( timeStr );

    printf("%d",x);

    printf("Low Memory Notification received on  %s  %s  ", dateStr,timeStr);

    main2();

    printf("  Memory status is printed to Memorystatus.txt \n");

    Sleep(30000); 

    goto loop; 

 

return 1;

}

 

;

 

 

void main()

 

{

     THandle=CreateThread( NULL, 8388608,(LPTHREAD_START_ROUTINE)Lowmemorynotification,NULL,0,NULL);

    

        //8388608=>Stack size in bytes which is 1 mb

    //    Lowmemorynotification();

     

     

     if (THandle==NULL)

     {

     printf("Failed",GetLastError());

          }

     

     else

     {

        printf("\nSuccess");

            WaitForSingleObject(THandle,INFINITE);

 

     

     }

     int a;

     scanf("%d",&a);

 

}

 

 

 

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,113 other followers

%d bloggers like this: