Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
Merge pull request #672 from hashrabbit/feature/systemd
Browse files Browse the repository at this point in the history
systemd watchdog and status notification
  • Loading branch information
ckolivas committed Jun 11, 2015
2 parents 1e36d6e + 790793a commit 5f19d7e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Expand Up @@ -25,7 +25,7 @@ bin_PROGRAMS = cgminer
cgminer_LDFLAGS = $(PTHREAD_FLAGS)
cgminer_LDADD = $(DLOPEN_FLAGS) @LIBCURL_LIBS@ @JANSSON_LIBS@ @PTHREAD_LIBS@ \
@NCURSES_LIBS@ @PDCURSES_LIBS@ @WS2_LIBS@ \
@LIBUSB_LIBS@ @MM_LIBS@ @RT_LIBS@ \
@LIBUSB_LIBS@ @MM_LIBS@ @RT_LIBS@ @LIBSYSTEMD_LIBS@ \
@MATH_LIBS@ lib/libgnu.a ccan/libccan.a

cgminer_CPPFLAGS += -I$(top_builddir)/lib -I$(top_srcdir)/lib
Expand Down
2 changes: 2 additions & 0 deletions README
Expand Up @@ -146,6 +146,8 @@ CGMiner specific configuration options:
disabled)
--disable-libcurl Disable building with libcurl for getwork and GBT
support
--enable-libsystemd Compile support for system watchdog and status
notifications (default disabled)
--without-curses Compile support for curses TUI (default enabled)
--with-system-libusb Compile against dynamic system libusb (default use
included static libusb)
Expand Down
54 changes: 53 additions & 1 deletion cgminer.c
Expand Up @@ -34,6 +34,10 @@
#include <semaphore.h>
#endif

#ifdef USE_LIBSYSTEMD
#include <systemd/sd-daemon.h>
#endif

#include <sys/stat.h>
#include <sys/types.h>

Expand Down Expand Up @@ -446,7 +450,7 @@ static int include_count;
static int forkpid;
#endif // defined(unix)

struct sigaction termhandler, inthandler;
struct sigaction termhandler, inthandler, abrthandler;

struct thread_q *getq;

Expand Down Expand Up @@ -4036,6 +4040,10 @@ static void clean_up(bool restarting);
void app_restart(void)
{
applog(LOG_WARNING, "Attempting to restart %s", packagename);
#ifdef USE_LIBSYSTEMD
sd_notify(false, "RELOADING=1\n"
"STATUS=Restarting...");
#endif

cg_completion_timeout(&__kill_work, NULL, 5000);
clean_up(true);
Expand All @@ -4056,6 +4064,7 @@ static void sighandler(int __maybe_unused sig)
/* Restore signal handlers so we can still quit if kill_work fails */
sigaction(SIGTERM, &termhandler, NULL);
sigaction(SIGINT, &inthandler, NULL);
sigaction(SIGABRT, &abrthandler, NULL);
kill_work();
}

Expand Down Expand Up @@ -6069,6 +6078,10 @@ static void hashmeter(int thr_id, uint64_t hashes_done)
}
mutex_unlock(&hash_lock);

#ifdef USE_LIBSYSTEMD
sd_notifyf(false, "STATUS=%s", statusline);
#endif

