span_stats.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2019 The TCMalloc Authors
  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. // https://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. #ifndef TCMALLOC_SPAN_STATS_H_
  15. #define TCMALLOC_SPAN_STATS_H_
  16. #include <stddef.h>
  17. #include "absl/base/macros.h"
  18. #include "tcmalloc/internal/config.h"
  19. GOOGLE_MALLOC_SECTION_BEGIN
  20. namespace tcmalloc {
  21. namespace tcmalloc_internal {
  22. struct SpanStats {
  23. size_t num_spans_requested = 0;
  24. size_t num_spans_returned = 0;
  25. size_t obj_capacity = 0; // cap of number of objs that could be live anywhere
  26. size_t num_live_spans() {
  27. if (num_spans_requested < num_spans_returned) {
  28. return 0;
  29. }
  30. return num_spans_requested - num_spans_returned;
  31. }
  32. // Probability that a span will be returned
  33. double prob_returned() {
  34. if (ABSL_PREDICT_FALSE(num_spans_requested == 0)) return 0.0;
  35. return static_cast<double>(num_spans_returned) / num_spans_requested;
  36. }
  37. };
  38. } // namespace tcmalloc_internal
  39. } // namespace tcmalloc
  40. GOOGLE_MALLOC_SECTION_END
  41. #endif // TCMALLOC_SPAN_STATS_H_