From 32814797ffeafccdc07491243deca5760759ab54 Mon Sep 17 00:00:00 2001 From: Drew Fustini Date: Sun, 19 Nov 2017 05:17:22 +0000 Subject: [PATCH] cache board type to avoid poor performance (#196) cache board type to avoid poor performance in functions that are called frequently like gpio_set_value() in source/event_gpio.c Signed-off-by: Drew Fustini --- source/common.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/source/common.c b/source/common.c index e000a66..d940042 100644 --- a/source/common.c +++ b/source/common.c @@ -601,23 +601,30 @@ int pocketbeagle(void) { */ int beaglebone_blue(void) { const char *cmd = "/bin/grep -c 'TI AM335x BeagleBone Blue' /proc/device-tree/model"; - char blue; + // cache the value to avoid poor performance + // in functions that are called frequently like + // gpio_set_value() in source/event_gpio.c + static int initialized = 0; + static int retval = 0; FILE *file = NULL; - file = popen(cmd, "r"); - if (file == NULL) { - fprintf(stderr, "error: beaglebone_blue() failed to run cmd=%s\n", cmd); - syslog(LOG_ERR, "Adafruit_BBIO: error: beaglebone_blue() failed to run cmd=%s\n", cmd); - return -1; + //fprintf(stderr, "beaglebone_blue(): initialized=[%d] retval=[%d]\n", initialized, retval); + if(!initialized) { + initialized = 1; + //fprintf(stderr, "beaglebone_blue(): not initialized\n"); + file = popen(cmd, "r"); + if (file == NULL) { + fprintf(stderr, "Adafruit_BBIO: error in beaglebone_blue(): failed to run cmd=%s\n", cmd); + syslog(LOG_ERR, "Adafruit_BBIO: error in beaglebone_blue(): failed to run cmd=%s\n", cmd); + return -1; + } + if( fgetc(file) == '1' ) { + retval = 1; + } + pclose(file); } - blue = fgetc(file); - pclose(file); - if(blue == '1') { - return 1; - } else { - return 0; - } + return retval; }