You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by ji...@apache.org on 2012/09/24 18:56:58 UTC

svn commit: r1389481 - in /httpd/httpd/trunk: configure.in include/httpd.h modules/generators/mod_status.c server/util.c

Author: jim
Date: Mon Sep 24 16:56:58 2012
New Revision: 1389481

URL: http://svn.apache.org/viewvc?rev=1389481&view=rev
Log:
Would be nice to have some sort of canonical definition
of server loading for Apache. So create a struct that
holds some useful data. The hope is that for those
platforms that lack getloadavg(), people will write
replacements.

Modified:
    httpd/httpd/trunk/configure.in
    httpd/httpd/trunk/include/httpd.h
    httpd/httpd/trunk/modules/generators/mod_status.c
    httpd/httpd/trunk/server/util.c

Modified: httpd/httpd/trunk/configure.in
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/configure.in?rev=1389481&r1=1389480&r2=1389481&view=diff
==============================================================================
--- httpd/httpd/trunk/configure.in (original)
+++ httpd/httpd/trunk/configure.in Mon Sep 24 16:56:58 2012
@@ -483,7 +483,8 @@ bindprocessor \
 prctl \
 timegm \
 getpgid \
-fopen64
+fopen64 \
+getloadavg
 )
 
 dnl confirm that a void pointer is large enough to store a long integer

Modified: httpd/httpd/trunk/include/httpd.h
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/include/httpd.h?rev=1389481&r1=1389480&r2=1389481&view=diff
==============================================================================
--- httpd/httpd/trunk/include/httpd.h (original)
+++ httpd/httpd/trunk/include/httpd.h Mon Sep 24 16:56:58 2012
@@ -1299,6 +1299,24 @@ struct server_rec {
 };
 
 /**
+ * @struct server_load_rec
+ * @brief  A structure to hold various server load params
+ */
+typedef struct ap_sload_t ap_sload_t;
+struct ap_sload_t {
+    /* 1 min loadavg, ala getloadavg() */
+    float loadavg1;
+    /* 5 min loadavg */
+    float loadavg5;
+    /* 15 min loadavg */
+    float loadavg15;
+    /* percentage of process/threads ready/idle (0->100)*/
+    int idle;
+    /* percentage of process/threads busy (0->100) */
+    int busy;
+};
+
+/**
  * Get the context_document_root for a request. This is a generalization of
  * the document root, which is too limited in the presence of mappers like
  * mod_userdir and mod_alias. The context_document_root is the directory
@@ -2187,6 +2205,11 @@ AP_DECLARE(void *) ap_realloc(void *ptr,
                    AP_FN_ATTR_WARN_UNUSED_RESULT
                    AP_FN_ATTR_ALLOC_SIZE(2);
 
+/**
+ * Get server load params
+ * @param ld struct to populate: -1 in fields means error
+ */
+AP_DECLARE(void) ap_get_sload(ap_sload_t *ld);
 
 #define AP_NORESTART APR_OS_START_USEERR + 1
 

Modified: httpd/httpd/trunk/modules/generators/mod_status.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/generators/mod_status.c?rev=1389481&r1=1389480&r2=1389481&view=diff
==============================================================================
--- httpd/httpd/trunk/modules/generators/mod_status.c (original)
+++ httpd/httpd/trunk/modules/generators/mod_status.c Mon Sep 24 16:56:58 2012
@@ -393,6 +393,8 @@ static int status_handler(request_rec *r
                                ap_scoreboard_image->global->restart_time);
 
     if (!short_report) {
+        ap_sload_t t;
+
         ap_rputs(DOCTYPE_HTML_3_2
                  "<html><head>\n"
                  "<title>Apache Status</title>\n"
@@ -419,6 +421,9 @@ static int status_handler(request_rec *r
         ap_rputs("<dt>Server uptime: ", r);
         show_time(r, up_time);
         ap_rputs("</dt>\n", r);
+        ap_get_sload(&t);
+        ap_rprintf(r, "<dt>Server load: %.2f %.2f %.2f [%d:%d]</dt>\n",
+                   t.loadavg1, t.loadavg5, t.loadavg15, t.idle, t.busy);
     }
 
     if (ap_extended_status) {

Modified: httpd/httpd/trunk/server/util.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/util.c?rev=1389481&r1=1389480&r2=1389481&view=diff
==============================================================================
--- httpd/httpd/trunk/server/util.c (original)
+++ httpd/httpd/trunk/server/util.c Mon Sep 24 16:56:58 2012
@@ -63,6 +63,8 @@
 #include <grp.h>
 #endif
 
+#include "ap_mpm.h"
+
 /* A bunch of functions in util.c scan strings looking for certain characters.
  * To make that more efficient we encode a lookup table.  The test_char_table
  * is generated automatically by gen_test_char.c.
@@ -2788,3 +2790,63 @@ AP_DECLARE(void *) ap_realloc(void *ptr,
         ap_abort_on_oom();
     return p;
 }
+
+AP_DECLARE(void) ap_get_sload(ap_sload_t *ld)
+{
+    double la[3];
+    int i, j, num, server_limit, thread_limit;
+    int ready = 0;
+    int busy = 0;
+    int total;
+    ap_generation_t mpm_generation;
+
+    /* preload errored fields, we overwrite */
+    ld->loadavg1 = -1.0;
+    ld->loadavg5 = -1.0;
+    ld->loadavg15 = -1.0;
+    ld->idle = -1;
+    ld->busy = -1;
+
+#if HAVE_GETLOADAVG
+    num = getloadavg(la, 3);
+    if (num > 0) {
+        ld->loadavg1 = (float)la[0];
+    }
+    if (num > 1) {
+        ld->loadavg5 = (float)la[1];
+    }
+    if (num > 2) {
+        ld->loadavg15 = (float)la[2];
+    }
+#endif
+    ap_mpm_query(AP_MPMQ_GENERATION, &mpm_generation);
+    ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit);
+    ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &server_limit);
+
+    for (i = 0; i < server_limit; i++) {
+        process_score *ps;
+        ps = ap_get_scoreboard_process(i);
+
+        for (j = 0; j < thread_limit; j++) {
+            int res;
+            worker_score *ws = NULL;
+            ws = &ap_scoreboard_image->servers[i][j];
+            res = ws->status;
+
+            if (res == SERVER_READY && ps->generation == mpm_generation) {
+                ready++;
+            }
+            else if (res != SERVER_DEAD &&
+                     res != SERVER_STARTING && res != SERVER_IDLE_KILL &&
+                     ps->generation == mpm_generation) {
+                busy++;
+            }
+        }
+    }
+    total = busy + ready;
+    if (total) {
+        ld->idle = ready * 100 / total;
+        ld->busy = busy * 100 / total;
+    }
+
+}