summaryrefslogtreecommitdiff
path: root/ACE/ace/Monitor_Control/Windows_Monitor.cpp
blob: 4177eb87ed76d25101c46ab82ba6f4954ca18d2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// $Id$

#include "ace/Monitor_Control/Windows_Monitor.h"

#if defined (ACE_HAS_WIN32_PDH)

#include "ace/Log_Msg.h"

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

namespace ACE
{
  namespace Monitor_Control
  {
    Windows_Monitor::Windows_Monitor (const ACE_TCHAR *path)
      : value_ (0.0)
      , path_ (path)
      , query_ (0)
      , counter_ (0)
      , status_ (ERROR_SUCCESS)
    {
      this->init ();
    }

    void
    Windows_Monitor::update_i (void)
    {
      PdhCollectQueryData (this->query_);
      PDH_FMT_COUNTERVALUE pdh_value;

      PdhGetFormattedCounterValue (this->counter_,
                                   PDH_FMT_DOUBLE,
                                   0,
                                   &pdh_value);

      this->value_ = pdh_value.doubleValue;
    }

    void
    Windows_Monitor::clear_impl (void)
    {
      this->init ();
    }

    void
    Windows_Monitor::init (void)
    {
      /// Create a query and a counter here so it doesn't have
      /// to be done with each update.

      this->status_ = ACE_TEXT_PdhOpenQuery (0, 0, &this->query_);

      if (ERROR_SUCCESS != this->status_)
        {
          ACE_ERROR ((LM_DEBUG, ACE_TEXT ("PdhOpenQuery failed\n")));
        }

      this->status_ =
        ACE_TEXT_PdhAddCounter (this->query_,
                                this->path_.c_str (),
                                0,
                                &this->counter_);

      if (ERROR_SUCCESS != this->status_)
        {
          ACE_ERROR ((LM_DEBUG,
                      ACE_TEXT ("PdhAddCounter %s failed\n"),
                      this->path_.c_str ()));
        }
    }
  }
}

ACE_END_VERSIONED_NAMESPACE_DECL

#endif /* defined (ACE_HAS_WIN32_PDH) */