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> |
| 28 | #include <wiringPi.h> |
| 29 | |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 30 | #include <sys/time.h> |
| 31 | |
| 32 | #define CYCLES 1000 |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 33 | |
| 34 | int main() |
| 35 | { |
| 36 | int x ; |
| 37 | struct timeval t1, t2 ; |
Gordon Drogon | dda3305 | 2012-12-11 20:59:52 +0000 | [diff] [blame] | 38 | int t ; |
| 39 | int max, min ; |
| 40 | int del ; |
| 41 | int underRuns, overRuns, exactRuns, total ; |
| 42 | int descheds ; |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 43 | |
| 44 | if (wiringPiSetup () == -1) |
| 45 | return 1 ; |
| 46 | |
Gordon Drogon | dda3305 | 2012-12-11 20:59:52 +0000 | [diff] [blame] | 47 | piHiPri (10) ; sleep (1) ; |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 48 | |
| 49 | // Baseline test |
| 50 | |
| 51 | gettimeofday (&t1, NULL) ; |
| 52 | gettimeofday (&t2, NULL) ; |
| 53 | |
| 54 | t = t2.tv_usec - t1.tv_usec ; |
Gordon Drogon | dda3305 | 2012-12-11 20:59:52 +0000 | [diff] [blame] | 55 | printf ("Baseline test: %d\n", t); |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 56 | |
Gordon Drogon | dda3305 | 2012-12-11 20:59:52 +0000 | [diff] [blame] | 57 | for (del = 1 ; del < 200 ; ++del) |
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 | underRuns = overRuns = exactRuns = total = 0 ; |
| 60 | descheds = 0 ; |
| 61 | max = del ; |
| 62 | min = del ; |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 63 | |
Gordon Drogon | dda3305 | 2012-12-11 20:59:52 +0000 | [diff] [blame] | 64 | for (x = 0 ; x < CYCLES ; ++x) |
| 65 | { |
| 66 | for (;;) // Repeat this if we get a delay over 999uS |
| 67 | { // -> High probability Linux has deschedulled us |
| 68 | gettimeofday (&t1, NULL) ; |
| 69 | delayMicroseconds (del) ; |
| 70 | gettimeofday (&t2, NULL) ; |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 71 | |
Gordon Drogon | dda3305 | 2012-12-11 20:59:52 +0000 | [diff] [blame] | 72 | if (t2.tv_usec < t1.tv_usec) // Counter wrapped |
| 73 | t = (1000000 + t2.tv_usec) - t1.tv_usec; |
| 74 | else |
| 75 | t = t2.tv_usec - t1.tv_usec ; |
| 76 | if (t > 999) |
| 77 | { |
| 78 | ++descheds ; |
| 79 | continue ; |
| 80 | } |
| 81 | else |
| 82 | break ; |
| 83 | } |
| 84 | |
| 85 | if (t > max) |
| 86 | { |
| 87 | max = t ; |
| 88 | ++overRuns ; |
| 89 | } |
| 90 | else if (t < min) |
| 91 | { |
| 92 | min = t ; |
| 93 | ++underRuns ; |
| 94 | } |
| 95 | else |
| 96 | ++exactRuns ; |
| 97 | |
| 98 | total += t ; |
| 99 | } |
| 100 | printf ("Delay: %3d. Min: %3d, Max: %3d, Unders: %3d, Overs: %3d, Exacts: %3d, Average: %3d, Descheds: %2d\n", |
| 101 | del, min, max, underRuns, overRuns, exactRuns, total / CYCLES, descheds) ; |
| 102 | fflush (stdout) ; |
| 103 | delay (1) ; |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 104 | } |
Gordon Henderson | ae40bda | 2012-08-28 18:37:54 +0100 | [diff] [blame] | 105 | |
| 106 | return 0 ; |
| 107 | } |