You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

circular_pool_test.cc 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  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. #include <string>
  17. #include <random>
  18. #include "minddata/dataset/util/task_manager.h"
  19. #include "minddata/dataset/util/circular_pool.h"
  20. #include "minddata/dataset/util/services.h"
  21. #include "common/common.h"
  22. #include "utils/ms_utils.h"
  23. #include "utils/log_adapter.h"
  24. #include "./securec.h"
  25. namespace common = mindspore::common;
  26. using namespace mindspore::dataset;
  27. using mindspore::MsLogLevel::INFO;
  28. using mindspore::ExceptionType::NoExceptionType;
  29. using mindspore::LogStream;
  30. class MindDataTestCircularPool : public UT::Common {
  31. public:
  32. std::shared_ptr<MemoryPool> mp_;
  33. TaskGroup vg_;
  34. MindDataTestCircularPool() {}
  35. void SetUp() {
  36. Status rc = CircularPool::CreateCircularPool(&mp_);
  37. ASSERT_TRUE(rc.IsOk());
  38. }
  39. };
  40. Status TestMem(MindDataTestCircularPool *tp, int32_t num_iterations) {
  41. const uint64_t min = 19 * 1024; // 19k
  42. const uint64_t max = 20 * 1024 * 1024; // 20M
  43. std::mt19937 gen{std::random_device{}()};
  44. std::uniform_int_distribution<uint64_t> dist(min, max);
  45. TaskManager::FindMe()->Post();
  46. for (int i = 0; i < num_iterations; i++) {
  47. uint64_t old_sz = dist(gen);
  48. uint64_t new_sz = dist(gen);
  49. std::string str = "Allocate " + std::to_string(old_sz) +
  50. " bytes of memory and then resize to " + std::to_string(new_sz);
  51. MS_LOG(DEBUG) << str << std::endl;
  52. std::string id = Services::GetUniqueID();
  53. void *p;
  54. RETURN_IF_NOT_OK(tp->mp_->Allocate(old_sz, &p));
  55. // Copy the id to the start of the memory.
  56. (void) memcpy_s(p, old_sz, common::SafeCStr(id), UNIQUEID_LEN);
  57. RETURN_IF_NOT_OK(tp->mp_->Reallocate(&p, old_sz, new_sz));
  58. int n = memcmp(p, common::SafeCStr(id), UNIQUEID_LEN);
  59. if (n) {
  60. RETURN_STATUS_UNEXPECTED("Expect match");
  61. }
  62. tp->mp_->Deallocate(p);
  63. }
  64. return Status::OK();
  65. }
  66. TEST_F(MindDataTestCircularPool, TestALLFunction) {
  67. const int32_t iteration = 100;
  68. Services::CreateInstance();
  69. auto f = std::bind(TestMem, this, iteration);
  70. for (int i = 0; i < 3; i++) {
  71. vg_.CreateAsyncTask("TestMem", f);
  72. }
  73. vg_.join_all();
  74. MS_LOG(DEBUG) << vg_.GetTaskErrorIfAny() << std::endl;
  75. ASSERT_TRUE(vg_.GetTaskErrorIfAny().IsOk());
  76. CircularPool *cp = dynamic_cast<CircularPool *>(mp_.get());
  77. MS_LOG(DEBUG) << *cp << std::endl;
  78. }