engines/cpu: fix potential overflow in thinktime calculation
[fio.git] / engines / cpu.c
CommitLineData
da751ca9
JA
1/*
2 * CPU engine
3 *
4 * Doesn't transfer any data, merely burns CPU cycles according to
5 * the settings.
6 *
7 */
5f350952 8#include "../fio.h"
d220c761 9#include "../optgroup.h"
2866c82d 10
0353050f 11struct cpu_options {
a1f871c7 12 void *pad;
0353050f
JA
13 unsigned int cpuload;
14 unsigned int cpucycle;
046395d7 15 unsigned int exit_io_done;
0353050f
JA
16};
17
18static struct fio_option options[] = {
19 {
20 .name = "cpuload",
21 .lname = "CPU load",
22 .type = FIO_OPT_INT,
23 .off1 = offsetof(struct cpu_options, cpuload),
24 .help = "Use this percentage of CPU",
cbd9148f 25 .category = FIO_OPT_C_ENGINE,
0353050f
JA
26 .group = FIO_OPT_G_INVALID,
27 },
28 {
29 .name = "cpuchunks",
30 .lname = "CPU chunk",
31 .type = FIO_OPT_INT,
32 .off1 = offsetof(struct cpu_options, cpucycle),
33 .help = "Length of the CPU burn cycles (usecs)",
34 .def = "50000",
35 .parent = "cpuload",
36 .hide = 1,
cbd9148f 37 .category = FIO_OPT_C_ENGINE,
0353050f
JA
38 .group = FIO_OPT_G_INVALID,
39 },
046395d7
JA
40 {
41 .name = "exit_on_io_done",
42 .lname = "Exit when IO threads are done",
43 .type = FIO_OPT_BOOL,
44 .off1 = offsetof(struct cpu_options, exit_io_done),
45 .help = "Exit when IO threads finish",
46 .def = "0",
cbd9148f 47 .category = FIO_OPT_C_ENGINE,
046395d7
JA
48 .group = FIO_OPT_G_INVALID,
49 },
0353050f
JA
50 {
51 .name = NULL,
52 },
53};
54
55
2e4ef4fb
JA
56static enum fio_q_status fio_cpuio_queue(struct thread_data *td,
57 struct io_u fio_unused *io_u)
ba0fbe10 58{
0353050f
JA
59 struct cpu_options *co = td->eo;
60
046395d7
JA
61 if (co->exit_io_done && !fio_running_or_pending_io_threads()) {
62 td->done = 1;
63 return FIO_Q_BUSY;
64 }
65
0353050f 66 usec_spin(co->cpucycle);
ba0fbe10
JA
67 return FIO_Q_COMPLETED;
68}
69
2866c82d
JA
70static int fio_cpuio_init(struct thread_data *td)
71{
2dc1bbeb 72 struct thread_options *o = &td->o;
0353050f 73 struct cpu_options *co = td->eo;
2dc1bbeb 74
0353050f 75 if (!co->cpuload) {
ba0fbe10 76 td_vmsg(td, EINVAL, "cpu thread needs rate (cpuload=)","cpuio");
2866c82d 77 return 1;
ba0fbe10
JA
78 }
79
0353050f
JA
80 if (co->cpuload > 100)
81 co->cpuload = 100;
2866c82d 82
ba0fbe10
JA
83 /*
84 * set thinktime_sleep and thinktime_spin appropriately
85 */
2dc1bbeb
JA
86 o->thinktime_blocks = 1;
87 o->thinktime_spin = 0;
2bce52c6 88 o->thinktime = ((unsigned long long) co->cpucycle * (100 - co->cpuload)) / co->cpuload;
2866c82d 89
2dc1bbeb 90 o->nr_files = o->open_files = 1;
0353050f 91
2403767a
TK
92 log_info("%s: ioengine=%s, cpuload=%u, cpucycle=%u\n",
93 td->o.name, td->io_ops->name, co->cpuload, co->cpucycle);
0353050f 94
ba0fbe10
JA
95 return 0;
96}
97
f4e62a5f
JA
98static int fio_cpuio_open(struct thread_data fio_unused *td,
99 struct fio_file fio_unused *f)
ba0fbe10 100{
2866c82d
JA
101 return 0;
102}
103
5f350952 104static struct ioengine_ops ioengine = {
2866c82d
JA
105 .name = "cpuio",
106 .version = FIO_IOOPS_VERSION,
ba0fbe10 107 .queue = fio_cpuio_queue,
2866c82d 108 .init = fio_cpuio_init,
ba0fbe10 109 .open_file = fio_cpuio_open,
1f809d15 110 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_NOIO,
0353050f
JA
111 .options = options,
112 .option_struct_size = sizeof(struct cpu_options),
2866c82d 113};
5f350952
JA
114
115static void fio_init fio_cpuio_register(void)
116{
117 register_ioengine(&ioengine);
118}
119
120static void fio_exit fio_cpuio_unregister(void)
121{
122 unregister_ioengine(&ioengine);
123}