Chinaunix首页 | 论坛 | 博客
  • 博客访问: 481838
  • 博文数量: 72
  • 博客积分: 1851
  • 博客等级: 上尉
  • 技术积分: 1464
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-16 17:50
文章分类

全部博文(72)

文章存档

2013年(1)

2012年(17)

2011年(51)

2010年(3)

分类: LINUX

2011-05-22 17:29:24

缓存信息

缓存主要用在保存文件,文件夹的描述信息stat,每个信息都有有效信息。

缓存结构体信息如下:


  1. struct cache
  2. {
  3. int on;//使用cache
  4. //各个不同类型的超时时间
  5. unsigned stat_timeout;
  6. unsigned dir_tiemout;
  7. unsigned link_timeout;
  8. //fuse的操作
  9. struct fuse_cache_operations *next_oper;
  10. //实现path /key-->node/value的hashtable映射
  11. GHashTable *table;
  12. pthread_mutex_t lock;
  13. //上次更新时间
  14. time_t last_cleaned;
  15. uint6_t wctr;//统计信息
  16. }

每个hashtable对应的value数据结构


  1. struct node {
  2. struct stat stat;
  3. time_t stat_valid;
  4. char** dir;
  5. time_t dir_valid;
  6. char* link;
  7. time_t link_valid;
  8. time_t valid;
  9. }

sshfs中使用了缓存信息的函数为


  1. static int sshfs_open_common(const char*path,mode_t mode,struct fuse_file_info*fi)
  2. {
  3. ………
  4. //这里每次的请求过来的stat信息都会进行缓存,但是如果多次打开文件的话,服务器也是
  5. //多次打开文件
  6. cache_add_attr(path,&stbuf,wrctr);
  7. ……..
  8. }
  9. cache_add_attr(const char*path,const struct stat*stbuf,uint6_t wctr)
  10. {
  11. ……..
  12. node = cache_get(path);
  13. now = time(NULL);
  14. node->stat= *stbuf;
  15. node->stat_valid = time(NULL)+cache.stat_timeout;
  16. ……
  17. //每次调用就会主动调用,如果缓存空间不够,就清除过时的信息
  18. //如果时间超时,或者hashtable中的大小达到,就开始删除
  19. cache_clean();
  20. }
  21. //只要sshfs_open/添加被调用,就会自动调用cache_clean();
  22. static void cache_clean(void)
  23. {    //默认超时时间为20s
  24.     time_t now = time(NULL);
  25.     //直接删除,不用进行回写
  26.     if(now>cache.last_cleaned+MIN_CACHE_CLEAN_INTERVAL &&
  27.     (g_hash_table_size(cache.table)>MAX_CACHE_SIZE||now>
  28.     cache.last_cleaned + CACHE_CLEAN_INTERVAL))
  29.     {
  30.     //这里能在删除时,不被回写的主要原因是只是保存读取的信息,不用回写,而且
  31.     //采用了定时机制,命中了即使超时也不能采用,另外修改操作没有进行缓存,修改后,    //还要清空缓存
  32.         g_hash_table_remove(cache.table,                 (GHRFunc)cache_clean_entry,&now);
  33.         cache.last_cleaned = now;
  34.     }
  35. }

而另外使用缓存的信息时,就是系统调用函数getdir (readdir),而在进行文件读的时候没有进行大小的合法性判断,但是在getdir中没有调用该目录的查看。


阅读(1624) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~