Add a check avoid segfault
[fio.git] / os / os-solaris.h
1 #ifndef FIO_OS_SOLARIS_H
2 #define FIO_OS_SOLARIS_H
3
4 #define FIO_OS  os_solaris
5
6 #include <errno.h>
7 #include <malloc.h>
8 #include <sys/types.h>
9 #include <sys/fcntl.h>
10 #include <sys/pset.h>
11 #include <sys/mman.h>
12 #include <sys/dkio.h>
13 #include <sys/byteorder.h>
14
15 #include "../file.h"
16
17 #define FIO_HAVE_SOLARISAIO
18 #define FIO_HAVE_CPU_AFFINITY
19 #define FIO_HAVE_PSHARED_MUTEX
20 #define FIO_HAVE_CHARDEV_SIZE
21 #define FIO_USE_GENERIC_BDEV_SIZE
22 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
23 #define FIO_HAVE_GETTID
24
25 #define OS_MAP_ANON             MAP_ANON
26 #define OS_RAND_MAX             2147483648UL
27
28 #if defined(_BIG_ENDIAN)
29 #define FIO_BIG_ENDIAN
30 #else
31 #define FIO_LITTLE_ENDIAN
32 #endif
33
34 #define fio_swap16(x)   BSWAP_16(x)
35 #define fio_swap32(x)   BSWAP_32(x)
36 #define fio_swap64(x)   BSWAP_64(x)
37
38 struct solaris_rand_seed {
39         unsigned short r[3];
40 };
41
42 #ifndef POSIX_MADV_SEQUENTIAL
43 #define posix_madvise   madvise
44 #define POSIX_MADV_SEQUENTIAL   MADV_SEQUENTIAL
45 #define POSIX_MADV_DONTNEED     MADV_DONTNEED
46 #define POSIX_MADV_RANDOM       MADV_RANDOM
47 #endif
48
49 #define os_ctime_r(x, y, z)     ctime_r((x), (y), (z))
50 #define FIO_OS_HAS_CTIME_R
51
52 typedef psetid_t os_cpu_mask_t;
53 typedef struct solaris_rand_seed os_random_state_t;
54
55 static inline int chardev_size(struct fio_file *f, unsigned long long *bytes)
56 {
57         struct dk_minfo info;
58
59         *bytes = 0;
60
61         if (ioctl(f->fd, DKIOCGMEDIAINFO, &info) < 0)
62                 return errno;
63
64         *bytes = info.dki_lbsize * info.dki_capacity;
65         return 0;
66 }
67
68 static inline int blockdev_invalidate_cache(struct fio_file *f)
69 {
70         return 0;
71 }
72
73 static inline unsigned long long os_phys_mem(void)
74 {
75         return 0;
76 }
77
78 static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
79 {
80         rs->r[0] = seed & 0xffff;
81         seed >>= 16;
82         rs->r[1] = seed & 0xffff;
83         seed >>= 16;
84         rs->r[2] = seed & 0xffff;
85         seed48(rs->r);
86 }
87
88 static inline long os_random_long(os_random_state_t *rs)
89 {
90         return nrand48(rs->r);
91 }
92
93 #define FIO_OS_DIRECTIO
94 extern int directio(int, int);
95 static inline int fio_set_odirect(int fd)
96 {
97         if (directio(fd, DIRECTIO_ON) < 0)
98                 return errno;
99
100         return 0;
101 }
102
103 /*
104  * pset binding hooks for fio
105  */
106 #define fio_setaffinity(pid, cpumask)           \
107         pset_bind((cpumask), P_PID, (pid), NULL)
108 #define fio_getaffinity(pid, ptr)       ({ 0; })
109
110 #define fio_cpu_clear(mask, cpu)        pset_assign(PS_NONE, (cpu), NULL)
111 #define fio_cpu_set(mask, cpu)          pset_assign(*(mask), (cpu), NULL)
112
113 static inline int fio_cpuset_init(os_cpu_mask_t *mask)
114 {
115         if (pset_create(mask) < 0)
116                 return -1;
117
118         return 0;
119 }
120
121 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
122 {
123         if (pset_destroy(*mask) < 0)
124                 return -1;
125
126         return 0;
127 }
128
129 static inline int gettid(void)
130 {
131         return pthread_self();
132 }
133
134 /*
135  * Should be enough, not aware of what (if any) restrictions Solaris has
136  */
137 #define FIO_MAX_CPUS                    16384
138
139 #ifdef MADV_FREE
140 #define FIO_MADV_FREE   MADV_FREE
141 #endif
142
143 #endif