Merge branch 'dev' of https://github.com/smartxworks/fio
[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 #include "../optgroup.h"
10
11 struct cpu_options {
12         void *pad;
13         unsigned int cpuload;
14         unsigned int cpucycle;
15         unsigned int exit_io_done;
16 };
17
18 static 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",
25                 .category = FIO_OPT_C_ENGINE,
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,
37                 .category = FIO_OPT_C_ENGINE,
38                 .group  = FIO_OPT_G_INVALID,
39         },
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",
47                 .category = FIO_OPT_C_ENGINE,
48                 .group  = FIO_OPT_G_INVALID,
49         },
50         {
51                 .name   = NULL,
52         },
53 };
54
55
56 static enum fio_q_status fio_cpuio_queue(struct thread_data *td,
57                                          struct io_u fio_unused *io_u)
58 {
59         struct cpu_options *co = td->eo;
60
61         if (co->exit_io_done && !fio_running_or_pending_io_threads()) {
62                 td->done = 1;
63                 return FIO_Q_BUSY;
64         }
65
66         usec_spin(co->cpucycle);
67         return FIO_Q_COMPLETED;
68 }
69
70 static int fio_cpuio_init(struct thread_data *td)
71 {
72         struct thread_options *o = &td->o;
73         struct cpu_options *co = td->eo;
74
75         if (!co->cpuload) {
76                 td_vmsg(td, EINVAL, "cpu thread needs rate (cpuload=)","cpuio");
77                 return 1;
78         }
79
80         if (co->cpuload > 100)
81                 co->cpuload = 100;
82
83         /*
84          * set thinktime_sleep and thinktime_spin appropriately
85          */
86         o->thinktime_blocks = 1;
87         o->thinktime_spin = 0;
88         o->thinktime = ((unsigned long long) co->cpucycle * (100 - co->cpuload)) / co->cpuload;
89
90         o->nr_files = o->open_files = 1;
91
92         log_info("%s: ioengine=%s, cpuload=%u, cpucycle=%u\n",
93                 td->o.name, td->io_ops->name, co->cpuload, co->cpucycle);
94
95         return 0;
96 }
97
98 static int fio_cpuio_open(struct thread_data fio_unused *td,
99                           struct fio_file fio_unused *f)
100 {
101         return 0;
102 }
103
104 static struct ioengine_ops ioengine = {
105         .name           = "cpuio",
106         .version        = FIO_IOOPS_VERSION,
107         .queue          = fio_cpuio_queue,
108         .init           = fio_cpuio_init,
109         .open_file      = fio_cpuio_open,
110         .flags          = FIO_SYNCIO | FIO_DISKLESSIO | FIO_NOIO,
111         .options                = options,
112         .option_struct_size     = sizeof(struct cpu_options),
113 };
114
115 static void fio_init fio_cpuio_register(void)
116 {
117         register_ioengine(&ioengine);
118 }
119
120 static void fio_exit fio_cpuio_unregister(void)
121 {
122         unregister_ioengine(&ioengine);
123 }