if (showlog) {
if (!curses_active) {
printf("%s \r", statusline);
Expand Down Expand Up @@ -8510,6 +8523,21 @@ static void *watchdog_thread(void __maybe_unused *userdata)
const unsigned int interval = WATCHDOG_INTERVAL;
struct timeval zero_tv;

#ifdef USE_LIBSYSTEMD
uint64_t notify_usec;
struct timeval notify_interval, notify_tv;

if (sd_watchdog_enabled(false, &notify_usec)) {
notify_usec = notify_usec / 2;
us_to_timeval(&notify_interval, notify_usec);
cgtime(&notify_tv);
addtime(&notify_interval, &notify_tv);

applog(LOG_DEBUG, "Watchdog notify interval: %.3gs",
notify_usec / 1000000.0);
}
#endif

pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

RenameThread("Watchdog");
Expand Down Expand Up @@ -8562,6 +8590,15 @@ static void *watchdog_thread(void __maybe_unused *userdata)

cgtime(&now);

#if USE_LIBSYSTEMD
if (notify_usec && !time_more(&notify_tv, &now)) {
sd_notify(false, "WATCHDOG=1");
copy_time(&notify_tv, &now);
addtime(&notify_interval, &notify_tv);
applog(LOG_DEBUG, "Notified watchdog");
}
#endif

if (!sched_paused && !should_run()) {
applog(LOG_WARNING, "Pausing execution as per stop time %02d:%02d scheduled",
schedstop.tm.tm_hour, schedstop.tm.tm_min);
Expand Down Expand Up @@ -8783,6 +8820,11 @@ void __quit(int status, bool clean)
{
pthread_t killall_t;

#ifdef USE_LIBSYSTEMD
sd_notify(false, "STOPPING=1\n"
"STATUS=Shutting down...");
#endif

if (unlikely(pthread_create(&killall_t, NULL, killall_thread, NULL)))
exit(1);

Expand Down Expand Up @@ -9465,6 +9507,10 @@ int main(int argc, char *argv[])
if (unlikely(curl_global_init(CURL_GLOBAL_ALL)))
early_quit(1, "Failed to curl_global_init");

#ifdef USE_LIBSYSTEMD
sd_notify(false, "STATUS=Starting up...");
#endif

# ifdef __linux
/* If we're on a small lowspec platform with only one CPU, we should
* yield after dropping a lock to allow a thread waiting for it to be
Expand Down Expand Up @@ -9523,6 +9569,7 @@ int main(int argc, char *argv[])
sigemptyset(&handler.sa_mask);
sigaction(SIGTERM, &handler, &termhandler);
sigaction(SIGINT, &handler, &inthandler);
sigaction(SIGABRT, &handler, &abrthandler);
#ifndef WIN32
signal(SIGPIPE, SIG_IGN);
#else
Expand Down Expand Up @@ -9870,6 +9917,11 @@ int main(int argc, char *argv[])

set_highprio();

#ifdef USE_LIBSYSTEMD
sd_notify(false, "READY=1\n"
"STATUS=Started");
#endif

/* Once everything is set up, main() becomes the getwork scheduler */
while (42) {
int ts, max_staged = max_queue;
Expand Down
25 changes: 24 additions & 1 deletion configure.ac
Expand Up @@ -600,6 +600,23 @@ else
fi
AC_SUBST(LIBCURL_LIBS)

libsystemd="no"

AC_ARG_ENABLE([libsystemd],
[AC_HELP_STRING([--enable-libsystemd],[Enable building with libsystemd for watchdog and status notification support])],
[libsystemd=$enableval]
)

if test "x$libsystemd" != xno; then
if test "x$have_linux" != xtrue; then
AC_MSG_ERROR([libsystemd is only supported on Linux platforms])
fi

PKG_CHECK_MODULES(LIBSYSTEMD, libsystemd, , AC_MSG_ERROR(Could not find libsystemd dev))
AC_DEFINE([USE_LIBSYSTEMD], [1], [Defined to 1 if libsystemd support is wanted])
else
LIBSYSTEMD_LIBS=""
fi

#check execv signature
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
Expand Down Expand Up @@ -684,6 +701,12 @@ else
echo " libcurl(GBT+getwork).: Disabled"
fi

if test "x$libsystemd" != xno; then
echo " libsystemd...........: Enabled: $LIBSYSTEMD_LIBS"
else
echo " libsystemd...........: Disabled"
fi

echo " curses.TUI...........: $cursesmsg"


Expand Down Expand Up @@ -848,7 +871,7 @@ echo "Compilation............: make (or gmake)"
echo " CPPFLAGS.............: $CPPFLAGS"
echo " CFLAGS...............: $CFLAGS"
echo " LDFLAGS..............: $LDFLAGS $PTHREAD_FLAGS"
echo " LDADD................: $DLOPEN_FLAGS $LIBCURL_LIBS $JANSSON_LIBS $PTHREAD_LIBS $NCURSES_LIBS $PDCURSES_LIBS $WS2_LIBS $MATH_LIBS $LIBUSB_LIBS $RT_LIBS"
echo " LDADD................: $DLOPEN_FLAGS $LIBCURL_LIBS $LIBSYSTEMD_LIBS $JANSSON_LIBS $PTHREAD_LIBS $NCURSES_LIBS $PDCURSES_LIBS $WS2_LIBS $MATH_LIBS $LIBUSB_LIBS $RT_LIBS"
echo
echo "Installation...........: make install (as root if needed, with 'su' or 'sudo')"
echo " prefix...............: $prefix"
Expand Down

0 comments on commit 5f19d7e

Please sign in to comment.