/include/cpustats/ThreadCpuUsage.h

http://github.com/CyanogenMod/android_frameworks_base · C++ Header · 113 lines · 34 code · 17 blank · 62 comment · 0 complexity · b2ceacb901799a01b4d814dcb1298550 MD5 · raw file

  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef _THREAD_CPU_USAGE_H
  17. #define _THREAD_CPU_USAGE_H
  18. #include <cpustats/CentralTendencyStatistics.h>
  19. // Track CPU usage for the current thread, and maintain statistics on
  20. // the CPU usage. Units are in per-thread CPU ns, as reported by
  21. // clock_gettime(CLOCK_THREAD_CPUTIME_ID). Simple usage: for cyclic
  22. // threads where you want to measure the execution time of the whole
  23. // cycle, just call sampleAndEnable() at the start of each cycle.
  24. // Then call statistics() to get the results, and resetStatistics()
  25. // to start a new set of measurements.
  26. // For acyclic threads, or for cyclic threads where you want to measure
  27. // only part of each cycle, call enable(), disable(), and/or setEnabled()
  28. // to demarcate the region(s) of interest, and then call sample() periodically.
  29. // This class is not thread-safe for concurrent calls from multiple threads;
  30. // the methods of this class may only be called by the current thread
  31. // which constructed the object.
  32. class ThreadCpuUsage
  33. {
  34. public:
  35. ThreadCpuUsage() :
  36. mIsEnabled(false),
  37. mWasEverEnabled(false),
  38. mAccumulator(0),
  39. // mPreviousTs
  40. // mMonotonicTs
  41. mMonotonicKnown(false)
  42. // mStatistics
  43. { }
  44. ~ThreadCpuUsage() { }
  45. // Return whether currently tracking CPU usage by current thread
  46. bool isEnabled() { return mIsEnabled; }
  47. // Enable tracking of CPU usage by current thread;
  48. // any CPU used from this point forward will be tracked.
  49. // Returns the previous enabled status.
  50. bool enable() { return setEnabled(true); }
  51. // Disable tracking of CPU usage by current thread;
  52. // any CPU used from this point forward will be ignored.
  53. // Returns the previous enabled status.
  54. bool disable() { return setEnabled(false); }
  55. // Set the enabled status and return the previous enabled status.
  56. // This method is intended to be used for safe nested enable/disabling.
  57. bool setEnabled(bool isEnabled);
  58. // Add a sample point for central tendency statistics, and also
  59. // enable tracking if needed. If tracking has never been enabled, then
  60. // enables tracking but does not add a sample (it is not possible to add
  61. // a sample the first time because no previous). Otherwise if tracking is
  62. // enabled, then adds a sample for tracked CPU ns since the previous
  63. // sample, or since the first call to sampleAndEnable(), enable(), or
  64. // setEnabled(true). If there was a previous sample but tracking is
  65. // now disabled, then adds a sample for the tracked CPU ns accumulated
  66. // up until the most recent disable(), resets this accumulator, and then
  67. // enables tracking. Calling this method rather than enable() followed
  68. // by sample() avoids a race condition for the first sample.
  69. void sampleAndEnable();
  70. // Add a sample point for central tendency statistics, but do not
  71. // change the tracking enabled status. If tracking has either never been
  72. // enabled, or has never been enabled since the last sample, then log a warning
  73. // and don't add sample. Otherwise, adds a sample for tracked CPU ns since
  74. // the previous sample or since the first call to sampleAndEnable(),
  75. // enable(), or setEnabled(true) if no previous sample.
  76. void sample();
  77. // Return the elapsed delta wall clock ns since initial enable or statistics reset,
  78. // as reported by clock_gettime(CLOCK_MONOTONIC).
  79. long long elapsed() const;
  80. // Reset statistics and elapsed. Has no effect on tracking or accumulator.
  81. void resetStatistics();
  82. // Return a const reference to the central tendency statistics.
  83. // Note that only the const methods can be called on this object.
  84. const CentralTendencyStatistics& statistics() const {
  85. return mStatistics;
  86. }
  87. private:
  88. bool mIsEnabled; // whether tracking is currently enabled
  89. bool mWasEverEnabled; // whether tracking was ever enabled
  90. long long mAccumulator; // accumulated thread CPU time since last sample, in ns
  91. struct timespec mPreviousTs; // most recent thread CPU time, valid only if mIsEnabled is true
  92. struct timespec mMonotonicTs; // most recent monotonic time
  93. bool mMonotonicKnown; // whether mMonotonicTs has been set
  94. CentralTendencyStatistics mStatistics;
  95. };
  96. #endif // _THREAD_CPU_USAGE_H