+ flags = fcntl(fd, F_GETFL);
+ if (flags < 0)
+ log_err("fio: %s failed to get file flags: %s\n", who, strerror(errno));
+ else {
+ int new_flags = flags | O_NONBLOCK;
+
+ new_flags = fcntl(fd, F_SETFL, new_flags);
+ if (new_flags < 0)
+ log_err("fio: %s failed to get file flags: %s\n", who, strerror(errno));
+ }
+
+ return flags;
+}
+
+enum {
+ ENDIAN_INVALID_BE = 1,
+ ENDIAN_INVALID_LE,
+ ENDIAN_INVALID_CONFIG,
+ ENDIAN_BROKEN,
+};
+
+static int endian_check(void)
+{
+ union {
+ uint8_t c[8];
+ uint64_t v;
+ } u;
+ int le = 0, be = 0;
+
+ u.v = 0x12;
+ if (u.c[7] == 0x12)
+ be = 1;
+ else if (u.c[0] == 0x12)
+ le = 1;
+
+#if defined(CONFIG_LITTLE_ENDIAN)
+ if (be)
+ return ENDIAN_INVALID_BE;
+#elif defined(CONFIG_BIG_ENDIAN)
+ if (le)
+ return ENDIAN_INVALID_LE;
+#else
+ return ENDIAN_INVALID_CONFIG;
+#endif
+
+ if (!le && !be)
+ return ENDIAN_BROKEN;
+
+ return 0;
+}
+
+int initialize_fio(char *envp[])
+{
+ long ps;
+ int err;
+
+ /*
+ * We need these to be properly 64-bit aligned, otherwise we
+ * can run into problems on archs that fault on unaligned fp
+ * access (ARM).
+ */
+ compiletime_assert((offsetof(struct thread_data, ts) % sizeof(void *)) == 0, "ts");
+ compiletime_assert((offsetof(struct thread_stat, percentile_list) % 8) == 0, "stat percentile_list");
+ compiletime_assert((offsetof(struct thread_stat, total_run_time) % 8) == 0, "total_run_time");
+ compiletime_assert((offsetof(struct thread_stat, total_err_count) % 8) == 0, "total_err_count");
+ compiletime_assert((offsetof(struct thread_stat, latency_percentile) % 8) == 0, "stat latency_percentile");
+ compiletime_assert((offsetof(struct thread_data, ts.clat_stat) % 8) == 0, "ts.clat_stat");
+ compiletime_assert((offsetof(struct thread_options_pack, zipf_theta) % 8) == 0, "zipf_theta");
+ compiletime_assert((offsetof(struct thread_options_pack, pareto_h) % 8) == 0, "pareto_h");
+ compiletime_assert((offsetof(struct thread_options_pack, percentile_list) % 8) == 0, "percentile_list");
+ compiletime_assert((offsetof(struct thread_options_pack, latency_percentile) % 8) == 0, "latency_percentile");
+ compiletime_assert((offsetof(struct jobs_eta, m_rate) % 8) == 0, "m_rate");
+
+ compiletime_assert(__TD_F_LAST <= TD_ENG_FLAG_SHIFT, "TD_ENG_FLAG_SHIFT");
+ compiletime_assert(BSSPLIT_MAX <= ZONESPLIT_MAX, "bsssplit/zone max");
+
+ err = endian_check();
+ if (err) {
+ log_err("fio: endianness settings appear wrong.\n");
+ switch (err) {
+ case ENDIAN_INVALID_BE:
+ log_err("fio: got big-endian when configured for little\n");
+ break;
+ case ENDIAN_INVALID_LE:
+ log_err("fio: got little-endian when configured for big\n");
+ break;
+ case ENDIAN_INVALID_CONFIG:
+ log_err("fio: not configured to any endianness\n");
+ break;
+ case ENDIAN_BROKEN:
+ log_err("fio: failed to detect endianness\n");
+ break;
+ default:
+ assert(0);
+ break;
+ }
+ log_err("fio: please report this to fio@vger.kernel.org\n");
+ return 1;
+ }
+
+#if !defined(CONFIG_GETTIMEOFDAY) && !defined(CONFIG_CLOCK_GETTIME)
+#error "No available clock source!"
+#endif
+
+ arch_init(envp);
+
+ sinit();
+
+ if (fio_filelock_init()) {
+ log_err("fio: failed initializing filelock subsys\n");
+ return 1;
+ }
+
+ file_hash_init();
+
+ /*
+ * We need locale for number printing, if it isn't set then just
+ * go with the US format.
+ */
+ if (!getenv("LC_NUMERIC"))
+ setlocale(LC_NUMERIC, "en_US");
+
+ ps = sysconf(_SC_PAGESIZE);
+ if (ps < 0) {
+ log_err("Failed to get page size\n");
+ return 1;
+ }
+
+ page_size = ps;
+ page_mask = ps - 1;
+
+ fio_keywords_init();
+ return 0;
+}
+
+void deinitialize_fio(void)
+{
+ fio_keywords_exit();
+}