10 #include "hep_concurrency/assert_only_one_thread.h" 25 explicit token_ignore(
const unsigned ntokens) : ntokens_{ntokens} {}
30 operator>>(std::istream& is, token_ignore
const&& ig)
34 while (i++ < ig.ntokens_)
41 new_proc_file_descriptor(pid_t
const pid)
43 std::ostringstream ost;
44 ost <<
"/proc/" << pid <<
"/stat";
46 auto file = fopen(ost.str().c_str(),
"r");
47 if (
file ==
nullptr) {
49 <<
" Failed to open: " << ost.str() <<
'\n' 50 <<
" errno: " <<
errno <<
" (" << std::strerror(
errno) <<
")\n";
59 LinuxProcMgr::LinuxProcMgr() noexcept(false)
61 , pgSize_{sysconf(_SC_PAGESIZE)}
62 , file_{new_proc_file_descriptor(pid_)}
65 LinuxProcMgr::~LinuxProcMgr() noexcept { fclose(file_); }
69 LinuxProcMgr::getCurrentData()
const noexcept(false)
71 HEP_CONCURRENCY_ASSERT_ONLY_ONE_THREAD();
73 int const seek_result{fseek(file_, 0, SEEK_SET)};
74 if (seek_result != 0) {
76 "Error while retrieving Linux proc data."}
77 <<
"\nCould not reset position indicator while retrieving proc " 78 "stat information.\n";
82 size_t const cnt{fread(buf, 1,
sizeof(buf), file_)};
86 "Error while retrieving Linux proc data."}
87 <<
"\nCould not read proc stat information.\n";
95 std::istringstream iss{buf};
96 iss >> token_ignore(22) >> vsize >> rss;
98 return LinuxProcData::make_proc_tuple(vsize, rss * pgSize_);
103 LinuxProcMgr::getStatusData_(
std::string const& field)
const noexcept(
false)
105 HEP_CONCURRENCY_ASSERT_ONLY_ONE_THREAD();
107 std::ostringstream ost;
108 ost <<
"/proc/" << pid_ <<
"/status";
110 auto file = fopen(ost.str().c_str(),
"r");
111 if (
file ==
nullptr) {
117 std::regex
const pattern{
"^" + field + R
"(:\s*(\d+)\s*kB)"}; 118 while (!feof(
file)) {
120 if (fgets(buffer,
sizeof(buffer),
file) !=
nullptr) {
122 if (std::regex_search(buffer, cm,
pattern)) {
125 value = std::stod(cm.str(1)) * LinuxProcData::KiB / LinuxProcData::MB;
static constexpr double cm
int errno
Contains the last error code.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
QDataStream & operator>>(QDataStream &s, QByteArray &a)
std::tuple< vsize_t, rss_t > proc_tuple
QTextStream & endl(QTextStream &s)