Skip to content

Commit

Permalink
[Shared] Remove hardcoded limit of 1024 pk3s. Try to increase the max…
Browse files Browse the repository at this point in the history
…imum allowed file descriptors to 4096 on start (can be overriden with "-maxfds" on launch).

Ported from jk2mv.
  • Loading branch information
Daggolin committed Nov 6, 2023
1 parent 19c162c commit fb888be
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 30 deletions.
17 changes: 4 additions & 13 deletions code/qcommon/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2727,7 +2727,6 @@ Sets fs_gamedir, adds the directory to the head of the path,
then loads the zip headers
================
*/
#define MAX_PAKFILES 1024
static void FS_AddGameDirectory( const char *path, const char *dir ) {
searchpath_t *sp;
int i;
Expand All @@ -2737,7 +2736,6 @@ static void FS_AddGameDirectory( const char *path, const char *dir ) {
char curpath[MAX_OSPATH + 1], *pakfile;
int numfiles;
char **pakfiles;
char *sorted[MAX_PAKFILES];

// this fixes the case where fs_basepath is the same as fs_cdpath
// which happens on full installs
Expand Down Expand Up @@ -2771,20 +2769,13 @@ static void FS_AddGameDirectory( const char *path, const char *dir ) {

pakfiles = Sys_ListFiles( curpath, ".pk3", NULL, &numfiles, qfalse );

// sort them so that later alphabetic matches override
// earlier ones. This makes pak1.pk3 override pak0.pk3
if ( numfiles > MAX_PAKFILES ) {
numfiles = MAX_PAKFILES;
if ( numfiles > 1 ) {
qsort( pakfiles, numfiles, sizeof(char*), paksort );
}
for ( i = 0 ; i < numfiles ; i++ ) {
sorted[i] = pakfiles[i];
}

qsort( sorted, numfiles, sizeof(char*), paksort );

for ( i = 0 ; i < numfiles ; i++ ) {
pakfile = FS_BuildOSPath( path, dir, sorted[i] );
if ( ( pak = FS_LoadZipFile( pakfile, sorted[i] ) ) == 0 )
pakfile = FS_BuildOSPath( path, dir, pakfiles[i] );
if ( ( pak = FS_LoadZipFile( pakfile, pakfiles[i] ) ) == 0 )
continue;
Q_strncpyz(pak->pakPathname, curpath, sizeof(pak->pakPathname));
// store the game name for downloading
Expand Down
17 changes: 4 additions & 13 deletions codemp/qcommon/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3047,7 +3047,6 @@ Sets fs_gamedir, adds the directory to the head of the path,
then loads the zip headers
================
*/
#define MAX_PAKFILES 1024
static void FS_AddGameDirectory( const char *path, const char *dir ) {
searchpath_t *sp;
int i;
Expand All @@ -3057,7 +3056,6 @@ static void FS_AddGameDirectory( const char *path, const char *dir ) {
char curpath[MAX_OSPATH + 1], *pakfile;
int numfiles;
char **pakfiles;
char *sorted[MAX_PAKFILES];
const char *filename;

// this fixes the case where fs_basepath is the same as fs_cdpath
Expand Down Expand Up @@ -3090,22 +3088,15 @@ static void FS_AddGameDirectory( const char *path, const char *dir ) {

pakfiles = Sys_ListFiles( curpath, ".pk3", NULL, &numfiles, qfalse );

// sort them so that later alphabetic matches override
// earlier ones. This makes pak1.pk3 override pak0.pk3
if ( numfiles > MAX_PAKFILES ) {
numfiles = MAX_PAKFILES;
if ( numfiles > 1 ) {
qsort( pakfiles, numfiles, sizeof(char*), paksort );
}
for ( i = 0 ; i < numfiles ; i++ ) {
sorted[i] = pakfiles[i];
}

qsort( sorted, numfiles, sizeof(char*), paksort );

for ( i = 0 ; i < numfiles ; i++ ) {
pakfile = FS_BuildOSPath( path, dir, sorted[i] );
pakfile = FS_BuildOSPath( path, dir, pakfiles[i] );
filename = get_filename(pakfile);

if ( ( pak = FS_LoadZipFile( pakfile, sorted[i] ) ) == 0 )
if ( ( pak = FS_LoadZipFile( pakfile, pakfiles[i] ) ) == 0 )
continue;

// files beginning with "dl_" are only loaded when referenced by the server
Expand Down
2 changes: 1 addition & 1 deletion shared/sys/sys_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void IN_Frame( void );
void IN_Shutdown( void );
void IN_Restart( void );

void Sys_PlatformInit( void );
void Sys_PlatformInit( int argc, char *argv[] );
void Sys_PlatformExit( void );
qboolean Sys_GetPacket( netadr_t *net_from, msg_t *net_message );
char *Sys_ConsoleInput( void );
Expand Down
2 changes: 1 addition & 1 deletion shared/sys/sys_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ int main ( int argc, char* argv[] )
int i;
char commandLine[ MAX_STRING_CHARS ] = { 0 };

Sys_PlatformInit();
Sys_PlatformInit( argc, argv );
CON_Init();

// get the initial time base
Expand Down
26 changes: 25 additions & 1 deletion shared/sys/sys_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ along with this program; if not, see <http://www.gnu.org/licenses/>.
#include <libgen.h>
#include <sched.h>
#include <signal.h>
#include <sys/resource.h>

#include "qcommon/qcommon.h"
#include "qcommon/q_shared.h"
Expand All @@ -42,7 +43,11 @@ qboolean stdinIsATTY = qfalse;
// Used to determine where to store user-specific files
static char homePath[ MAX_OSPATH ] = { 0 };

void Sys_PlatformInit( void )
// Max open file descriptors. Mostly used by pk3 files with
// MAX_SEARCH_PATHS limit.
#define MAX_OPEN_FILES 4096

void Sys_PlatformInit( int argc, char *argv[] )
{
const char* term = getenv( "TERM" );

Expand All @@ -56,6 +61,25 @@ void Sys_PlatformInit( void )
stdinIsATTY = qtrue;
else
stdinIsATTY = qfalse;

// raise open file limit to allow more pk3 files
int retval;
struct rlimit rlim;
rlim_t maxfds = MAX_OPEN_FILES;

for (int i = 1; i + 1 < argc; i++) {
if (!Q_stricmp(argv[i], "-maxfds")) {
maxfds = atoi(argv[i + 1]);
}
}

getrlimit(RLIMIT_NOFILE, &rlim);
rlim.rlim_cur = Q_min(maxfds, rlim.rlim_max);
retval = setrlimit(RLIMIT_NOFILE, &rlim);

if (retval == -1) {
Com_Printf("Warning: Failed to raise open file limit. %s\n", strerror(errno));
}
}

void Sys_PlatformExit( void )
Expand Down
22 changes: 21 additions & 1 deletion shared/sys/sys_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,12 @@ Sys_PlatformInit
Platform-specific initialization
================
*/
void Sys_PlatformInit( void ) {

// Max open file descriptors. Mostly used by pk3 files with
// MAX_SEARCH_PATHS limit.
#define MAX_OPEN_FILES 4096

void Sys_PlatformInit( int argc, char *argv[] ) {
TIMECAPS ptc;
if ( timeGetDevCaps( &ptc, sizeof( ptc ) ) == MMSYSERR_NOERROR )
{
Expand All @@ -558,6 +563,21 @@ void Sys_PlatformInit( void ) {
}
else
timerResolution = 0;

// raise open file limit to allow more pk3 files
int maxfds = MAX_OPEN_FILES;

for (int i = 1; i + 1 < argc; i++) {
if (!Q_stricmp(argv[i], "-maxfds")) {
maxfds = atoi(argv[i + 1]);
}
}

maxfds = _setmaxstdio(maxfds);

if (maxfds == -1) {
Com_Printf("Warning: Failed to increase open file limit. %s\n", strerror(errno));
}
}

/*
Expand Down

0 comments on commit fb888be

Please sign in to comment.