Index: /trunk/libffado/src/libstreaming/amdtp/AmdtpTransmitStreamProcessor.cpp =================================================================== --- /trunk/libffado/src/libstreaming/amdtp/AmdtpTransmitStreamProcessor.cpp (revision 779) +++ /trunk/libffado/src/libstreaming/amdtp/AmdtpTransmitStreamProcessor.cpp (revision 783) @@ -28,4 +28,6 @@ #include "devicemanager.h" +#include "libutil/Time.h" + #include "libieee1394/ieee1394service.h" #include "libieee1394/IsoHandlerManager.h" @@ -180,5 +182,5 @@ // for example: - // usleep(125); // one cycle + // SleepRelativeUsec(125); // one cycle // goto try_block_of_frames; Index: /trunk/libffado/src/libstreaming/StreamProcessorManager.cpp =================================================================== --- /trunk/libffado/src/libstreaming/StreamProcessorManager.cpp (revision 776) +++ /trunk/libffado/src/libstreaming/StreamProcessorManager.cpp (revision 783) @@ -26,4 +26,6 @@ #include "generic/Port.h" #include "libieee1394/cycletimer.h" + +#include "libutil/Time.h" #include @@ -263,5 +265,5 @@ } - usleep(125); + SleepRelativeUsec(125); cnt--; } @@ -332,5 +334,5 @@ if(time_till_next_period > 0) { // wait for the period - usleep(time_till_next_period); + SleepRelativeUsec(time_till_next_period); } } @@ -412,5 +414,5 @@ int cnt = CYCLES_FOR_STARTUP * 20; // by then it should have started while (!m_SyncSource->isRunning() && cnt) { - usleep(125); + SleepRelativeUsec(125); cnt--; } @@ -557,5 +559,5 @@ ready &= ((*it)->isDryRunning() || (*it)->isStopped()); } - usleep(125); + SleepRelativeUsec(125); cnt--; } @@ -597,5 +599,5 @@ ready &= (*it)->isStopped(); } - usleep(125); + SleepRelativeUsec(125); cnt--; } @@ -670,5 +672,5 @@ // wait for the period - usleep(time_till_next_period); + SleepRelativeUsec(time_till_next_period); // check for underruns on the ISO side, @@ -737,5 +739,5 @@ if (!ready_for_transfer) { - usleep(125); // MAGIC: one cycle sleep... + SleepRelativeUsec(125); // MAGIC: one cycle sleep... // in order to avoid this in the future, we increase the sync delay of the sync source SP Index: /trunk/libffado/src/libstreaming/generic/StreamProcessor.cpp =================================================================== --- /trunk/libffado/src/libstreaming/generic/StreamProcessor.cpp (revision 776) +++ /trunk/libffado/src/libstreaming/generic/StreamProcessor.cpp (revision 783) @@ -30,4 +30,6 @@ #include "libieee1394/IsoHandlerManager.h" #include "libieee1394/cycletimer.h" + +#include "libutil/Time.h" #include "libutil/Atomic.h" @@ -923,5 +925,5 @@ int cnt = timeout_ms; while (m_state != state && cnt) { - usleep(1000); + SleepRelativeUsec(1000); cnt--; } Index: /trunk/libffado/src/motu/motu_avdevice.cpp =================================================================== --- /trunk/libffado/src/motu/motu_avdevice.cpp (revision 750) +++ /trunk/libffado/src/motu/motu_avdevice.cpp (revision 783) @@ -37,4 +37,5 @@ #include "libutil/DelayLockedLoop.h" +#include "libutil/Time.h" #include @@ -959,5 +960,5 @@ } - usleep(100); + SleepRelativeUsec(100); return (err==0)?0:-1; } Index: /trunk/libffado/src/libavc/general/avc_generic.cpp =================================================================== --- /trunk/libffado/src/libavc/general/avc_generic.cpp (revision 742) +++ /trunk/libffado/src/libavc/general/avc_generic.cpp (revision 783) @@ -27,4 +27,6 @@ #include "debugmodule/debugmodule.h" + +#include "libutil/Time.h" #include @@ -265,5 +267,5 @@ m_p1394Service->transactionBlockClose(); - usleep( m_time ); + SleepRelativeUsec( m_time ); return result; Index: /trunk/libffado/src/libutil/Time.c =================================================================== --- /trunk/libffado/src/libutil/Time.c (revision 742) +++ /trunk/libffado/src/libutil/Time.c (revision 783) @@ -22,89 +22,4 @@ */ -/* - * Copied from the jackd/jackdmp sources - * function names changed in order to avoid naming problems when using this in - * a jackd backend. - */ - -/* Original license: - * - * Copyright (C) 2001 Paul Davis - * Copyright (C) 2004-2006 Grame - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - */ #include "Time.h" -#ifdef GETCYCLE_TIME - -#include -freebob_microsecs_t GetMhz(void) -{ - FILE *f = fopen("/proc/cpuinfo", "r"); - if (f == 0) - { - perror("can't open /proc/cpuinfo\n"); - exit(1); - } - - for ( ; ; ) - { - freebob_microsecs_t mhz; - int ret; - char buf[1000]; - - if (fgets(buf, sizeof(buf), f) == NULL) { - fprintf (stderr,"FATAL: cannot locate cpu MHz in " - "/proc/cpuinfo\n"); - exit(1); - } - -#if defined(__powerpc__) - ret = sscanf(buf, "clock\t: %" SCNu64 "MHz", &mhz); -#elif defined( __i386__ ) || defined (__hppa__) || defined (__ia64__) || \ - defined(__x86_64__) - ret = sscanf(buf, "cpu MHz : %" SCNu64, &mhz); -#elif defined( __sparc__ ) - ret = sscanf(buf, "Cpu0Bogo : %" SCNu64, &mhz); -#elif defined( __mc68000__ ) - ret = sscanf(buf, "Clocking: %" SCNu64, &mhz); -#elif defined( __s390__ ) - ret = sscanf(buf, "bogomips per cpu: %" SCNu64, &mhz); -#else /* MIPS, ARM, alpha */ - ret = sscanf(buf, "BogoMIPS : %" SCNu64, &mhz); -#endif - if (ret == 1) - { - fclose(f); - return (freebob_microsecs_t)mhz; - } - } -} - -freebob_microsecs_t __freebob_cpu_mhz; - -void InitTime() -{ - __freebob_cpu_mhz = GetMhz(); -} - -#else -void InitTime() -{} - -#endif - Index: /trunk/libffado/src/libutil/Time.h =================================================================== --- /trunk/libffado/src/libutil/Time.h (revision 742) +++ /trunk/libffado/src/libutil/Time.h (revision 783) @@ -22,31 +22,4 @@ */ -/* - * Copied from the jackd/jackdmp sources - * function names changed in order to avoid naming problems when using this in - * a jackd backend. - */ - -/* Original license: - * - * Copyright (C) 2001-2003 Paul Davis - * Copyright (C) 2004-2006 Grame - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - */ - #ifndef __Time__ #define __Time__ @@ -54,6 +27,7 @@ #include #include +#include -// #define GETCYCLE_TIME +#include /** @@ -61,46 +35,11 @@ * monotonic clock with units of microseconds. */ - typedef uint64_t ffado_microsecs_t; - -#ifdef __cplusplus -extern "C" -{ -#endif - - -#include - - static inline void FFADOSleep(long usec) { - usleep(usec); - } - -#ifdef GETCYCLE_TIME - #include "cycles.h" - extern ffado_microsecs_t __ffado_cpu_mhz; - extern ffado_microsecs_t GetMhz(); - extern void InitTime(); - static inline ffado_microsecs_t GetMicroSeconds (void) { - return get_cycles() / __ffado_cpu_mhz; - } -#else - #include - extern void InitTime(); - static inline ffado_microsecs_t GetMicroSeconds (void) { - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - return (ffado_microsecs_t)ts.tv_sec * 1000000LL + ts.tv_nsec / 1000; - } -#endif - - -#ifdef __cplusplus +static inline void SleepRelativeUsec(unsigned long usec) { + usleep(usec); } #endif -#endif - - Index: /trunk/libffado/src/libutil/SystemTimeSource.cpp =================================================================== --- /trunk/libffado/src/libutil/SystemTimeSource.cpp (revision 742) +++ /trunk/libffado/src/libutil/SystemTimeSource.cpp (revision 783) @@ -38,9 +38,10 @@ ffado_microsecs_t SystemTimeSource::getCurrentTime() { - struct timeval tv; - gettimeofday(&tv, NULL); - return tv.tv_sec * 1000000ULL + tv.tv_usec; - -// return GetMicroSeconds(); +// struct timeval tv; +// gettimeofday(&tv, NULL); +// return tv.tv_sec * 1000000ULL + tv.tv_usec; + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (ffado_microsecs_t)(ts.tv_sec * 1000000LL + ts.tv_nsec / 1000LL); } Index: /trunk/libffado/src/bebob/focusrite/focusrite_saffirepro.cpp =================================================================== --- /trunk/libffado/src/bebob/focusrite/focusrite_saffirepro.cpp (revision 750) +++ /trunk/libffado/src/bebob/focusrite/focusrite_saffirepro.cpp (revision 783) @@ -25,4 +25,6 @@ #include "focusrite_cmd.h" +#include "libutil/Time.h" + namespace BeBoB { namespace Focusrite { @@ -385,5 +387,5 @@ // the device needs quite some time to reboot - usleep(2 * 1000 * 1000); + SleepRelativeUsec(2 * 1000 * 1000); int timeout = 5; // multiples of 1s @@ -393,5 +395,5 @@ { // wait for a while - usleep(1000 * 1000); + SleepRelativeUsec(1000 * 1000); } if (!timeout) { @@ -400,5 +402,5 @@ // the device needs quite some time to reboot - usleep(2 * 1000 * 1000); + SleepRelativeUsec(2 * 1000 * 1000); // wait for the device to finish the reboot @@ -408,5 +410,5 @@ { // wait for a while - usleep(1000 * 1000); + SleepRelativeUsec(1000 * 1000); } if (!timeout) { @@ -425,5 +427,5 @@ // wait for a while - usleep(4 * 1000 * 1000); + SleepRelativeUsec(4 * 1000 * 1000); } while (gen_current != get1394Service().getGeneration() && --timeout); @@ -438,5 +440,5 @@ // wait some more - usleep(1 * 1000 * 1000); + SleepRelativeUsec(1 * 1000 * 1000); // we have to rediscover the device Index: /trunk/libffado/src/bebob/bebob_dl_mgr.cpp =================================================================== --- /trunk/libffado/src/bebob/bebob_dl_mgr.cpp (revision 742) +++ /trunk/libffado/src/bebob/bebob_dl_mgr.cpp (revision 783) @@ -30,4 +30,5 @@ #include "libutil/cmd_serialize.h" +#include "libutil/Time.h" #include @@ -436,5 +437,5 @@ break; } - usleep( 100 ); + SleepRelativeUsec( 100 ); if ( !readResponse( ccBlock ) ) { Index: /trunk/libffado/src/bounce/bounce_slave_avdevice.cpp =================================================================== --- /trunk/libffado/src/bounce/bounce_slave_avdevice.cpp (revision 742) +++ /trunk/libffado/src/bounce/bounce_slave_avdevice.cpp (revision 783) @@ -27,4 +27,6 @@ #include "libieee1394/configrom.h" #include "libieee1394/ieee1394service.h" + +#include "libutil/Time.h" #include "bounce_slave_avdevice.h" @@ -313,5 +315,5 @@ return false; } - usleep(100000); + SleepRelativeUsec(100000); } Index: /trunk/libffado/tests/test-focusrite.cpp =================================================================== --- /trunk/libffado/tests/test-focusrite.cpp (revision 742) +++ /trunk/libffado/tests/test-focusrite.cpp (revision 783) @@ -32,4 +32,5 @@ #include "libutil/cmd_serialize.h" #include "libavc/general/avc_generic.h" +#include "libutil/Time.h" #include "bebob/focusrite/focusrite_cmd.h" @@ -191,5 +192,5 @@ } } - usleep(1000000); + SleepRelativeUsec(1000000); } Index: /trunk/libffado/tests/test-timestampedbuffer.cpp =================================================================== --- /trunk/libffado/tests/test-timestampedbuffer.cpp (revision 748) +++ /trunk/libffado/tests/test-timestampedbuffer.cpp (revision 783) @@ -40,4 +40,5 @@ #include "src/libutil/TimestampedBuffer.h" +#include "libutil/Time.h" #include @@ -297,5 +298,5 @@ t->prepare(); - usleep(1000); + SleepRelativeUsec(1000); debugOutput(DEBUG_LEVEL_NORMAL, "Start setBufferHeadTimestamp test...\n"); @@ -365,5 +366,5 @@ // allow for the messagebuffer thread to catch up - usleep(200); + SleepRelativeUsec(200); if(!run) break; @@ -480,5 +481,5 @@ // allow for the messagebuffer thread to catch up - usleep(200); + SleepRelativeUsec(200); if(!run) break; @@ -580,5 +581,5 @@ // allow for the messagebuffer thread to catch up - usleep(200); + SleepRelativeUsec(200); if(!run) break; Index: /trunk/libffado/tests/test-ieee1394service.cpp =================================================================== --- /trunk/libffado/tests/test-ieee1394service.cpp (revision 752) +++ /trunk/libffado/tests/test-ieee1394service.cpp (revision 783) @@ -44,4 +44,5 @@ #include +#include "libutil/Time.h" DECLARE_GLOBAL_DEBUG_MODULE; @@ -130,5 +131,5 @@ fflush(stderr); fflush(stdout); - usleep(900*1000); + SleepRelativeUsec(900*1000); uint32_t cycle_timer; Index: /trunk/libffado/tests/streaming/teststreaming3.c =================================================================== --- /trunk/libffado/tests/streaming/teststreaming3.c (revision 750) +++ /trunk/libffado/tests/streaming/teststreaming3.c (revision 783) @@ -37,4 +37,5 @@ #include +#include #include "libffado/ffado.h" @@ -47,4 +48,36 @@ { run = 0; + set_realtime_priority(0); +} + +int set_realtime_priority(unsigned int prio) +{ + if (prio > 0) { + struct sched_param schp; + /* + * set the process to realtime privs + */ + memset(&schp, 0, sizeof(schp)); + schp.sched_priority = prio; + + if (sched_setscheduler(0, SCHED_FIFO, &schp) != 0) { + perror("sched_setscheduler"); + exit(1); + } + } else { + struct sched_param schp; + /* + * set the process to realtime privs + */ + memset(&schp, 0, sizeof(schp)); + schp.sched_priority = prio; + + if (sched_setscheduler(0, SCHED_OTHER, &schp) != 0) { + perror("sched_setscheduler"); + exit(1); + } + + } + return 0; } @@ -69,5 +102,5 @@ run=1; - printf("Ffado streaming test application (3)\n"); + printf("FFADO streaming test application (3)\n"); signal (SIGINT, sighandler); @@ -86,5 +119,5 @@ dev_options.realtime=1; - dev_options.packetizer_priority=70; + dev_options.packetizer_priority=60; dev_options.verbose = 6; @@ -203,4 +236,5 @@ start_flag = ffado_streaming_start(dev); + set_realtime_priority(dev_options.packetizer_priority-1); fprintf(stderr,"Entering receive loop (%d,%d)\n",nb_in_channels,nb_out_channels); while(run && start_flag==0) { Index: /trunk/libffado/tests/test-echo.cpp =================================================================== --- /trunk/libffado/tests/test-echo.cpp (revision 742) +++ /trunk/libffado/tests/test-echo.cpp (revision 783) @@ -26,4 +26,5 @@ #include #include +#include "libutil/Time.h" #include @@ -145,5 +146,5 @@ // cmd[1] = 0x01FF0000; // avc1394_transaction_block2(pHandle, 0xffc0 | iNodeId, cmd, 2, &response_len, 10); -// usleep(100000); +// SleepRelativeUsec(100000); // // cerr << "Reading descriptor" << endl; @@ -156,5 +157,5 @@ // // avc1394_transaction_block2(pHandle, 0xffc0 | iNodeId, cmd, 3, &response_len, 10); -// usleep(100000); +// SleepRelativeUsec(100000); // // cerr << "Closing descriptor" << endl; @@ -162,5 +163,5 @@ // cmd[1] = 0x00FF0000; // avc1394_transaction_block2(pHandle, 0xffc0 | iNodeId, cmd, 2, &response_len, 10); -// usleep(100000); +// SleepRelativeUsec(100000); cerr << "getting signal source" << endl; @@ -196,10 +197,10 @@ cmd[1] = 0xFFFE6000; avc1394_transaction_block2(pHandle, 0xffc0 | iNodeId, cmd, 2, &response_len, 10); -// usleep(100000); +// SleepRelativeUsec(100000); cmd[0] = 0x01FF1AFF; cmd[1] = 0xFFFEFF00; avc1394_transaction_block2(pHandle, 0xffc0 | iNodeId, cmd, 2, &response_len, 10); - usleep(100000); + SleepRelativeUsec(100000); Index: /trunk/libffado/support/dbus/controlserver.cpp =================================================================== --- /trunk/libffado/support/dbus/controlserver.cpp (revision 742) +++ /trunk/libffado/support/dbus/controlserver.cpp (revision 783) @@ -26,4 +26,5 @@ #include "libcontrol/BasicElements.h" #include "libcontrol/MatrixMixer.h" +#include "libutil/Time.h" namespace DBusControl { @@ -172,5 +173,5 @@ m_Slave.setValue(value); /* - usleep(1000*500); + SleepRelativeUsec(1000*500); debugOutput( DEBUG_LEVEL_VERBOSE, "setValue(%lf) => %lf\n", value, m_Slave.getValue() ); @@ -202,5 +203,5 @@ m_Slave.setValue(value); -/* usleep(1000*500); +/* SleepRelativeUsec(1000*500); debugOutput( DEBUG_LEVEL_VERBOSE, "setValue(%d) => %d\n", value, m_Slave.getValue() );