Merge branch 'master' into gfio
[fio.git] / engines / cpu.c
1 /*
2  * CPU engine
3  *
4  * Doesn't transfer any data, merely burns CPU cycles according to
5  * the settings.
6  *
7  */
8 #include "../fio.h"
9
10 struct cpu_options {
11         struct thread_data *td;
12         unsigned int cpuload;
13         unsigned int cpucycle;
14 };
15
16 static struct fio_option options[] = {
17         {
18                 .name   = "cpuload",
19                 .lname  = "CPU load",
20                 .type   = FIO_OPT_INT,
21                 .off1   = offsetof(struct cpu_options, cpuload),
22                 .help   = "Use this percentage of CPU",
23                 .category = FIO_OPT_C_GENERAL,
24                 .group  = FIO_OPT_G_INVALID,
25         },
26         {
27                 .name   = "cpuchunks",
28                 .lname  = "CPU chunk",
29                 .type   = FIO_OPT_INT,
30                 .off1   = offsetof(struct cpu_options, cpucycle),
31                 .help   = "Length of the CPU burn cycles (usecs)",
32                 .def    = "50000",
33                 .parent = "cpuload",
34                 .hide   = 1,
35                 .category = FIO_OPT_C_GENERAL,
36                 .group  = FIO_OPT_G_INVALID,
37         },
38         {
39                 .name   = NULL,
40         },
41 };
42
43
44 static int fio_cpuio_queue(struct thread_data *td, struct io_u fio_unused *io_u)
45 {
46         struct cpu_options *co = td->eo;
47
48         usec_spin(co->cpucycle);
49         return FIO_Q_COMPLETED;
50 }
51
52 static int fio_cpuio_init(struct thread_data *td)
53 {
54         struct thread_options *o = &td->o;
55         struct cpu_options *co = td->eo;
56
57         if (!co->cpuload) {
58                 td_vmsg(td, EINVAL, "cpu thread needs rate (cpuload=)","cpuio");
59                 return 1;
60         }
61
62         if (co->cpuload > 100)
63                 co->cpuload = 100;
64
65         /*
66          * set thinktime_sleep and thinktime_spin appropriately
67          */
68         o->thinktime_blocks = 1;
69         o->thinktime_spin = 0;
70         o->thinktime = (co->cpucycle * (100 - co->cpuload)) / co->cpuload;
71
72         o->nr_files = o->open_files = 1;
73
74         log_info("%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->o.name,
75                                                 co->cpuload, co->cpucycle);
76
77         return 0;
78 }
79
80 static int fio_cpuio_open(struct thread_data fio_unused *td,
81                           struct fio_file fio_unused *f)
82 {
83         return 0;
84 }
85
86 static struct ioengine_ops ioengine = {
87         .name           = "cpuio",
88         .version        = FIO_IOOPS_VERSION,
89         .queue          = fio_cpuio_queue,
90         .init           = fio_cpuio_init,
91         .open_file      = fio_cpuio_open,
92         .flags          = FIO_SYNCIO | FIO_DISKLESSIO | FIO_NOIO,
93         .options                = options,
94         .option_struct_size     = sizeof(struct cpu_options),
95 };
96
97 static void fio_init fio_cpuio_register(void)
98 {
99         register_ioengine(&ioengine);
100 }
101
102 static void fio_exit fio_cpuio_unregister(void)
103 {
104         unregister_ioengine(&ioengine);
105 }