You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cpp 4.2 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // g++ -o main main.cpp
  2. #include <unistd.h>
  3. #include <dirent.h>
  4. #include <sys/stat.h>
  5. #include <signal.h>
  6. #include <cstdio>
  7. #include <cstdint>
  8. #include <iostream>
  9. #include <fstream>
  10. #include <algorithm>
  11. #include <string>
  12. #include <cstring>
  13. #include <chrono>
  14. #include <thread>
  15. static bool should_exit = false;
  16. static void SignalHandler(int sig)
  17. {
  18. if (sig == SIGINT)
  19. {
  20. std::cerr << "KeyboardInterrupt" << std::endl;
  21. should_exit = true;
  22. }
  23. }
  24. class Sampler
  25. {
  26. public:
  27. inline Sampler(double frequency)
  28. : expected_cycle_time_(static_cast<int32_t>((1.0 / frequency) * 1000000))
  29. {
  30. signal(SIGINT, SignalHandler);
  31. }
  32. template <typename Fn, typename... Args>
  33. inline void Run(Fn&& fx, Args&& ... args)
  34. {
  35. auto func = std::bind(std::forward<Fn>(fx), std::forward<Args>(args)...);
  36. start_ = std::chrono::steady_clock::now();
  37. while (!should_exit)
  38. {
  39. func();
  40. Sleep();
  41. }
  42. }
  43. private:
  44. inline bool Sleep()
  45. {
  46. std::chrono::steady_clock::time_point expected_end = start_ + expected_cycle_time_;
  47. std::chrono::steady_clock::time_point actual_end = std::chrono::steady_clock::now();
  48. // detect backward jumps in time
  49. if (actual_end < start_)
  50. {
  51. expected_end = actual_end + expected_cycle_time_;
  52. }
  53. // calculate the time we'll sleep for
  54. std::chrono::duration<int32_t, std::micro> sleep_time = std::chrono::duration_cast<std::chrono::microseconds>(expected_end - actual_end);
  55. // make sure to reset our start time
  56. start_ = expected_end;
  57. // if we've taken too much time we won't sleep
  58. if (sleep_time <= std::chrono::duration<int32_t, std::micro>(0))
  59. {
  60. // if we've jumped forward in time, or the loop has taken more than
  61. a full extra
  62. // cycle, reset our cycle
  63. if (actual_end > expected_end + expected_cycle_time_)
  64. {
  65. start_ = actual_end;
  66. }
  67. // return false to show that the desired rate was not met
  68. return false;
  69. }
  70. std::this_thread::sleep_for(sleep_time);
  71. return true;
  72. }
  73. private:
  74. std::chrono::steady_clock::time_point start_;
  75. std::chrono::duration<int32_t, std::micro> expected_cycle_time_;
  76. };
  77. inline uint64_t NowTimestampMs()
  78. {
  79. return static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
  80. }
  81. inline bool IsNumber(std::string &s)
  82. {
  83. return std::all_of(s.begin(), s.end(), [](char c){ return std::isdigit(c) != 0; });
  84. }
  85. inline bool IsNumber(const char *s)
  86. {
  87. return std::all_of(s, s + std::strlen(s), [](char c){ return std::isdigit(c) != 0; });
  88. }
  89. int64_t CollectMemoryInfo()
  90. {
  91. static const std::string ProcPath = "/proc";
  92. DIR *dir {nullptr};
  93. struct dirent *ptr {nullptr};
  94. std::string stmt_path{};
  95. char line_buffer[256];
  96. int64_t all = 0;
  97. if ((dir = opendir(ProcPath.c_str())) == nullptr)
  98. {
  99. std::cerr << "Failed to open " << ProcPath << std::endl;
  100. exit(1);
  101. }
  102. while ((ptr = readdir(dir)) != nullptr)
  103. {
  104. struct stat buff{};
  105. if (ptr->d_type == DT_DIR && IsNumber(ptr->d_name))
  106. {
  107. stmt_path.clear();
  108. stmt_path.append(ProcPath).append("/").append(ptr->d_name).append("/").append("smaps");
  109. if (access(stmt_path.c_str(), R_OK) == 0)
  110. {
  111. std::ifstream ifs(stmt_path);
  112. while (ifs.getline(line_buffer, sizeof(line_buffer)))
  113. {
  114. if (std::sscanf(line_buffer, "Pss: %d", &pss) == 1)
  115. {
  116. all += pss;
  117. }
  118. }
  119. }
  120. }
  121. }
  122. closedir(dir);
  123. return all;
  124. }
  125. int main ()
  126. {
  127. Sampler sampler(50);
  128. sampler.Run([]{
  129. auto memory_usage_in_kb = CollectMemoryInfo();
  130. auto now_timestamp_ms = NowTimestampMs();
  131. std::cout << now_timestamp_ms << "," << memory_usage_in_kb << "\n";
  132. });
  133. return 0;
  134. }