Test for_each_td()
[fio.git] / dedupe.c
CommitLineData
0d71aa98
BD
1#include "fio.h"
2
c49cfc76
BD
3/**
4 * initializes the global dedup workset.
5 * this needs to be called after all jobs' seeds
6 * have been initialized
7 */
8int init_global_dedupe_working_set_seeds(void)
0d71aa98 9{
c49cfc76 10 int i;
c49cfc76
BD
11
12 for_each_td(td, i) {
13 if (!td->o.dedupe_global)
14 continue;
15
16 if (init_dedupe_working_set_seeds(td, 1))
17 return 1;
18 }
19
20 return 0;
21}
22
23int init_dedupe_working_set_seeds(struct thread_data *td, bool global_dedup)
24{
25 int tindex;
26 struct thread_data *td_seed;
27 unsigned long long i, j, num_seed_advancements, pages_per_seed;
0d71aa98
BD
28 struct frand_state dedupe_working_set_state = {0};
29
30 if (!td->o.dedupe_percentage || !(td->o.dedupe_mode == DEDUPE_MODE_WORKING_SET))
31 return 0;
32
c49cfc76 33 tindex = td->thread_number - 1;
eb57e710
BD
34 num_seed_advancements = td->o.min_bs[DDIR_WRITE] /
35 min_not_zero(td->o.min_bs[DDIR_WRITE], (unsigned long long) td->o.compress_chunk);
0d71aa98
BD
36 /*
37 * The dedupe working set keeps seeds of unique data (generated by buf_state).
38 * Dedupe-ed pages will be generated using those seeds.
39 */
40 td->num_unique_pages = (td->o.size * (unsigned long long)td->o.dedupe_working_set_percentage / 100) / td->o.min_bs[DDIR_WRITE];
41 td->dedupe_working_set_states = malloc(sizeof(struct frand_state) * td->num_unique_pages);
42 if (!td->dedupe_working_set_states) {
43 log_err("fio: could not allocate dedupe working set\n");
44 return 1;
45 }
c49cfc76 46
0d71aa98 47 frand_copy(&dedupe_working_set_state, &td->buf_state);
c49cfc76
BD
48 frand_copy(&td->dedupe_working_set_states[0], &dedupe_working_set_state);
49 pages_per_seed = max(td->num_unique_pages / thread_number, 1ull);
50 for (i = 1; i < td->num_unique_pages; i++) {
eb57e710
BD
51 /*
52 * When compression is used the seed is advanced multiple times to
53 * generate the buffer. We want to regenerate the same buffer when
54 * deduping against this page
55 */
56 for (j = 0; j < num_seed_advancements; j++)
57 __get_next_seed(&dedupe_working_set_state);
c49cfc76
BD
58
59 /*
60 * When global dedup is used, we rotate the seeds to allow
61 * generating same buffers across different jobs. Deduplication buffers
62 * are spread evenly across jobs participating in global dedupe
63 */
64 if (global_dedup && i % pages_per_seed == 0) {
65 td_seed = tnumber_to_td(++tindex % thread_number);
66 frand_copy(&dedupe_working_set_state, &td_seed->buf_state);
67 }
68
69 frand_copy(&td->dedupe_working_set_states[i], &dedupe_working_set_state);
0d71aa98
BD
70 }
71
72 return 0;
73}