memory_usage.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #pragma once
  6. #include <cstddef>
  7. #include <unordered_map>
  8. #ifdef USE_FOLLY
  9. #include <folly/container/F14Map.h>
  10. #endif
  11. #include "rocksdb/rocksdb_namespace.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. // Helper methods to estimate memroy usage by std containers.
  14. template <class Key, class Value, class Hash>
  15. size_t ApproximateMemoryUsage(
  16. const std::unordered_map<Key, Value, Hash>& umap) {
  17. using Map = std::unordered_map<Key, Value, Hash>;
  18. return sizeof(umap) +
  19. // Size of all items plus a next pointer for each item.
  20. (sizeof(typename Map::value_type) + sizeof(void*)) * umap.size() +
  21. // Size of hash buckets.
  22. umap.bucket_count() * sizeof(void*);
  23. }
  24. #ifdef USE_FOLLY
  25. template <class Key, class Value, class Hash>
  26. size_t ApproximateMemoryUsage(const folly::F14FastMap<Key, Value, Hash>& umap) {
  27. return sizeof(umap) + umap.getAllocatedMemorySize();
  28. }
  29. #endif
  30. } // namespace ROCKSDB_NAMESPACE