summaryrefslogtreecommitdiff
path: root/target.c
blob: bc153008b2569e9de5320d493269535cb701eb87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <unistd.h>

#include "fio.h"
#include "target.h"
#include "smalloc.h"
#include "stat.h"

void lat_fatal(struct thread_data *td, unsigned long long tnsec,
	       unsigned long long max_nsec)
{
	if (!td->error)
		log_err("fio: latency of %llu nsec exceeds specified max (%llu nsec)\n", tnsec, max_nsec);
	td_verror(td, ETIMEDOUT, "max latency exceeded");
}

static void lat_ios_note(struct thread_data *td)
{
	int i;

	for (i = 0; i < DDIR_RWDIR_CNT; i++)
		td->latency_ios[i] = td->io_blocks[i];
}

static void lat_new_cycle(struct thread_data *td)
{
	fio_gettime(&td->latency_ts, NULL);
	lat_ios_note(td);
	td->latency_failed = 0;
}

/*
 * We had an IO outside the latency target. Reduce the queue depth. If we
 * are at QD=1, then it's time to give up.
 */
static bool __lat_target_failed(struct thread_data *td)
{
	if (td->latency_qd == 1)
		return true;

	td->latency_qd_high = td->latency_qd;

	if (td->latency_qd == td->latency_qd_low)
		td->latency_qd_low--;

	td->latency_qd = (td->latency_qd + td->latency_qd_low) / 2;

	dprint(FD_RATE, "Ramped down: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);

	/*
	 * When we ramp QD down, quiesce existing IO to prevent
	 * a storm of ramp downs due to pending higher depth.
	 */
	io_u_quiesce(td);
	lat_new_cycle(td);
	return false;
}

bool lat_target_failed(struct thread_data *td)
{
	if (td->o.latency_percentile.u.f == 100.0)
		return __lat_target_failed(td);

	td->latency_failed++;
	return false;
}

static void lat_step_init(struct thread_data *td)
{
	struct thread_options *o = &td->o;

	fio_gettime(&td->latency_ts, NULL);
	td->latency_state = IOD_STATE_PROBE_RAMP;
	td->latency_step = 0;
	td->latency_qd = td->o.iodepth;
	dprint(FD_RATE, "Stepped: %d-%d/%d,%d/%d\n", o->lat_step_low,
				o->lat_step_high, o->lat_step_inc,
				o->lat_step_ramp, o->lat_step_run);
}

void lat_target_init(struct thread_data *td)
{
	td->latency_end_run = 0;

	if (td->o.latency_target) {
		dprint(FD_RATE, "Latency target=%llu\n", td->o.latency_target);
		fio_gettime(&td->latency_ts, NULL);
		td->latency_qd = 1;
		td->latency_qd_high = td->o.iodepth;
		td->latency_qd_low = 1;
		lat_ios_note(td);
	} else if (td->o.iodepth_mode == IOD_STEPPED)
		lat_step_init(td);
	else
		td->latency_qd = td->o.iodepth;
}

void lat_target_reset(struct thread_data *td)
{
	if (td->o.latency_target && !td->latency_end_run)
		lat_target_init(td);
}

static void lat_target_success(struct thread_data *td)
{
	const unsigned int qd = td->latency_qd;
	struct thread_options *o = &td->o;

	td->latency_qd_low = td->latency_qd;

	/*
	 * If we haven't failed yet, we double up to a failing value instead
	 * of bisecting from highest possible queue depth. If we have set
	 * a limit other than td->o.iodepth, bisect between that.
	 */
	if (td->latency_qd_high != o->iodepth)
		td->latency_qd = (td->latency_qd + td->latency_qd_high) / 2;
	else
		td->latency_qd *= 2;

	if (td->latency_qd > o->iodepth)
		td->latency_qd = o->iodepth;

	dprint(FD_RATE, "Ramped up: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);

	/*
	 * Same as last one, we are done. Let it run a latency cycle, so
	 * we get only the results from the targeted depth.
	 */
	if (td->latency_qd == qd) {
		if (td->latency_end_run) {
			dprint(FD_RATE, "We are done\n");
			td->done = 1;
		} else {
			dprint(FD_RATE, "Quiesce and final run\n");
			io_u_quiesce(td);
			td->latency_end_run = 1;
			reset_all_stats(td);
			reset_io_stats(td);
		}
	}

	lat_new_cycle(td);
}

void __lat_target_check(struct thread_data *td)
{
	uint64_t usec_window;
	uint64_t ios;
	double success_ios;

	usec_window = utime_since_now(&td->latency_ts);
	if (usec_window < td->o.latency_window)
		return;

	ios = ddir_rw_sum(td->io_blocks) - ddir_rw_sum(td->latency_ios);
	success_ios = (double) (ios - td->latency_failed) / (double) ios;
	success_ios *= 100.0;

	dprint(FD_RATE, "Success rate: %.2f%% (target %.2f%%)\n", success_ios, td->o.latency_percentile.u.f);

	if (success_ios >= td->o.latency_percentile.u.f)
		lat_target_success(td);
	else
		__lat_target_failed(td);
}

static void lat_clear_rate(struct thread_data *td)
{
	int i;

	td->flags &= ~TD_F_CHECK_RATE;
	for (i = 0; i < DDIR_RWDIR_CNT; i++)
		td->o.rate_iops[i] = 0;
}

/*
 * Returns true if we're done stepping
 */
static bool lat_step_recalc(struct thread_data *td)
{
	struct thread_options *o = &td->o;
	unsigned int cur, perc;

	cur = td->latency_step * o->lat_step_inc;
	if (cur >= o->lat_step_high)
		return true;

	perc = (td->latency_step + 1) * o->lat_step_inc;
	if (perc < 100) {
		int i;

		for (i = 0; i < DDIR_RWDIR_CNT; i++) {
			unsigned int this_iops;

			this_iops = (perc * td->latency_iops[i]) / 100;
			td->o.rate_iops[i] = this_iops;
		}
		setup_rate(td);
		td->flags |= TD_F_CHECK_RATE;
		td->latency_qd = td->o.iodepth * 100 / o->lat_step_high;
	} else {
		td->latency_qd = td->o.iodepth * perc / o->lat_step_high;
		lat_clear_rate(td);
	}
		
	dprint(FD_RATE, "Stepped: step=%d, perc=%d, qd=%d\n", td->latency_step,
						perc, td->latency_qd);
	return false;
}

static void lat_step_reset(struct thread_data *td)
{
	struct thread_stat *ts = &td->ts;
	struct io_stat *ios = &ts->clat_stat[DDIR_RWDIR_CNT];

	ios->max_val = ios->min_val = ios->samples = 0;
	ios->mean.u.f = ios->S.u.f = 0;

	lat_clear_rate(td);
	reset_all_stats(td);
	reset_io_stats(td);
}

static uint64_t lat_iops_since(struct thread_data *td, uint64_t msec,
			       enum fio_ddir ddir)
{
	if (msec) {
		uint64_t ios;

		ios = td->io_blocks[ddir] - td->latency_ios[ddir];
		return (ios * 1000) / msec;
	}

	return 0;
}

static void lat_step_add_sample(struct thread_data *td, uint64_t msec)
{
	struct thread_stat *ts = &td->ts;
	unsigned long long min, max;
	struct lat_step_stats *ls;
	double mean[DDIR_RWDIR_CNT], dev;
	int i;

	if (td->nr_lat_stats == ARRAY_SIZE(td->ts.step_stats)) {
		log_err("fio: ts->step_stats too small, dropping entries\n");
		return;
	}

	for (i = 0; i < DDIR_RWDIR_CNT; i++)
		calc_lat(&ts->clat_stat[i], &min, &max, &mean[i], &dev);

	for (i = 0; i < DDIR_RWDIR_CNT; i++) {
		ls = &td->ts.step_stats[td->nr_lat_stats];

		ls->iops[i] = lat_iops_since(td, msec, i);
		ls->avg[i].u.f = mean[i];
	}

	td->nr_lat_stats++;
}

bool __lat_ts_has_stats(struct thread_stat *ts, enum fio_ddir ddir)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(ts->step_stats); i++) {
		struct lat_step_stats *ls = &ts->step_stats[i];

		if (ls->iops[ddir])
			return true;
	}

	return false;
}

