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