Fossil SCM

fossil-scm / src / loadctrl.c
Source Blame History 89 lines
5e26a1b… drh 1 /*
5e26a1b… drh 2 ** Copyright (c) 2014 D. Richard Hipp
5e26a1b… drh 3 **
5e26a1b… drh 4 ** This program is free software; you can redistribute it and/or
5e26a1b… drh 5 ** modify it under the terms of the Simplified BSD License (also
5e26a1b… drh 6 ** known as the "2-Clause License" or "FreeBSD License".)
5e26a1b… drh 7
5e26a1b… drh 8 ** This program is distributed in the hope that it will be useful,
5e26a1b… drh 9 ** but without any warranty; without even the implied warranty of
5e26a1b… drh 10 ** merchantability or fitness for a particular purpose.
5e26a1b… drh 11 **
5e26a1b… drh 12 ** Author contact information:
5e26a1b… drh 13 ** [email protected]
5e26a1b… drh 14 ** http://www.hwaci.com/drh/
5e26a1b… drh 15 **
5e26a1b… drh 16 *******************************************************************************
5e26a1b… drh 17 **
5e26a1b… drh 18 ** This file contains code to check the host load-average and abort
5e26a1b… drh 19 ** CPU-intensive operations if the load-average is too high.
5e26a1b… drh 20 */
5e26a1b… drh 21 #include "config.h"
5e26a1b… drh 22 #include "loadctrl.h"
5e26a1b… drh 23 #include <assert.h>
5e26a1b… drh 24
5e26a1b… drh 25 /*
5e26a1b… drh 26 ** Return the load average for the host processor
5e26a1b… drh 27 */
5e26a1b… drh 28 double load_average(void){
1707129… jan.nijtmans 29 #if !defined(_WIN32) && !defined(FOSSIL_OMIT_LOAD_AVERAGE)
5e26a1b… drh 30 double a[3];
1707129… jan.nijtmans 31 if( getloadavg(a, 3)>0 ){
dede237… drh 32 return a[0]>=0.000001 ? a[0] : 0.000001;
1707129… jan.nijtmans 33 }
5e26a1b… drh 34 #endif
1707129… jan.nijtmans 35 return 0.0;
5e26a1b… drh 36 }
5e26a1b… drh 37
5e26a1b… drh 38 /*
5e26a1b… drh 39 ** COMMAND: test-loadavg
c3b5f1d… jan.nijtmans 40 **
5e26a1b… drh 41 ** %fossil test-loadavg
5e26a1b… drh 42 **
5e26a1b… drh 43 ** Print the load average on the host machine.
5e26a1b… drh 44 */
5e26a1b… drh 45 void loadavg_test_cmd(void){
5e26a1b… drh 46 fossil_print("load-average: %f\n", load_average());
5e26a1b… drh 47 }
5e26a1b… drh 48
5e26a1b… drh 49 /*
278507e… drh 50 ** WEBPAGE: test-overload
278507e… drh 51 **
278507e… drh 52 ** Generate the response that would normally be shown only when
278507e… drh 53 ** service is denied due to an overload condition. This is for
278507e… drh 54 ** testing of the overload warning page.
278507e… drh 55 */
278507e… drh 56 void overload_page(void){
a10f931… drh 57 double mxLoad = atof(db_get("max-loadavg", "0.0"));
278507e… drh 58 style_set_current_feature("test");
278507e… drh 59 style_header("Server Overload");
278507e… drh 60 @ <h2>The server load is currently too high.
278507e… drh 61 @ Please try again later.</h2>
278507e… drh 62 @ <p>Current load average: %f(load_average())<br>
278507e… drh 63 @ Load average limit: %f(mxLoad)<br>
278507e… drh 64 @ URL: %h(g.zBaseURL)%h(P("PATH_INFO"))<br>
278507e… drh 65 @ Timestamp: %h(db_text("","SELECT datetime()"))Z</p>
278507e… drh 66 style_finish_page();
278507e… drh 67 }
278507e… drh 68
278507e… drh 69 /*
9413395… drh 70 ** Abort the current page request if the load average of the host
9413395… drh 71 ** computer is too high. Admin and Setup users are exempt from this
9413395… drh 72 ** restriction.
9413395… drh 73 */
9413395… drh 74 void load_control(void){
a10f931… drh 75 double mxLoad = atof(db_get("max-loadavg", "0.0"));
9413395… drh 76 #if 1
9413395… drh 77 /* Disable this block only to test load restrictions */
9413395… drh 78 if( mxLoad<=0.0 || mxLoad>=load_average() ) return;
9413395… drh 79
9413395… drh 80 login_check_credentials();
9413395… drh 81 if(g.perm.Admin || g.perm.Setup){
9413395… drh 82 return;
9413395… drh 83 }
9413395… drh 84 #endif
278507e… drh 85 overload_page();
d0fa157… drh 86 cgi_set_status(503,"Server Overload");
5e26a1b… drh 87 cgi_reply();
5e26a1b… drh 88 exit(0);
5e26a1b… drh 89 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button