@[toc]linux
下面給xenomai添加一個系統調用get_timer_hits()
,用於獲取應用程序運行CPU的定時器中斷產生的次數,相似於VxWorks裏的tickGet()。須要說明一下VxWorks是採用週期tick的方式來驅動系統運做,tickGet()獲取的也就是tick定時器中斷的次數,但xenomai使用的tickless,即定時器不是週期產生tick的。因此get_timer_hits()
用於獲取定時器中斷次數,get_timer_hits()
沒有具體用途,這裏主要用來舉例怎麼爲xenomai添加一個實時系統調用。shell
在前兩篇文中說到,xenomai每一個系統的系統系統調用號在\cobalt\uapi\syscall.h
中:api
#define sc_cobalt_bind 0
#define sc_cobalt_thread_create 1
#define sc_cobalt_thread_getpid 2
......
#define sc_cobalt_extend 96
複製代碼
在此添加sc_cobalt_get_timer_hits
的系統,爲了不與xenomai系統調用衝突(xenomai官方添加的系統調用號從小到大),那咱們就從最後一個系統調用添加,即127號系統調用,以下。bash
#define sc_cobalt_bind 0
#define sc_cobalt_thread_create 1
#define sc_cobalt_thread_getpid 2
......
#define sc_cobalt_extend 96
#define sc_cobalt_ftrace_puts 97
#define sc_cobalt_recvmmsg 98
#define sc_cobalt_sendmmsg 99
#define sc_cobalt_clock_adjtime 100
#define sc_cobalt_thread_setschedprio 101
#define sc_cobalt_get_timer_hits 127
#define __NR_COBALT_SYSCALLS 128 /* Power of 2 */
複製代碼
先肯定一下咱們這個函數的API形式,因爲是一個非標準的形式,這裏表示以下:cookie
int get_timer_hits(unsigned long *u_tick); 複製代碼
參數爲保存hits的變量地址;less
返回值:成功0;出錯 <0;dom
系統調用的頭文件,而後添加一個系統調用的聲明,以爲它和clock相關,那就放在kernel\xenomai\posix\clock.h
中吧。函數
#include <linux/ipipe_tickdev.h>
COBALT_SYSCALL_DECL(get_timer_hits,
(unsigned long __user *u_tick));
複製代碼
而後是該函數的內核實現,放在/kernel\xenomai\posix\clock.c
,以下:ui
COBALT_SYSCALL(get_timer_hits, primary,
(unsigned long __user *u_tick))
{
struct xnthread *thread;
unsigned long tick;
int cpu;
int ret = 0;
unsigned int irq;
thread = xnthread_current();
if (thread == NULL)
return -EPERM;
/*獲得當前任務CPU號*/
cpu = xnsched_cpu(thread->sched);
irq = per_cpu(ipipe_percpu.hrtimer_irq, cpu);
/*讀取該CPU中斷計數*/
tick = __ipipe_cpudata_irq_hits(&xnsched_realtime_domain, cpu,
irq);
if (cobalt_copy_to_user(u_tick, &tick, sizeof(tick)))
return -EFAULT;
return ret;
}
複製代碼
須要注意的是該系統調用的權限,這裏使用primary
,表示只有cobalt上下文(實時線程)才能調用。spa
修改完成後從新編譯內核並安裝。
在前兩篇文中說到,xenomai系統調用由libcobalt發起,因此修改應用庫來添加該函數接口,添加聲明include\cobalt\time.h
:
COBALT_DECL(int, get_timer_hits(unsigned long tick));
複製代碼
xenomai3.x.x\lib\cobalt\clock.c
添加該接口定義:
COBALT_IMPL(int, get_timer_hits, (unsigned long * tick))
{
int ret;
ret = -XENOMAI_SYSCALL1(sc_cobalt_get_tick,
tick);
return ret;
}
複製代碼
從新編譯並安裝xenomai庫,詳見本博客其餘文章。
因爲咱們添加get_timer_hits()
系統調用時,指定了系統調用的權限爲primary,這裏建立一個實時任務,使用宏__RT()
指定連接到libcobalt庫。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sched.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <alchemy/task.h>
#include <alchemy/timer.h>
#include <alchemy/sem.h>
#include <boilerplate/trace.h>
#include <xenomai/init.h>
#define PRIO 50
void test(void *cookie) {
unsigned long tick;
int ret;
ret = __RT(get_timer_hits(&tick));
if (ret){
fprintf(stderr,
"%s: failed to get_tick,%s\n",
__func__,strerror(-ret));
return ret;
}
fprintf(stdout,"timer_hits:%ld\n",tick);
/*....*/
return 0;
}
int main(int argc, char *const *argv) {
struct sigaction sa __attribute__((unused));
int sig, cpu = 0;
char sem_name[16];
sigset_t mask;
RT_TASK task;
int ret;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGHUP);
sigaddset(&mask, SIGALRM);
pthread_sigmask(SIG_BLOCK, &mask, NULL);
setlinebuf(stdout);
ret = rt_task_spawn(&task, "test_task", 0, PRIO,
T_JOINABLE, test, NULL);
if (ret){
fprintf(stderr,
"%s: failed to create task,%s\n",
__func__,strerror(-ret));
return ret;
}
__STD(sigwait(&mask, &sig));
rt_task_join(&task);
rt_task_delete(&task);
return 0;
}
複製代碼
編譯Makefile:
XENO_CONFIG := /usr/xenomai/bin/xeno-config
PROJPATH = .
CFLAGS := $(shell $(XENO_CONFIG) --posix --alchemy --cflags)
LDFLAGS := $(shell $(XENO_CONFIG) --posix --alchemy --ldflags)
INCFLAGS= -I$(PROJPATH)/include/
EXECUTABLE := get-timer-hits
src = $(wildcard ./*.c)
obj = $(patsubst %.c, %.o, $(src))
all: $(EXECUTABLE)
$(EXECUTABLE): $(obj)
$(CC) -g -o $@ $^ $(INCFLAGS) $(CFLAGS) $(LDFLAGS)
%.o:%.c
$(CC) -g -o $@ -c $< $(INCFLAGS) $(CFLAGS) $(LDFLAGS)
.PHONY: clean
clean:
rm -f $(EXECUTABLE) $(obj)
複製代碼
運行結果:
$./get-timer-hits
timer_hits:3
複製代碼
能夠看到,雖然系統已經啓動十幾分鍾了,但一直沒有運行xenomai應用,xenomai tick相關中斷才產生了3次,這就是tickless,後面會出xenomai調度及時間子系統相關文章,敬請關注。