1 /*
2 Copyright (c) 2020, Intel Corporation
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9     * Neither the name of Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10 
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 */
13 // written by Roman Dementiev
14 
15 #ifdef __linux__
16 
17 #include "resctrl.h"
18 #include "cpucounters.h"
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <string>
22 #include <iostream>
23 #include <cstdlib>
24 
25 namespace pcm
26 {
isMounted()27     bool Resctrl::isMounted()
28     {
29         struct stat st;
30         if (stat("/sys/fs/resctrl/mon_groups", &st) < 0)
31         {
32             return false;
33         }
34         return true;
35     }
init()36     void Resctrl::init()
37     {
38         if (isMounted() == false)
39         {
40             std::cerr << "ERROR: /sys/fs/resctrl is not mounted\n";
41             std::cerr << "ERROR: RDT metrics (L3OCC,LMB,RMB) will not be available\n";
42             std::cerr << "Mount it to make it work: mount -t resctrl resctrl /sys/fs/resctrl\n";
43             return;
44         }
45         const auto numCores = pcm.getNumCores();
46         for (unsigned int c = 0; c < numCores; ++c)
47         {
48             if (pcm.isCoreOnline(c))
49             {
50                 const auto C = std::to_string(c);
51                 const auto dir = std::string(PCMPath) + C;
52                 struct stat st;
53                 if (stat(dir.c_str(), &st) < 0 && mkdir(dir.c_str(), 0700) < 0)
54                 {
55                     std::cerr << "INFO: can't create directory " << dir << " error: " << strerror(errno) << "\n";
56                     const auto containerDir = std::string("/pcm") + dir;
57                     if (stat(containerDir.c_str(), &st) < 0 && mkdir(containerDir.c_str(), 0700) < 0)
58                     {
59                         std::cerr << "INFO: can't create directory " << containerDir << " error: " << strerror(errno) << "\n";
60                         std::cerr << "ERROR: RDT metrics (L3OCC,LMB,RMB) will not be available\n";
61                         break;
62                     }
63                 }
64                 const auto cpus_listFilename = dir + "/cpus_list";
65                 writeSysFS(cpus_listFilename.c_str(), C, false);
66                 auto generateMetricFiles = [&dir, c] (PCM & pcm, const std::string & metric, FileMapType & fileMap)
67                 {
68                     auto getMetricFilename = [] (const std::string & dir, const uint64 s, const std::string & metric)
69                     {
70                         std::ostringstream ostr;
71                         ostr << dir << "/mon_data/mon_L3_" << std::setfill('0') << std::setw(2) << s << "/" << metric;
72                         return ostr.str();
73                     };
74                     for (uint64 s = 0; s < pcm.getNumSockets(); ++s)
75                     {
76                         fileMap[c].push_back(getMetricFilename(dir, s, metric));
77                     }
78                 };
79                 if (pcm.L3CacheOccupancyMetricAvailable())
80                 {
81                     generateMetricFiles(pcm, "llc_occupancy", L3OCC);
82                 }
83                 if (pcm.CoreLocalMemoryBWMetricAvailable())
84                 {
85                     generateMetricFiles(pcm, "mbm_local_bytes", MBL);
86                 }
87                 if (pcm.CoreRemoteMemoryBWMetricAvailable())
88                 {
89                     generateMetricFiles(pcm, "mbm_total_bytes", MBT);
90                 }
91             }
92         }
93     }
cleanup()94     void Resctrl::cleanup()
95     {
96         const auto numCores = pcm.getNumCores();
97         for (unsigned int c = 0; c < numCores; ++c)
98         {
99             if (pcm.isCoreOnline(c))
100             {
101                 const auto dir = std::string(PCMPath) + std::to_string(c);
102                 rmdir(dir.c_str());
103                 const auto containerDir = std::string("/pcm") + dir;
104                 rmdir(containerDir.c_str());
105             }
106         }
107     }
getMetric(Resctrl::FileMapType & fileMap,int core)108     size_t Resctrl::getMetric(Resctrl::FileMapType & fileMap, int core)
109     {
110         auto files = fileMap[core];
111         size_t result = 0;
112         for (auto f : files)
113         {
114             const auto data = readSysFS(f.c_str(), false);
115             if (data.empty() == false)
116             {
117                 result += atoll(data.c_str());
118             }
119             else
120             {
121                 static std::mutex lock;
122                 std::lock_guard<std::mutex> _(lock);
123                 std::cerr << "Error reading " << f << ". Error: " << strerror(errno) << "\n";
124                 if (errno == 24)
125                 {
126                     std::cerr << "try executing 'ulimit -n 10000' to increase the limit on the number of open files.\n";
127                 }
128             }
129         }
130         return result;
131     }
getL3OCC(int core)132     size_t Resctrl::getL3OCC(int core)
133     {
134         return getMetric(L3OCC, core);
135     }
getMBL(int core)136     size_t Resctrl::getMBL(int core)
137     {
138         return getMetric(MBL, core);
139     }
getMBT(int core)140     size_t Resctrl::getMBT(int core)
141     {
142         return getMetric(MBT, core);
143     }
144 };
145 
146  #endif // __linux__