"Fossies" - the Fresh Open Source Software Archive

Member "honggfuzz-2.2/libhfuzz/performance.c" (23 Apr 2020, 2220 Bytes) of package /linux/privat/honggfuzz-2.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "performance.c" see the Fossies "Dox" file reference documentation.

    1 #include "libhfuzz/performance.h"
    2 
    3 #include <inttypes.h>
    4 #include <limits.h>
    5 #include <stdbool.h>
    6 #include <stdint.h>
    7 #include <stdlib.h>
    8 #include <unistd.h>
    9 
   10 #include "honggfuzz.h"
   11 #include "libhfcommon/log.h"
   12 #include "libhfcommon/util.h"
   13 #include "libhfuzz/instrument.h"
   14 
   15 #define HF_USEC_PER_SEC 1000000
   16 #define HF_CHECK_INTERVAL_USECS (HF_USEC_PER_SEC * 20) /* Peform this check every 20 sec. */
   17 #define HF_RESET_RATIO 5 /* Reset ourselves, if currently n times slower than in the beginning */
   18 
   19 static uint64_t iterCnt = 0;
   20 static time_t firstInputUSecs = 0;
   21 
   22 static uint64_t initialUSecsPerExec = 0;
   23 
   24 static uint64_t lastCheckUSecs = 0;
   25 static uint64_t lastCheckIters = 0;
   26 
   27 static bool performanceInit(void) {
   28     if (iterCnt == 1) {
   29         firstInputUSecs = util_timeNowUSecs();
   30     }
   31 
   32     uint64_t timeDiffUSecs = util_timeNowUSecs() - firstInputUSecs;
   33     if (iterCnt == 5000 || timeDiffUSecs > HF_CHECK_INTERVAL_USECS) {
   34         initialUSecsPerExec = timeDiffUSecs / iterCnt;
   35         lastCheckUSecs = util_timeNowUSecs();
   36         lastCheckIters = iterCnt;
   37 
   38         LOG_I("Thread %u (pid=%d) initial speed set at %" PRIu64 " us/exec", instrumentThreadNo(),
   39             (int)getpid(), initialUSecsPerExec);
   40         return true;
   41     }
   42 
   43     return false;
   44 }
   45 
   46 bool performanceTooSlow(void) {
   47     uint64_t timeDiffUSecs = util_timeNowUSecs() - lastCheckUSecs;
   48     if (timeDiffUSecs > HF_CHECK_INTERVAL_USECS) {
   49         uint64_t currentUSecsPerExec = timeDiffUSecs / (iterCnt - lastCheckIters);
   50         if (currentUSecsPerExec > (initialUSecsPerExec * HF_RESET_RATIO)) {
   51             LOG_W("Thread %u (pid=%d) became too slow to process fuzzing data, initial: %" PRIu64
   52                   " us/exec, current: %" PRIu64 " us/exec. Restaring myself!",
   53                 instrumentThreadNo(), (int)getpid(), initialUSecsPerExec, currentUSecsPerExec);
   54             return true;
   55         }
   56         lastCheckIters = iterCnt;
   57         lastCheckUSecs = util_timeNowUSecs();
   58     }
   59 
   60     return false;
   61 }
   62 
   63 void performanceCheck(void) {
   64     iterCnt += 1;
   65 
   66     static bool initialized = false;
   67     if (!initialized) {
   68         initialized = performanceInit();
   69         return;
   70     }
   71 
   72     if (performanceTooSlow()) {
   73         exit(0);
   74     }
   75 }