bool lat_ts_has_stats(struct thread_stat *ts)
{
	int i;

	for (i = 0; i < DDIR_RWDIR_CNT; i++)
		if (__lat_ts_has_stats(ts, i))
			return true;

	return false;
}

void lat_step_report(struct thread_stat *ts, struct buf_output *out)
{
	int i, j;

	for (i = 0; i < ARRAY_SIZE(ts->step_stats); i++) {
		struct lat_step_stats *ls = &ts->step_stats[i];

		for (j = 0; j < DDIR_RWDIR_CNT; j++) {
			if (!ls->iops[j])
				continue;

			if (!j)
				__log_buf(out, "    [%2d] ", i);
			else
				__log_buf(out, "         ");

			__log_buf(out, "%5s: iops=%llu, lat=%.1f nsec\n",
					io_ddir_name(j),
					(unsigned long long) ls->iops[j],
					ls->avg[j].u.f);
		}
	}
}

static void lat_next_state(struct thread_data *td, int new_state)
{
	td->latency_state = new_state;
	fio_gettime(&td->latency_ts, NULL);
}

bool lat_step_check(struct thread_data *td)
{
	struct thread_options *o = &td->o;
	uint64_t msec;

	msec = mtime_since_now(&td->latency_ts);

	switch (td->latency_state) {
	case IOD_STATE_PROBE_RAMP:
		if (msec < o->lat_step_ramp)
			break;

		lat_step_reset(td);
		lat_ios_note(td);

		lat_next_state(td, IOD_STATE_PROBE_RUN);
		break;
	case IOD_STATE_PROBE_RUN: {
		int i;

		if (msec < o->lat_step_run)
			break;

		io_u_quiesce(td);

		for (i = 0; i < DDIR_RWDIR_CNT; i++)
			td->latency_iops[i] = lat_iops_since(td, msec, i);

		lat_step_reset(td);
		lat_step_recalc(td);

		io_u_quiesce(td);
		lat_next_state(td, IOD_STATE_RAMP);
		break;
		}
	case IOD_STATE_RAMP:
		if (msec < o->lat_step_ramp)
			break;

		lat_ios_note(td);
		lat_next_state(td, IOD_STATE_RUN);
		break;
	case IOD_STATE_RUN:
		if (msec < o->lat_step_run)
			break;

		io_u_quiesce(td);
		fio_gettime(&td->latency_ts, NULL);
		td->latency_step++;

		lat_step_add_sample(td, msec);
		lat_step_reset(td);

		if (!lat_step_recalc(td))
			break;

		td->done = 1;
		lat_next_state(td, IOD_STATE_DONE);
		break;
	};

	return td->latency_state == IOD_STATE_DONE;
}