Skip to content

Commit d42971d

Browse files
committed
Refine dudect
The consistency of the dudect source files is revised to adhere to the coding style.
1 parent bc44ede commit d42971d

File tree

6 files changed

+60
-59
lines changed

6 files changed

+60
-59
lines changed

dudect/constant.c

+19-32
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,18 @@
1-
#include "constant.h"
21
#include <assert.h>
3-
#include <signal.h>
42
#include <stdint.h>
5-
#include <stdio.h>
6-
#include <stdlib.h>
73
#include <string.h>
8-
#include <unistd.h>
4+
5+
#include "constant.h"
96
#include "cpucycles.h"
107
#include "queue.h"
118
#include "random.h"
129

13-
#define N_MEASURE 150
14-
15-
/* Allow random number range from 0 to 65535 */
16-
const size_t chunk_size = 16;
17-
18-
/* Number of measurements per test */
19-
const size_t n_measure = N_MEASURE;
20-
21-
const int drop_size = 20;
22-
2310
/* Maintain a queue independent from the qtest since
2411
* we do not want the test to affect the original functionality
2512
*/
2613
static struct list_head *l = NULL;
2714

28-
static char random_string[N_MEASURE][8];
15+
static char random_string[N_MEASURES][8];
2916
static int random_string_iter = 0;
3017

3118
enum {
@@ -41,22 +28,22 @@ void init_dut(void)
4128
l = NULL;
4229
}
4330

44-
char *get_random_string(void)
31+
static char *get_random_string(void)
4532
{
46-
random_string_iter = (random_string_iter + 1) % N_MEASURE;
33+
random_string_iter = (random_string_iter + 1) % N_MEASURES;
4734
return random_string[random_string_iter];
4835
}
4936

