procfile.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // procfile.h :
  2. //
  3. #if !defined(AGD_PROCFILE_H__2D6A6629_BE8B_45D1_949D_3A434336E4DF__INCLUDED_)
  4. #define AGD_PROCFILE_H__2D6A6629_BE8B_45D1_949D_3A434336E4DF__INCLUDED_
  5. #include <unistd.h>
  6. #include <string>
  7. #include <vector>
  8. #include <map>
  9. #ifdef __cplusplus
  10. // https://man7.org/linux/man-pages/man5/proc.5.html
  11. /////////////////////////////////////////////////////////////////////////////
  12. // procfile.h - Declarations:
  13. #define _DEFAULT_REGEX_EXPRESSION "[^\\s]+"
  14. class CProcFile
  15. {
  16. public:
  17. typedef std::map<std::string, std::vector<std::string>> KeyMultiValueMap;
  18. public:
  19. CProcFile(void);
  20. virtual ~CProcFile(void);
  21. virtual bool ReadFile(const char *pszFilePath);
  22. const std::string & GetValue(const std::string &sKey, int index) {
  23. if(index >= 0)
  24. {
  25. const std::vector<std::string> &m = m_vMap[sKey];
  26. if(m.size() > (size_t)index)
  27. return m[index];
  28. }
  29. return m_sEmpty;
  30. }
  31. const std::vector<std::string> & operator[] (const std::string &sKey) {
  32. return m_vMap[sKey];
  33. }
  34. public:
  35. static std::string FormatString(const char *fmt, ...);
  36. static int SplitLine(const std::string &str, std::vector<std::string> &vec, const char *rxe = _DEFAULT_REGEX_EXPRESSION);
  37. static long long StrToIntegral(const std::string &str, long long nDefault = 0, int nBase = 10);
  38. static double StrToReal(const std::string &str, double fDefault = 0.0);
  39. protected:
  40. long m_nClkPerSec;
  41. private:
  42. static const std::string m_sEmpty;
  43. KeyMultiValueMap m_vMap;
  44. };
  45. /////////////////////////////////////////////////////////////////////////////
  46. class CProcStatFile : public CProcFile
  47. {
  48. public:
  49. virtual bool ReadFile(void) {
  50. return CProcFile::ReadFile("/proc/stat");}
  51. double utime(void) {
  52. double val = StrToReal(GetValue("cpu", 1));
  53. return (val / (double)m_nClkPerSec);
  54. }
  55. double stime(void) {
  56. double val = StrToReal(GetValue("cpu", 3));
  57. return (val / (double)m_nClkPerSec);
  58. }
  59. };
  60. /////////////////////////////////////////////////////////////////////////////
  61. class CProcPidStatFile : public CProcStatFile
  62. {
  63. public:
  64. virtual bool ReadFile(pid_t pid) {
  65. m_sPid = FormatString("%d", pid);
  66. std::string s = FormatString("/proc/%d/stat", pid);
  67. return CProcFile::ReadFile(s.c_str());
  68. }
  69. virtual bool ReadFile(const char *pszProcName);
  70. const std::string & operator[] (int index) {
  71. return GetValue(m_sPid, index);
  72. }
  73. pid_t pid(void) {
  74. return (pid_t)StrToIntegral((*this)[0]);
  75. }
  76. double utime(void) {
  77. double val = StrToReal((*this)[13]);
  78. return (val / (double)m_nClkPerSec);
  79. }
  80. double stime(void) {
  81. double val = StrToReal((*this)[14]);
  82. return (val / (double)m_nClkPerSec);
  83. }
  84. unsigned long long starttime(void) {
  85. double val = StrToReal((*this)[21]);
  86. return (val / (double)m_nClkPerSec);
  87. }
  88. private:
  89. std::string m_sPid;
  90. };
  91. /////////////////////////////////////////////////////////////////////////////
  92. #endif // __cplusplus
  93. #endif // !defined(AGD_PROCFILE_H__2D6A6629_BE8B_45D1_949D_3A434336E4DF__INCLUDED_)