2f137234ca3266288cb34d763982da191806cfb3
[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 <unistd.h>
9 #include <sys/types.h>
10 #include <sys/fcntl.h>
11 #include <sys/pset.h>
12 #include <sys/mman.h>
13 #include <sys/dkio.h>
14 #include <sys/byteorder.h>
15 #include <sys/statvfs.h>
16 #include <pthread.h>
17
18 #include "../file.h"
19
20 #define FIO_HAVE_CPU_AFFINITY
21 #define FIO_HAVE_CHARDEV_SIZE
22 #define FIO_USE_GENERIC_BDEV_SIZE
23 #define FIO_HAVE_FS_STAT
24 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
25 #define FIO_HAVE_GETTID
26
27 #define OS_MAP_ANON             MAP_ANON
28 #define OS_RAND_MAX             2147483648UL
29
30 #define fio_swap16(x)   BSWAP_16(x)
31 #define fio_swap32(x)   BSWAP_32(x)
32 #define fio_swap64(x)   BSWAP_64(x)
33
34 struct solaris_rand_seed {
35         unsigned short r[3];
36 };
37
38 #ifndef POSIX_MADV_SEQUENTIAL
39 #define posix_madvise   madvise
40 #define POSIX_MADV_SEQUENTIAL   MADV_SEQUENTIAL
41 #define POSIX_MADV_DONTNEED     MADV_DONTNEED
42 #define POSIX_MADV_RANDOM       MADV_RANDOM
43 #endif
44
45 #define os_ctime_r(x, y, z)     ctime_r((x), (y), (z))
46 #define FIO_OS_HAS_CTIME_R
47
48 typedef psetid_t os_cpu_mask_t;
49 typedef struct solaris_rand_seed os_random_state_t;
50
51 static inline int chardev_size(struct fio_file *f, unsigned long long *bytes)
52 {
53         struct dk_minfo info;
54
55         *bytes = 0;
56
57         if (ioctl(f->fd, DKIOCGMEDIAINFO, &info) < 0)
58                 return errno;
59
60         *bytes = info.dki_lbsize * info.dki_capacity;
61         return 0;
62 }
63
64 static inline int blockdev_invalidate_cache(struct fio_file *f)
65 {
66         return ENOTSUP;
67 }
68
69 static inline unsigned long long os_phys_mem(void)
70 {
71         long pagesize, pages;
72
73         pagesize = sysconf(_SC_PAGESIZE);
74         pages = sysconf(_SC_PHYS_PAGES);
75         if (pages == -1 || pagesize == -1)
76                 return 0;
77
78         return (unsigned long long) pages * (unsigned long long) pagesize;
79 }
80
81 static inline unsigned long long get_fs_free_size(const char *path)
82 {
83         unsigned long long ret;
84         struct statvfs s;
85
86         if (statvfs(path, &s) < 0)
87                 return -1ULL;
88
89         ret = s.f_frsize;
90         ret *= (unsigned long long) s.f_bfree;
91         return ret;
92 }
93
94 static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
95 {
96         rs->r[0] = seed & 0xffff;
97         seed >>= 16;
98         rs->r[1] = seed & 0xffff;
99         seed >>= 16;
100         rs->r[2] = seed & 0xffff;
101         seed48(rs->r);
102 }
103
104 static inline long os_random_long(os_random_state_t *rs)
105 {
106         return nrand48(rs->r);
107 }
108
109 #define FIO_OS_DIRECTIO
110 extern int directio(int, int);
111 static inline int fio_set_odirect(struct fio_file *f)
112 {
113         if (directio(f->fd, DIRECTIO_ON) < 0)
114                 return errno;
115
116         return 0;
117 }
118
119 /*
120  * pset binding hooks for fio
121  */
122 #define fio_setaffinity(pid, cpumask)           \
123         pset_bind((cpumask), P_LWPID, (pid), NULL)
124 #define fio_getaffinity(pid, ptr)       ({ 0; })
125
126 #define fio_cpu_clear(mask, cpu)        pset_assign(PS_NONE, (cpu), NULL)
127 #define fio_cpu_set(mask, cpu)          pset_assign(*(mask), (cpu), NULL)
128
129 static inline int fio_cpu_isset(os_cpu_mask_t *mask, int cpu)
130 {
131         const unsigned int max_cpus = sysconf(_SC_NPROCESSORS_ONLN);
132         unsigned int num_cpus;
133         processorid_t *cpus;
134         int i, ret;
135
136         cpus = malloc(sizeof(*cpus) * max_cpus);
137
138         if (pset_info(*mask, NULL, &num_cpus, cpus) < 0) {
139                 free(cpus);
140                 return 0;
141         }
142
143         ret = 0;
144         for (i = 0; i < num_cpus; i++) {
145                 if (cpus[i] == cpu) {
146                         ret = 1;
147                         break;
148                 }
149         }
150
151         free(cpus);
152         return ret;
153 }
154
155 static inline int fio_cpu_count(os_cpu_mask_t *mask)
156 {
157         unsigned int num_cpus;
158
159         if (pset_info(*mask, NULL, &num_cpus, NULL) < 0)
160                 return 0;
161
162         return num_cpus;
163 }
164
165 static inline int fio_cpuset_init(os_cpu_mask_t *mask)
166 {
167         if (pset_create(mask) < 0)
168                 return -1;
169
170         return 0;
171 }
172
173 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
174 {
175         if (pset_destroy(*mask) < 0)
176                 return -1;
177
178         return 0;
179 }
180
181 static inline int gettid(void)
182 {
183         return pthread_self();
184 }
185
186 /*
187  * Should be enough, not aware of what (if any) restrictions Solaris has
188  */
189 #define FIO_MAX_CPUS                    16384
190
191 #ifdef MADV_FREE
192 #define FIO_MADV_FREE   MADV_FREE
193 #endif
194
195 #endif