TTK
Loading...
Searching...
No Matches
Os.cpp
Go to the documentation of this file.
1#include <Debug.h>
2#include <Os.h>
3
4#ifdef _WIN32
5
6#ifndef NOMINMAX
7#define NOMINMAX
8#endif
9#ifndef WIN32_LEAN_AND_MEAN
10#define WIN32_LEAN_AND_MEAN
11#endif
12
13#include <windows.h>
14
15#include <cwchar>
16#include <direct.h>
17#include <stdint.h>
18
19#elif defined(__unix__) || defined(__APPLE__)
20
21#include <dirent.h>
22#include <sys/resource.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
26#endif
27
28#include <algorithm>
29#include <iostream>
30#include <sstream>
31
32namespace ttk {
33
34 int OsCall::getCurrentDirectory(std::string &directoryPath) {
35#ifdef _WIN32
36 directoryPath = _getcwd(NULL, 0);
37#else
38 std::vector<char> cwdName(PATH_MAX);
39 char *returnedString = getcwd(cwdName.data(), cwdName.size());
40 directoryPath = std::string{returnedString};
41#endif
42 directoryPath += "/";
43
44 return 0;
45 }
46
48#ifdef __linux__
49 // horrible hack since getrusage() doesn't seem to work well under
50 // linux
51 std::stringstream procFileName;
52 procFileName << "/proc/" << getpid() << "/statm";
53
54 std::ifstream procFile(procFileName.str().data(), std::ios::in);
55 if(procFile) {
56 float memoryUsage;
57 procFile >> memoryUsage;
58 procFile.close();
59 return memoryUsage / 1024.0;
60 }
61#endif
62 return 0;
63 }
64
66 int max_use{0};
67#ifdef __linux__
68 int ru_maxrss;
69 struct rusage use;
70 getrusage(RUSAGE_SELF, &use);
71 ru_maxrss = static_cast<int>(use.ru_maxrss);
72#ifdef TTK_ENABLE_MPI
73 if(ttk::hasInitializedMPI()) {
74 MPI_Reduce(
75 &ru_maxrss, &max_use, 1, MPI_INTEGER, MPI_MAX, 0, ttk::MPIcomm_);
76 } else {
77 max_use = ru_maxrss;
78 }
79#else
80 max_use = ru_maxrss;
81#endif // TTK_ENABLE_MPI
82#endif // __linux__
83 // In Kilo Bytes
84 return (double)max_use;
85 }
86
88#ifdef TTK_ENABLE_OPENMP
89 return omp_get_max_threads();
90#endif
91 return 1;
92 }
93
94 std::vector<std::string>
95 OsCall::listFilesInDirectory(const std::string &directoryName,
96 const std::string &extension) {
97
98 std::vector<std::string> filesInDir;
99
100#ifdef _WIN32
101
102#ifdef UNICODE
103 auto toWString = [](const std::string &str) {
104 if(str.empty())
105 return std::wstring();
106 int wcharCount
107 = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0);
108 std::wstring wstr;
109 wstr.resize(wcharCount);
110 MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wstr[0], wcharCount);
111 return wstr;
112 };
113 auto toString = [](const WCHAR wstr[]) {
114 int charCount = WideCharToMultiByte(
115 CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr);
116 std::string str;
117 str.resize(charCount);
118 WideCharToMultiByte(
119 CP_UTF8, 0, wstr, -1, &str[0], charCount, nullptr, nullptr);
120 return str;
121 };
122#else
123 auto toWString = [](const std::string &str) { return str; };
124 auto toString = [](const char *c) { return std::string(c); };
125#endif
126
127 WIN32_FIND_DATA FindFileData;
128 char *buffer;
129 buffer = _getcwd(NULL, 0);
130 if((buffer = _getcwd(NULL, 0)) == NULL)
131 perror("_getcwd error");
132 else {
133 free(buffer);
134 }
135
136 HANDLE hFind
137 = FindFirstFile(toWString(directoryName).c_str(), &FindFileData);
138 if(hFind == INVALID_HANDLE_VALUE) {
139 std::string s;
140 s = "Could not open directory `";
141 s += directoryName;
142 s += "'. Error: ";
143 s += GetLastError();
144 Debug d;
145 d.printErr(s);
146 } else {
147 const std::string filename = toString(FindFileData.cFileName);
148
149 std::string entryExtension(filename);
150 entryExtension
151 = entryExtension.substr(entryExtension.find_last_of('.') + 1);
152
153 if(entryExtension == extension)
154 filesInDir.push_back(filename);
155 std::string dir = directoryName;
156 dir.resize(dir.size() - 1);
157 while(FindNextFile(hFind, &FindFileData)) {
158 if(extension.size()) {
159 std::string entryExtension(filename);
160 entryExtension
161 = entryExtension.substr(entryExtension.find_last_of('.') + 1);
162 if(entryExtension == extension)
163 filesInDir.push_back(dir + filename);
164 } else {
165 if((filename != ".") && (filename != "..")) {
166 filesInDir.push_back(directoryName + "/" + filename);
167 }
168 }
169 }
170 }
171 FindClose(hFind);
172#else
173 DIR *d = opendir((directoryName + "/").data());
174 if(!d) {
175 std::string msg;
176 msg = "Could not open directory `";
177 msg += directoryName;
178 msg += "'...";
179 const Debug dbg;
180 dbg.printErr(msg);
181 } else {
182 struct dirent *dirEntry;
183 while((dirEntry = readdir(d)) != nullptr) {
184 if(extension.size()) {
185 std::string entryExtension(dirEntry->d_name);
186 entryExtension
187 = entryExtension.substr(entryExtension.find_last_of('.') + 1);
188 if(entryExtension == extension)
189 filesInDir.push_back(directoryName + "/"
190 + std::string(dirEntry->d_name));
191 } else {
192 if((std::string(dirEntry->d_name) != ".")
193 && (std::string(dirEntry->d_name) != ".."))
194 filesInDir.push_back(directoryName + "/"
195 + std::string(dirEntry->d_name));
196 }
197 }
198 closedir(d);
199 }
200#endif
201
202 std::sort(filesInDir.begin(), filesInDir.end());
203
204 return filesInDir;
205 }
206
207 int OsCall::mkDir(const std::string &directoryName) {
208
209#ifdef _WIN32
210 return _mkdir(directoryName.data());
211#else
212 return mkdir(directoryName.data(), 0777);
213#endif
214 }
215
216 int OsCall::nearbyint(const double &x) {
217 const double upperBound = ceil(x);
218 const double lowerBound = floor(x);
219
220 if(upperBound - x <= x - lowerBound)
221 return (int)upperBound;
222 else
223 return (int)lowerBound;
224 }
225
226 int OsCall::rmDir(const std::string &directoryName) {
227 return std::remove(directoryName.c_str());
228 }
229
230 int OsCall::rmFile(const std::string &fileName) {
231 return std::remove(fileName.c_str());
232 }
233
234} // namespace ttk
Minimalist debugging class.
Definition Debug.h:88
int printErr(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
Definition Debug.h:149
static float getTotalMemoryUsage()
Definition Os.cpp:65
static int getCurrentDirectory(std::string &directoryPath)
Definition Os.cpp:34
static std::vector< std::string > listFilesInDirectory(const std::string &directoryName, const std::string &extension)
Definition Os.cpp:95
static float getMemoryInstantUsage()
Definition Os.cpp:47
static int nearbyint(const double &x)
Definition Os.cpp:216
static int rmDir(const std::string &directoryName)
Definition Os.cpp:226
static int mkDir(const std::string &directoryName)
Definition Os.cpp:207
static int getNumberOfCores()
Definition Os.cpp:87
static int rmFile(const std::string &fileName)
Definition Os.cpp:230
The Topology ToolKit.