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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 <string>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  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)
  29. float GetMemoryUsage() {
  30. char buf[128] = {0};
  31. FILE *fd;
  32. fd = fopen("/proc/meminfo", "r");
  33. if (fd == nullptr) {
  34. MS_LOG(WARNING) << "The meminfo file: /proc/meminfo is opened failed.";
  35. return 0.0;
  36. }
  37. uint32_t status_count = 0;
  38. uint64_t mem_total = 0L;
  39. uint64_t mem_available = 0L;
  40. while (fgets(buf, sizeof(buf), fd)) {
  41. if (status_count == 2) { // get MemTotal and MemAvailable yet
  42. break;
  43. }
  44. // get title
  45. std::string line(buf);
  46. std::string::size_type position = line.find(":");
  47. std::string title = line.substr(0, position);
  48. // get the value when MemTotal or MemAvailable
  49. if (title == "MemTotal") {
  50. std::string::size_type pos1 = line.find_last_of(" ");
  51. std::string::size_type pos2 = line.find_last_of(" ", pos1 - 1);
  52. mem_total = atol(line.substr(pos2, pos1 - pos2).c_str());
  53. status_count++;
  54. } else if (title == "MemAvailable") {
  55. std::string::size_type pos1 = line.find_last_of(" ");
  56. std::string::size_type pos2 = line.find_last_of(" ", pos1 - 1);
  57. mem_available = atol(line.substr(pos2, pos1 - pos2).c_str());
  58. status_count++;
  59. }
  60. (void)memset_s(buf, sizeof(buf), 0, sizeof(buf));
  61. }
  62. fclose(fd);
  63. if (status_count != 2 || mem_total == 0 || mem_available > mem_total) {
  64. MS_LOG(WARNING) << "Get memory usage failed.";
  65. return 0.0;
  66. }
  67. return (1.0 - static_cast<float>(static_cast<double>(mem_available) / static_cast<double>(mem_total)));
  68. }
  69. #endif
  70. } // namespace dataset
  71. } // namespace mindspore