rt-tests: cyclictest: Extract check_and_report_clock_resolution() from main()
PATCH 1/1 rt-tests: cyclictest: Extract check_and_report_clock_resolution() from main()
The cyclictest absorbed a lot of development debt over the long history.
The longest function is main() with length 453:
lizard --warnings_only --length 320 --CCN 100 cyclictest.c
cyclictest.c:1095: warning: process_options has 312 NLOC, 140 CCN, 1824 token, 2 PARAM, 346 length, 0 ND cyclictest.c:1910: warning: main has 342 NLOC, 107 CCN, 2187 token, 2 PARAM, 453 length, 0 ND
Extract check_and_report_clock_resolution() from main(). Locate the function between check_timer() and sighand() because they are used in the same order. After the change the length of main() became 365 instead of 453:
lizard --warnings_only --length 320 --CCN 100 cyclictest.c
cyclictest.c:1189: warning: process_options has 312 NLOC, 140 CCN, 1824 token, 2 PARAM, 346 length, 0 ND cyclictest.c:2004: warning: main has 287 NLOC, 94 CCN, 1826 token, 2 PARAM, 365 length, 0 ND
--- src/cyclictest/cyclictest.c | 179 ++++++++++++++++++------------------ 1 file changed, 89 insertions(+), 90 deletions(-)
src/cyclictest/cyclictest.c +89 -90
@@ -1449,6 +1449,93 @@ static int check_timer(void)return (ts.tv_sec !=0 || ts.tv_nsec !=1 ); } +static void check_and_report_clock_resolution () +{ +int clock; +uint64_t diff; +int k; +uint64_t min_non_zero_diff =UINT64_MAX ; +struct timespec now; +struct timespec prev; +uint64_t reported_resolution =UINT64_MAX ; +struct timespec res; +struct timespec *time; +int times; + + clock = clocksources[clocksel]; + +if (clock_getres (clock, &res)) +warn ("clock_getres failed" ); +else + reported_resolution = (NSEC_PER_SEC * res.tv_sec) + res.tv_nsec; + +/* +* Calculate how many calls to clock_gettime are needed. +* Then call it that many times. +* Goal is to collect timestamps for ~ 0.001 sec. +* This will reliably capture resolution <= 500 usec. +*/ + times =1000 ; +clock_gettime (clock, &prev); +for (k =0 ; k < times; k++) +clock_gettime (clock, &now); + + diff =calcdiff_ns (now, prev); +if (diff ==0 ) { +/* +* No clock rollover occurred. +* Use the default value for times. +*/ + times =-1 ; + }else { +int call_time; + call_time = diff / times;/* duration 1 call */ + times =NSEC_PER_SEC / call_time;/* calls per second */ + times /=1000 ;/* calls per msec */ +if (times <1000 ) + times =1000 ; + } +/* sanity check */ +if ((times <=0 ) || (times >100000 )) + times =100000 ; + + time =calloc (times,sizeof (*time)); + +for (k =0 ; k < times; k++) +clock_gettime (clock, &time[k]); + +info (ct_debug,"For %d consecutive calls to clock_gettime():\n" , times); +info (ct_debug,"time, delta time (nsec)\n" ); + + prev = time[0 ]; +for (k =1 ; k < times; k++) { + diff =calcdiff_ns (time[k], prev); + prev = time[k]; + +if (diff && (diff < min_non_zero_diff)) + min_non_zero_diff = diff; + +info (ct_debug,"%ld.%06ld %5llu\n" , + time[k].tv_sec, time[k].tv_nsec, + (unsigned long long )diff); + } + +free (time); + +if (verbose || + (min_non_zero_diff && (min_non_zero_diff > reported_resolution))) { +/* +* Measured clock resolution includes the time to call +* clock_gettime(), so it will be slightly larger than +* actual resolution. +*/ +warn ("reported clock resolution: %llu nsec\n" , + (unsigned long long )reported_resolution); +warn ("measured clock resolution approximately: %llu nsec\n" , + (unsigned long long )min_non_zero_diff); + } +} +static void sighand (int sig) {if (sig ==SIGUSR1 ) {
@@ -1981,96 +2068,8 @@ int main(int argc, char **argv)if (check_timer ())warn ("High resolution timers not available\n" ); -if (check_clock_resolution) { -int clock; -uint64_t diff; -int k; -uint64_t min_non_zero_diff =UINT64_MAX ; -struct timespec now; -struct timespec prev; -uint64_t reported_resolution =UINT64_MAX ; -struct timespec res; -struct timespec *time; -int times; - - clock = clocksources[clocksel]; - -if (clock_getres (clock, &res)) -warn ("clock_getres failed" ); -else - reported_resolution = (NSEC_PER_SEC * res.tv_sec) + res.tv_nsec; - - -/* -* Calculate how many calls to clock_gettime are needed. -* Then call it that many times. -* Goal is to collect timestamps for ~ 0.001 sec. -* This will reliably capture resolution <= 500 usec. -*/ - times =1000 ; -clock_gettime (clock, &prev); -for (k=0 ; k < times; k++) -clock_gettime (clock, &now); - - diff =calcdiff_ns (now, prev); -if (diff ==0 ) { -/* -* No clock rollover occurred. -* Use the default value for times. -*/ - times =-1 ; - }else { -int call_time; - call_time = diff / times;/* duration 1 call */ - times =NSEC_PER_SEC / call_time;/* calls per second */ - times /=1000 ;/* calls per msec */ -if (times <1000 ) - times =1000 ; - } -/* sanity check */ -if ((times <=0 ) || (times >100000 )) - times =100000 ; - - time =calloc (times,sizeof (*time)); - -for (k=0 ; k < times; k++) -clock_gettime (clock, &time[k]); - -info (ct_debug,"For %d consecutive calls to clock_gettime():\n" , times); -info (ct_debug,"time, delta time (nsec)\n" ); - - prev = time[0 ]; -for (k=1 ; k < times; k++) { - - diff =calcdiff_ns (time[k], prev); - prev = time[k]; - -if (diff && (diff < min_non_zero_diff)) - min_non_zero_diff = diff; - -info (ct_debug,"%ld.%06ld %5llu\n" , - time[k].tv_sec, time[k].tv_nsec, - (unsigned long long )diff); - } - -free (time); - - -if (verbose || - (min_non_zero_diff && (min_non_zero_diff > reported_resolution))) { -/* -* Measured clock resolution includes the time to call -* clock_gettime(), so it will be slightly larger than -* actual resolution. -*/ -warn ("reported clock resolution: %llu nsec\n" , - (unsigned long long )reported_resolution); -warn ("measured clock resolution approximately: %llu nsec\n" , - (unsigned long long )min_non_zero_diff); - } - - } - +if (check_clock_resolution) +check_and_report_clock_resolution (); mode = use_nanosleep + use_system;
Signature · 1 line
2.51.0