Skip to content

Commit f412397

Browse files
ij-intelshuahkh
authored andcommitted
selftests/resctrl: Improve parameter consistency in fill_buf
fill_buf's arguments can be improved in multiple ways: - Multiple functions in fill_buf have start_ptr as one of their argument which is a bit long and the extra "start" is pretty obvious when it comes to pointers. - Some of the functions take end_ptr and others size_t to indicate the end of the buffer. - Some arguments meaning buffer size are called just 's' - mem_flush() takes void * but immediately converts it to char * Cleanup the parameters to make things simpler and more consistent: - Rename start_ptr to simply buf as it's shorter. - Replace end_ptr and s parameters with buf_size and only calculate end_ptr in the functions that truly use it. - Make mem_flush() parameters to follow the same convention as the other functions in fill_buf. - convert mem_flush() char * to unsigned char *. While at it, fix also a typo in a comment. Signed-off-by: Ilpo Järvinen <[email protected]> Reviewed-by: Reinette Chatre <[email protected]> Tested-by: Babu Moger <[email protected]> Tested-by: Shaopeng Tan (Fujitsu) <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 5e3e4f1 commit f412397

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

tools/testing/selftests/resctrl/fill_buf.c

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,32 @@ static void cl_flush(void *p)
3838
#endif
3939
}
4040

41-
static void mem_flush(void *p, size_t s)
41+
static void mem_flush(unsigned char *buf, size_t buf_size)
4242
{
43-
char *cp = (char *)p;
43+
unsigned char *cp = buf;
4444
size_t i = 0;
4545

46-
s = s / CL_SIZE; /* mem size in cache llines */
46+
buf_size = buf_size / CL_SIZE; /* mem size in cache lines */
4747

48-
for (i = 0; i < s; i++)
48+
for (i = 0; i < buf_size; i++)
4949
cl_flush(&cp[i * CL_SIZE]);
5050

5151
sb();
5252
}
5353

54-
static void *malloc_and_init_memory(size_t s)
54+
static void *malloc_and_init_memory(size_t buf_size)
5555
{
5656
void *p = NULL;
5757
uint64_t *p64;
5858
size_t s64;
5959
int ret;
6060

61-
ret = posix_memalign(&p, PAGE_SIZE, s);
61+
ret = posix_memalign(&p, PAGE_SIZE, buf_size);
6262
if (ret < 0)
6363
return NULL;
6464

6565
p64 = (uint64_t *)p;
66-
s64 = s / sizeof(uint64_t);
66+
s64 = buf_size / sizeof(uint64_t);
6767

6868
while (s64 > 0) {
6969
*p64 = (uint64_t)rand();
@@ -74,12 +74,13 @@ static void *malloc_and_init_memory(size_t s)
7474
return p;
7575
}
7676

77-
static int fill_one_span_read(unsigned char *start_ptr, unsigned char *end_ptr)
77+
static int fill_one_span_read(unsigned char *buf, size_t buf_size)
7878
{
79+
unsigned char *end_ptr = buf + buf_size;
7980
unsigned char sum, *p;
8081

8182
sum = 0;
82-
p = start_ptr;
83+
p = buf;
8384
while (p < end_ptr) {
8485
sum += *p;
8586
p += (CL_SIZE / 2);
@@ -88,26 +89,26 @@ static int fill_one_span_read(unsigned char *start_ptr, unsigned char *end_ptr)
8889
return sum;
8990
}
9091

91-
static
92-
void fill_one_span_write(unsigned char *start_ptr, unsigned char *end_ptr)
92+
static void fill_one_span_write(unsigned char *buf, size_t buf_size)
9393
{
94+
unsigned char *end_ptr = buf + buf_size;
9495
unsigned char *p;
9596

96-
p = start_ptr;
97+
p = buf;
9798
while (p < end_ptr) {
9899
*p = '1';
99100
p += (CL_SIZE / 2);
100101
}
101102
}
102103

103-
static int fill_cache_read(unsigned char *start_ptr, unsigned char *end_ptr,
104+
static int fill_cache_read(unsigned char *buf, size_t buf_size,
104105
char *resctrl_val)
105106
{
106107
int ret = 0;
107108
FILE *fp;
108109

109110
while (1) {
110-
ret = fill_one_span_read(start_ptr, end_ptr);
111+
ret = fill_one_span_read(buf, buf_size);
111112
if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)))
112113
break;
113114
}
@@ -124,11 +125,11 @@ static int fill_cache_read(unsigned char *start_ptr, unsigned char *end_ptr,
124125
return 0;
125126
}
126127

127-
static int fill_cache_write(unsigned char *start_ptr, unsigned char *end_ptr,
128+
static int fill_cache_write(unsigned char *buf, size_t buf_size,
128129
char *resctrl_val)
129130
{
130131
while (1) {
131-
fill_one_span_write(start_ptr, end_ptr);
132+
fill_one_span_write(buf, buf_size);
132133
if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)))
133134
break;
134135
}
@@ -138,25 +139,23 @@ static int fill_cache_write(unsigned char *start_ptr, unsigned char *end_ptr,
138139

139140
static int fill_cache(size_t buf_size, int memflush, int op, char *resctrl_val)
140141
{
141-
unsigned char *start_ptr, *end_ptr;
142+
unsigned char *buf;
142143
int ret;
143144

144-
start_ptr = malloc_and_init_memory(buf_size);
145-
if (!start_ptr)
145+
buf = malloc_and_init_memory(buf_size);
146+
if (!buf)
146147
return -1;
147148

148-
end_ptr = start_ptr + buf_size;
149-
150149
/* Flush the memory before using to avoid "cache hot pages" effect */
151150
if (memflush)
152-
mem_flush(start_ptr, buf_size);
151+
mem_flush(buf, buf_size);
153152

154153
if (op == 0)
155-
ret = fill_cache_read(start_ptr, end_ptr, resctrl_val);
154+
ret = fill_cache_read(buf, buf_size, resctrl_val);
156155
else
157-
ret = fill_cache_write(start_ptr, end_ptr, resctrl_val);
156+
ret = fill_cache_write(buf, buf_size, resctrl_val);
158157

159-
free(start_ptr);
158+
free(buf);
160159

161160
if (ret) {
162161
printf("\n Error in fill cache read/write...\n");

0 commit comments

Comments
 (0)