Add support for idletime profiling
[fio.git] / os / os-linux.h
1 #ifndef FIO_OS_LINUX_H
2 #define FIO_OS_LINUX_H
3
4 #define FIO_OS  os_linux
5
6 #include <sys/ioctl.h>
7 #include <sys/uio.h>
8 #include <sys/syscall.h>
9 #include <sys/vfs.h>
10 #include <sys/mman.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <errno.h>
14 #include <sched.h>
15 #include <linux/unistd.h>
16 #include <linux/raw.h>
17 #include <linux/major.h>
18
19 #include "binject.h"
20 #include "../file.h"
21
22 #define FIO_HAVE_CPU_AFFINITY
23 #define FIO_HAVE_DISK_UTIL
24 #define FIO_HAVE_SGIO
25 #define FIO_HAVE_IOPRIO
26 #define FIO_HAVE_IOSCHED_SWITCH
27 #define FIO_HAVE_ODIRECT
28 #define FIO_HAVE_HUGETLB
29 #define FIO_HAVE_RAWBIND
30 #define FIO_HAVE_BLKTRACE
31 #define FIO_HAVE_PSHARED_MUTEX
32 #define FIO_HAVE_CL_SIZE
33 #define FIO_HAVE_CGROUPS
34 #define FIO_HAVE_FS_STAT
35 #define FIO_HAVE_TRIM
36 #define FIO_HAVE_BINJECT
37 #define FIO_HAVE_GETTID
38 #define FIO_HAVE_SCHED_IDLE
39 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
40
41 #ifdef MAP_HUGETLB
42 #define FIO_HAVE_MMAP_HUGE
43 #endif
44
45 #define OS_MAP_ANON             MAP_ANONYMOUS
46
47 typedef cpu_set_t os_cpu_mask_t;
48
49 typedef struct drand48_data os_random_state_t;
50
51 #ifdef CONFIG_3ARG_AFFINITY
52 #define fio_setaffinity(pid, cpumask)           \
53         sched_setaffinity((pid), sizeof(cpumask), &(cpumask))
54 #define fio_getaffinity(pid, ptr)       \
55         sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
56 #elif defined(CONFIG_2ARG_AFFINITY)
57 #define fio_setaffinity(pid, cpumask)   \
58         sched_setaffinity((pid), &(cpumask))
59 #define fio_getaffinity(pid, ptr)       \
60         sched_getaffinity((pid), (ptr))
61 #endif
62
63 #define fio_cpu_clear(mask, cpu)        (void) CPU_CLR((cpu), (mask))
64 #define fio_cpu_set(mask, cpu)          (void) CPU_SET((cpu), (mask))
65
66 static inline int fio_cpuset_init(os_cpu_mask_t *mask)
67 {
68         CPU_ZERO(mask);
69         return 0;
70 }
71
72 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
73 {
74         return 0;
75 }
76
77 #define FIO_MAX_CPUS                    CPU_SETSIZE
78
79 static inline int ioprio_set(int which, int who, int ioprio)
80 {
81         return syscall(__NR_ioprio_set, which, who, ioprio);
82 }
83
84 static inline int gettid(void)
85 {
86         return syscall(__NR_gettid);
87 }
88
89 #define SPLICE_DEF_SIZE (64*1024)
90
91 enum {
92         IOPRIO_CLASS_NONE,
93         IOPRIO_CLASS_RT,
94         IOPRIO_CLASS_BE,
95         IOPRIO_CLASS_IDLE,
96 };
97
98 enum {
99         IOPRIO_WHO_PROCESS = 1,
100         IOPRIO_WHO_PGRP,
101         IOPRIO_WHO_USER,
102 };
103
104 #define IOPRIO_BITS             16
105 #define IOPRIO_CLASS_SHIFT      13
106
107 #ifndef BLKGETSIZE64
108 #define BLKGETSIZE64    _IOR(0x12,114,size_t)
109 #endif
110
111 #ifndef BLKFLSBUF
112 #define BLKFLSBUF       _IO(0x12,97)
113 #endif
114
115 #ifndef BLKDISCARD
116 #define BLKDISCARD      _IO(0x12,119)
117 #endif
118
119 static inline int blockdev_invalidate_cache(struct fio_file *f)
120 {
121         return ioctl(f->fd, BLKFLSBUF);
122 }
123
124 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
125 {
126         if (!ioctl(f->fd, BLKGETSIZE64, bytes))
127                 return 0;
128
129         return errno;
130 }
131
132 static inline unsigned long long os_phys_mem(void)
133 {
134         long pagesize, pages;
135
136         pagesize = sysconf(_SC_PAGESIZE);
137         pages = sysconf(_SC_PHYS_PAGES);
138         if (pages == -1 || pagesize == -1)
139                 return 0;
140
141         return (unsigned long long) pages * (unsigned long long) pagesize;
142 }
143
144 static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
145 {
146         srand48_r(seed, rs);
147 }
148
149 static inline long os_random_long(os_random_state_t *rs)
150 {
151         long val;
152
153         lrand48_r(rs, &val);
154         return val;
155 }
156
157 static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
158 {
159         struct raw_config_request rq;
160         int fd;
161
162         if (major(dev) != RAW_MAJOR)
163                 return 1;
164
165         /*
166          * we should be able to find /dev/rawctl or /dev/raw/rawctl
167          */
168         fd = open("/dev/rawctl", O_RDONLY);
169         if (fd < 0) {
170                 fd = open("/dev/raw/rawctl", O_RDONLY);
171                 if (fd < 0)
172                         return 1;
173         }
174
175         rq.raw_minor = minor(dev);
176         if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
177                 close(fd);
178                 return 1;
179         }
180
181         close(fd);
182         *majdev = rq.block_major;
183         *mindev = rq.block_minor;
184         return 0;
185 }
186
187 #ifdef O_NOATIME
188 #define FIO_O_NOATIME   O_NOATIME
189 #else
190 #define FIO_O_NOATIME   0
191 #endif
192
193 #ifdef MADV_REMOVE
194 #define FIO_MADV_FREE   MADV_REMOVE
195 #endif
196
197 #define fio_swap16(x)   __bswap_16(x)
198 #define fio_swap32(x)   __bswap_32(x)
199 #define fio_swap64(x)   __bswap_64(x)
200
201 #define CACHE_LINE_FILE \
202         "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
203
204 static inline int arch_cache_line_size(void)
205 {
206         char size[32];
207         int fd, ret;
208
209         fd = open(CACHE_LINE_FILE, O_RDONLY);
210         if (fd < 0)
211                 return -1;
212
213         ret = read(fd, size, sizeof(size));
214
215         close(fd);
216
217         if (ret <= 0)
218                 return -1;
219         else
220                 return atoi(size);
221 }
222
223 static inline unsigned long long get_fs_size(const char *path)
224 {
225         unsigned long long ret;
226         struct statfs s;
227
228         if (statfs(path, &s) < 0)
229                 return -1ULL;
230
231         ret = s.f_bsize;
232         ret *= (unsigned long long) s.f_bfree;
233         return ret;
234 }
235
236 static inline int os_trim(int fd, unsigned long long start,
237                           unsigned long long len)
238 {
239         uint64_t range[2];
240
241         range[0] = start;
242         range[1] = len;
243
244         if (!ioctl(fd, BLKDISCARD, range))
245                 return 0;
246
247         return errno;
248 }
249
250 static inline int fio_set_sched_idle(void)
251 {
252         struct sched_param p = { .sched_priority = 0, };
253         return sched_setscheduler(gettid(), SCHED_IDLE, &p);
254 }
255
256 #endif