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