SIRIUS 7.5.0
Electronic structure library and applications
system_tools.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2023 Anton Kozhevnikov, Thomas Schulthess
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
5// the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8// following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
10// and the following disclaimer in the documentation and/or other materials provided with the distribution.
11//
12// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
13// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
15// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
18// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
20/** \file system_tools.hpp
21 *
22 * \brief System-level helper functions.
23 */
24
25#ifndef __SYSTEM_TOOLS_HPP__
26#define __SYSTEM_TOOLS_HPP__
27
28namespace sirius {
29
30/// Check if file exists.
31/** \param[in] file_name Full path to the file being checked.
32 * \return True if file exists, false otherwise.
33 */
34inline bool file_exists(std::string file_name)
35{
36 std::ifstream ifs(file_name.c_str());
37 return ifs.is_open();
38}
39
40/// Get host name.
41inline auto hostname()
42{
43 const int len{1024};
44 char nm[len];
45 gethostname(nm, len);
46 nm[len - 1] = 0;
47 return std::string(nm);
48}
49
50inline long get_page_size()
51{
52 return sysconf(_SC_PAGESIZE);
53}
54
55inline long get_num_pages()
56{
57 return sysconf(_SC_PHYS_PAGES);
58}
59
60inline long get_total_memory()
61{
62 return get_page_size() * get_num_pages();
63}
64
65inline auto get_proc_status()
66{
67 /* virtul memory high water mark */
68 size_t VmHWM{0};
69 /* virtual memory resident set size */
70 size_t VmRSS{0};
71
72 std::ifstream ifs("/proc/self/status");
73 if (ifs.is_open()) {
74 size_t tmp;
75 std::string str;
76 std::string units;
77 while (std::getline(ifs, str)) {
78 auto p = str.find("VmHWM:");
79 if (p != std::string::npos) {
80 std::stringstream s(str.substr(p + 7));
81 s >> tmp;
82 s >> units;
83
84 if (units != "kB") {
85 std::printf("runtime::get_proc_status(): wrong units");
86 } else {
87 VmHWM = tmp * 1024;
88 }
89 }
90
91 p = str.find("VmRSS:");
92 if (p != std::string::npos) {
93 std::stringstream s(str.substr(p + 7));
94 s >> tmp;
95 s >> units;
96
97 if (units != "kB") {
98 std::printf("runtime::get_proc_status(): wrong units");
99 } else {
100 VmRSS = tmp * 1024;
101 }
102 }
103 }
104 }
105 struct {
106 size_t VmHWM;
107 size_t VmRSS;
108 } res{VmHWM, VmRSS};
109
110 return res;
111}
112
113inline int get_proc_threads()
114{
115 int num_threads{-1};
116
117 std::ifstream ifs("/proc/self/status");
118 if (ifs.is_open()) {
119 std::string str;
120 while (std::getline(ifs, str)) {
121 auto p = str.find("Threads:");
122 if (p != std::string::npos) {
123 std::stringstream s(str.substr(p + 9));
124 s >> num_threads;
125 break;
126 }
127 }
128 }
129
130 return num_threads;
131}
132
133} // namespace sirius
134
135#endif
Namespace of the SIRIUS library.
Definition: sirius.f90:5
auto hostname()
Get host name.
@ nm
Non-magnetic case.
bool file_exists(std::string file_name)
Check if file exists.