Skip to content

Commit 22c2f12

Browse files
committed
Developing...
1 parent 302edc3 commit 22c2f12

22 files changed

+1416
-373
lines changed

msrtool/Makefile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#
2+
# Makefile for MSRs
3+
#
4+
5+
CC = g++
6+
CFLAGS =-std=c++11 -Wall -g -O2 -fomit-frame-pointer -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64
7+
LDFLAGS =
8+
9+
BIN = msr_write msr_read
10+
11+
sbindir = /usr/sbin
12+
13+
all: $(BIN)
14+
15+
clean:
16+
rm -f *.o $(BIN)
17+
18+
distclean: clean
19+
rm -f *~ \#*
20+
21+
install: all
22+
install -m 755 $(BIN) $(sbindir)
23+
24+
.o:
25+
$(CC) $(LDFLAGS) -o $@ $<
26+
27+
.c.o:
28+
$(CC) $(CFLAGS) -o $@ $<
29+
30+
.c:
31+
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $<

msrtool/README

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
Fixing uncore frequency
3+
Uncore frequency can be set by writing the same value for both minimum and maximum
4+
frequency through MSR 0x620 on each socket.
5+
6+
In MSR 0x620, bits 0-7 indicate the maximum frequency (in units of 100Mhz),
7+
while bits 8-15 capture the minimum frequency.
8+
9+
For example, if you want to set the uncore frequency to 2700 MHz,
10+
you need to write the value 27 (0x1b in hex) for both max and min frequency.
11+
So, 0x1b1b would be written to MSR 0x620.
12+
13+
./wrmsr -a 0x620 0x1b1b
14+
./wrmsr -p0 0x620 0x1b1b
15+
16+
Set LLC cache ways
17+
Cache size is controlled by reducing the number of ways in each set.
18+
By default, we have 20 ways in each cache set.
19+
The following recipe is used to cut the # of ways down thus reducing the available total cache size.
20+
21+
22+
./rdmsr -a 0xc8f
23+
./rdmsr -a 0xc90
24+
25+
将Cache的默认20Way(0xfffff, 20bits setted)修改为15Way(0x7fff, 15bits setted)
26+
./wrmsr -a 0xc8f 0 1. Write 0 to msr 0xc8f on all cores
27+
./wrmsr -a 0xc90 0x7fff 2. Write <X> to msr 0xc90 on all the cores
28+
29+
a. The default value for <X> is 0xfffff (one bit set for each of the 20 ways of the set).
30+
By setting this value lower, you can disable cache ways and reduce the size.
31+
For example to reduce the cache size to 1/4 the size, the # of ways should be cut from 20 to 5.
32+
That would be writing 0x1F to msr 0xc90.
33+
b. The above write should be done only once and if additional sizes are needed, a reboot is recommended
34+
(where in you will set the new value after the reboot)
35+
c. Note that <X> value should have contiguous bits set i.e 0xFF is ok while 0xF0F is not ok
36+
37+

