On Wednesday 23 November 2005 00:10, Iain Duncan wrote: > I would like to know what the best way is to allow csound to run with > lower latency in a host where csound is one thread and fltk another, on > linux. The fltk thread has quite a lot of potential drawing going on. I > can't use --sched with the csd in an api host. Here is a simplified version of the --sched code in Csound. Just add this to your build as a separate .c file, and declare and call the following function: int set_rt_priority(int priority, int cpuMax, int secs); 'priority' is the scheduling priority (-20 to -1 for nice only, 0 for SCHED_OTHER with normal priority, and 1 to 99 for SCHED_RR with the specified priority - 99 is the same as --sched with no additional parameters; on all settings, memory is locked). If 'cpuMax' and 'secs' are non-zero, a watchdog thread is created which will terminate the program if the CPU usage exceeds 'cpuMax' (1 to 99) over a time period of 'secs' (1 to 60). #include "csound.h" #include #include #include #include #include #include #include #include #include #include #include #include static int err_msg(const char *fmt, ...) { va_list args; va_start(args, fmt); vfprintf(stderr, fmt, args); va_end(args); fprintf(stderr, "\n"); return -1; } static void *wd_thread_routine(void *parm) { int cpuMax, secs; uint32_t t0, t1; double p; cpuMax = (int) ((uintptr_t) parm & (uintptr_t) 0xFF); secs = (int) (((uintptr_t) parm & (uintptr_t) 0xFF00) >> 8); for ( ; ; ) { t0 = (uint32_t) clock(); csoundSleep((size_t) (secs * 1000)); t1 = (uint32_t) clock(); p = (double) ((int32_t) (t1 - t0)) * (100.0 / (double) CLOCKS_PER_SEC); if ((p / (double) secs) > (double) cpuMax) { kill(0, SIGTERM); csoundSleep(1500); kill(0, SIGKILL); csoundSleep(1500); exit(-1); } } return NULL; } int set_rt_priority(int priority, int cpuMax, int secs) { struct sched_param p; memset(&p, 0, sizeof(struct sched_param)); if (priority > sched_get_priority_max(SCHED_RR)) priority = sched_get_priority_max(SCHED_RR); else if (priority < -20) priority = -20; if ((int) geteuid() != 0) { err_msg("WARNING: not running as root, --sched ignored"); csoundSleep(1000); return 0; } #ifndef __FreeBSD__ /* lock all pages into physical memory */ if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0) { return err_msg("cannot lock memory pages: %s", strerror(errno)); } #endif /* create watchdog thread if enabled */ if (priority > 0 && cpuMax && secs) { pthread_t th; cpuMax = (cpuMax > 1 ? (cpuMax < 99 ? cpuMax : 99) : 1); secs = (secs > 1 ? (secs < 60 ? secs : 60) : 1); if (pthread_create(&th, (pthread_attr_t*) NULL, wd_thread_routine, (void*) ((uintptr_t) (cpuMax + (secs << 8)))) != 0) return err_msg("--sched: error creating watchdog thread"); p.sched_priority = sched_get_priority_max(SCHED_RR); pthread_setschedparam(th, SCHED_RR, &p); pthread_detach(th); } /* set scheduling policy and priority */ if (priority > 0) { p.sched_priority = priority; if (sched_setscheduler(0, SCHED_RR, &p) != 0) { return err_msg("cannot set scheduling policy to SCHED_RR: %s", strerror(errno)); } } else if (priority == 0) { p.sched_priority = priority; /* hope this does not fail ! */ sched_setscheduler(0, SCHED_OTHER, &p); } else { /* nice requested */ if (setpriority(PRIO_PROCESS, 0, priority) != 0) { return err_msg("cannot set nice level to %d: %s", priority, strerror(errno)); } } /* give up root privileges */ setuid(getuid()); return 0; } > Can I lower the fltk thread priority somehow? I am using the csound > threads but I could revert to raw pthreads. Should I lower the priority > of the X server? Istvan, I remember you posting an example of how to add > the --sched code to an app. Is it possible to add that to only one > thread? Could you repost that if you have time? ( I tried looking but > can't find it. ) I would prefer not to have to have the fltk stuff > running a seperate process, though that is an option. The FLTK opcodes in Csound lower the priority by simply calling pthread_setschedparam from the widget thread: static uintptr_t fltkRun(void *userdata) { volatile widgetsGlobals_t *p; CSOUND *csound = (CSOUND*) userdata; int j; p = (widgetsGlobals_t*) csound->QueryGlobalVariable(csound, "_widgets_globals"); #ifdef LINUX { struct sched_param sp; // IV - Aug 27 2002: widget thread is always run with normal priority memset(&sp, 0, sizeof(struct sched_param)); pthread_setschedparam(pthread_self(), SCHED_OTHER, &sp); } #endif ... ------------------------------------------------------- This SF.Net email is sponsored by the JBoss Inc. Get Certified Today Register for a JBoss Training Course. Free Certification Exam for All Training Attendees Through End of 2005. For more info visit: http://ads.osdn.com/?ad_id=7628&alloc_id=16845&op=click _______________________________________________ Csound-devel mailing list Csound-devel@lists.sourceforge.net