Merge branch 'multi_job_flow' of https://github.com/bardavid/fio into master
[fio.git] / flow.c
1 #include "fio.h"
2 #include "fio_sem.h"
3 #include "smalloc.h"
4 #include "flist.h"
5
6 struct fio_flow {
7         unsigned int refs;
8         struct flist_head list;
9         unsigned int id;
10         unsigned long long flow_counter;
11         unsigned int total_weight;
12 };
13
14 static struct flist_head *flow_list;
15 static struct fio_sem *flow_lock;
16
17 int flow_threshold_exceeded(struct thread_data *td)
18 {
19         struct fio_flow *flow = td->flow;
20         double flow_counter_ratio, flow_weight_ratio;
21
22         if (!flow)
23                 return 0;
24
25         flow_counter_ratio = (double)td->flow_counter /
26                 atomic_load_relaxed(&flow->flow_counter);
27         flow_weight_ratio = (double)td->o.flow /
28                 atomic_load_relaxed(&flow->total_weight);
29
30         /*
31          * each thread/process executing a fio job will stall based on the
32          * expected  user ratio for a given flow_id group. the idea is to keep
33          * 2 counters, flow and job-specific counter to test if the
34          * ratio between them is proportional to other jobs in the same flow_id
35          */
36         if (flow_counter_ratio > flow_weight_ratio) {
37                 if (td->o.flow_sleep) {
38                         io_u_quiesce(td);
39                         usleep(td->o.flow_sleep);
40                 }
41
42                 return 1;
43         }
44
45         /*
46          * increment flow(shared counter, therefore atomically)
47          * and job-specific counter
48          */
49         atomic_add(&flow->flow_counter, 1);
50         ++td->flow_counter;
51
52         return 0;
53 }
54
55 static struct fio_flow *flow_get(unsigned int id)
56 {
57         struct fio_flow *flow = NULL;
58         struct flist_head *n;
59
60         if (!flow_lock)
61                 return NULL;
62
63         fio_sem_down(flow_lock);
64
65         flist_for_each(n, flow_list) {
66                 flow = flist_entry(n, struct fio_flow, list);
67                 if (flow->id == id)
68                         break;
69
70                 flow = NULL;
71         }
72
73         if (!flow) {
74                 flow = smalloc(sizeof(*flow));
75                 if (!flow) {
76                         fio_sem_up(flow_lock);
77                         return NULL;
78                 }
79                 flow->refs = 0;
80                 INIT_FLIST_HEAD(&flow->list);
81                 flow->id = id;
82                 flow->flow_counter = 1;
83                 flow->total_weight = 0;
84
85                 flist_add_tail(&flow->list, flow_list);
86         }
87
88         flow->refs++;
89         fio_sem_up(flow_lock);
90         return flow;
91 }
92
93 static void flow_put(struct fio_flow *flow, unsigned long long flow_counter,
94                                         unsigned int weight)
95 {
96         if (!flow_lock)
97                 return;
98
99         fio_sem_down(flow_lock);
100
101         atomic_sub(&flow->flow_counter, flow_counter);
102         atomic_sub(&flow->total_weight, weight);
103
104         if (!--flow->refs) {
105                 assert(flow->flow_counter == 1);
106                 flist_del(&flow->list);
107                 sfree(flow);
108         }
109
110         fio_sem_up(flow_lock);
111 }
112
113 void flow_init_job(struct thread_data *td)
114 {
115         if (td->o.flow) {
116                 td->flow = flow_get(td->o.flow_id);
117                 td->flow_counter = 0;
118                 atomic_add(&td->flow->total_weight, td->o.flow);
119         }
120 }
121
122 void flow_exit_job(struct thread_data *td)
123 {
124         if (td->flow) {
125                 flow_put(td->flow, td->flow_counter, td->o.flow);
126                 td->flow = NULL;
127         }
128 }
129
130 void flow_init(void)
131 {
132         flow_list = smalloc(sizeof(*flow_list));
133         if (!flow_list) {
134                 log_err("fio: smalloc pool exhausted\n");
135                 return;
136         }
137
138         flow_lock = fio_sem_init(FIO_SEM_UNLOCKED);
139         if (!flow_lock) {
140                 log_err("fio: failed to allocate flow lock\n");
141                 sfree(flow_list);
142                 return;
143         }
144
145         INIT_FLIST_HEAD(flow_list);
146 }
147
148 void flow_exit(void)
149 {
150         if (flow_lock)
151                 fio_sem_remove(flow_lock);
152         if (flow_list)
153                 sfree(flow_list);
154 }