Heartbeat apps thoughts
Joerg Dorchain
joerg at dorchain.net
Tue Mar 15 02:45:04 EST 2005
Hi list,
I am pondering a heartbeat functionality implementation. Currently, I
have a patch that adds a few line to the timer interrupt to switch the
led on and off when appropriate.
On the list, there were opinions that switching the led on and off would
be best done via userspace. While I in principle agree, I have some
considerations:
- The heartbeat frequency is calculated dynamically as a function of the
current load. While this is available e.g. via /proc/loadavg, there is
the effect that a daemon serving the led would need more cpu on high
loads (led is blinking faster), even though it is less likely to be
scheduled. This leads the heartbeat idea ad absurdum.
- Can real time scheduling of the heartbeat daemon avoid the effect
described above?
- If so, is locking some pages of memory and a real time process still
more adequate than a few lines in the timer interrupt?
For a discussion base I've attached my current idea of an heartbeatd.
(The DEBUG define might help those without an ibook to get something
running ;-)
Curous for any comments,
Joerg
/*
* heartbeatd 2005-03-14 Joerg Dorchain <joerg at dorchain.net>
* led code taken from ibook-led Nico Schottelius (nico-linux at schottelius.org) 2005-02-18 v0.3
*
* might replace the kernel heartbeat code in arch/.../time.c
*
*/
/* open() */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
/* write */
#include <unistd.h>
/* fopen.. */
#include <stdio.h>
/* exit */
#include <stdlib.h>
/* nanosleep..*/
#include <time.h>
/* sigaction */
#include <signal.h>
/* HZ */
#include <sys/param.h>
/* ioctl */
#include <sys/ioctl.h>
/* realtime scheduling */
#include <sched.h>
/* mlockall */
#include <sys/mman.h>
/* Linux */
#define FSHIFT 11
#define ADB_DEVICE "/dev/adb"
#define LOADAVG "/proc/loadavg"
#define PIDFILE "/var/run/heartbeatd.pid"
#undef DEBUG
#ifdef DEBUG
#undef LOADAVG
#define LOADAVG "/tmp/loadavg"
#endif
int adbfd;
inline void heartbeat_init(void)
{
#ifndef DEBUG
/* open /dev/adb */
if((adbfd = open(ADB_DEVICE,O_RDWR)) == -1) exit(1);
#endif
}
inline void heartbeat(int i)
{
char adb_on[5] = {0x06, 0xee, 0x04, 0x00, 0x01};
char adb_off[5] = {0x06, 0xee, 0x04, 0x00, 0x00};
#ifndef DEBUG
if (i)
write(adbfd, adb_on, 5);
else
write(adbfd, adb_off, 5);
#else
if (i)
printf("pump\r");
else
printf(" \r");
fflush(stdout);
#endif
}
inline unsigned int period_calc(void)
{
FILE *lf;
unsigned int ldint, ldfrac, load;
if (( lf = fopen(LOADAVG, "r")) == NULL)
exit(1);
fscanf(lf, "%u.%u", &ldint, &ldfrac);
fclose(lf);
load = (ldint << FSHIFT) + (ldfrac << FSHIFT) / 100;
return ((672<<FSHIFT)/(5*load+(7<<FSHIFT))) + 30;
}
void cleanup(int unused_signal)
{
unlink(PIDFILE);
exit(1);
}
int main(int argc, char **argv)
{
unsigned int period;
struct timespec ts0, ts1, ts2, ts3;
int fd;
struct sched_param sp;
FILE *pidf;
struct sigaction sa;
#ifndef DEBUG
/* daemonize */
if (fork() != 0)
exit(0);
if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
ioctl(fd, TIOCNOTTY);
close(fd);
}
/* make pidfile and remove once done */
if ((pidf = fopen(PIDFILE,"w+")) != NULL) {
fprintf(pidf, "%d\n", getpid());
fclose(pidf);
sa.sa_handler = cleanup;
sigemptyset(&sa.sa_mask);
sa.sa_flags= SA_ONESHOT;
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
}
/* get priority */
if (nice(-19) != 0)
perror("nice");
if (mlockall(MCL_CURRENT|MCL_FUTURE) != 0)
perror("mlockall");
sp.sched_priority=10;
if (sched_setscheduler(0, SCHED_RR, &sp) != 0)
perror("sched_setscheduler");
#endif
heartbeat_init();
while (1) {
period = period_calc();
ts0.tv_sec = 0;
ts0.tv_nsec = 7*(1000000000/HZ);
ts1.tv_sec = 0;
ts1.tv_nsec = (period/4)*(1000000000/HZ) - ts0.tv_nsec;
ts2.tv_sec = 0;
ts2.tv_nsec = (period/4+7)*(1000000000/HZ) - ts0.tv_nsec - ts1.tv_nsec;
ts3.tv_sec = 0;
ts3.tv_nsec = (period)*(1000000000/HZ) - ts0.tv_nsec - ts1.tv_nsec - ts2.tv_nsec;
heartbeat(1);
nanosleep(&ts0,NULL);
heartbeat(0);
nanosleep(&ts1,NULL);
heartbeat(1);
nanosleep(&ts2,NULL);
heartbeat(0);
nanosleep(&ts3,NULL);
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.ozlabs.org/pipermail/linuxppc-dev/attachments/20050314/db6c6d91/attachment.pgp>
More information about the Linuxppc-dev
mailing list