5037
void prepare_inputs(uint8_t *input_data, uint8_t *classes)
5138
{
52-
randombytes(input_data, n_measure * chunk_size);
53-
for (size_t i = 0; i < n_measure; i++) {
39+
randombytes(input_data, N_MEASURES * CHUNK_SIZE);
40+
for (size_t i = 0; i < N_MEASURES; i++) {
5441
classes[i] = randombit();
5542
if (classes[i] == 0)
56-
memset(input_data + (size_t) i * chunk_size, 0, chunk_size);
43+
memset(input_data + (size_t) i * CHUNK_SIZE, 0, CHUNK_SIZE);
5744
}
5845

59-
for (size_t i = 0; i < N_MEASURE; ++i) {
46+
for (size_t i = 0; i < N_MEASURES; ++i) {
6047
/* Generate random string */
6148
randombytes((uint8_t *) random_string[i], 7);
6249
random_string[i][7] = 0;
@@ -73,37 +60,37 @@ void measure(int64_t *before_ticks,
7360

7461
switch (mode) {
7562
case test_insert_head:
76-
for (size_t i = drop_size; i < n_measure - drop_size; i++) {
63+
for (size_t i = DROP_SIZE; i < N_MEASURES - DROP_SIZE; i++) {
7764
char *s = get_random_string();
7865
dut_new();
7966
dut_insert_head(
8067
get_random_string(),
81-
*(uint16_t *) (input_data + i * chunk_size) % 10000);
68+
*(uint16_t *) (input_data + i * CHUNK_SIZE) % 10000);
8269
before_ticks[i] = cpucycles();
8370
dut_insert_head(s, 1);
8471
after_ticks[i] = cpucycles();
8572
dut_free();
8673
}
8774
break;
8875
case test_insert_tail:
89-
for (size_t i = drop_size; i < n_measure - drop_size; i++) {
76+
for (size_t i = DROP_SIZE; i < N_MEASURES - DROP_SIZE; i++) {
9077
char *s = get_random_string();
9178
dut_new();
9279
dut_insert_head(
9380
get_random_string(),
94-
*(uint16_t *) (input_data + i * chunk_size) % 10000);
81+
*(uint16_t *) (input_data + i * CHUNK_SIZE) % 10000);
9582
before_ticks[i] = cpucycles();
9683
dut_insert_tail(s, 1);
9784
after_ticks[i] = cpucycles();
9885
dut_free();
9986
}
10087
break;
10188
case test_remove_head:
102-
for (size_t i = drop_size; i < n_measure - drop_size; i++) {
89+
for (size_t i = DROP_SIZE; i < N_MEASURES - DROP_SIZE; i++) {
10390
dut_new();
10491
dut_insert_head(
10592
get_random_string(),
106-
*(uint16_t *) (input_data + i * chunk_size) % 10000);
93+
*(uint16_t *) (input_data + i * CHUNK_SIZE) % 10000);
10794
before_ticks[i] = cpucycles();
10895
element_t *e = q_remove_head(l, NULL, 0);
10996
after_ticks[i] = cpucycles();
@@ -113,11 +100,11 @@ void measure(int64_t *before_ticks,
113100
}
114101
break;
115102
case test_remove_tail:
116-
for (size_t i = drop_size; i < n_measure - drop_size; i++) {
103+
for (size_t i = DROP_SIZE; i < N_MEASURES - DROP_SIZE; i++) {
117104
dut_new();
118105
dut_insert_head(
119106
get_random_string(),
120-
*(uint16_t *) (input_data + i * chunk_size) % 10000);
107+
*(uint16_t *) (input_data + i * CHUNK_SIZE) % 10000);
121108
before_ticks[i] = cpucycles();
122109
element_t *e = q_remove_tail(l, NULL, 0);
123110
after_ticks[i] = cpucycles();
@@ -127,11 +114,11 @@ void measure(int64_t *before_ticks,
127114
}
128115
break;
129116
default:
130-
for (size_t i = drop_size; i < n_measure - drop_size; i++) {
117+
for (size_t i = DROP_SIZE; i < N_MEASURES - DROP_SIZE; i++) {
131118
dut_new();
132119
dut_insert_head(
133120
get_random_string(),
134-
*(uint16_t *) (input_data + i * chunk_size) % 10000);
121+
*(uint16_t *) (input_data + i * CHUNK_SIZE) % 10000);
135122
before_ticks[i] = cpucycles();
136123
dut_size(1);
137124
after_ticks[i] = cpucycles();

dudect/constant.h

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
#define DUDECT_CONSTANT_H
33

44
#include <stdint.h>
5+
6+
/* Number of measurements per test */
7+
#define N_MEASURES 150
8+
9+
/* Allow random number range from 0 to 65535 */
10+
#define CHUNK_SIZE 16
11+
12+
#define DROP_SIZE 20
13+
514
#define dut_new() ((void) (l = q_new()))
615

716
#define dut_size(n) \

dudect/cpucycles.h

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
#ifndef DUDECT_CPUCYCLES_H
2+
#define DUDECT_CPUCYCLES_H
3+
14
#include <stdint.h>
5+
26
// http://www.intel.com/content/www/us/en/embedded/training/ia-32-ia-64-benchmark-code-execution-paper.html
37
static inline int64_t cpucycles(void)
48
{
@@ -21,3 +25,5 @@ static inline int64_t cpucycles(void)
2125
#error Unsupported Architecture
2226
#endif
2327
}
28+
29+
#endif

dudect/fixture.c

+23-24
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,23 @@
2626
* variable time.
2727
*/
2828

29-
#include "fixture.h"
3029
#include <assert.h>
3130
#include <math.h>
3231
#include <stdint.h>
3332
#include <stdio.h>
3433
#include <stdlib.h>
3534
#include <string.h>
35+
3636
#include "../console.h"
3737
#include "../random.h"
38+
3839
#include "constant.h"
40+
#include "fixture.h"
3941
#include "ttest.h"
4042

41-
#define enough_measure 10000
42-
#define test_tries 10
43+
#define ENOUGH_MEASURE 10000
44+
#define TEST_TRIES 10
4345

44-
extern const int drop_size;
45-
extern const size_t chunk_size;
46-
extern const size_t n_measure;
4746
static t_context_t *t;
4847

4948
/* threshold values for Welch's t-test */
@@ -61,13 +60,13 @@ static void differentiate(int64_t *exec_times,
6160
const int64_t *before_ticks,
6261
const int64_t *after_ticks)
6362
{
64-
for (size_t i = 0; i < n_measure; i++)
63+
for (size_t i = 0; i < N_MEASURES; i++)
6564
exec_times[i] = after_ticks[i] - before_ticks[i];
6665
}
6766

6867
static void update_statistics(const int64_t *exec_times, uint8_t *classes)
6968
{
70-
for (size_t i = 0; i < n_measure; i++) {
69+
for (size_t i = 0; i < N_MEASURES; i++) {
7170
int64_t difference = exec_times[i];
7271
/* CPU cycle counter overflowed or dropped measurement */
7372
if (difference <= 0)
@@ -86,9 +85,9 @@ static bool report(void)
8685

8786
printf("\033[A\033[2K");
8887
printf("meas: %7.2lf M, ", (number_traces_max_t / 1e6));
89-
if (number_traces_max_t < enough_measure) {
88+
if (number_traces_max_t < ENOUGH_MEASURE) {
9089
printf("not enough measurements (%.0f still to go).\n",
91-
enough_measure - number_traces_max_t);
90+
ENOUGH_MEASURE - number_traces_max_t);
9291
return false;
9392
}
9493

@@ -119,11 +118,11 @@ static bool report(void)
119118

120119
static bool doit(int mode)
121120
{
122-
int64_t *before_ticks = calloc(n_measure + 1, sizeof(int64_t));
123-
int64_t *after_ticks = calloc(n_measure + 1, sizeof(int64_t));
124-
int64_t *exec_times = calloc(n_measure, sizeof(int64_t));
125-
uint8_t *classes = calloc(n_measure, sizeof(uint8_t));
126-
uint8_t *input_data = calloc(n_measure * chunk_size, sizeof(uint8_t));
121+
int64_t *before_ticks = calloc(N_MEASURES + 1, sizeof(int64_t));
122+
int64_t *after_ticks = calloc(N_MEASURES + 1, sizeof(int64_t));
123+
int64_t *exec_times = calloc(N_MEASURES, sizeof(int64_t));
124+
uint8_t *classes = calloc(N_MEASURES, sizeof(uint8_t));
125+
uint8_t *input_data = calloc(N_MEASURES * CHUNK_SIZE, sizeof(uint8_t));
127126

128127
if (!before_ticks || !after_ticks || !exec_times || !classes ||
129128
!input_data) {
@@ -152,19 +151,19 @@ static void init_once(void)
152151
t_init(t);
153152
}
154153

155-
static bool TEST_CONST(char *text, int mode)
154+
static bool test_const(char *text, int mode)
156155
{
157156
bool result = false;
158157
t = malloc(sizeof(t_context_t));
159158

160-
for (int cnt = 0; cnt < test_tries; ++cnt) {
161-
printf("Testing %s...(%d/%d)\n\n", text, cnt, test_tries);
159+
for (int cnt = 0; cnt < TEST_TRIES; ++cnt) {
160+
printf("Testing %s...(%d/%d)\n\n", text, cnt, TEST_TRIES);
162161
init_once();
163-
for (int i = 0; i < enough_measure / (n_measure - drop_size * 2) + 1;
162+
for (int i = 0; i < ENOUGH_MEASURE / (N_MEASURES - DROP_SIZE * 2) + 1;
164163
++i)
165164
result = doit(mode);
166165
printf("\033[A\033[2K\033[A\033[2K");
167-
if (result == true)
166+
if (result)
168167
break;
169168
}
170169
free(t);
@@ -173,20 +172,20 @@ static bool TEST_CONST(char *text, int mode)
173172

174173
bool is_insert_head_const(void)
175174
{
176-
return TEST_CONST("insert_head", 0);
175+
return test_const("insert_head", 0);
177176
}
178177

179178
bool is_insert_tail_const(void)
180179
{
181-
return TEST_CONST("insert_tail", 1);
180+
return test_const("insert_tail", 1);
182181
}
183182

184183
bool is_remove_head_const(void)
185184
{
186-
return TEST_CONST("remove_head", 2);
185+
return test_const("remove_head", 2);
187186
}
188187

189188
bool is_remove_tail_const(void)
190189
{
191-
return TEST_CONST("remove_tail", 3);
190+
return test_const("remove_tail", 3);
192191
}

dudect/ttest.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
* See https://en.wikipedia.org/wiki/Welch%27s_t-test
99
*/
1010

11-
#include "ttest.h"
1211
#include <assert.h>
1312
#include <math.h>
1413
#include <stdint.h>
15-
#include <stdio.h>
16-
#include <stdlib.h>
14+
15+
#include "ttest.h"
1716

1817
void t_push(t_context_t *ctx, double x, uint8_t class)
1918
{

dudect/ttest.h

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define DUDECT_TTEST_H
33

44
#include <stdint.h>
5+
56
typedef struct {
67
double mean[2];
78
double m2[2];

0 commit comments

Comments
 (0)