muduo:获取进程相关信息
生活随笔
收集整理的這篇文章主要介紹了
muduo:获取进程相关信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ? muduo里面有專門獲取進程信息的類,記錄一下。
// Use of this source code is governed by a BSD-style license // that can be found in the License file. // // Author: Shuo Chen (chenshuo at chenshuo dot com)#include "muduo/base/ProcessInfo.h" #include "muduo/base/CurrentThread.h" #include "muduo/base/FileUtil.h"#include <algorithm>#include <assert.h> #include <dirent.h> #include <pwd.h> #include <stdio.h> // snprintf #include <stdlib.h> #include <unistd.h> #include <sys/resource.h> #include <sys/times.h>namespace muduo { namespace detail { __thread int t_numOpenedFiles = 0; int fdDirFilter(const struct dirent* d) {if (::isdigit(d->d_name[0])){++t_numOpenedFiles;}return 0; }__thread std::vector<pid_t>* t_pids = NULL; int taskDirFilter(const struct dirent* d) {if (::isdigit(d->d_name[0])){t_pids->push_back(atoi(d->d_name));}return 0; }int scanDir(const char *dirpath, int (*filter)(const struct dirent *)) {struct dirent** namelist = NULL;int result = ::scandir(dirpath, &namelist, filter, alphasort);assert(namelist == NULL);return result; }Timestamp g_startTime = Timestamp::now();// 獲取進程啟動時間 // assume those won't change during the life time of a process. int g_clockTicks = static_cast<int>(::sysconf(_SC_CLK_TCK));// 獲取時鐘頻率 int g_pageSize = static_cast<int>(::sysconf(_SC_PAGE_SIZE));// 獲取內存頁大小 } // namespace detail } // namespace muduousing namespace muduo; using namespace muduo::detail;pid_t ProcessInfo::pid() {return ::getpid(); //進程ID }string ProcessInfo::pidString() {char buf[32];snprintf(buf, sizeof buf, "%d", pid());return buf; }uid_t ProcessInfo::uid() {return ::getuid(); //調用程序的真實用戶ID }string ProcessInfo::username() {struct passwd pwd;struct passwd* result = NULL;char buf[8192];const char* name = "unknownuser";getpwuid_r(uid(), &pwd, buf, sizeof buf, &result);if (result){name = pwd.pw_name;}return name; }uid_t ProcessInfo::euid() {return ::geteuid();//有效用戶ID }Timestamp ProcessInfo::startTime() {return g_startTime; }int ProcessInfo::clockTicksPerSecond()// 一秒的時鐘數 {return g_clockTicks; }int ProcessInfo::pageSize() {return g_pageSize; }bool ProcessInfo::isDebugBuild() { #ifdef NDEBUGreturn false; #elsereturn true; #endif }string ProcessInfo::hostname() {// HOST_NAME_MAX 64// _POSIX_HOST_NAME_MAX 255char buf[256];if (::gethostname(buf, sizeof buf) == 0)//gethostname返回本地主機的標準主機名{buf[sizeof(buf)-1] = '\0';return buf;}else{return "unknownhost";} }string ProcessInfo::procname() {return procname(procStat()).as_string(); }StringPiece ProcessInfo::procname(const string& stat) {StringPiece name;size_t lp = stat.find('(');size_t rp = stat.rfind(')');if (lp != string::npos && rp != string::npos && lp < rp){name.set(stat.data()+lp+1, static_cast<int>(rp-lp-1));}return name; }string ProcessInfo::procStatus()// 通過/proc/進程id/status 文件 ,獲取進程相關信息 {string result;FileUtil::readFile("/proc/self/status", 65536, &result);return result; }string ProcessInfo::procStat()// 通過/proc/進程id/stat 文件 ,獲取進程相關信息 {string result;FileUtil::readFile("/proc/self/stat", 65536, &result);return result; }string ProcessInfo::threadStat()// 通過/proc/進程id/stat 文件 ,獲取進程的線程相關信息 {char buf[64];snprintf(buf, sizeof buf, "/proc/self/task/%d/stat", CurrentThread::tid());string result;FileUtil::readFile(buf, 65536, &result);return result; }string ProcessInfo::exePath()// 執行路徑,從根目錄開始的絕對路徑。 通過命令readlink打開文件/proc/進程id/exe,可以進行查看 {string result;char buf[1024];ssize_t n = ::readlink("/proc/self/exe", buf, sizeof buf);if (n > 0){result.assign(buf, n);}return result; }int ProcessInfo::openedFiles()//返回打開的文件描述符 {t_numOpenedFiles = 0;scanDir("/proc/self/fd", fdDirFilter);//self標識當前進程,正常是進程號return t_numOpenedFiles; }int ProcessInfo::maxOpenFiles()// 獲取進程可以打開的最大文件描述符的數量 {struct rlimit rl;if (::getrlimit(RLIMIT_NOFILE, &rl)){return openedFiles();}else{return static_cast<int>(rl.rlim_cur);} }ProcessInfo::CpuTime ProcessInfo::cpuTime()// 獲取CPU時間 {ProcessInfo::CpuTime t;struct tms tms;if (::times(&tms) >= 0){const double hz = static_cast<double>(clockTicksPerSecond());t.userSeconds = static_cast<double>(tms.tms_utime) / hz;t.systemSeconds = static_cast<double>(tms.tms_stime) / hz;}return t; }int ProcessInfo::numThreads()// 線程數目,通過命令`cat /proc/進程id/status | grep 'Threads' 可以獲得線程數目` {int result = 0;string status = procStatus();size_t pos = status.find("Threads:");if (pos != string::npos){result = ::atoi(status.c_str() + pos + 8);}return result; }std::vector<pid_t> ProcessInfo::threads()// 線程id,通過命令'ls /proc/進程id/task 可以獲得進程中的線程號' {std::vector<pid_t> result;t_pids = &result;scanDir("/proc/self/task", taskDirFilter);t_pids = NULL;std::sort(result.begin(), result.end());return result; }一些關于linux內核的知識。
總結
以上是生活随笔為你收集整理的muduo:获取进程相关信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: muduo:高效整型转换为字符串
- 下一篇: muduo采用计时函数gettimeof