MAdvise::MAdvise(void* p, unsigned len, Advice a) { _p = _pageAlign(p); _len = len + static_cast<unsigned>(reinterpret_cast<size_t>(p) - reinterpret_cast<size_t>(_p)); int advice = 0; switch (a) { case Sequential: advice = MADV_SEQUENTIAL; break; case Random: advice = MADV_RANDOM; break; } if (madvise(_p, _len, advice)) { error() << "madvise failed: " << errnoWithDescription(); } }
// Given a SOCKET, turns off nonblocking mode static void disableNonblockingMode(SOCKET socket) { unsigned long resultBuffer = 0; unsigned long resultBufferBytesWritten = 0; unsigned long newNonblockingEnabled = 0; const int status = WSAIoctl(socket, FIONBIO, &newNonblockingEnabled, sizeof(unsigned long), &resultBuffer, sizeof(resultBuffer), &resultBufferBytesWritten, NULL, NULL); if (status == SOCKET_ERROR) { const int mongo_errno = WSAGetLastError(); error() << "Windows WSAIoctl returned " << errnoWithDescription(mongo_errno) << endl; fassertFailed(16726); } }
void* MemoryMappedFile::createPrivateMap() { verify(maphandle); stdx::lock_guard<stdx::mutex> lk(mapViewMutex); LPVOID thisAddress = getNextMemoryMappedFileLocation(len); void* privateMapAddress = NULL; int current_retry = 0; while (true) { privateMapAddress = MapViewOfFileEx(maphandle, // file mapping handle FILE_MAP_READ, // access 0, 0, // file offset, high and low 0, // bytes to map, 0 == all thisAddress); // address to place file if (privateMapAddress == 0) { DWORD dosError = GetLastError(); ++current_retry; // If we failed to allocate a memory mapped file, try again in case we picked // an address that Windows is also trying to use for some other VM allocations if (dosError == ERROR_INVALID_ADDRESS && current_retry < 5) { continue; } severe() << "MapViewOfFileEx for " << filename() << " failed with error " << errnoWithDescription(dosError) << " (file size is " << len << ")" << " in MemoryMappedFile::createPrivateMap" << endl; fassertFailed(16167); } break; } views.push_back(privateMapAddress); return privateMapAddress; }
vector<string> getMyAddrs() { ifaddrs * addrs; int status = getifaddrs(&addrs); massert(13469, "getifaddrs failure: " + errnoWithDescription(errno), status == 0); vector<string> out; // based on example code from linux getifaddrs manpage for (ifaddrs * addr = addrs; addr != NULL; addr = addr->ifa_next) { if ( addr->ifa_addr == NULL ) continue; int family = addr->ifa_addr->sa_family; char host[NI_MAXHOST]; if (family == AF_INET || family == AF_INET6) { status = getnameinfo(addr->ifa_addr, (family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6)), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); if ( status != 0 ) { freeifaddrs( addrs ); addrs = NULL; msgasserted( 13470, string("getnameinfo() failed: ") + gai_strerror(status) ); } out.push_back(host); } } freeifaddrs( addrs ); addrs = NULL; if (logLevel >= 1) { log(1) << "getMyAddrs():"; for (vector<string>::const_iterator it=out.begin(), end=out.end(); it!=end; ++it) { log(1) << " [" << *it << ']'; } log(1) << endl; } return out; }
Status StorageEngineLockFile::open() { try { if (!boost::filesystem::exists(_dbpath)) { return Status(ErrorCodes::NonExistentPath, str::stream() << "Data directory " << _dbpath << " not found."); } } catch (const std::exception& ex) { return Status(ErrorCodes::UnknownError, str::stream() << "Unable to check existence of data directory " << _dbpath << ": " << ex.what()); } HANDLE lockFileHandle = CreateFileW(toNativeString(_filespec.c_str()).c_str(), GENERIC_READ | GENERIC_WRITE, 0 /* do not allow anyone else access */, NULL, OPEN_ALWAYS /* success if fh can open */, 0, NULL); if (lockFileHandle == INVALID_HANDLE_VALUE) { int errorcode = GetLastError(); if (errorcode == ERROR_ACCESS_DENIED) { return Status(ErrorCodes::IllegalOperation, str::stream() << "Attempted to create a lock file on a read-only directory: " << _dbpath); } return Status(ErrorCodes::DBPathInUse, str::stream() << "Unable to create/open the lock file: " << _filespec << " (" << errnoWithDescription(errorcode) << ")." << " Ensure the user executing mongod is the owner of the lock " "file and has the appropriate permissions. Also make sure " "that another mongod instance is not already running on the " << _dbpath << " directory"); } _lockFileHandle->_handle = lockFileHandle; return Status::OK(); }
bool Socket::connect(SockAddr& remote) { _remote = remote; _fd = socket(remote.getType(), SOCK_STREAM, 0); if ( _fd == INVALID_SOCKET ) { LOG(_logLevel) << "ERROR: connect invalid socket " << errnoWithDescription() << endl; return false; } if ( _timeout > 0 ) { setTimeout( _timeout ); } ConnectBG bg(_fd, remote); bg.go(); if ( bg.wait(5000) ) { if ( bg.inError() ) { close(); return false; } } else { // time out the connect close(); bg.wait(); // so bg stays in scope until bg thread terminates return false; } if (remote.getType() != AF_UNIX) disableNagle(_fd); #ifdef SO_NOSIGPIPE // ignore SIGPIPE signals on osx, to avoid process exit const int one = 1; setsockopt( _fd , SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(int)); #endif _local = getLocalAddrForBoundSocketFd(_fd); _fdCreationMicroSec = curTimeMicros64(); return true; }
void* MemoryMappedFile::createPrivateMap() { void * x = mmap( /*start*/0 , len , PROT_READ|PROT_WRITE , MAP_PRIVATE|MAP_NORESERVE , fd , 0 ); if( x == MAP_FAILED ) { if ( errno == ENOMEM ) { if( sizeof(void*) == 4 ) { error() << "mmap private failed with out of memory. You are using a 32-bit build and probably need to upgrade to 64" << endl; } else { error() << "mmap private failed with out of memory. (64 bit build)" << endl; } } else { error() << "mmap private failed " << errnoWithDescription() << endl; } return 0; } views.push_back(x); return x; }
void Socket::recv( char * buf , int len ) { unsigned retries = 0; while( len > 0 ) { int ret = unsafe_recv( buf , len ); if ( ret > 0 ) { if ( len <= 4 && ret != len ) log(_logLevel) << "Socket recv() got " << ret << " bytes wanted len=" << len << endl; assert( ret <= len ); len -= ret; buf += ret; } else if ( ret == 0 ) { log(3) << "Socket recv() conn closed? " << remoteString() << endl; throw SocketException( SocketException::CLOSED , remoteString() ); } else { /* ret < 0 */ int e = errno; #if defined(EINTR) && !defined(_WIN32) if( e == EINTR ) { if( ++retries == 1 ) { log() << "EINTR retry" << endl; continue; } } #endif if ( ( e == EAGAIN #ifdef _WINDOWS || e == WSAETIMEDOUT #endif ) && _timeout > 0 ) { // this is a timeout log(_logLevel) << "Socket recv() timeout " << remoteString() <<endl; throw SocketException( SocketException::RECV_TIMEOUT, remoteString() ); } log(_logLevel) << "Socket recv() " << errnoWithDescription(e) << " " << remoteString() <<endl; throw SocketException( SocketException::RECV_ERROR , remoteString() ); } } }
Status StorageEngineLockFile::writePid() { if (!_lockFileHandle->isValid()) { return Status(ErrorCodes::FileNotOpen, str::stream() << "Unable to write process ID to " << _filespec << " because file has not been opened."); } Status status = _truncateFile(_lockFileHandle->_handle); if (!status.isOK()) { return status; } ProcessId pid = ProcessId::getCurrent(); std::stringstream ss; ss << pid << std::endl; std::string pidStr = ss.str(); DWORD bytesWritten = 0; if (::WriteFile(_lockFileHandle->_handle, static_cast<LPCVOID>(pidStr.c_str()), static_cast<DWORD>(pidStr.size()), &bytesWritten, NULL) == FALSE) { int errorcode = GetLastError(); return Status(ErrorCodes::FileStreamFailed, str::stream() << "Unable to write process id " << pid.toString() << " to file: " << _filespec << ' ' << errnoWithDescription(errorcode)); } else if (bytesWritten == 0) { return Status(ErrorCodes::FileStreamFailed, str::stream() << "Unable to write process id " << pid.toString() << " to file: " << _filespec << " no data written."); } ::FlushFileBuffers(_lockFileHandle->_handle); return Status::OK(); }
static void shutdownServer() { log() << "shutdown: going to close listening sockets..." << endl; ListeningSockets::get()->closeAll(); log() << "shutdown: going to flush diaglog..." << endl; _diaglog.flush(); /* must do this before unmapping mem or you may get a seg fault */ log() << "shutdown: going to close sockets..." << endl; boost::thread close_socket_thread( boost::bind(MessagingPort::closeAllSockets, 0) ); { LOCK_REASON(lockReason, "shutting down"); Lock::GlobalWrite lk(lockReason); log() << "shutdown: going to close databases..." << endl; dbHolderW().closeDatabases(dbpath); log() << "shutdown: going to unload all plugins..." << endl; plugins::loader->shutdown(); log() << "shutdown: going to shutdown TokuMX..." << endl; storage::shutdown(); } #if !defined(__sunos__) if ( lockFile ) { log() << "shutdown: removing fs lock..." << endl; /* This ought to be an unlink(), but Eliot says the last time that was attempted, there was a race condition with acquirePathLock(). */ #ifdef _WIN32 if( _chsize( lockFile , 0 ) ) log() << "couldn't remove fs lock " << WSAGetLastError() << endl; CloseHandle(lockFileHandle); #else if( ftruncate( lockFile , 0 ) ) log() << "couldn't remove fs lock " << errnoWithDescription() << endl; flock( lockFile, LOCK_UN ); #endif } #endif }
void* MemoryMappedFile::createReadOnlyMap() { verify( maphandle ); scoped_lock lk(mapViewMutex); void* readOnlyMapAddress = MapViewOfFile( maphandle, // file mapping handle FILE_MAP_READ, // access 0, 0, // file offset, high and low 0 ); // bytes to map, 0 == all if ( 0 == readOnlyMapAddress ) { DWORD dosError = GetLastError(); log() << "MapViewOfFile for " << filename() << " failed with error " << errnoWithDescription( dosError ) << " (file size is " << len << ")" << " in MemoryMappedFile::createReadOnlyMap" << endl; fassertFailed( 16165 ); } memconcept::is( readOnlyMapAddress, memconcept::concept::other, filename() ); views.push_back( readOnlyMapAddress ); return readOnlyMapAddress; }
void StorageEngineLockFile::clearPidAndUnlock() { if (!_lockFileHandle->isValid()) { return; } log() << "shutdown: removing fs lock..."; // This ought to be an unlink(), but Eliot says the last // time that was attempted, there was a race condition // with acquirePathLock(). if (::ftruncate(_lockFileHandle->_fd, 0)) { int errorcode = errno; log() << "couldn't remove fs lock " << errnoWithDescription(errorcode); } #if !defined(__sun) ::flock(_lockFileHandle->_fd, LOCK_UN); #else struct flock fileLockInfo = {0}; fileLockInfo.l_type = F_UNLCK; fileLockInfo.l_whence = SEEK_SET; ::fcntl(_lockFileHandle->_fd, F_SETLK, &fileLockInfo); #endif // !defined(__sun) }
void start( const string& lp , bool append ){ uassert( 10268 , "LoggingManager already started" , ! _enabled ); _append = append; // test path FILE * test = fopen( lp.c_str() , _append ? "a" : "w" ); if ( ! test ){ if (boost::filesystem::is_directory(lp)){ cout << "logpath [" << lp << "] should be a file name not a directory" << endl; } else { cout << "can't open [" << lp << "] for log file: " << errnoWithDescription() << endl; } dbexit( EXIT_BADOPTIONS ); assert( 0 ); } fclose( test ); _path = lp; _enabled = 1; rotate(); }
void Logstream::logLockless( const StringData& s ) { if ( s.size() == 0 ) return; if ( doneSetup == 1717 ) { #if defined(_WIN32) // fwrite() has a behavior problem in Windows when writing to the console // when the console is in the UTF-8 code page: fwrite() sends a single // byte and then the rest of the string. If the first character is // non-ASCII, the console then displays two UTF-8 replacement characters // instead of the single UTF-8 character we want. write() doesn't have // this problem. int fd = fileno( logfile ); if ( _isatty( fd ) ) { fflush( logfile ); _write( fd, s.data(), s.size() ); return; } #else if ( isSyslog ) { syslog( LOG_INFO , "%s" , s.data() ); return; } #endif if (fwrite(s.data(), s.size(), 1, logfile)) { fflush(logfile); } else { int x = errno; cout << "Failed to write to logfile: " << errnoWithDescription(x) << endl; } } else { cout << s.data(); cout.flush(); } }
void FileAllocator::ensureLength(int fd , long size) { #if defined(__linux__) int ret = posix_fallocate(fd,0,size); if ( ret == 0 ) return; log() << "FileAllocator: posix_fallocate failed: " << errnoWithDescription( ret ) << " falling back" << endl; #endif off_t filelen = lseek(fd, 0, SEEK_END); if ( filelen < size ) { if (filelen != 0) { stringstream ss; ss << "failure creating new datafile; lseek failed for fd " << fd << " with errno: " << errnoWithDescription(); uassert( 10440 , ss.str(), filelen == 0 ); } // Check for end of disk. uassert( 10441 , str::stream() << "Unable to allocate new file of size " << size << ' ' << errnoWithDescription(), size - 1 == lseek(fd, size - 1, SEEK_SET) ); uassert( 10442 , str::stream() << "Unable to allocate new file of size " << size << ' ' << errnoWithDescription(), 1 == write(fd, "", 1) ); lseek(fd, 0, SEEK_SET); const long z = 256 * 1024; const boost::scoped_array<char> buf_holder (new char[z]); char* buf = buf_holder.get(); memset(buf, 0, z); long left = size; while ( left > 0 ) { long towrite = left; if ( towrite > z ) towrite = z; int written = write( fd , buf , towrite ); uassert( 10443 , errnoWithPrefix("FileAllocator: file write failed" ), written > 0 ); left -= written; } } }
void SSLManager::_handleSSLError(int code) { switch (code) { case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_WRITE: // should not happen because we turned on AUTO_RETRY // However, it turns out this CAN happen during a connect, if the other side // accepts the socket connection but fails to do the SSL handshake in a timely // manner. error() << "SSL error: " << code << ", possibly timed out during connect" << endl; throw SocketException(SocketException::CONNECT_ERROR, ""); break; case SSL_ERROR_SYSCALL: if (code < 0) { error() << "socket error: " << errnoWithDescription() << endl; throw SocketException(SocketException::CONNECT_ERROR, ""); } error() << "could not negotiate SSL connection: EOF detected" << endl; throw SocketException(SocketException::CONNECT_ERROR, ""); break; case SSL_ERROR_SSL: { int ret = ERR_get_error(); error() << _getSSLErrorMessage(ret) << endl; throw SocketException(SocketException::CONNECT_ERROR, ""); break; } case SSL_ERROR_ZERO_RETURN: error() << "could not negotiate SSL connection: EOF detected" << endl; throw SocketException(SocketException::CONNECT_ERROR, ""); break; default: error() << "unrecognized SSL error" << endl; throw SocketException(SocketException::CONNECT_ERROR, ""); break; } }
void* MemoryMappedFile::remapPrivateView(void* oldPrivateAddr) { #if defined(__sunos__) // SERVER-8795 LockMongoFilesExclusive lockMongoFiles; #endif // don't unmap, just mmap over the old region void* x = mmap(oldPrivateAddr, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_NORESERVE | MAP_FIXED, fd, 0); if (x == MAP_FAILED) { int err = errno; error() << "13601 Couldn't remap private view: " << errnoWithDescription(err) << endl; log() << "aborting" << endl; printMemInfo(); abort(); } verify(x == oldPrivateAddr); return x; }
bool Socket::connect(SockAddr& remote) { _remote = remote; _fd = socket(remote.getType(), SOCK_STREAM, 0); if ( _fd == INVALID_SOCKET ) { log(_logLevel) << "ERROR: connect invalid socket " << errnoWithDescription() << endl; return false; } if ( _timeout > 0 ) { setSockTimeouts( _fd, _timeout ); } ConnectBG bg(_fd, remote); bg.go(); if ( bg.wait(5000) ) { if ( bg.inError() ) { close(); return false; } } else { // time out the connect close(); bg.wait(); // so bg stays in scope until bg thread terminates return false; } if (remote.getType() != AF_UNIX) disableNagle(_fd); #ifdef SO_NOSIGPIPE // osx const int one = 1; setsockopt( _fd , SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(int)); #endif return true; }
LogFile::LogFile(const std::string& name, bool readwrite) : _name(name) { int options = O_CREAT | (readwrite ? O_RDWR : O_WRONLY) #if defined(O_DIRECT) | O_DIRECT #endif #if defined(O_NOATIME) | O_NOATIME #endif ; _fd = open(name.c_str(), options, S_IRUSR | S_IWUSR); _blkSize = g_minOSPageSizeBytes; #if defined(O_DIRECT) _direct = true; if (_fd < 0) { _direct = false; options &= ~O_DIRECT; _fd = open(name.c_str(), options, S_IRUSR | S_IWUSR); } #ifdef __linux__ ssize_t tmpBlkSize = ioctl(_fd, BLKBSZGET); // TODO: We need some sanity checking on tmpBlkSize even if ioctl() did not fail. if (tmpBlkSize > 0) { _blkSize = (size_t)tmpBlkSize; } #endif #else _direct = false; #endif if (_fd < 0) { uasserted(13516, str::stream() << "couldn't open file " << name << " for writing " << errnoWithDescription()); } flushMyDirectory(name); }
MAdvise::MAdvise(void *p, unsigned len, Advice a) { _p = (void*)((long)p & ~(g_minOSPageSizeBytes-1)); _len = len +((unsigned long long)p-(unsigned long long)_p); int advice = 0; switch ( a ) { case Sequential: advice = MADV_SEQUENTIAL; break; case Random: advice = MADV_RANDOM; break; default: verify(0); } if ( madvise(_p,_len,advice ) ) { error() << "madvise failed: " << errnoWithDescription() << endl; } }
/** Set process wide current working directory. */ BSONObj cd(const BSONObj& args, void* data) { uassert(16830, "cd requires one argument -- cd(directory)", args.nFields() == 1); uassert(16831, "cd requires a string argument -- cd(directory)", args.firstElement().type() == String); #if defined(_WIN32) std::wstring dir = toWideString(args.firstElement().String().c_str()); if (SetCurrentDirectoryW(dir.c_str())) { return BSONObj(); } #else std::string dir = args.firstElement().String(); if (chdir(dir.c_str()) == 0) { return BSONObj(); } #endif uasserted(16832, mongoutils::str::stream() << "cd command failed: " << errnoWithDescription()); return BSONObj(); }
void SSLManager::_handleSSLError(int code) { switch (code) { case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_WRITE: // should not happen because we turned on AUTO_RETRY error() << "SSL error: " << code << endl; fassertFailed( 16676 ); break; case SSL_ERROR_SYSCALL: if (code < 0) { error() << "socket error: " << errnoWithDescription() << endl; throw SocketException(SocketException::CONNECT_ERROR, ""); } error() << "could not negotiate SSL connection: EOF detected" << endl; throw SocketException(SocketException::CONNECT_ERROR, ""); break; case SSL_ERROR_SSL: { int ret = ERR_get_error(); error() << _getSSLErrorMessage(ret) << endl; throw SocketException(SocketException::CONNECT_ERROR, ""); break; } case SSL_ERROR_ZERO_RETURN: error() << "could not negotiate SSL connection: EOF detected" << endl; throw SocketException(SocketException::CONNECT_ERROR, ""); break; default: error() << "unrecognized SSL error" << endl; throw SocketException(SocketException::CONNECT_ERROR, ""); break; } }
double ProcessInfo::getSystemMemoryPressurePercentage() { MEMORYSTATUSEX mse; mse.dwLength = sizeof(mse); BOOL status = GlobalMemoryStatusEx(&mse); if (!status) { DWORD gle = GetLastError(); error() << "GlobalMemoryStatusEx failed with " << errnoWithDescription(gle); fassert(28623, status); } DWORDLONG totalPageFile = mse.ullTotalPageFile; if (totalPageFile == 0) { return false; } // If the page file is >= 50%, say we are low on system memory // If the page file is >= 75%, we are running very low on system memory // DWORDLONG highWatermark = totalPageFile / 2; DWORDLONG veryHighWatermark = 3 * (totalPageFile / 4); DWORDLONG usedPageFile = mse.ullTotalPageFile - mse.ullAvailPageFile; // Below the watermark, we are fine // Also check we will not do a divide by zero below if (usedPageFile < highWatermark || veryHighWatermark <= highWatermark) { return 0.0; } // Above the high watermark, we tell MMapV1 how much to remap // < 1.0, we have some pressure, but not much so do not be very aggressive // 1.0 = we are at very high watermark, remap everything // > 1.0, the user may run out of memory, remap everything // i.e., Example (N - 50) / (75 - 50) return static_cast<double>(usedPageFile - highWatermark) / (veryHighWatermark - highWatermark); }
void* MemoryMappedFile::createPrivateMap() { verify( maphandle ); scoped_lock lk(mapViewMutex); LPVOID thisAddress = getNextMemoryMappedFileLocation( len ); void* privateMapAddress = MapViewOfFileEx( maphandle, // file mapping handle FILE_MAP_READ, // access 0, 0, // file offset, high and low 0, // bytes to map, 0 == all thisAddress ); // address to place file if ( privateMapAddress == 0 ) { DWORD dosError = GetLastError(); log() << "MapViewOfFileEx for " << filename() << " failed with error " << errnoWithDescription( dosError ) << " (file size is " << len << ")" << " in MemoryMappedFile::createPrivateMap" << endl; fassertFailed( 16167 ); } clearWritableBits( privateMapAddress ); views.push_back( privateMapAddress ); memconcept::is( privateMapAddress, memconcept::concept::memorymappedfile, filename() ); return privateMapAddress; }
void LogFile::synchronousAppend(const void* _buf, size_t _len) { const size_t BlockSize = 8 * 1024 * 1024; verify(_fd); verify(_len % g_minOSPageSizeBytes == 0); const char* buf = (const char*)_buf; size_t left = _len; while (left) { size_t toWrite = std::min(left, BlockSize); DWORD written; if (!WriteFile(_fd, buf, toWrite, &written, NULL)) { DWORD e = GetLastError(); if (e == 87) msgasserted(13519, "error 87 appending to file - invalid parameter"); else uasserted(13517, str::stream() << "error appending to file " << _name << ' ' << _len << ' ' << toWrite << ' ' << errnoWithDescription(e)); } else { dassert(written == toWrite); } left -= written; buf += written; } }
void disableNagle(int sock) { int x = 1; #ifdef SOL_TCP int level = SOL_TCP; #else int level = SOL_SOCKET; #endif if ( setsockopt(sock, level, TCP_NODELAY, (char *) &x, sizeof(x)) ) error() << "disableNagle failed: " << errnoWithDescription() << endl; #ifdef SO_KEEPALIVE if ( setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *) &x, sizeof(x)) ) error() << "SO_KEEPALIVE failed: " << errnoWithDescription() << endl; # ifdef __linux__ socklen_t len = sizeof(x); if ( getsockopt(sock, level, TCP_KEEPIDLE, (char *) &x, &len) ) error() << "can't get TCP_KEEPIDLE: " << errnoWithDescription() << endl; if (x > 300) { x = 300; if ( setsockopt(sock, level, TCP_KEEPIDLE, (char *) &x, sizeof(x)) ) { error() << "can't set TCP_KEEPIDLE: " << errnoWithDescription() << endl; } } len = sizeof(x); // just in case it changed if ( getsockopt(sock, level, TCP_KEEPINTVL, (char *) &x, &len) ) error() << "can't get TCP_KEEPINTVL: " << errnoWithDescription() << endl; if (x > 300) { x = 300; if ( setsockopt(sock, level, TCP_KEEPINTVL, (char *) &x, sizeof(x)) ) { error() << "can't set TCP_KEEPINTVL: " << errnoWithDescription() << endl; } } # endif #endif }
void* MemoryMappedFile::map(const char *filenameIn, unsigned long long &length, int options) { setFilename(filenameIn); /* big hack here: Babble uses db names with colons. doesn't seem to work on windows. temporary perhaps. */ char filename[256]; strncpy(filename, filenameIn, 255); filename[255] = 0; { size_t len = strlen( filename ); for ( size_t i=len-1; i>=0; i-- ) { if ( filename[i] == '/' || filename[i] == '\\' ) break; if ( filename[i] == ':' ) filename[i] = '_'; } } updateLength( filename, length ); { DWORD createOptions = FILE_ATTRIBUTE_NORMAL; if ( options & SEQUENTIAL ) createOptions |= FILE_FLAG_SEQUENTIAL_SCAN; DWORD rw = GENERIC_READ | GENERIC_WRITE; fd = CreateFile( toNativeString(filename).c_str(), rw, // desired access FILE_SHARE_WRITE | FILE_SHARE_READ, // share mode NULL, // security OPEN_ALWAYS, // create disposition createOptions , // flags NULL); // hTempl if ( fd == INVALID_HANDLE_VALUE ) { DWORD e = GetLastError(); log() << "Create/OpenFile failed " << filename << " errno:" << e << endl; return 0; } } mapped += length; { DWORD flProtect = PAGE_READWRITE; //(options & READONLY)?PAGE_READONLY:PAGE_READWRITE; maphandle = CreateFileMapping(fd, NULL, flProtect, length >> 32 /*maxsizehigh*/, (unsigned) length /*maxsizelow*/, NULL/*lpName*/); if ( maphandle == NULL ) { DWORD e = GetLastError(); // log() call was killing lasterror before we get to that point in the stream log() << "CreateFileMapping failed " << filename << ' ' << errnoWithDescription(e) << endl; return 0; } } void *view = 0; { scoped_lock lk(mapViewMutex); DWORD access = (options&READONLY)? FILE_MAP_READ : FILE_MAP_ALL_ACCESS; view = MapViewOfFile(maphandle, access, /*f ofs hi*/0, /*f ofs lo*/ 0, /*dwNumberOfBytesToMap 0 means to eof*/0); } if ( view == 0 ) { DWORD e = GetLastError(); log() << "MapViewOfFile failed " << filename << " " << errnoWithDescription(e) << endl; } else { views.push_back(view); } len = length; return view; }
// // system warnings // void show_warnings() { // each message adds a leading and a trailing newline bool warned = false; { const char * foo = strchr( versionString , '.' ) + 1; int bar = atoi( foo ); if ( ( 2 * ( bar / 2 ) ) != bar ) { log() << startupWarningsLog; log() << "** NOTE: This is a development version (" << versionString << ") of MongoDB." << startupWarningsLog; log() << "** Not recommended for production." << startupWarningsLog; warned = true; } } if ( sizeof(int*) == 4 ) { log() << startupWarningsLog; log() << "** NOTE: This is a 32 bit MongoDB binary." << startupWarningsLog; log() << "** 32 bit builds are limited to less than 2GB of data (or less with --journal)." << startupWarningsLog; if( !cmdLine.dur ) { log() << "** Note that journaling defaults to off for 32 bit and is currently off." << startupWarningsLog; } log() << "** See http://dochub.mongodb.org/core/32bit" << startupWarningsLog; warned = true; } if ( !ProcessInfo::blockCheckSupported() ) { log() << startupWarningsLog; log() << "** NOTE: your operating system version does not support the method that MongoDB" << startupWarningsLog; log() << "** uses to detect impending page faults." << startupWarningsLog; log() << "** This may result in slower performance for certain use cases" << startupWarningsLog; warned = true; } #ifdef __linux__ if (boost::filesystem::exists("/proc/vz") && !boost::filesystem::exists("/proc/bc")) { log() << startupWarningsLog; log() << "** WARNING: You are running in OpenVZ. This is known to be broken!!!" << startupWarningsLog; warned = true; } if (boost::filesystem::exists("/sys/devices/system/node/node1")){ // We are on a box with a NUMA enabled kernel and more than 1 numa node (they start at node0) // Now we look at the first line of /proc/self/numa_maps // // Bad example: // $ cat /proc/self/numa_maps // 00400000 default file=/bin/cat mapped=6 N4=6 // // Good example: // $ numactl --interleave=all cat /proc/self/numa_maps // 00400000 interleave:0-7 file=/bin/cat mapped=6 N4=6 std::ifstream f("/proc/self/numa_maps", std::ifstream::in); if (f.is_open()) { std::string line; //we only need the first line std::getline(f, line); if (f.fail()) { warning() << "failed to read from /proc/self/numa_maps: " << errnoWithDescription() << startupWarningsLog; warned = true; } else { // skip over pointer std::string::size_type where = line.find(' '); if ( (where == std::string::npos) || (++where == line.size()) ) { log() << startupWarningsLog; log() << "** WARNING: cannot parse numa_maps line: '" << line << "'" << startupWarningsLog; warned = true; } // if the text following the space doesn't begin with 'interleave', then // issue the warning. else if ( line.find("interleave", where) != where ) { log() << startupWarningsLog; log() << "** WARNING: You are running on a NUMA machine." << startupWarningsLog; log() << "** We suggest launching mongod like this to avoid performance problems:" << startupWarningsLog; log() << "** numactl --interleave=all mongod [other options]" << startupWarningsLog; warned = true; } } } } if (cmdLine.dur){ fstream f ("/proc/sys/vm/overcommit_memory", ios_base::in); unsigned val; f >> val; if (val == 2) { log() << startupWarningsLog; log() << "** WARNING: /proc/sys/vm/overcommit_memory is " << val << startupWarningsLog; log() << "** Journaling works best with it set to 0 or 1" << startupWarningsLog; } }
LogFile::LogFile(const std::string& name, bool readwrite) : _name(name) { _fd = CreateFile( toNativeString(name.c_str()).c_str(), (readwrite?GENERIC_READ:0)|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_FLAG_NO_BUFFERING, NULL); if( _fd == INVALID_HANDLE_VALUE ) { DWORD e = GetLastError(); uasserted(13518, str::stream() << "couldn't open file " << name << " for writing " << errnoWithDescription(e)); } SetFilePointer(_fd, 0, 0, FILE_BEGIN); }
void setSocketKeepAliveParams(int sock, unsigned int maxKeepIdleSecs, unsigned int maxKeepIntvlSecs) { #ifdef _WIN32 // Defaults per MSDN when registry key does not exist. // Expressed in seconds here to be consistent with posix, // though Windows uses milliseconds. const DWORD kWindowsKeepAliveTimeSecsDefault = 2 * 60 * 60; const DWORD kWindowsKeepAliveIntervalSecsDefault = 1; const auto getKey = [](const CString& key, DWORD default_value) { auto withval = windows::getDWORDRegistryKey(kKeepAliveGroup, key); if (withval.isOK()) { auto val = withval.getValue(); // Return seconds return val ? (val.get() / 1000) : default_value; } error() << "can't get KeepAlive parameter: " << withval.getStatus(); return default_value; }; const auto keepIdleSecs = getKey(kKeepAliveTime, kWindowsKeepAliveTimeSecsDefault); const auto keepIntvlSecs = getKey(kKeepAliveInterval, kWindowsKeepAliveIntervalSecsDefault); if ((keepIdleSecs > maxKeepIdleSecs) || (keepIntvlSecs > maxKeepIntvlSecs)) { DWORD sent = 0; struct tcp_keepalive keepalive; keepalive.onoff = TRUE; keepalive.keepalivetime = std::min<DWORD>(keepIdleSecs, maxKeepIdleSecs) * 1000; keepalive.keepaliveinterval = std::min<DWORD>(keepIntvlSecs, maxKeepIntvlSecs) * 1000; if (WSAIoctl(sock, SIO_KEEPALIVE_VALS, &keepalive, sizeof(keepalive), nullptr, 0, &sent, nullptr, nullptr)) { error() << "failed setting keepalive values: " << WSAGetLastError(); } } #elif defined(__APPLE__) || defined(__linux__) const auto updateSockOpt = [sock](int level, int optnum, unsigned int maxval, StringData optname) { unsigned int optval = 1; socklen_t len = sizeof(optval); if (getsockopt(sock, level, optnum, (char*)&optval, &len)) { error() << "can't get " << optname << ": " << errnoWithDescription(); } if (optval > maxval) { optval = maxval; if (setsockopt(sock, level, optnum, (char*)&optval, sizeof(optval))) { error() << "can't set " << optname << ": " << errnoWithDescription(); } } }; #ifdef __APPLE__ updateSockOpt(IPPROTO_TCP, TCP_KEEPALIVE, maxKeepIdleSecs, "TCP_KEEPALIVE"); #endif #ifdef __linux__ #ifdef SOL_TCP const int level = SOL_TCP; #else const int level = SOL_SOCKET; #endif updateSockOpt(level, TCP_KEEPIDLE, maxKeepIdleSecs, "TCP_KEEPIDLE"); updateSockOpt(level, TCP_KEEPINTVL, maxKeepIntvlSecs, "TCP_KEEPINTVL"); #endif #endif }