Skip to content

Commit 797edd6

Browse files
author
George Wang
committed
LiteSpeed SAPI v7.9: Process manager keeps forked child process alive for longer time based on load. Reduces PM overhead for busy setup.
1 parent 51e2015 commit 797edd6

File tree

2 files changed

+55
-38
lines changed

2 files changed

+55
-38
lines changed

sapi/litespeed/lsapi_main.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ static int sapi_lsapi_activate()
603603
static sapi_module_struct lsapi_sapi_module =
604604
{
605605
"litespeed",
606-
"LiteSpeed V7.7",
606+
"LiteSpeed V7.9",
607607

608608
php_lsapi_startup, /* startup */
609609
php_module_shutdown_wrapper, /* shutdown */
@@ -806,7 +806,7 @@ static int alter_ini( const char * pKey, int keyLen, const char * pValue, int va
806806
Use ACTIVATE stage in legacy mode only.
807807
808808
RUNTIME stage should be used here,
809-
as with ACTIVATE it's impossible to change the option from script with ini_set
809+
as with ACTIVATE it's impossible to change the option from script with ini_set
810810
*/
811811
if(!mod_lsapi_mode)
812812
{

sapi/litespeed/lsapilib.c

+53-36
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ static int s_ppid;
152152
static int s_restored_ppid = 0;
153153
static int s_pid = 0;
154154
static int s_slow_req_msecs = 0;
155-
static int s_keepListener = 0;
155+
static int s_keep_listener = 1;
156156
static int s_dump_debug_info = 0;
157157
static int s_pid_dump_debug_info = 0;
158158
static int s_req_processed = 0;
@@ -244,8 +244,8 @@ void LSAPI_Log(int flag, const char * fmt, ...)
244244
{
245245
char buf[1024];
246246
char *p = buf;
247-
if ((flag & LSAPI_LOG_TIMESTAMP_BITS) &&
248-
!((flag & LSAPI_LOG_TIMESTAMP_STDERR) && s_stderr_is_pipe))
247+
if ((flag & LSAPI_LOG_TIMESTAMP_BITS)
248+
&& !(s_stderr_is_pipe))
249249
{
250250
struct timeval tv;
251251
struct tm tm;
@@ -272,7 +272,7 @@ void LSAPI_Log(int flag, const char * fmt, ...)
272272

273273
if (flag & LSAPI_LOG_PID)
274274
{
275-
p += snprintf(p, 100, "[%d] ", s_pid);
275+
p += snprintf(p, 100, "[UID:%d][%d] ", getuid(), s_pid);
276276
}
277277

278278
if (p > buf)
@@ -294,7 +294,13 @@ void LSAPI_Log(int flag, const char * fmt, ...)
294294

295295
#endif
296296

297-
#define lsapi_log(...) LSAPI_Log(LSAPI_LOG_TIMESTAMP_FULL|LSAPI_LOG_TIMESTAMP_STDERR|LSAPI_LOG_PID, __VA_ARGS__)
297+
#define lsapi_log(...) LSAPI_Log(LSAPI_LOG_TIMESTAMP_FULL|LSAPI_LOG_PID, __VA_ARGS__)
298+
299+
300+
void lsapi_perror(const char * pMessage, int err_no)
301+
{
302+
lsapi_log("%s, errno: %d (%s)\n", pMessage, err_no, strerror(err_no));
303+
}
298304

299305

300306
static int lsapi_parent_dead()
@@ -893,7 +899,8 @@ int LSAPI_is_suEXEC_Daemon(void)
893899
static int LSAPI_perror_r( LSAPI_Request * pReq, const char * pErr1, const char *pErr2 )
894900
{
895901
char achError[4096];
896-
int n = snprintf(achError, sizeof(achError), "[%d] %s:%s: %s\n", getpid(),
902+
int n = snprintf(achError, sizeof(achError), "[UID:%d][%d] %s:%s: %s\n",
903+
getuid(), getpid(),
897904
pErr1, (pErr2)?pErr2:"", strerror(errno));
898905
if (n > (int)sizeof(achError))
899906
n = sizeof(achError);
@@ -939,7 +946,7 @@ static int lsapi_enterLVE( LSAPI_Request * pReq, uid_t uid )
939946
ret = (*fp_lve_enter)(s_lve, uid, -1, -1, &cookie);
940947
if ( ret < 0 )
941948
{
942-
lsapi_log("enter LVE (%d) : ressult: %d !\n", uid, ret );
949+
//lsapi_log("enter LVE (%d) : ressult: %d !\n", uid, ret );
943950
LSAPI_perror_r(pReq, "LSAPI: lve_enter() failure, reached resource limit.", NULL );
944951
lsapi_lve_error( pReq );
945952
return -1;
@@ -2938,14 +2945,14 @@ static void lsapi_sigchild( int signal )
29382945

29392946
static int lsapi_init_children_status(void)
29402947
{
2948+
char * pBuf;
29412949
int size = 4096;
29422950
int max_children;
29432951
if (g_prefork_server->m_pChildrenStatus)
29442952
return 0;
29452953
max_children = g_prefork_server->m_iMaxChildren
29462954
+ g_prefork_server->m_iExtraChildren;
29472955

2948-
char * pBuf;
29492956
size = max_children * sizeof( lsapi_child_status ) * 2 + 3 * sizeof(int);
29502957
size = (size + 4095) / 4096 * 4096;
29512958
pBuf =( char*) mmap( NULL, size, PROT_READ | PROT_WRITE,
@@ -2978,9 +2985,9 @@ static void dump_debug_info( lsapi_child_status * pStatus, long tmCur )
29782985
return;
29792986
}
29802987

2981-
lsapi_log("Possible runaway process, PPID: %d, PID: %d, "
2988+
lsapi_log("Possible runaway process, UID: %d, PPID: %d, PID: %d, "
29822989
"reqCount: %d, process time: %ld, checkpoint time: %ld, start "
2983-
"time: %ld\n", getpid(), pStatus->m_pid,
2990+
"time: %ld\n", getuid(), getppid(), pStatus->m_pid,
29842991
pStatus->m_iReqCounter, tmCur - pStatus->m_tmReqBegin,
29852992
tmCur - pStatus->m_tmLastCheckPoint, tmCur - pStatus->m_tmStart );
29862993

@@ -3008,12 +3015,11 @@ static void lsapi_check_child_status( long tmCur )
30083015
while( pStatus < pEnd )
30093016
{
30103017
tobekilled = 0;
3011-
if ( pStatus->m_pid != 0 )
3018+
if ( pStatus->m_pid != 0 && pStatus->m_pid != -1)
30123019
{
30133020
++count;
30143021
if ( !pStatus->m_inProcess )
30153022
{
3016-
30173023
if (g_prefork_server->m_iCurChildren - dying
30183024
> g_prefork_server->m_iMaxChildren
30193025
|| idle > g_prefork_server->m_iMaxIdleChildren)
@@ -3030,7 +3036,7 @@ static void lsapi_check_child_status( long tmCur )
30303036
//tobekilled = SIGUSR1;
30313037
}
30323038
}
3033-
if ( !tobekilled )
3039+
if (!pStatus->m_iKillSent)
30343040
++idle;
30353041
}
30363042
else
@@ -3184,6 +3190,9 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
31843190

31853191
#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
31863192
*s_avail_pages = sysconf(_SC_AVPHYS_PAGES);
3193+
// lsapi_log("Memory total: %zd, free: %zd, free %%%zd\n",
3194+
// s_total_pages, *s_avail_pages, *s_avail_pages * 100 / s_total_pages);
3195+
31873196
#endif
31883197
FD_ZERO( &readfds );
31893198
FD_SET( pServer->m_fd, &readfds );
@@ -3196,7 +3205,8 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
31963205
if (s_accepting_workers)
31973206
accepting = __sync_add_and_fetch(s_accepting_workers, 0);
31983207

3199-
if (pServer->m_iCurChildren > 0 && accepting > 0)
3208+
if (pServer->m_iCurChildren > 0
3209+
&& accepting > 0)
32003210
{
32013211
usleep(400);
32023212
while(accepting-- > 0)
@@ -3265,10 +3275,10 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
32653275
__sync_add_and_fetch(s_busy_workers, 1);
32663276
lsapi_set_nblock( pReq->m_fd, 0 );
32673277
//keep it open if busy_count is used.
3268-
if (s_busy_workers
3278+
if (!s_keep_listener && s_busy_workers
32693279
&& *s_busy_workers > (pServer->m_iMaxChildren >> 1))
3270-
s_keepListener = 1;
3271-
if ((s_uid == 0 || !s_keepListener || !is_enough_free_mem())
3280+
s_keep_listener = 1;
3281+
if ((s_uid == 0 || !s_keep_listener || !is_enough_free_mem())
32723282
&& pReq->m_fdListen != -1 )
32733283
{
32743284
close( pReq->m_fdListen );
@@ -3289,7 +3299,9 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
32893299
}
32903300
else if ( pid == -1 )
32913301
{
3292-
perror( "fork() failed, please increase process limit" );
3302+
lsapi_perror("fork() failed, please increase process limit", errno);
3303+
if (child_status)
3304+
child_status->m_pid = 0;
32933305
}
32943306
else
32953307
{
@@ -3312,7 +3324,7 @@ static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer,
33123324
{
33133325
if (( errno == EINTR )||( errno == EAGAIN))
33143326
continue;
3315-
perror( "accept() failed" );
3327+
lsapi_perror("accept() failed", errno);
33163328
return -1;
33173329
}
33183330
}
@@ -3350,10 +3362,10 @@ int LSAPI_Postfork_Child(LSAPI_Request * pReq)
33503362
__sync_add_and_fetch(s_busy_workers, 1);
33513363
lsapi_set_nblock( pReq->m_fd, 0 );
33523364
//keep it open if busy_count is used.
3353-
if (s_busy_workers
3365+
if (!s_keep_listener && s_busy_workers
33543366
&& *s_busy_workers > (max_children >> 1))
3355-
s_keepListener = 1;
3356-
if ((s_uid == 0 || !s_keepListener || !is_enough_free_mem())
3367+
s_keep_listener = 1;
3368+
if ((s_uid == 0 || !s_keep_listener || !is_enough_free_mem())
33573369
&& pReq->m_fdListen != -1 )
33583370
{
33593371
close(pReq->m_fdListen);
@@ -3512,7 +3524,7 @@ int LSAPI_Accept_Before_Fork(LSAPI_Request * pReq)
35123524
{
35133525
if ((errno == EINTR) || (errno == EAGAIN))
35143526
continue;
3515-
perror( "accept() failed" );
3527+
lsapi_perror("accept() failed", errno);
35163528
ret = -1;
35173529
break;
35183530
}
@@ -3528,13 +3540,6 @@ int LSAPI_Accept_Before_Fork(LSAPI_Request * pReq)
35283540
}
35293541

35303542

3531-
void lsapi_perror( const char * pMessage, int err_no )
3532-
{
3533-
lsapi_log("%s, errno: %d (%s)\n", pMessage, err_no,
3534-
strerror( err_no ) );
3535-
}
3536-
3537-
35383543
int LSAPI_Prefork_Accept_r( LSAPI_Request * pReq )
35393544
{
35403545
int fd;
@@ -3620,9 +3625,21 @@ int LSAPI_Prefork_Accept_r( LSAPI_Request * pReq )
36203625
if ( s_worker_status )
36213626
{
36223627
s_worker_status->m_inProcess = 0;
3623-
if (fd == pReq->m_fdListen
3624-
&& (s_keepListener != 2 || !is_enough_free_mem()))
3625-
return -1;
3628+
if (fd == pReq->m_fdListen)
3629+
{
3630+
if (s_keep_listener == 0 || !is_enough_free_mem())
3631+
return -1;
3632+
if (s_keep_listener == 1)
3633+
{
3634+
int wait_time = 10;
3635+
if (s_busy_workers)
3636+
wait_time += *s_busy_workers * 10;
3637+
if (s_accepting_workers)
3638+
wait_time >>= (*s_accepting_workers);
3639+
if (wait_secs >= wait_time)
3640+
return -1;
3641+
}
3642+
}
36263643
}
36273644
++wait_secs;
36283645
if (( s_max_idle_secs > 0 )&&(wait_secs >= s_max_idle_secs ))
@@ -3657,7 +3674,7 @@ int LSAPI_Prefork_Accept_r( LSAPI_Request * pReq )
36573674

36583675
lsapi_set_nblock( fd, 0 );
36593676
//init_conn_key( pReq->m_fd );
3660-
if (!s_keepListener)
3677+
if (!s_keep_listener)
36613678
{
36623679
close( pReq->m_fdListen );
36633680
pReq->m_fdListen = -1;
@@ -3949,7 +3966,7 @@ int LSAPI_Init_Env_Parameters( fn_select_t fp )
39493966
if ( p )
39503967
{
39513968
n = atoi( p );
3952-
s_keepListener = n;
3969+
s_keep_listener = n;
39533970
}
39543971

39553972
p = getenv( "LSAPI_AVOID_FORK" );
@@ -3958,7 +3975,7 @@ int LSAPI_Init_Env_Parameters( fn_select_t fp )
39583975
avoidFork = atoi( p );
39593976
if (avoidFork)
39603977
{
3961-
s_keepListener = 2;
3978+
s_keep_listener = 2;
39623979
ch = *(p + strlen(p) - 1);
39633980
if ( ch == 'G' || ch == 'g' )
39643981
avoidFork *= 1024 * 1024 * 1024;

0 commit comments

Comments
 (0)