msrtool/msr_read.cpp

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
#ifndef _GNU_SOURCE
2+
#define _GNU_SOURCE
3+
#endif
4+
5+
#include <cstdio>
6+
#include <cstdlib>
7+
#include <cstring>
8+
#include <cctype>
9+
10+
#include <vector>
11+
#include <string>
12+
13+
#include <inttypes.h>
14+
#include <sys/types.h>
15+
16+
#include <unistd.h>
17+
#include <dirent.h>
18+
#include <getopt.h>
19+
#include <libgen.h>
20+
21+
#include <errno.h>
22+
#include <fcntl.h>
23+
#include <err.h>
24+
25+
#include "msr_version.hpp"
26+
27+
#define program "msr_read"
28+
29+
#define msr_warn(fmt, ...) fprintf(stderr, program ": " fmt, ##__VA_ARGS__)
30+
31+
namespace msr {
32+
33+
const struct option long_options[] = {
34+
{"help", no_argument, NULL, 'h'},
35+
{"version", no_argument, NULL, 'V'},
36+
{"all", no_argument, NULL, 'a'},
37+
{"processor", required_argument, NULL, 'p'},
38+
{"cpu", required_argument, NULL, 'p'},
39+
{NULL, no_argument, NULL, 0 }
40+
};
41+
42+
const char shot_options[] = "hVap:";
43+
44+
typedef struct {
45+
int cpu;
46+
uint32_t reg;
47+
} options_t;
48+
49+
options_t options;
50+
51+
52+
void usage()
53+
{
54+
fprintf(stderr,
55+
"Usage:\n"
56+
" %s [options] <register-id> <register-value> [...]\n"
57+
"Options:\n"
58+
" --help -h print this help\n"
59+
" --version -V print current version\n"
60+
" --all -a all processors\n"
61+
" --processor # -p select processor number (default 0)\n",
62+
program
63+
);
64+
}
65+
66+
void version()
67+
{
68+
fprintf(stderr, "%s: version %s\n", program, MSR_VERSION_STRING);
69+
}
70+
71+
int is_cpu(const struct dirent *dirp)
72+
{
73+
return std::isdigit(dirp->d_name[0]); /* /dev/cpu/<cpu> */
74+
}
75+
76+
bool is_onln(int cpu)
77+
{
78+
if (cpu < 0) {
79+
return false;
80+
}
81+
82+
// the boot cpu do *not* support hotplug (x86 platform, linux-2.6.x, linux-4.7.x)
83+
if (cpu == 0) {
84+
return true;
85+
}
86+
87+
// if online, the cache directory exist, otherwise not (x86 platform, linux-2.6.x, linux-4.7.x)
88+
std::string cpu_path = "/sys/devices/system/cpu/cpu" + std::to_string(cpu) + "/cache";
89+
90+
if (::access(cpu_path.c_str(), F_OK) != 0) {
91+
return false;
92+
}
93+
94+
return true;
95+
}
96+
97+
/**
98+
* msr_print - print value in the given format
99+
*
100+
* @val value to print
101+
*
102+
* FIXME:
103+
*
104+
*/
105+
void msr_print(uint64_t val)
106+
{
107+
FILE *fp = stdout;
108+
109+
fprintf(fp, "%zx\n", val);
110+
}
111+
112+
/**
113+
* msr_read - read values from @cpu's msr register specified by @reg
114+
*
115+
* @cpu which processor
116+
* @reg the register id
117+
*
118+
* Return:
119+
* true - read succ
120+
* false - read fail
121+
*/
122+
bool msr_read(int cpu, uint32_t reg)
123+
{
124+
if (!is_onln(cpu)) {
125+
msr_warn("cpu %2d does not online\n", cpu);
126+
return true;
127+
}
128+
129+
std::string msr_path = "/dev/cpu/" + std::to_string(cpu) + "/msr";
130+
131+
int fd = ::open(msr_path.c_str(), O_RDONLY);
132+
if (fd == -1) {
133+
switch(errno) {
134+
135+
// the file is a device special file and no corresponding device exists
136+
case ENXIO:
137+
msr_warn("cpu %2d does not exist\n", cpu);
138+
return false;
139+
140+
//
141+
case EIO:
142+
msr_warn("cpu %2d does not support MSR\n", cpu);
143+
return false;
144+
145+
default:
146+
msr_warn("failed on open MSR file for cpu %d, %s\n", cpu, strerror_r(errno, NULL, 0));
147+
return false;
148+
}
149+
}
150+
151+
uint64_t val = 0;
152+
153+
if (::pread(fd, &val, sizeof(val), reg) != sizeof(val)) {
154+
msr_warn("failed to read MSR file for cpu %d, %s\n", cpu, strerror_r(errno, NULL, 0));
155+
::close(fd);
156+
return false;
157+
}
158+
159+
::close(fd);
160+
161+
msr_print(val);
162+
163+
return true;
164+
}
165+
166+
167+
/**
168+
* msr_read - read values from all (online) cpus' msr register specified by @reg
169+
*
170+
* @reg the register id
171+
*
172+
* Return:
173+
* true - read succ
174+
* false - read fail
175+
*/
176+
bool msr_read(uint32_t reg)
177+
{
178+
struct dirent **namelist;
179+
size_t nr_dirent = 0;
180+
181+
nr_dirent = ::scandir("/dev/cpu", &namelist, is_cpu, 0);
182+
183+
for (size_t cpu = 0; cpu < nr_dirent; ++cpu) {
184+
msr_read(std::stoi(namelist[cpu]->d_name), reg);
185+
}
186+
187+
for (size_t cpu = 0; cpu < nr_dirent; ++cpu) {
188+
free(namelist[cpu]);
189+
}
190+
191+
free(namelist);
192+
193+
return true;
194+
}
195+
196+
} /* namespace msr */
197+
198+
199+
int main(int argc, char **argv)
200+
{
201+
char ch;
202+
while ((ch = getopt_long(argc, argv, msr::shot_options, msr::long_options, NULL)) != -1) {
203+
switch (ch) {
204+
case 'h':
205+
msr::usage();
206+
exit(EXIT_SUCCESS);
207+
208+
case 'V':
209+
msr::version();
210+
exit(EXIT_SUCCESS);
211+
212+
case 'a':
213+
msr::options.cpu = -1;
214+
break;
215+
216+
case 'p':
217+
msr::options.cpu = std::stoi(optarg);
218+
break;
219+
220+
default:
221+
msr::usage();
222+
exit(EXIT_FAILURE);
223+
}
224+
}
225+
226+
if (optind + 1 > argc) {
227+
msr::usage();
228+
exit(EXIT_FAILURE);
229+
}
230+
231+
if (geteuid() != 0) {
232+
msr_warn("you need root privilege to run\n");
233+
exit(EXIT_FAILURE);
234+
}
235+
236+
msr::options.reg = std::stoul(argv[optind++], 0, 16);
237+
238+
if (msr::options.cpu == -1) {
239+
msr::msr_read(msr::options.reg);
240+
} else {
241+
msr::msr_read(msr::options.cpu, msr::options.reg);
242+
}
243+
}

msrtool/msr_version.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef __MSR_VERSION_HPP__
2+
#define __MSR_VERSION_HPP__
3+
4+
#define MSR_VERSION_STRING "developing"
5+
6+
#endif /* __MSR_VERSION_HPP__ */

0 commit comments

Comments
 (0)