| 1 | /************************************************************************** |
|---|
| 2 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> * |
|---|
| 3 | * * |
|---|
| 4 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp * |
|---|
| 5 | * * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify * |
|---|
| 7 | * it under the terms of the GNU General Public License as published by * |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
|---|
| 9 | * (at your option) any later version. * |
|---|
| 10 | * * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, * |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 14 | * GNU General Public License for more details. * |
|---|
| 15 | * * |
|---|
| 16 | * You should have received a copy of the GNU General Public License * |
|---|
| 17 | * along with this program; if not, write to the * |
|---|
| 18 | * Free Software Foundation, Inc., * |
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
|---|
| 20 | ***************************************************************************/ |
|---|
| 21 | #pragma once |
|---|
| 22 | |
|---|
| 23 | typedef BOOL (WINAPI *PFGlobalMemoryStatusEx)(LPMEMORYSTATUSEX lpBuffer); |
|---|
| 24 | typedef BOOL (WINAPI *PFGlobalMemoryStatus)(LPMEMORYSTATUS lpBuffer); |
|---|
| 25 | |
|---|
| 26 | namespace CheckMemMethod |
|---|
| 27 | { |
|---|
| 28 | const int None = 0x0; |
|---|
| 29 | const int Extended = 0x1; |
|---|
| 30 | const int Normal = 0x2; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | class CheckMemoryException { |
|---|
| 34 | private: |
|---|
| 35 | std::wstring name_; |
|---|
| 36 | std::wstring msg_; |
|---|
| 37 | public: |
|---|
| 38 | CheckMemoryException(std::wstring name,std::wstring msg) : name_(name), msg_(msg) {}; |
|---|
| 39 | |
|---|
| 40 | std::wstring getError() { |
|---|
| 41 | return _T("Service: ") + name_ + _T(" caused: ") + msg_; |
|---|
| 42 | } |
|---|
| 43 | }; |
|---|
| 44 | |
|---|
| 45 | class CheckMemory { |
|---|
| 46 | private: |
|---|
| 47 | typedef struct { |
|---|
| 48 | unsigned long long total; |
|---|
| 49 | unsigned long long avail; |
|---|
| 50 | } memType; |
|---|
| 51 | public: |
|---|
| 52 | typedef struct { |
|---|
| 53 | unsigned long dwMemoryLoad; |
|---|
| 54 | memType phys; |
|---|
| 55 | memType pageFile; |
|---|
| 56 | memType virtualMem; |
|---|
| 57 | double availExtendedVirtual; |
|---|
| 58 | } memData; |
|---|
| 59 | |
|---|
| 60 | public: |
|---|
| 61 | CheckMemory() : FEGlobalMemoryStatusEx(NULL), FEGlobalMemoryStatus(NULL), method_(CheckMemMethod::None) |
|---|
| 62 | { |
|---|
| 63 | hKernel32 = ::LoadLibrary(_TEXT("Kernel32")); |
|---|
| 64 | if (hKernel32) |
|---|
| 65 | { |
|---|
| 66 | FEGlobalMemoryStatusEx = (PFGlobalMemoryStatusEx)::GetProcAddress(hKernel32, "GlobalMemoryStatusEx"); |
|---|
| 67 | FEGlobalMemoryStatus = (PFGlobalMemoryStatus)::GetProcAddress(hKernel32, "GlobalMemoryStatus"); |
|---|
| 68 | } |
|---|
| 69 | int method = getAvailableMethods(); |
|---|
| 70 | if ((method&CheckMemMethod::Extended)==CheckMemMethod::Extended) |
|---|
| 71 | method_ = CheckMemMethod::Extended; |
|---|
| 72 | else |
|---|
| 73 | method_ = method; |
|---|
| 74 | } |
|---|
| 75 | virtual ~CheckMemory() { |
|---|
| 76 | if (hKernel32) FreeLibrary(hKernel32); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | int getAvailableMethods() { |
|---|
| 80 | int ret = CheckMemMethod::None; |
|---|
| 81 | if (FEGlobalMemoryStatusEx) |
|---|
| 82 | ret |= CheckMemMethod::Extended; |
|---|
| 83 | if (FEGlobalMemoryStatus) |
|---|
| 84 | ret |= CheckMemMethod::Normal; |
|---|
| 85 | return ret; |
|---|
| 86 | } |
|---|
| 87 | int getSuggestedMethod(); |
|---|
| 88 | int setMethod(int method) { |
|---|
| 89 | if ((getAvailableMethods()&method) != 0) |
|---|
| 90 | method_ = method; |
|---|
| 91 | return method_; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | memData getMemoryStatus() { |
|---|
| 95 | memData ret; |
|---|
| 96 | if (method_ == CheckMemMethod::Extended) { |
|---|
| 97 | MEMORYSTATUSEX buffer; |
|---|
| 98 | buffer.dwLength = sizeof(buffer); |
|---|
| 99 | if (!FEGlobalMemoryStatusEx(&buffer)) |
|---|
| 100 | throw CheckMemoryException(_T("CheckMemory"), _T("GlobalMemoryStatusEx failed: ") + error::lookup::last_error()); |
|---|
| 101 | ret.phys.total = buffer.ullTotalPhys; |
|---|
| 102 | ret.phys.avail = buffer.ullAvailPhys; |
|---|
| 103 | ret.virtualMem.total = buffer.ullTotalVirtual; |
|---|
| 104 | ret.virtualMem.avail = buffer.ullAvailVirtual; |
|---|
| 105 | ret.pageFile.total = buffer.ullTotalPageFile; |
|---|
| 106 | ret.pageFile.avail = buffer.ullAvailPageFile; |
|---|
| 107 | } else if (method_ == CheckMemMethod::Normal) { |
|---|
| 108 | MEMORYSTATUS buffer; |
|---|
| 109 | buffer.dwLength = sizeof(buffer); |
|---|
| 110 | if (!FEGlobalMemoryStatus(&buffer)) |
|---|
| 111 | throw CheckMemoryException(_T("CheckMemory"), _T("GlobalMemoryStatus failed: ") + error::lookup::last_error()); |
|---|
| 112 | ret.phys.total = buffer.dwTotalPhys; |
|---|
| 113 | ret.phys.avail = buffer.dwAvailPhys; |
|---|
| 114 | ret.virtualMem.total = buffer.dwTotalVirtual; |
|---|
| 115 | ret.virtualMem.avail = buffer.dwAvailVirtual; |
|---|
| 116 | ret.pageFile.total = buffer.dwTotalPageFile; |
|---|
| 117 | ret.pageFile.avail = buffer.dwAvailPageFile; |
|---|
| 118 | } |
|---|
| 119 | return ret; |
|---|
| 120 | } |
|---|
| 121 | private: |
|---|
| 122 | int method_; |
|---|
| 123 | HMODULE hKernel32; |
|---|
| 124 | PFGlobalMemoryStatusEx FEGlobalMemoryStatusEx; |
|---|
| 125 | PFGlobalMemoryStatus FEGlobalMemoryStatus; |
|---|
| 126 | }; |
|---|