| 1 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // PDH Collector |
|---|
| 3 | // |
|---|
| 4 | // Functions from this file collects data from the PDH subsystem and stores |
|---|
| 5 | // it for later use |
|---|
| 6 | // *NOTICE* that this is done in a separate thread so threading issues has |
|---|
| 7 | // to be handled. I handle threading issues in the CounterListener's get/ |
|---|
| 8 | // set accessors. |
|---|
| 9 | // |
|---|
| 10 | // Copyright (c) 2004 MySolutions NORDIC (http://www.medin.nu) |
|---|
| 11 | // |
|---|
| 12 | // Date: 2004-03-13 |
|---|
| 13 | // Author: Michael Medin (mickem@medin.nu) |
|---|
| 14 | // |
|---|
| 15 | // This software is provided "AS IS", without a warranty of any kind. |
|---|
| 16 | // You are free to use/modify this code but leave this header intact. |
|---|
| 17 | // |
|---|
| 18 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 19 | |
|---|
| 20 | #include "stdafx.h" |
|---|
| 21 | #include "PDHCollector.h" |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | PDHCollector::PDHCollector() : hStopEvent_(NULL) { |
|---|
| 25 | checkIntervall_ = NSCModuleHelper::getSettingsInt(C_SYSTEM_SECTION_TITLE, C_SYSTEM_CHECK_RESOLUTION, C_SYSTEM_CHECK_RESOLUTION_DEFAULT); |
|---|
| 26 | std::string s = NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_CPU_BUFFER_TIME, C_SYSTEM_CPU_BUFFER_TIME_DEFAULT); |
|---|
| 27 | unsigned int i = strEx::stoui_as_time(s, checkIntervall_*100); |
|---|
| 28 | cpu.resize(i/(checkIntervall_*100)); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | PDHCollector::~PDHCollector() |
|---|
| 32 | { |
|---|
| 33 | if (hStopEvent_) |
|---|
| 34 | CloseHandle(hStopEvent_); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * Thread that collects the data every "CHECK_INTERVAL" seconds. |
|---|
| 39 | * |
|---|
| 40 | * @param lpParameter Not used |
|---|
| 41 | * @return thread exit status |
|---|
| 42 | * |
|---|
| 43 | * @author mickem |
|---|
| 44 | * |
|---|
| 45 | * @date 03-13-2004 |
|---|
| 46 | * |
|---|
| 47 | * @bug If we have "custom named" counters ? |
|---|
| 48 | * @bug This whole concept needs work I think. |
|---|
| 49 | * |
|---|
| 50 | */ |
|---|
| 51 | DWORD PDHCollector::threadProc(LPVOID lpParameter) { |
|---|
| 52 | PDH::PDHQuery pdh; |
|---|
| 53 | pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_MEM_PAGE_LIMIT, C_SYSTEM_MEM_PAGE_LIMIT_DEFAULT), &memCmtLim); |
|---|
| 54 | pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_MEM_PAGE, C_SYSTEM_MEM_PAGE_DEFAULT), &memCmt); |
|---|
| 55 | pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_UPTIME, C_SYSTEM_UPTIME_DEFAULT), &upTime); |
|---|
| 56 | pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_CPU, C_SYSTEM_MEM_CPU_DEFAULT), &cpu); |
|---|
| 57 | |
|---|
| 58 | try { |
|---|
| 59 | pdh.open(); |
|---|
| 60 | } catch (const PDH::PDHException &e) { |
|---|
| 61 | NSC_LOG_ERROR_STD("Failed to open performance counters: " + e.str_); |
|---|
| 62 | return 0; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | hStopEvent_ = CreateEvent(NULL, TRUE, FALSE, NULL); |
|---|
| 66 | if (!hStopEvent_) { |
|---|
| 67 | NSC_LOG_ERROR_STD("Create StopEvent failed: " + strEx::itos(GetLastError())); |
|---|
| 68 | return 0; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | do { |
|---|
| 72 | MutexLock mutex(mutexHandler); |
|---|
| 73 | if (!mutex.hasMutex()) |
|---|
| 74 | NSC_LOG_ERROR("Failed to get Mutex!"); |
|---|
| 75 | else { |
|---|
| 76 | try { |
|---|
| 77 | pdh.collect(); |
|---|
| 78 | } catch (const PDH::PDHException &e) { |
|---|
| 79 | NSC_LOG_ERROR_STD("Failed to query performance counters: " + e.str_); |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | }while (!(WaitForSingleObject(hStopEvent_, checkIntervall_*100) == WAIT_OBJECT_0)); |
|---|
| 83 | |
|---|
| 84 | { |
|---|
| 85 | MutexLock mutex(mutexHandler); |
|---|
| 86 | if (!mutex.hasMutex()) { |
|---|
| 87 | NSC_LOG_ERROR("Failed to get Mute when closing thread!"); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | if (!CloseHandle(hStopEvent_)) |
|---|
| 91 | NSC_LOG_ERROR_STD("Failed to close stopEvent handle: " + strEx::itos(GetLastError())); |
|---|
| 92 | else |
|---|
| 93 | hStopEvent_ = NULL; |
|---|
| 94 | try { |
|---|
| 95 | pdh.close(); |
|---|
| 96 | } catch (const PDH::PDHException &e) { |
|---|
| 97 | NSC_LOG_ERROR_STD("Failed to close performance counters: " + e.str_); |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | return 0; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | /** |
|---|
| 105 | * Request termination of the thread (waiting for thread termination is not handled) |
|---|
| 106 | */ |
|---|
| 107 | void PDHCollector::exitThread(void) { |
|---|
| 108 | MutexLock mutex(mutexHandler); |
|---|
| 109 | if (!mutex.hasMutex()) { |
|---|
| 110 | NSC_LOG_ERROR("Failed to get Mute when trying to close thread!"); |
|---|
| 111 | return; |
|---|
| 112 | } |
|---|
| 113 | if (hStopEvent_ == NULL) |
|---|
| 114 | NSC_LOG_ERROR("Failed to get stop event!"); |
|---|
| 115 | else |
|---|
| 116 | if (!SetEvent(hStopEvent_)) { |
|---|
| 117 | NSC_LOG_ERROR_STD("SetStopEvent failed"); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | /** |
|---|
| 121 | * Get the average CPU usage for "time" |
|---|
| 122 | * @param time Time to check |
|---|
| 123 | * @return average CPU usage |
|---|
| 124 | */ |
|---|
| 125 | int PDHCollector::getCPUAvrage(std::string time) { |
|---|
| 126 | unsigned int mseconds = strEx::stoui_as_time(time, checkIntervall_*100); |
|---|
| 127 | MutexLock mutex(mutexHandler); |
|---|
| 128 | if (!mutex.hasMutex()) { |
|---|
| 129 | NSC_LOG_ERROR("Failed to get Mutex!"); |
|---|
| 130 | return -1; |
|---|
| 131 | } |
|---|
| 132 | return cpu.getAvrage(mseconds / (checkIntervall_*100)); |
|---|
| 133 | } |
|---|
| 134 | /** |
|---|
| 135 | * Get uptime from counter |
|---|
| 136 | * @bug Do we need to collect this all the time ? (perhaps we can collect this in real time ?) |
|---|
| 137 | * @return uptime for the system |
|---|
| 138 | * @bug Are we overflow protected here ? (seem to recall some issues with overflow before ?) |
|---|
| 139 | */ |
|---|
| 140 | long long PDHCollector::getUptime() { |
|---|
| 141 | MutexLock mutex(mutexHandler); |
|---|
| 142 | if (!mutex.hasMutex()) { |
|---|
| 143 | NSC_LOG_ERROR("Failed to get Mutex!"); |
|---|
| 144 | return -1; |
|---|
| 145 | } |
|---|
| 146 | return upTime.getValue(); |
|---|
| 147 | } |
|---|
| 148 | /** |
|---|
| 149 | * Memory commit limit (your guess is as good as mine to what this is :) |
|---|
| 150 | * @return Some form of memory check |
|---|
| 151 | */ |
|---|
| 152 | long long PDHCollector::getMemCommitLimit() { |
|---|
| 153 | MutexLock mutex(mutexHandler); |
|---|
| 154 | if (!mutex.hasMutex()) { |
|---|
| 155 | NSC_LOG_ERROR("Failed to get Mutex!"); |
|---|
| 156 | return -1; |
|---|
| 157 | } |
|---|
| 158 | return memCmtLim.getValue(); |
|---|
| 159 | } |
|---|
| 160 | /** |
|---|
| 161 | * |
|---|
| 162 | * Memory committed bytes (your guess is as good as mine to what this is :) |
|---|
| 163 | * @return Some form of memory check |
|---|
| 164 | */ |
|---|
| 165 | long long PDHCollector::getMemCommit() { |
|---|
| 166 | MutexLock mutex(mutexHandler); |
|---|
| 167 | if (!mutex.hasMutex()) { |
|---|
| 168 | NSC_LOG_ERROR("Failed to get Mutex!"); |
|---|
| 169 | return -1; |
|---|
| 170 | } |
|---|
| 171 | return memCmt.getValue(); |
|---|
| 172 | } |
|---|