2005-12-28   Thomas Kho <tkho at google.com>, Dan Kegel <dank at kegel.com>
Patch to add self-monitoring capabilities to distccd, part 1/2.
Add dcc_getloadavg(), dcc_getcurrentload().
Signed-off-by: Dan Kegel <dank at kegel.com>

diff -Naur distcc-2.18.3-pristine/configure distcc-2.18.3-loadshed/configure
--- distcc-2.18.3-pristine/configure	2005-11-08 12:00:11.000000000 -0800
+++ distcc-2.18.3-loadshed/configure	2005-12-13 19:10:57.969984000 -0800
@@ -6466,6 +6466,90 @@
 done
 
 
+for ac_func in getloadavg
+do
+as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
+echo "$as_me:$LINENO: checking for $ac_func" >&5
+echo $ECHO_N "checking for $ac_func... $ECHO_C" >&6
+if eval "test \"\${$as_ac_var+set}\" = set"; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  cat >conftest.$ac_ext <<_ACEOF
+#line $LINENO "configure"
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+/* System header to define __stub macros and hopefully few prototypes,
+    which can conflict with char $ac_func (); below.
+    Prefer <limits.h> to <assert.h> if __STDC__ is defined, since
+    <limits.h> exists even on freestanding compilers.  */
+#ifdef __STDC__
+# include <limits.h>
+#else
+# include <assert.h>
+#endif
+/* Override any gcc2 internal prototype to avoid an error.  */
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+/* We use char because int might match the return type of a gcc2
+   builtin and then its argument prototype would still apply.  */
+char $ac_func ();
+/* The GNU C library defines this for functions which it implements
+    to always fail with ENOSYS.  Some functions are actually named
+    something starting with __ and the normal name is an alias.  */
+#if defined (__stub_$ac_func) || defined (__stub___$ac_func)
+choke me
+#else
+char (*f) () = $ac_func;
+#endif
+#ifdef __cplusplus
+}
+#endif
+
+int
+main ()
+{
+return f != $ac_func;
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext conftest$ac_exeext
+if { (eval echo "$as_me:$LINENO: \"$ac_link\"") >&5
+  (eval $ac_link) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+         { ac_try='test -s conftest$ac_exeext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  eval "$as_ac_var=yes"
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+eval "$as_ac_var=no"
+fi
+rm -f conftest.$ac_objext conftest$ac_exeext conftest.$ac_ext
+fi
+echo "$as_me:$LINENO: result: `eval echo '${'$as_ac_var'}'`" >&5
+echo "${ECHO_T}`eval echo '${'$as_ac_var'}'`" >&6
+if test `eval echo '${'$as_ac_var'}'` = yes; then
+  cat >>confdefs.h <<_ACEOF
+#define `echo "HAVE_$ac_func" | $as_tr_cpp` 1
+_ACEOF
+
+fi
+done
+
+
 echo "$as_me:$LINENO: checking whether snprintf is declared" >&5
 echo $ECHO_N "checking whether snprintf is declared... $ECHO_C" >&6
 if test "${ac_cv_have_decl_snprintf+set}" = set; then
diff -Naur distcc-2.18.3-pristine/configure.ac distcc-2.18.3-loadshed/configure.ac
--- distcc-2.18.3-pristine/configure.ac	2005-11-08 12:00:11.000000000 -0800
+++ distcc-2.18.3-loadshed/configure.ac	2005-12-13 19:10:57.974969000 -0800
@@ -304,6 +304,8 @@
 AC_CHECK_FUNCS([getaddrinfo getnameinfo inet_ntop inet_ntoa])
 AC_CHECK_FUNCS([strndup mmap strlcpy])
 
+AC_CHECK_FUNCS([getloadavg])
+
 AC_CHECK_DECLS([snprintf, vsnprintf, vasprintf, asprintf, strndup])
 
 AC_MSG_CHECKING([if mmap() supports MAP_FAILED])
diff -Naur distcc-2.18.3-pristine/src/config.h.in distcc-2.18.3-loadshed/src/config.h.in
--- distcc-2.18.3-pristine/src/config.h.in	2005-11-08 11:59:55.000000000 -0800
+++ distcc-2.18.3-loadshed/src/config.h.in	2005-12-13 19:10:57.986956000 -0800
@@ -56,6 +56,9 @@
 /* Define to 1 if you have the `geteuid' function. */
 #undef HAVE_GETEUID
 
+/* Define to 1 if you have the `getloadavg' function. */
+#undef HAVE_GETLOADAVG
+
 /* Define to 1 if you have the `getnameinfo' function. */
 #undef HAVE_GETNAMEINFO
 
diff -Naur distcc-2.18.3-pristine/src/util.c distcc-2.18.3-loadshed/src/util.c
--- distcc-2.18.3-pristine/src/util.c	2005-11-08 11:59:56.000000000 -0800
+++ distcc-2.18.3-loadshed/src/util.c	2005-12-13 19:10:58.029917000 -0800
@@ -38,6 +39,10 @@
 #include <sys/resource.h>
 #endif
 
+#ifdef HAVE_SYS_LOADAVG_H
+#include <sys/loadavg.h>
+#endif
+
 #include <sys/param.h>
 
 #include "distcc.h"
@@ -344,6 +349,56 @@
     return buf;
 }
 
+/* Return the current number of running processes. */
+int dcc_getcurrentload(void) {
+#if defined(linux)
+  double stats[3];
+  int running;
+  int total;
+  int last_pid;
+  int retval;
+
+  FILE *f = fopen("/proc/loadavg", "r");
+  if (NULL == f)
+      return -1;
+
+  retval = fscanf(f, "%lf %lf %lf %d/%d %d", &stats[0], &stats[1], &stats[2],
+                  &running, &total, &last_pid);
+  fclose(f);
+
+  if (6 != retval)
+      return -1;
+
+  return running;
+#else
+  return -1;
+#endif
+}
+
+/**
+ *  Wrapper for getloadavg() that tries to return all 3 samples, and reports
+ *  -1 for those samples that are not available.
+ *
+ *  Averages are over the last 1, 5, and 15 minutes, respectively.
+ **/
+void dcc_getloadavg(double loadavg[3]) {
+  int num;
+  int i;
+
+#if defined(HAVE_GETLOADAVG)
+  num = getloadavg(loadavg, 3);
+#else
+  num = 0;
+#endif
+
+  /* If getloadavg() didn't return 3 we want to fill
+   * in the invalid elements with -1 */
+  if (num < 0)
+      num = 0;
+
+  for (i=num; i < 3; ++i)
+      loadavg[i] = -1;
+}
 
 
 /**
diff -Naur distcc-2.18.3-pristine/src/util.h distcc-2.18.3-loadshed/src/util.h
--- distcc-2.18.3-pristine/src/util.h	2005-11-08 11:59:57.000000000 -0800
+++ distcc-2.18.3-loadshed/src/util.h	2005-12-13 19:10:58.033910000 -0800
@@ -23,6 +23,8 @@
 #include <setjmp.h>
 
 /* util.c */
+int dcc_getcurrentload(void);
+void dcc_getloadavg(double loadavg[3]);
 int argv_contains(char **argv, const char *s);
 int dcc_redirect_fd(int, const char *fname, int);
 int str_startswith(const char *head, const char *worm);
