QueryMemoryResourceNotification & CreateMemoryResourceNotification (How SQL Server identifies low system memory on the system and respond to low system memory?)
Posted by Karthick P.K on December 2, 2010
#include <windows.h> #include <string> #include <winbase.h> #include <iostream> using namespace std; #include <psapi.h> #pragma comment(lib,"psapi.lib") #include <time.h> 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 processmemory() { 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)); SIZE_T CommitTotal=Perfinfo.CommitTotal; fprintf (pFile,"\n\n\nSYSTEM PERFORMANCE INFORMATION"); fprintf (pFile,"\nCommitTotal=%d",Perfinfo.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; 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); processmemory(); printf(" Memory status is printed to Memorystatus.txt \n"); Sleep(3000); 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("Create thread failed",GetLastError()); } else { printf("\nSuccess"); WaitForSingleObject(THandle,INFINITE); } }
Thanks
Karthick P.K
This entry was posted on December 2, 2010 at 2:55 AM and is filed under Memory, Programming, SQL Server Engine. Tagged: CreateMemoryResourceNotification, GetPerformanceInfo, GetProcessMemoryInfo, OpenProcess, OSVERSIONINFOEX, QueryMemoryResourceNotification. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Jacky said
Karthick,This is the source code of the SQL Server ?
Karthick P.K said
No
Sent from my Windows Phone ________________________________
Top SQL Server blogs from MSSQLWIKI « MSSQLWIKI said
[…] QueryMemoryResourceNotification & CreateMemoryResourceNotification (How SQL Server identifies lo… […]