[PATCH] Docu update
[fio.git] / fio.c
diff --git a/fio.c b/fio.c
index 241e64e6cee1c0f3e018357933f2447d3daf2443..1248971aca2a75da0fc32074898312ebe0895696 100644 (file)
--- a/fio.c
+++ b/fio.c
@@ -63,7 +63,7 @@ enum {
 
 #define should_fsync(td)       ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
 
-static sem_t startup_sem;
+static volatile int startup_sem;
 
 #define TERMINATE_ALL          (-1)
 #define JOB_START_TIMEOUT      (5 * 1000)
@@ -160,7 +160,7 @@ static int get_next_offset(struct thread_data *td, unsigned long long *offset)
                int loops = 50;
 
                do {
-                       lrand48_r(&td->random_state, &r);
+                       r = os_random_long(&td->random_state);
                        b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
                        rb = b + (td->file_offset / td->min_bs);
                        loops--;
@@ -188,7 +188,7 @@ static unsigned int get_next_buflen(struct thread_data *td)
        if (td->min_bs == td->max_bs)
                buflen = td->min_bs;
        else {
-               lrand48_r(&td->bsrange_state, &r);
+               r = os_random_long(&td->bsrange_state);
                buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
                buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
        }
@@ -250,7 +250,7 @@ static void fill_random_bytes(struct thread_data *td,
        double r;
 
        while (len) {
-               drand48_r(&td->verify_state, &r);
+               r = os_random_double(&td->verify_state);
 
                /*
                 * lrand48_r seems to be broken and only fill the bottom
@@ -360,11 +360,11 @@ static int get_rw_ddir(struct thread_data *td)
                 * Check if it's time to seed a new data direction.
                 */
                if (elapsed >= td->rwmixcycle) {
-                       unsigned long v;
+                       int v;
                        long r;
 
-                       lrand48_r(&td->random_state, &r);
-                       v = 100UL * r / (unsigned long) (RAND_MAX + 1.0);
+                       r = os_random_long(&td->rwmix_state);
+                       v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
                        if (v < td->rwmixread)
                                td->rwmix_ddir = DDIR_READ;
                        else
@@ -737,6 +737,10 @@ static void do_verify(struct thread_data *td)
        td_set_runstate(td, TD_RUNNING);
 }
 
+/*
+ * Main IO worker functions. It retrieves io_u's to process and queues
+ * and reaps them, checking for rate and errors along the way.
+ */
 static void do_io(struct thread_data *td)
 {
        struct io_completion_data icd;
@@ -929,22 +933,6 @@ static int init_io_u(struct thread_data *td)
        return 0;
 }
 
-static void cleanup_allocs(struct thread_data *td)
-{
-       if (td->directory)
-               free(td->directory);
-       if (td->iolog_file)
-               free(td->iolog_file);
-       if (td->exec_prerun)
-               free(td->exec_prerun);
-       if (td->exec_postrun)
-               free(td->exec_postrun);
-       if (td->ioscheduler)
-               free(td->ioscheduler);
-       if (td->sysfs_root)
-               free(td->sysfs_root);
-}
-
 static int create_file(struct thread_data *td, unsigned long long size,
                       int extend)
 {
@@ -1185,7 +1173,7 @@ static int setup_file(struct thread_data *td)
        }
 
        if (td->odirect)
-               flags |= O_DIRECT;
+               flags |= OS_O_DIRECT;
 
        if (td_write(td) || td_rw(td)) {
                if (td->filetype == FIO_TYPE_FILE) {
@@ -1332,8 +1320,8 @@ static void *thread_main(void *data)
                goto err;
 
        td_set_runstate(td, TD_INITIALIZED);
-       sem_post(&startup_sem);
-       sem_wait(&td->mutex);
+       fio_sem_up(&startup_sem);
+       fio_sem_down(&td->mutex);
 
        if (!td->create_serialize && setup_file(td))
                goto err;
@@ -1400,7 +1388,6 @@ err:
        }
        if (td->mmap)
                munmap(td->mmap, td->file_size);
-       cleanup_allocs(td);
        cleanup_io(td);
        cleanup_io_u(td);
        td_set_runstate(td, TD_EXITED);
@@ -1670,7 +1657,7 @@ static void fio_unpin_memory(void *pinned)
 
 static void *fio_pin_memory(void)
 {
-       long pagesize, pages;
+       unsigned long long phys_mem;
        void *ptr;
 
        if (!mlock_size)
@@ -1679,13 +1666,10 @@ static void *fio_pin_memory(void)
        /*
         * Don't allow mlock of more than real_mem-128MB
         */
-       pagesize = sysconf(_SC_PAGESIZE);
-       pages = sysconf(_SC_PHYS_PAGES);
-       if (pages != -1 && pagesize != -1) {
-               unsigned long long real_mem = pages * pagesize;
-
-               if ((mlock_size + 128 * 1024 * 1024) > real_mem) {
-                       mlock_size = real_mem - 128 * 1024 * 1024;
+       phys_mem = os_phys_mem();
+       if (phys_mem) {
+               if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
+                       mlock_size = phys_mem - 128 * 1024 * 1024;
                        printf("fio: limiting mlocked memory to %lluMiB\n",
                                                        mlock_size >> 20);
                }
@@ -1787,7 +1771,7 @@ static void run_threads(void)
                         */
                        td_set_runstate(td, TD_CREATED);
                        map[this_jobs++] = td;
-                       sem_init(&startup_sem, 0, 1);
+                       fio_sem_init(&startup_sem, 1);
                        nr_started++;
 
                        if (td->use_thread) {
@@ -1797,7 +1781,7 @@ static void run_threads(void)
                                }
                        } else {
                                if (fork())
-                                       sem_wait(&startup_sem);
+                                       fio_sem_down(&startup_sem);
                                else {
                                        fork_main(shm_id, i);
                                        exit(0);
@@ -1861,7 +1845,7 @@ static void run_threads(void)
                        m_rate += td->ratemin;
                        t_rate += td->rate;
                        todo--;
-                       sem_post(&td->mutex);
+                       fio_sem_up(&td->mutex);
                }
 
                reap_threads(&nr_running, &t_rate, &m_rate);