Gordon Henderson | 3fbc564 | 2013-03-24 20:04:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * delayTest.c: |
| 3 | * Just a little test program I'm using to experiment with |
| 4 | * various timings and latency, etc. |
| 5 | * |
| 6 | * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net> |
| 7 | *********************************************************************** |
| 8 | * This file is part of wiringPi: |
| 9 | * https://projects.drogon.net/raspberry-pi/wiringpi/ |
| 10 | * |
| 11 | * wiringPi is free software: you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU Lesser General Public License as published by |
| 13 | * the Free Software Foundation, either version 3 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * wiringPi is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Lesser General Public License |
| 22 | * along with wiringPi. If not, see <http://www.gnu.org/licenses/>. |
| 23 | *********************************************************************** |
| 24 | */ |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <unistd.h> |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 28 | |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 29 | #include <sys/time.h> |
| 30 | |
| 31 | #define CYCLES 1000 |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 32 | |
| 33 | int main() |
| 34 | { |
| 35 | int x ; |
Deokgyu Yang | 89cf23c | 2020-03-19 17:21:21 +0900 | [diff] [blame] | 36 | struct timeval t1, t2, t3 ; |
Gordon Drogon | dda3305 | 2012-12-11 20:59:52 +0000 | [diff] [blame] | 37 | int t ; |
| 38 | int max, min ; |
| 39 | int del ; |
Deokgyu Yang | 89cf23c | 2020-03-19 17:21:21 +0900 | [diff] [blame] | 40 | int underRuns, overRuns, exactRuns, bogusRuns, total ; |
Gordon Drogon | dda3305 | 2012-12-11 20:59:52 +0000 | [diff] [blame] | 41 | int descheds ; |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 42 | |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 43 | |
| 44 | // Baseline test |
| 45 | |
| 46 | gettimeofday (&t1, NULL) ; |
| 47 | gettimeofday (&t2, NULL) ; |
| 48 | |
| 49 | t = t2.tv_usec - t1.tv_usec ; |
Gordon Drogon | dda3305 | 2012-12-11 20:59:52 +0000 | [diff] [blame] | 50 | printf ("Baseline test: %d\n", t); |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 51 | |
Gordon Drogon | dda3305 | 2012-12-11 20:59:52 +0000 | [diff] [blame] | 52 | for (del = 1 ; del < 200 ; ++del) |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 53 | { |
Gordon Drogon | dda3305 | 2012-12-11 20:59:52 +0000 | [diff] [blame] | 54 | underRuns = overRuns = exactRuns = total = 0 ; |
| 55 | descheds = 0 ; |
Deokgyu Yang | 89cf23c | 2020-03-19 17:21:21 +0900 | [diff] [blame] | 56 | max = 0 ; |
| 57 | min = 999 ; |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 58 | |
Gordon Drogon | dda3305 | 2012-12-11 20:59:52 +0000 | [diff] [blame] | 59 | for (x = 0 ; x < CYCLES ; ++x) |
| 60 | { |
| 61 | for (;;) // Repeat this if we get a delay over 999uS |
| 62 | { // -> High probability Linux has deschedulled us |
| 63 | gettimeofday (&t1, NULL) ; |
Deokgyu Yang | 89cf23c | 2020-03-19 17:21:21 +0900 | [diff] [blame] | 64 | usleep (del) ; |
| 65 | // delayMicroseconds (del) ; |
Gordon Drogon | dda3305 | 2012-12-11 20:59:52 +0000 | [diff] [blame] | 66 | gettimeofday (&t2, NULL) ; |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 67 | |
Deokgyu Yang | 89cf23c | 2020-03-19 17:21:21 +0900 | [diff] [blame] | 68 | timersub (&t2, &t1, &t3) ; |
| 69 | |
| 70 | t = t3.tv_usec ; |
| 71 | |
Gordon Drogon | dda3305 | 2012-12-11 20:59:52 +0000 | [diff] [blame] | 72 | if (t > 999) |
| 73 | { |
| 74 | ++descheds ; |
| 75 | continue ; |
| 76 | } |
| 77 | else |
| 78 | break ; |
| 79 | } |
| 80 | |
Deokgyu Yang | 89cf23c | 2020-03-19 17:21:21 +0900 | [diff] [blame] | 81 | if (t == del) |
Gordon Drogon | dda3305 | 2012-12-11 20:59:52 +0000 | [diff] [blame] | 82 | ++exactRuns ; |
Deokgyu Yang | 89cf23c | 2020-03-19 17:21:21 +0900 | [diff] [blame] | 83 | else if (t < del) |
| 84 | ++underRuns ; |
| 85 | else if (t > del) |
| 86 | ++overRuns ; |
| 87 | |
| 88 | if (t > max) |
| 89 | max = t ; |
| 90 | else if (t < min) |
| 91 | min = t ; |
Gordon Drogon | dda3305 | 2012-12-11 20:59:52 +0000 | [diff] [blame] | 92 | |
| 93 | total += t ; |
| 94 | } |
| 95 | printf ("Delay: %3d. Min: %3d, Max: %3d, Unders: %3d, Overs: %3d, Exacts: %3d, Average: %3d, Descheds: %2d\n", |
| 96 | del, min, max, underRuns, overRuns, exactRuns, total / CYCLES, descheds) ; |
| 97 | fflush (stdout) ; |
Deokgyu Yang | 89cf23c | 2020-03-19 17:21:21 +0900 | [diff] [blame] | 98 | usleep (1000) ; |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 99 | } |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 100 | |
| 101 | return 0 ; |
| 102 | } |