XenStore:使用,结构和原理(4. 监视: xs_watch)

Xenstore的监视(watch)功能很实用,在xenstore监视目标文件夹里发生的任何修改,都会通知watch的注册者。xen虚拟机的后端驱动程序,就是通过watch来检测前端设备的改变。

需要注意的:

(1)注册watch不需要开始一个transaction,只要用xs_open打开连接就行了。内核可以直接调用register_xenbus_watch

(2)watch在注册时,xenstored会马上产生一个事件。它就是这么设计的。如果不想处理这个时间,需要设法把它忽略掉。


watch的用法:

内核空间:(转自http://wiki.xen.org/xenwiki/XenBus

  1. static struct xenbus_watch xb_watch = {  
  2.     .node = "memory",  
  3.     .callback = watch_target;  
  4. };  
  5. ret = register_xenbus_watch(&xb_watch);  
  6.     if(IS_ERR(ret)) {  
  7.       IPRINTK("Failed to initialize balloon watcher\n");  
  8.     } else {  
  9.       IPRINTK("Balloon xenbus watcher initialized\n");  
  10.     }  


用户空间:用户空间和内核稍有不同。用户空间可以用xs_watch函数定义一个监视,但是不能指定回调函数,而是需要调用xs_read_watch进行阻塞式的读取,像这样:

  1. xs_handle *xh=xs_open(0);  
  1. assert(xs_watch(xh, path, token));  
  2. char **result = 0;  
  3. result = xs_read_watch(xh, &num);             //会阻塞  
  4. free(result);  
  5. xs_unwatch(xh, path, token);  

如果不想阻塞,就得自己开一个线程用来监听。但是这里有一个bug(4.1.3版本xen):xs_read_watch不是线程安全的。如果在xs_read_watch阻塞的时候,使用pthread_cancel来终止线程,xs_read_watch不会释放所有的资源(锁)。而主线程如果调用xs_close关闭与xenstore的连接,则会因为不能得到这些资源而死锁。具体分析如下:

tools/xenstore/xs.c

  1. char **xs_read_watch(struct xs_handle *h, unsigned int *num)  
  2. {  
  3.         struct xs_stored_msg *msg;  
  4.         char **ret, *strings, c = 0;  
  5.         unsigned int num_strings, i;  
  6.   
  7.         mutex_lock(&h->watch_mutex);                 //------------------>加锁  
  8.                                               //此处应该加一个pthread_cleanup_push(pthread_mutex_unlock, &h->watch_mutex)  
  9. #ifdef USE_PTHREAD  
  10.         /* Wait on the condition variable for a watch to fire. 
  11.          * If the reader thread doesn't exist yet, then that's because 
  12.          * we haven't called xs_watch.  Presumably the application 
  13.          * will do so later; in the meantime we just block. 
  14.          */  
  15.         while (list_empty(&h->watch_list) && h->fd != -1)  
  16.                 condvar_wait(&h->watch_condvar, &h->watch_mutex);  
  17. #else /* !defined(USE_PTHREAD) */  
  18.         /* Read from comms channel ourselves if there are no threads 
  19.          * and therefore no reader thread. */  
  20.   
  21.         assert(!read_thread_exists(h)); /* not threadsafe but worth a check */  
  22.         if ((read_message(h) == -1))                //----------------------->没有消息时阻塞在这里  
  23.                 return NULL;  


xs_read_watch一开始加锁,紧接着调用read_message。read_message会阻塞调用read函数。如果这时主线程调用pthread_cancel来取消运行xs_read_watch的线程,h->watch_mutex就不会被释放,会造成后面的死锁。

很挫的解决办法:在调用xs_unwatch和xs_close之前,检查xs_handle内部锁的情况,强行解锁资源。

xs_handle这个结构体在头文件里只有类型声明,具体的定义在xs.c文件里,仅供xenstore内部使用。要调用xs_handle内部的对象,首先得把xs_handle的详细声明提取出来:

  1. //used to extract (>_<) xs_handle internal members.  
  2. typedef struct list_head {  
  3.         struct list_head *next, *prev;  
  4. }list_head_struct;  
  5. typedef struct  
  6. {  
  7.     int fd;  
  8.     pthread_t read_thr;  
  9.     int read_thr_exists;  
  10.     struct list_head watch_list;  
  11.     pthread_mutex_t watch_mutex;//监视信号量,可能死锁  
  12.     pthread_cond_t watch_condvar;  
  13.     int watch_pipe[2];  
  14.     struct list_head reply_list;  
  15.     pthread_mutex_t reply_mutex;//回复信号量  
  16.     pthread_cond_t reply_condvar;  
  17.     pthread_mutex_t request_mutex;//请求信号量  
  18. }my_xs_handle;  
  19.   
  20. #if __GNUC__ > 3  
  21. #define offsetof(a,b) __builtin_offsetof(a,b)//提取偏移量的宏。  
  22. #else  
  23. #define offsetof(a,b) ((unsigned long)&(((a *)0)->b))  
  24. #endif  

然后像这样提取xh内部成员:

pthread_mutex_t *pm_watch = (pthread_mutex_t *)(((void *)xh) + offsetof(my_xs_handle, watch_mutex));

解锁的宏。其实写成个函数也行

  1. #define check_xh_lock(x) do {\  
  2.         pthread_mutex_t *pm = (pthread_mutex_t *)(((void *)pthis->xh) + offsetof(my_xs_handle, x));  \  
  3.         if (pthread_mutex_trylock(pm) == EBUSY){            \           //pthread_mutex_trylock 如果没上锁,则加锁;否则返回EBUSY  
  4.             cout << "thread_cleanup -> " #x " is already locked!" << endl;   \  
  5.             if (0 != pthread_mutex_unlock(pm))              \         
  6.             cout << "thread_cleanup -> error unlocking!" << endl;            \  
  7.             else cout << "thread_cleanup -> unlocking " #x << endl;          \  
  8.         } else assert(pthread_mutex_unlock(pm)==0);             \  
  9.     } while (0)  
  1. check_xh_lock(watch_mutex);  
  2. check_xh_lock(request_mutex);  
  3. check_xh_lock(reply_mutex);  
  4. cout << "----- unwatch -----" << endl;  
  5. xs_unwatch(pthis->xh, pthis->path.c_str(), map_path("watch", pthis->path).c_str());  
  6.   
  7. check_xh_lock(watch_mutex);  
  8. check_xh_lock(request_mutex);  
  9. check_xh_lock(reply_mutex);  
  10. cout << "-----  close  -----" << endl;  
  11. xs_close(pthis->xh);  
  12. pthis->xh=0;  


测试如下:

  1. viktor@buxiang-OptiPlex-330:~/proj/xc_map$ sudo ./domu domu.cpp  
  2. watch -> add watch on path mmap/domu-cpp callback func 0x8049aad                       //主线程打开监视线程  
  3.   
  4. thread_func -> begin watch thread path= mmap/domu-cpp                                  //监视线程注册watch  
  5. thread_func -> watch event path= mmap/domu-cpp token= watch/mmap/domu-cpp            //注册时会发生一次事件,忽略之  
  6. watch_callback -> entering  
  7.                                          //(在这里等待,此时按下Ctrl+C)  
  8. ^Cmain received signal Interrupt  
  9. unwatch -> **stopping work thread. waiting here...work thread already stopped.          //主线程用pthread_cancel关闭监视线程  
  10. thread_cleanup -> path= mmap/domu-cpp thread=b6d6eb70                                    //监视线程退出,主线程开始清理  
  11. ----- unwatch -----  
  12. map_path -> watch/mmap/domu-cpp  
  13. thread_cleanup -> watch_mutex is already locked!                                   //此时watch_mutex上锁了,说明xs_read_watch没有释放资源  
  14. thread_cleanup -> unlocking watch_mutex                                          //强行解锁  
  15. -----  close  -----                 //调用xs_unwatch和xs_close关闭连接。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值