Runtime ATL Server Information


As we previously discussed, in order to decide whether you need to add or remove threads from your thread pool, you need to know whether or not there are any free threads in the pool. This information, as well as other pieces of information, is available from the IRequestStats interface. This information is defined as follows :

 IRequestStats : public IUnknown  {      long GetTotalRequests();      long GetFailedRequests();      long GetAvgResponseTime();      long GetCurrWaiting();      long GetMaxWaiting();      long GetActiveThreads();  }; 

You ll call the GetActiveThreads() method to determine how many threads are occupied with requests .

In order to get a pointer to the IRequestStats interface, you first need a pointer to the IIsapiExtension interface. Luckily, a pointer to this interface is passed as the first parameter to the CThreadPool::Initialize method. This IIsapiExtension pointer is passed as a void* , so you ll have to cast it first. After that, you just need to call QueryInterface to get a pointer to IRequestStats . You ll store the pointer to this interface as a class member so you can use it when you re processing requests to the thread pool. The code for all of this is as follows (with error checking omitted for conciseness):

 IIsapiExtension *isapiExtension = NULL;  isapiExtension = static_cast<IIsapiExtension*> (pvWorkerParam);  isapiExtension->QueryInterface(__uuidof(IRequestStats),          (void**)&m_requestStats))); 

You don t want to indiscriminately add threads as requests come in, so your next step is to determine how many threads the server that you re running on can support. You ll use a simple formula to determine this value. The maximum number of threads your pool can support will be 25 threads per CPU. You can determine the number of processors your server has using a call to GetSystemInfo . The code for doing so is as follows:

 SYSTEM_INFO systemInfo;  ZeroMemory(&systemInfo, sizeof(SYSTEM_INFO));  GetSystemInfo(&systemInfo);  m_maxThreads =  systemInfo.dwNumberOfProcessors * CPU_MAX_THREADS; 

The final piece of initialization code that you need is code to read the CPU utilization of your server. You ll use the performance counter API to read the % Processor Time performance counter to determine the CPU utilization of your server at any given time. Here s the code necessary to initialize the performance counter API:

 PDH_STATUS pdhStatus = PdhOpenQuery(NULL, NULL, &m_pdhQuery);  PDH_COUNTER_PATH_ELEMENTS pdhPathElements[1];  pdhPathElements[0].dwInstanceIndex  = (DWORD)-1;  pdhPathElements[0].szCounterName    = TEXT("% Processor Time");  pdhPathElements[0].szInstanceName   = TEXT("_Total");  pdhPathElements[0].szMachineName    = NULL;  pdhPathElements[0].szObjectName     = TEXT("Processor");  pdhPathElements[0].szParentInstance = NULL;  DWORD counterPathSize = 0;  pdhStatus = PdhMakeCounterPath(pdhPathElements,                                 NULL,                                 &counterPathSize,                                 NULL);  TCHAR *counterPath = NULL;  counterPath = new TCHAR[counterPathSize];  pdhStatus = PdhMakeCounterPath(pdhPathElements,                                 counterPath,                                 &counterPathSize,                                 NULL);  m_pdhCounter = NULL;  pdhStatus = PdhAddCounter(m_pdhQuery,                            counterPath,                            NULL,                            &m_pdhCounter); 

You still want the default implementation of CThreadPool::Initialize , to be called, so finish by calling the base implementation and returning:

 return __super::Initialize(pvWorkerParam, nNumThreads, dwStackSize, hCompletion); 

With your initialization out of the way, you only need to implement the logic necessary for adding or removing threads from your pool. You can make this decision as each request comes in.




ATL Server. High Performance C++ on. NET
Observing the User Experience: A Practitioners Guide to User Research
ISBN: B006Z372QQ
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net