usage_timer.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "test/cpp/qps/usage_timer.h"
  19. #include <fstream>
  20. #include <sstream>
  21. #include <string>
  22. #include <grpc/support/log.h>
  23. #include <grpc/support/time.h>
  24. #ifdef __linux__
  25. #include <sys/resource.h>
  26. #include <sys/time.h>
  27. static double time_double(struct timeval* tv) {
  28. return tv->tv_sec + 1e-6 * tv->tv_usec;
  29. }
  30. #endif
  31. UsageTimer::UsageTimer() : start_(Sample()) {}
  32. double UsageTimer::Now() {
  33. auto ts = gpr_now(GPR_CLOCK_REALTIME);
  34. return ts.tv_sec + 1e-9 * ts.tv_nsec;
  35. }
  36. static void get_resource_usage(double* utime, double* stime) {
  37. #ifdef __linux__
  38. struct rusage usage;
  39. getrusage(RUSAGE_SELF, &usage);
  40. *utime = time_double(&usage.ru_utime);
  41. *stime = time_double(&usage.ru_stime);
  42. #else
  43. *utime = 0;
  44. *stime = 0;
  45. #endif
  46. }
  47. static void get_cpu_usage(unsigned long long* total_cpu_time,
  48. unsigned long long* idle_cpu_time) {
  49. #ifdef __linux__
  50. std::ifstream proc_stat("/proc/stat");
  51. proc_stat.ignore(5);
  52. std::string cpu_time_str;
  53. std::string first_line;
  54. std::getline(proc_stat, first_line);
  55. std::stringstream first_line_s(first_line);
  56. for (int i = 0; i < 10; ++i) {
  57. std::getline(first_line_s, cpu_time_str, ' ');
  58. *total_cpu_time += std::stol(cpu_time_str);
  59. if (i == 3) {
  60. *idle_cpu_time = std::stol(cpu_time_str);
  61. }
  62. }
  63. #else
  64. // Use the parameters to avoid unused-parameter warning
  65. (void)total_cpu_time;
  66. (void)idle_cpu_time;
  67. gpr_log(GPR_INFO, "get_cpu_usage(): Non-linux platform is not supported.");
  68. #endif
  69. }
  70. UsageTimer::Result UsageTimer::Sample() {
  71. Result r;
  72. r.wall = Now();
  73. get_resource_usage(&r.user, &r.system);
  74. r.total_cpu_time = 0;
  75. r.idle_cpu_time = 0;
  76. get_cpu_usage(&r.total_cpu_time, &r.idle_cpu_time);
  77. return r;
  78. }
  79. UsageTimer::Result UsageTimer::Mark() const {
  80. Result s = Sample();
  81. Result r;
  82. r.wall = s.wall - start_.wall;
  83. r.user = s.user - start_.user;
  84. r.system = s.system - start_.system;
  85. r.total_cpu_time = s.total_cpu_time - start_.total_cpu_time;
  86. r.idle_cpu_time = s.idle_cpu_time - start_.idle_cpu_time;
  87. return r;
  88. }