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.

status.cc 2.7 kB

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "minddata/dataset/util/status.h"
  17. #include <cstdio>
  18. #include <cstdlib>
  19. #include <string>
  20. #include "./securec.h"
  21. #ifndef ENABLE_ANDROID
  22. #include "minddata/dataset/util/task_manager.h"
  23. #else
  24. #include "minddata/dataset/util/log_adapter.h"
  25. #endif
  26. namespace mindspore {
  27. namespace dataset {
  28. #if !defined(_WIN32) && !defined(_WIN64) && !defined(__APPLE__)
  29. float GetMemoryUsage() {
  30. char buf[128] = {0};
  31. FILE *fd = fopen("/proc/meminfo", "r");
  32. if (fd == nullptr) {
  33. MS_LOG(WARNING) << "The meminfo file: /proc/meminfo is opened failed.";
  34. return 0.0;
  35. }
  36. uint32_t status_count = 0;
  37. uint64_t mem_total = 0L;
  38. uint64_t mem_available = 0L;
  39. while (fgets(buf, sizeof(buf), fd)) {
  40. if (status_count == 2) { // get MemTotal and MemAvailable yet
  41. break;
  42. }
  43. // get title
  44. std::string line(buf);
  45. std::string::size_type position = line.find(":");
  46. std::string title = line.substr(0, position);
  47. // get the value when MemTotal or MemAvailable
  48. if (title == "MemTotal") {
  49. std::string::size_type pos1 = line.find_last_of(" ");
  50. std::string::size_type pos2 = line.find_last_of(" ", pos1 - 1);
  51. mem_total = atol(line.substr(pos2, pos1 - pos2).c_str());
  52. status_count++;
  53. } else if (title == "MemAvailable") {
  54. std::string::size_type pos1 = line.find_last_of(" ");
  55. std::string::size_type pos2 = line.find_last_of(" ", pos1 - 1);
  56. mem_available = atol(line.substr(pos2, pos1 - pos2).c_str());
  57. status_count++;
  58. }
  59. auto ret = memset_s(buf, sizeof(buf), 0, sizeof(buf));
  60. if (ret != 0) {
  61. MS_LOG(WARNING) << "memset_s failed when get memory usage. This might be caused by insufficient memory.";
  62. fclose(fd);
  63. return 0.0;
  64. }
  65. }
  66. fclose(fd);
  67. if (status_count != 2 || mem_total == 0 || mem_available > mem_total) {
  68. MS_LOG(WARNING) << "Get memory usage failed.";
  69. return 0.0;
  70. }
  71. return (1.0 - static_cast<float>(static_cast<double>(mem_available) / static_cast<double>(mem_total)));
  72. }
  73. #endif
  74. } // namespace dataset
  75. } // namespace mindspore