@@ -109,14 +109,6 @@ static bool maybeCloseFile(HANDLE&);
109109static jrd_file* seek_file (jrd_file*, BufferDesc*, OVERLAPPED*);
110110static jrd_file* setup_file (Database*, const Firebird::PathName&, HANDLE, bool , bool );
111111static bool nt_error (const TEXT*, const jrd_file*, ISC_STATUS, FbStatusVector* const );
112- static void adjustFileSystemCacheSize ();
113-
114- struct AdjustFsCache
115- {
116- static void init () { adjustFileSystemCacheSize (); }
117- };
118-
119- static InitMutex<AdjustFsCache> adjustFsCacheOnce (" AdjustFsCacheOnce" );
120112
121113inline static DWORD getShareFlags (const bool shared_access, bool temporary = false )
122114{
@@ -192,8 +184,6 @@ jrd_file* PIO_create(thread_db* tdbb, const Firebird::PathName& string,
192184 * Create a new database file.
193185 *
194186 **************************************/
195- adjustFsCacheOnce.init ();
196-
197187 Database* const dbb = tdbb->getDatabase ();
198188
199189 const TEXT* file_name = string.c_str ();
@@ -515,8 +505,6 @@ jrd_file* PIO_open(thread_db* tdbb,
515505 bool readOnly = false ;
516506 const bool shareMode = dbb->dbb_config ->getServerMode () != MODE_SUPER;
517507
518- adjustFsCacheOnce.init ();
519-
520508 HANDLE desc = CreateFile (ptr,
521509 GENERIC_READ | GENERIC_WRITE,
522510 getShareFlags (shareMode),
@@ -954,159 +942,3 @@ static bool nt_error(const TEXT* string,
954942
955943 return false ;
956944}
957-
958- // These are defined in Windows Server 2008 SDK
959- #ifndef FILE_CACHE_FLAGS_DEFINED
960- #define FILE_CACHE_MAX_HARD_ENABLE 0x00000001
961- #define FILE_CACHE_MAX_HARD_DISABLE 0x00000002
962- #define FILE_CACHE_MIN_HARD_ENABLE 0x00000004
963- #define FILE_CACHE_MIN_HARD_DISABLE 0x00000008
964- #endif // FILE_CACHE_FLAGS_DEFINED
965-
966- BOOL SetPrivilege (
967- HANDLE hToken, // access token handle
968- LPCTSTR lpszPrivilege, // name of privilege to enable/disable
969- BOOL bEnablePrivilege) // to enable or disable privilege
970- {
971- TOKEN_PRIVILEGES tp;
972- LUID luid;
973-
974- if (!LookupPrivilegeValue (
975- NULL , // lookup privilege on local system
976- lpszPrivilege, // privilege to lookup
977- &luid)) // receives LUID of privilege
978- {
979- // gds__log("LookupPrivilegeValue error: %u", GetLastError() );
980- return FALSE ;
981- }
982-
983- tp.PrivilegeCount = 1 ;
984- tp.Privileges [0 ].Luid = luid;
985- if (bEnablePrivilege)
986- tp.Privileges [0 ].Attributes = SE_PRIVILEGE_ENABLED;
987- else
988- tp.Privileges [0 ].Attributes = 0 ;
989-
990- // Enable or disable the privilege
991-
992- if (!AdjustTokenPrivileges (hToken, FALSE , &tp, sizeof (TOKEN_PRIVILEGES),
993- (PTOKEN_PRIVILEGES) NULL , (PDWORD) NULL ))
994- {
995- // gds__log("AdjustTokenPrivileges error: %u", GetLastError() );
996- return FALSE ;
997- }
998-
999- if (GetLastError () == ERROR_NOT_ALL_ASSIGNED)
1000- {
1001- // gds__log("The token does not have the specified privilege");
1002- return FALSE ;
1003- }
1004-
1005- return TRUE ;
1006- }
1007-
1008- static void adjustFileSystemCacheSize ()
1009- {
1010- int percent = Config::getFileSystemCacheSize ();
1011-
1012- // firebird.conf asks to do nothing
1013- if (percent == 0 )
1014- return ;
1015-
1016- // Ensure that the setting has a sensible value
1017- if (percent > 95 || percent < 10 )
1018- {
1019- gds__log (" Incorrect FileSystemCacheSize setting %d. Using default (30 percent)." , percent);
1020- percent = 30 ;
1021- }
1022-
1023- HMODULE hmodKernel32 = GetModuleHandle (" kernel32.dll" );
1024-
1025- // This one requires 64-bit XP or Windows Server 2003 SP1
1026- typedef BOOL (WINAPI *PFnSetSystemFileCacheSize)(SIZE_T, SIZE_T, DWORD);
1027-
1028- typedef BOOL (WINAPI *PFnGetSystemFileCacheSize)(PSIZE_T, PSIZE_T, PDWORD);
1029-
1030- // This one needs any NT, but load it dynamically anyways just in case
1031- typedef BOOL (WINAPI *PFnGlobalMemoryStatusEx)(LPMEMORYSTATUSEX);
1032-
1033- PFnSetSystemFileCacheSize pfnSetSystemFileCacheSize =
1034- (PFnSetSystemFileCacheSize) GetProcAddress (hmodKernel32, " SetSystemFileCacheSize" );
1035- PFnGetSystemFileCacheSize pfnGetSystemFileCacheSize =
1036- (PFnGetSystemFileCacheSize) GetProcAddress (hmodKernel32, " GetSystemFileCacheSize" );
1037- PFnGlobalMemoryStatusEx pfnGlobalMemoryStatusEx =
1038- (PFnGlobalMemoryStatusEx) GetProcAddress (hmodKernel32, " GlobalMemoryStatusEx" );
1039-
1040- // If we got too old OS and functions are not there - do not bother
1041- if (!pfnGetSystemFileCacheSize || !pfnSetSystemFileCacheSize || !pfnGlobalMemoryStatusEx)
1042- return ;
1043-
1044- MEMORYSTATUSEX msex;
1045- msex.dwLength = sizeof (msex);
1046-
1047- // This should work
1048- if (!pfnGlobalMemoryStatusEx (&msex))
1049- system_call_failed::raise (" GlobalMemoryStatusEx" , GetLastError ());
1050-
1051- SIZE_T origMinimumFileCacheSize, origMaximumFileCacheSize;
1052- DWORD origFlags;
1053-
1054- BOOL result = pfnGetSystemFileCacheSize (&origMinimumFileCacheSize,
1055- &origMaximumFileCacheSize, &origFlags);
1056-
1057- if (!result)
1058- {
1059- const DWORD error = GetLastError ();
1060- #ifndef _WIN64
1061- // This error is returned on 64-bit Windows when the file cache size
1062- // overflows the ULONG limit restricted by the 32-bit Windows API.
1063- // Let's avoid writing it into the log as it's not a critical failure.
1064- if (error != ERROR_ARITHMETIC_OVERFLOW)
1065- #endif
1066- gds__log (" GetSystemFileCacheSize error %d" , error);
1067- return ;
1068- }
1069-
1070- // Somebody has already configured maximum cache size limit
1071- // Hope it is a sensible one - do not bother to adjust it
1072- if ((origFlags & FILE_CACHE_MAX_HARD_ENABLE) != 0 )
1073- return ;
1074-
1075- DWORDLONG maxMem = (msex.ullTotalPhys / 100 ) * percent;
1076-
1077- #ifndef _WIN64
1078- // If we are trying to set the limit so high that it doesn't fit
1079- // in 32-bit API - leave settings alone and write a message to log file
1080- if (maxMem > (SIZE_T)(-2 ))
1081- {
1082- gds__log (" Could not use 32-bit SetSystemFileCacheSize API to set cache size limit to %I64d."
1083- " Please use 64-bit engine or configure cache size limit externally" , maxMem);
1084- return ;
1085- }
1086- #endif
1087-
1088- HANDLE hToken;
1089- if (!OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES, &hToken))
1090- {
1091- gds__log (" OpenProcessToken error %d" , GetLastError ());
1092- return ;
1093- }
1094-
1095- if (SetPrivilege (hToken, " SeIncreaseQuotaPrivilege" , TRUE ))
1096- {
1097- result = pfnSetSystemFileCacheSize (0 , maxMem, FILE_CACHE_MAX_HARD_ENABLE);
1098- const DWORD error = GetLastError ();
1099- SetPrivilege (hToken, " SeIncreaseQuotaPrivilege" , FALSE );
1100-
1101- if (!result)
1102- {
1103- // If we do not have enough permissions - silently ignore the error
1104- gds__log (" SetSystemFileCacheSize error %d. "
1105- " The engine will continue to operate, but the system "
1106- " performance may degrade significantly when working with "
1107- " large databases" , error);
1108- }
1109- }
1110-
1111- CloseHandle (hToken);
1112- }
0 commit comments