From 3e8471c36aa0c86ca81ee54c11d6fea5f41f156e Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 9 Jan 2011 02:18:01 +0000 Subject: [PATCH] realtime: new check_rusage feature --- hostside/realtime.c | 51 ++++++++++++++++++++++++++++++++++++++++++++- hostside/realtime.h | 6 +++++- hostside/startup.c | 4 ++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/hostside/realtime.c b/hostside/realtime.c index a2be33d..ae26852 100644 --- a/hostside/realtime.c +++ b/hostside/realtime.c @@ -2,6 +2,8 @@ * main program for realtime control */ +#include + #include "realtime.h" const char *progname= "realtime"; @@ -325,6 +327,52 @@ void serial_transmit(const PicInsn *pi) { serial_transmit_now(pi->d, pi->l); } +/*---------- reporting page faults etc. ----------*/ + +#define CHECK_RUSAGE_FIELDS(F) \ + F(ru_majflt) \ + F(ru_nswap) \ + F(ru_nivcsw) + +/* F(ru_minflt) \ + */ + +#define CRF_DECL(f) static long check_rusage_last_##f; +CHECK_RUSAGE_FIELDS(CRF_DECL) + +static void getru(struct rusage *ru) { + int r= getrusage(RUSAGE_SELF, ru); if (r) diee("getrusage"); +} + +void check_rusage_baseline(void) { + struct rusage ru; + getru(&ru); + #define CRF_BASE(f) check_rusage_last_##f= ru.f; + CHECK_RUSAGE_FIELDS(CRF_BASE) +} + +static void check_rusage_field(const char *f, long *last, long this, int alw) { + long diff= this - *last; + if (alw || diff) { + ouprintf(" %s+=%ld", f, diff); + *last= diff; + } +} + +void check_rusage_check(int always_report) { + struct rusage ru; + getru(&ru); + #define CRF_CHANGED(f) || ru.f != check_rusage_last_##f + if (always_report + CHECK_RUSAGE_FIELDS(CRF_CHANGED)) { + ouprintf("info rusage :"); + #define CRF_SHOW(f) \ + check_rusage_field(STR(f), &check_rusage_last_##f, ru.f, always_report); + CHECK_RUSAGE_FIELDS(CRF_SHOW); + ouprintf(".\n"); + } +} + /*---------- debugging ----------*/ unsigned long eventcounter; @@ -486,6 +534,7 @@ int main(int argc, const char **argv) { case 'P': rtfeats_use |= RTFEAT_ALL(CPU); break; case 'm': rtfeats_use |= RTFEAT_MEM; break; case 'M': rtfeats_use |= RTFEAT_ALL(MEM); break; + case 'r': rtfeats_use |= RTFEAT_RUSAGE; break; default: badusage("unknown -R suboption"); } } @@ -519,7 +568,7 @@ int main(int argc, const char **argv) { events->on_fd(events, serial_fd, OOP_EXCEPTION, read_exception, 0); if (rtfeats_use & RTFEAT_DEFAULTS) - rtfeats_use |= RTFEAT_CPU | RTFEAT_MEM; + rtfeats_use |= RTFEAT_CPU | RTFEAT_MEM | RTFEAT_RUSAGE; } else { sim_initialise(logcopy_fn); sys_events= 0; diff --git a/hostside/realtime.h b/hostside/realtime.h index 83e5362..3624c75 100644 --- a/hostside/realtime.h +++ b/hostside/realtime.h @@ -156,6 +156,9 @@ void serial_transmit(const PicInsn *pi); void command_doline(ParseState *ps, CommandInput *cmdi_arg); const CmdInfo *current_cmd; +void check_rusage_baseline(void); +void check_rusage_check(int always_report); + /*---------- for/from simulate.c ----------*/ void serial_indata_process(int buf_used); @@ -223,9 +226,10 @@ void toev_stop(TimeoutEvent*); /* IR -> I */ void realtime_priority(void); -#define RTFEAT_DEFAULTS 0100u /* turns on MLOCK and SCHEDPRIO iff not sim */ +#define RTFEAT_DEFAULTS 0100u /* turns things on iff not sim */ #define RTFEAT_MEM 0001u /* mlock */ #define RTFEAT_CPU 0002u /* hard CPU scheduling priority */ +#define RTFEAT_RUSAGE 0004u /* check up on faults etc. in getrusage */ #define RTFEAT_ALL_SHIFT 16 #define RTFEAT_ALL(x) (RTFEAT_##x << RTFEAT_ALL_SHIFT) diff --git a/hostside/startup.c b/hostside/startup.c index 460c789..0688d9a 100644 --- a/hostside/startup.c +++ b/hostside/startup.c @@ -51,11 +51,13 @@ static TimeoutEvent watchdog_toev= { static PicInsn watchdog_piob; static void watchdog_transmit(TimeoutEvent *toev) { + check_rusage_check(0); toev_start(&watchdog_toev); serial_transmit(&watchdog_piob); } static void watchdog_start(void) { + check_rusage_baseline(); watchdog_toev.callback= watchdog_transmit; enco_pic_watchdog(&watchdog_piob, UNMARGIN_WATCHDOG_16); watchdog_transmit(0); @@ -66,6 +68,7 @@ static void watchdog_stop(void) { toev_stop(&watchdog_toev); enco_pic_watchdog(&watchdog_piob, 0); serial_transmit(&watchdog_piob); + check_rusage_check(1); } /*---------- main startup algorithm ----------*/ @@ -270,6 +273,7 @@ void on_pic_fault(const PicInsnInfo *pii, const PicInsn *pi, int objnum) { void on_pic_wtimeout(const PicInsnInfo *pii, const PicInsn *pi, int objnum) { if (sta_state <= Sta_Settling) return; + check_rusage_check(1); die("microcontrollers' watchdog timer triggered\n"); } -- 2.30.2