b4c02c9bf236803227cdb053f35975bb2b6f26d6
[fio.git] / os / os-openbsd.h
1 #ifndef FIO_OS_OPENBSD_H
2 #define FIO_OS_OPENBSD_H
3
4 #define FIO_OS  os_openbsd
5
6 #include <errno.h>
7 #include <sys/param.h>
8 #include <sys/statvfs.h>
9 #include <sys/ioctl.h>
10 #include <sys/dkio.h>
11 #include <sys/disklabel.h>
12 #include <sys/endian.h>
13 #include <sys/utsname.h>
14 #include <sys/sysctl.h>
15
16 /* XXX hack to avoid conflicts between rbtree.h and <sys/tree.h> */
17 #undef RB_BLACK
18 #undef RB_RED
19 #undef RB_ROOT
20
21 #include "../file.h"
22
23 #define FIO_USE_GENERIC_RAND
24 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
25 #define FIO_HAVE_FS_STAT
26 #define FIO_HAVE_GETTID
27 #define FIO_HAVE_SHM_ATTACH_REMOVED
28
29 #define OS_MAP_ANON             MAP_ANON
30
31 #ifndef PTHREAD_STACK_MIN
32 #define PTHREAD_STACK_MIN 4096
33 #endif
34
35 #define fio_swap16(x)   bswap16(x)
36 #define fio_swap32(x)   bswap32(x)
37 #define fio_swap64(x)   bswap64(x)
38
39 typedef off_t off64_t;
40
41 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
42 {
43         struct disklabel dl;
44
45         if (!ioctl(f->fd, DIOCGDINFO, &dl)) {
46                 *bytes = ((unsigned long long)dl.d_secperunit) * dl.d_secsize;
47                 return 0;
48         }
49
50         *bytes = 0;
51         return errno;
52 }
53
54 static inline int blockdev_invalidate_cache(struct fio_file *f)
55 {
56         return ENOTSUP;
57 }
58
59 static inline unsigned long long os_phys_mem(void)
60 {
61         int mib[2] = { CTL_HW, HW_PHYSMEM64 };
62         uint64_t mem;
63         size_t len = sizeof(mem);
64
65         sysctl(mib, 2, &mem, &len, NULL, 0);
66         return mem;
67 }
68
69 static inline int gettid(void)
70 {
71         return (int)(intptr_t) pthread_self();
72 }
73
74 static inline unsigned long long get_fs_free_size(const char *path)
75 {
76         unsigned long long ret;
77         struct statvfs s;
78
79         if (statvfs(path, &s) < 0)
80                 return -1ULL;
81
82         ret = s.f_frsize;
83         ret *= (unsigned long long) s.f_bfree;
84         return ret;
85 }
86
87 #ifdef MADV_FREE
88 #define FIO_MADV_FREE   MADV_FREE
89 #endif
90
91 static inline int shm_attach_to_open_removed(void)
92 {
93         struct utsname uts;
94         int major, minor;
95
96         if (uname(&uts) == -1)
97                 return 0;
98
99         /*
100          * Return 1 if >= OpenBSD 5.1 according to 97900ebf,
101          * assuming both major/minor versions are < 10.
102          */
103         if (uts.release[0] > '9' || uts.release[0] < '0')
104                 return 0;
105         if (uts.release[1] != '.')
106                 return 0;
107         if (uts.release[2] > '9' || uts.release[2] < '0')
108                 return 0;
109
110         major = uts.release[0] - '0';
111         minor = uts.release[2] - '0';
112
113         if (major > 5)
114                 return 1;
115         if (major == 5 && minor >= 1)
116                 return 1;
117
118         return 0;
119 }
120
121 #endif