timer.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (c) 2018 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if defined(SPIRV_TIMER_ENABLED)
  15. #include "source/util/timer.h"
  16. #include <sys/resource.h>
  17. #include <sys/time.h>
  18. #include <iomanip>
  19. #include <iostream>
  20. #include <string>
  21. namespace spvtools {
  22. namespace utils {
  23. void PrintTimerDescription(std::ostream* out, bool measure_mem_usage) {
  24. if (out) {
  25. *out << std::setw(30) << "PASS name" << std::setw(12) << "CPU time"
  26. << std::setw(12) << "WALL time" << std::setw(12) << "USR time"
  27. << std::setw(12) << "SYS time";
  28. if (measure_mem_usage) {
  29. *out << std::setw(12) << "RSS delta" << std::setw(16) << "PGFault delta";
  30. }
  31. *out << std::endl;
  32. }
  33. }
  34. // Do not change the order of invoking system calls. We want to make CPU/Wall
  35. // time correct as much as possible. Calling functions to get CPU/Wall time must
  36. // closely surround the target code of measuring.
  37. void Timer::Start() {
  38. if (report_stream_) {
  39. if (getrusage(RUSAGE_SELF, &usage_before_) == -1)
  40. usage_status_ |= kGetrusageFailed;
  41. if (clock_gettime(CLOCK_MONOTONIC, &wall_before_) == -1)
  42. usage_status_ |= kClockGettimeWalltimeFailed;
  43. if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &cpu_before_) == -1)
  44. usage_status_ |= kClockGettimeCPUtimeFailed;
  45. }
  46. }
  47. // The order of invoking system calls is important with the same reason as
  48. // Timer::Start().
  49. void Timer::Stop() {
  50. if (report_stream_ && usage_status_ == kSucceeded) {
  51. if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &cpu_after_) == -1)
  52. usage_status_ |= kClockGettimeCPUtimeFailed;
  53. if (clock_gettime(CLOCK_MONOTONIC, &wall_after_) == -1)
  54. usage_status_ |= kClockGettimeWalltimeFailed;
  55. if (getrusage(RUSAGE_SELF, &usage_after_) == -1)
  56. usage_status_ = kGetrusageFailed;
  57. }
  58. }
  59. void Timer::Report(const char* tag) {
  60. if (!report_stream_) return;
  61. report_stream_->precision(2);
  62. *report_stream_ << std::fixed << std::setw(30) << tag;
  63. if (usage_status_ & kClockGettimeCPUtimeFailed)
  64. *report_stream_ << std::setw(12) << "Failed";
  65. else
  66. *report_stream_ << std::setw(12) << CPUTime();
  67. if (usage_status_ & kClockGettimeWalltimeFailed)
  68. *report_stream_ << std::setw(12) << "Failed";
  69. else
  70. *report_stream_ << std::setw(12) << WallTime();
  71. if (usage_status_ & kGetrusageFailed) {
  72. *report_stream_ << std::setw(12) << "Failed" << std::setw(12) << "Failed";
  73. if (measure_mem_usage_) {
  74. *report_stream_ << std::setw(12) << "Failed" << std::setw(12) << "Failed";
  75. }
  76. } else {
  77. *report_stream_ << std::setw(12) << UserTime() << std::setw(12)
  78. << SystemTime();
  79. if (measure_mem_usage_) {
  80. *report_stream_ << std::fixed << std::setw(12) << RSS() << std::setw(16)
  81. << PageFault();
  82. }
  83. }
  84. *report_stream_ << std::endl;
  85. }
  86. } // namespace utils
  87. } // namespace spvtools
  88. #endif // defined(SPIRV_TIMER_ENABLED)