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

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