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