From ff3110a8fb51c1369d0877de68e2d06ba636e950 Mon Sep 17 00:00:00 2001 From: Tor Egge Date: Tue, 12 Mar 2019 11:06:51 +0100 Subject: Restore support for compiling fastos on darwin. --- fastos/src/tests/backtracetest.cpp | 2 +- fastos/src/tests/filetest.cpp | 2 ++ fastos/src/tests/timetest.cpp | 4 +-- fastos/src/vespa/fastos/app.cpp | 2 ++ fastos/src/vespa/fastos/file.cpp | 4 +++ fastos/src/vespa/fastos/file.h | 5 +++ fastos/src/vespa/fastos/linux_file.cpp | 2 ++ fastos/src/vespa/fastos/unix_file.cpp | 58 ++++++++++++++++++++++++++++++++ fastos/src/vespa/fastos/unix_file.h | 3 ++ fastos/src/vespa/fastos/unix_process.cpp | 3 ++ fastos/src/vespa/fastos/unix_thread.cpp | 2 ++ 11 files changed, 84 insertions(+), 3 deletions(-) (limited to 'fastos') diff --git a/fastos/src/tests/backtracetest.cpp b/fastos/src/tests/backtracetest.cpp index 6c68765c1ee..8fa3b42bfc9 100644 --- a/fastos/src/tests/backtracetest.cpp +++ b/fastos/src/tests/backtracetest.cpp @@ -6,7 +6,7 @@ #include "tests.h" -#if defined __x86_64__ +#if defined(__x86_64__) && defined(__linux__) class Tracker { private: diff --git a/fastos/src/tests/filetest.cpp b/fastos/src/tests/filetest.cpp index 43051dd1667..15f8ccb526b 100644 --- a/fastos/src/tests/filetest.cpp +++ b/fastos/src/tests/filetest.cpp @@ -813,7 +813,9 @@ public: ScanDirectoryTest(); ReadBufTest(); MemoryMapTest(0); +#ifdef __linux__ MemoryMapTest(MAP_HUGETLB); +#endif PrintSeparator(); printf("END OF TEST (%s)\n", _argv[0]); diff --git a/fastos/src/tests/timetest.cpp b/fastos/src/tests/timetest.cpp index 0618e28bcd0..c0c49884fb1 100644 --- a/fastos/src/tests/timetest.cpp +++ b/fastos/src/tests/timetest.cpp @@ -260,8 +260,8 @@ public: using fastos::TimeStamp; Progress(TimeStamp(97).ns() == 97l, "TimeStamp(int)"); Progress(TimeStamp(97u).ns() == 97l, "TimeStamp(unsigned int)"); - Progress(TimeStamp(97l).ns() == 97l, "TimeStamp(long)"); - Progress(TimeStamp(97ul).ns() == 97l, "TimeStamp(unsigned long)"); + Progress(TimeStamp(INT64_C(97)).ns() == 97l, "TimeStamp(int64_t)"); + Progress(TimeStamp(UINT64_C(97)).ns() == 97l, "TimeStamp(uint64_t)"); Progress(TimeStamp(TimeStamp::Seconds(97.3)).ns() == 97300000000l, "TimeStamp(double)"); PrintSeparator(); } diff --git a/fastos/src/vespa/fastos/app.cpp b/fastos/src/vespa/fastos/app.cpp index aecdc683649..b5c15aea831 100644 --- a/fastos/src/vespa/fastos/app.cpp +++ b/fastos/src/vespa/fastos/app.cpp @@ -34,6 +34,7 @@ FastOS_ApplicationInterface::FastOS_ApplicationInterface() : _argv(nullptr) { FastOS_ProcessInterface::_app = this; +#ifdef __linux__ char * fadvise = getenv("VESPA_FADVISE_OPTIONS"); if (fadvise != nullptr) { int fadviseOptions(0); @@ -44,6 +45,7 @@ FastOS_ApplicationInterface::FastOS_ApplicationInterface() : if (strstr(fadvise, "NOREUSE")) { fadviseOptions |= POSIX_FADV_NOREUSE; } FastOS_FileInterface::setDefaultFAdviseOptions(fadviseOptions); } +#endif } FastOS_ApplicationInterface::~FastOS_ApplicationInterface () diff --git a/fastos/src/vespa/fastos/file.cpp b/fastos/src/vespa/fastos/file.cpp index aff4d1c54b5..56f6addd743 100644 --- a/fastos/src/vespa/fastos/file.cpp +++ b/fastos/src/vespa/fastos/file.cpp @@ -28,7 +28,11 @@ DirectIOException::DirectIOException(const char * fileName, const void * buffer, DirectIOException::~DirectIOException() {} FastOS_FileInterface::FailedHandler FastOS_FileInterface::_failedHandler = nullptr; +#ifdef __linux__ int FastOS_FileInterface::_defaultFAdviseOptions = POSIX_FADV_NORMAL; +#else +int FastOS_FileInterface::_defaultFAdviseOptions = 0; +#endif static const size_t MAX_WRITE_CHUNK_SIZE = 0x4000000; // 64 MB diff --git a/fastos/src/vespa/fastos/file.h b/fastos/src/vespa/fastos/file.h index 8424967f655..15c4dfa33cd 100644 --- a/fastos/src/vespa/fastos/file.h +++ b/fastos/src/vespa/fastos/file.h @@ -804,6 +804,11 @@ public: virtual bool IsValidScan() const = 0; }; +#ifdef __linux__ #include typedef FastOS_Linux_File FASTOS_PREFIX(File); +#else +#include +typedef FastOS_UNIX_File FASTOS_PREFIX(File); +#endif typedef FastOS_UNIX_DirectoryScan FASTOS_PREFIX(DirectoryScan); diff --git a/fastos/src/vespa/fastos/linux_file.cpp b/fastos/src/vespa/fastos/linux_file.cpp index 6af3bd207a0..b4acaaa6073 100644 --- a/fastos/src/vespa/fastos/linux_file.cpp +++ b/fastos/src/vespa/fastos/linux_file.cpp @@ -7,6 +7,7 @@ * Implementation of FastOS_Linux_File methods. *****************************************************************************/ +#ifdef __linux__ #include "file.h" #include #include @@ -434,3 +435,4 @@ void forceStaticLinkOf_backtrace() void * dummy[2]; FastOS_backtrace(dummy, 2); } +#endif diff --git a/fastos/src/vespa/fastos/unix_file.cpp b/fastos/src/vespa/fastos/unix_file.cpp index 0b144c1f423..17a11cd68f3 100644 --- a/fastos/src/vespa/fastos/unix_file.cpp +++ b/fastos/src/vespa/fastos/unix_file.cpp @@ -16,7 +16,26 @@ #include #include #include +#ifdef __linux__ #include +#else +#include +#endif + +ssize_t +FastOS_UNIX_File::Read(void *buffer, size_t len) +{ + ssize_t nRead = read(_filedes, buffer, len); + return nRead; +} + + +ssize_t +FastOS_UNIX_File::Write2(const void *buffer, size_t len) +{ + ssize_t writeRes = write(_filedes, buffer, len); + return writeRes; +} bool FastOS_UNIX_File::SetPosition(int64_t desiredPosition) @@ -33,6 +52,22 @@ FastOS_UNIX_File::GetPosition(void) return lseek(_filedes, 0, SEEK_CUR); } +void FastOS_UNIX_File::ReadBuf(void *buffer, size_t length, + int64_t readOffset) +{ + ssize_t readResult; + + readResult = pread(_filedes, buffer, length, readOffset); + if (static_cast(readResult) != length) { + std::string errorString = readResult != -1 ? + std::string("short read") : + FastOS_FileInterface::getLastErrorString(); + std::ostringstream os; + os << "Fatal: Reading " << length << " bytes, got " << readResult << " from '" + << GetFileName() << "' failed: " << errorString; + throw std::runtime_error(os.str()); + } +} bool FastOS_UNIX_File::Stat(const char *filename, FastOS_StatInfo *statInfo) @@ -51,9 +86,18 @@ FastOS_UNIX_File::Stat(const char *filename, FastOS_StatInfo *statInfo) statInfo->_isDirectory = S_ISDIR(stbuf.st_mode); statInfo->_size = static_cast(stbuf.st_size); statInfo->_modifiedTime = stbuf.st_mtime; +#ifdef __linux__ statInfo->_modifiedTimeNS = stbuf.st_mtim.tv_sec; statInfo->_modifiedTimeNS *= 1000000000; statInfo->_modifiedTimeNS += stbuf.st_mtim.tv_nsec; +#elif defined(__APPLE__) + statInfo->_modifiedTimeNS = stbuf.st_mtimespec.tv_sec; + statInfo->_modifiedTimeNS *= 1000000000; + statInfo->_modifiedTimeNS += stbuf.st_mtimespec.tv_nsec; +#else + statInfo->_modifiedTimeNS = stbuf.st_mtime; + statInfo->_modifiedTimeNS *= 1000000000; +#endif rc = true; } else { if (errno == ENOENT) { @@ -150,9 +194,11 @@ FastOS_UNIX_File::CalcAccessFlags(unsigned int openFlags) accessFlags |= O_FSYNC; #endif +#ifdef __linux__ if ((openFlags & FASTOS_FILE_OPEN_DIRECTIO) != 0) { accessFlags |= O_DIRECT; } +#endif if ((openFlags & FASTOS_FILE_OPEN_TRUNCATE) != 0) { // Truncate file on open @@ -161,7 +207,11 @@ FastOS_UNIX_File::CalcAccessFlags(unsigned int openFlags) return accessFlags; } +#ifdef __linux__ constexpr int ALWAYS_SUPPORTED_MMAP_FLAGS = ~MAP_HUGETLB; +#else +constexpr int ALWAYS_SUPPORTED_MMAP_FLAGS = ~0; +#endif bool FastOS_UNIX_File::Open(unsigned int openFlags, const char *filename) @@ -190,7 +240,11 @@ FastOS_UNIX_File::Open(unsigned int openFlags, const char *filename) abort(); } +#ifdef __linux__ _filedes = file->_fileno; +#else + _filedes = fileno(file); +#endif _openFlags = openFlags; rc = true; } else { @@ -214,6 +268,7 @@ FastOS_UNIX_File::Open(unsigned int openFlags, const char *filename) mbase = mmap(nullptr, mlen, PROT_READ, MAP_SHARED | (_mmapFlags & ALWAYS_SUPPORTED_MMAP_FLAGS), _filedes, 0); } if (mbase != MAP_FAILED) { +#ifdef __linux__ int fadviseOptions = getFAdviseOptions(); int eCode(0); if (POSIX_FADV_RANDOM == fadviseOptions) { @@ -224,6 +279,7 @@ FastOS_UNIX_File::Open(unsigned int openFlags, const char *filename) if (eCode != 0) { fprintf(stderr, "Failed: posix_madvise(%p, %ld, %d) = %d\n", mbase, mlen, fadviseOptions, eCode); } +#endif _mmapbase = mbase; _mmaplen = mlen; } else { @@ -245,7 +301,9 @@ FastOS_UNIX_File::Open(unsigned int openFlags, const char *filename) void FastOS_UNIX_File::dropFromCache() const { +#ifdef __linux__ posix_fadvise(_filedes, 0, 0, POSIX_FADV_DONTNEED); +#endif } diff --git a/fastos/src/vespa/fastos/unix_file.h b/fastos/src/vespa/fastos/unix_file.h index 5702f511ca5..3bca340cb90 100644 --- a/fastos/src/vespa/fastos/unix_file.h +++ b/fastos/src/vespa/fastos/unix_file.h @@ -56,6 +56,9 @@ public: _mmapEnabled(false) { } + void ReadBuf(void *buffer, size_t length, int64_t readOffset) override; + ssize_t Read(void *buffer, size_t len) override; + ssize_t Write2(const void *buffer, size_t len) override; bool Open(unsigned int openFlags, const char *filename) override; bool Close() override; bool IsOpened() const override { return _filedes >= 0; } diff --git a/fastos/src/vespa/fastos/unix_process.cpp b/fastos/src/vespa/fastos/unix_process.cpp index c695c8d79a0..f1c726a4f51 100644 --- a/fastos/src/vespa/fastos/unix_process.cpp +++ b/fastos/src/vespa/fastos/unix_process.cpp @@ -9,6 +9,9 @@ #include #include #include +#ifndef __linux__ +#include +#endif #ifndef AF_LOCAL #define AF_LOCAL AF_UNIX diff --git a/fastos/src/vespa/fastos/unix_thread.cpp b/fastos/src/vespa/fastos/unix_thread.cpp index 8ff1913b246..5218bde2630 100644 --- a/fastos/src/vespa/fastos/unix_thread.cpp +++ b/fastos/src/vespa/fastos/unix_thread.cpp @@ -40,6 +40,7 @@ bool FastOS_UNIX_Thread::Initialize (int stackSize, int stackGuardSize) pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); pthread_attr_setstacksize(&attr, stackSize); +#ifdef __linux__ if (_G_maxNumCpus > 0) { int cpuid = _G_nextCpuId.fetch_add(1)%_G_maxNumCpus; cpu_set_t cpuset; @@ -50,6 +51,7 @@ bool FastOS_UNIX_Thread::Initialize (int stackSize, int stackGuardSize) fprintf(stderr, "Pinning FAILURE retval = %d, errno=%d sizeof(cpuset_t)=%ld cpuid(%d)\n", retval, errno, sizeof(cpuset), cpuid); } } +#endif if (stackGuardSize != 0) { pthread_attr_setguardsize(&attr, stackGuardSize); -- cgit v1.2.3