找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 3696|回复: 0

ACE的性能监视者

[复制链接]
发表于 2013-8-8 09:22:24 | 显示全部楼层 |阅读模式
ACE目录下的example中的readme.txt,其实并不全。那些没有说明的一些example往往有很强的功能。
今天就来说说Monitor这个非常有趣而且非常有用的例子。
Monitor,顾名思义,监控者。
Monitor用例包含了大概8个用例
分别实现了可以监控指定的进程,CPU,内存,网络IO输入,输出,线程数,消息队列数。
由于谷歌上找不到相应的资料,ace官网上也没有它的多少介绍(至多一两句不关痛痒的英文)
而通过实际的代码走查,发现其实还支持更多的参数,比如内存换页,命中率等。
在windows下,它会调用Windows_Monitor(实际是 PdhAddCounter性能计数器),在Linux下,它会获取/proc/stat文件下的参数。
来看看windows下的实现关键代码,以获取当前CPU为例
windows下是
  1.     void
  2.     Windows_Monitor::init (void)
  3.     {
  4.       /// Create a query and a counter here so it doesn't have
  5.       /// to be done with each update.
  6.       this->status_ = ACE_TEXT_PdhOpenQuery (0, 0, &this->query_);
  7.       if (ERROR_SUCCESS != this->status_)
  8.         {
  9.           ACELIB_ERROR ((LM_DEBUG, ACE_TEXT ("PdhOpenQuery failed\n")));
  10.         }
  11.       this->status_ =
  12.         ACE_TEXT_PdhAddCounter (this->query_,
  13.                                 this->path_.c_str (),
  14.                                 0,
  15.                                 &this->counter_);
  16.       if (ERROR_SUCCESS != this->status_)
  17.         {
  18.           ACELIB_ERROR ((LM_DEBUG,
  19.                       ACE_TEXT ("PdhAddCounter %s failed\n"),
  20.                       this->path_.c_str ()));
  21.         }
  22.     }
复制代码
这里是初始化一个性能计数器用例,非常清晰的代码。
然后是获得相关参数
  1.     void
  2.     Windows_Monitor::update_i (void)
  3.     {
  4.       PdhCollectQueryData (this->query_);
  5.       PDH_FMT_COUNTERVALUE pdh_value;
  6.       PdhGetFormattedCounterValue (this->counter_,
  7.                                    PDH_FMT_DOUBLE,
  8.                                    0,
  9.                                    &pdh_value);
  10.       this->value_ = pdh_value.doubleValue;
  11.     }
复制代码
在linux下获得的更简单。方法如下:
  1.     void
  2.     CPU_Load_Monitor::access_proc_stat (unsigned long *which_idle)
  3.     {
  4.       this->file_ptr_ = ACE_OS::fopen (ACE_TEXT ("/proc/stat"),
  5.                                        ACE_TEXT ("r"));
  6.       if (this->file_ptr_ == 0)
  7.         {
  8.           ACELIB_ERROR ((LM_ERROR,
  9.                       ACE_TEXT ("CPU load - opening /proc/stat failed\n")));
  10.           return;
  11.         }
  12.       char *item = 0;
  13.       char *arg = 0;
  14.       while ((ACE_OS::fgets (buf_, sizeof (buf_), file_ptr_)) != 0)
  15.         {
  16.           item = ACE_OS::strtok (this->buf_, " \t\n");
  17.           arg = ACE_OS::strtok (0, "\n");
  18.           if (item == 0 || arg == 0)
  19.             {
  20.               continue;
  21.             }
  22.           if (ACE_OS::strcmp (item, "cpu") == 0)
  23.             {
  24.               sscanf (arg,
  25.                       "%lu %lu %lu %lu",
  26.                       &this->user_,
  27.                       &this->wait_,
  28.                       &this->kernel_,
  29.                       which_idle);
  30.               break;
  31.             }
  32.         }
  33.       ACE_OS::fclose (this->file_ptr_);
  34.     }
复制代码
代码就不一行行的解释了,有基本功的同学我想一看就懂。
因为这里ace也没有对此用例的相关文档,我在这里补全一下,并展现里面的关键实际调用。
用这个例子,完全可以实现一个监控指定进程各项参数的工具,关键代码例子里面都做好了,运维最喜欢的。
所以说,ACE的例子里面,还是有很多精华可以挖取的。
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-4-29 09:00 , Processed in 0.013787 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表