venti-cache 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. .TH VENTI-CACHE 2
  2. .SH NAME
  3. VtBlock, VtCache,
  4. vtblockcopy,
  5. vtblockduplock,
  6. vtblockput,
  7. vtblockwrite,
  8. vtcachealloc,
  9. vtcacheallocblock,
  10. vtcacheblocksize,
  11. vtcachefree,
  12. vtcacheglobal,
  13. vtcachelocal,
  14. vtcachesetwrite,
  15. vtglobaltolocal,
  16. vtlocaltoglobal \- Venti block cache
  17. .SH SYNOPSIS
  18. .ft L
  19. #include <u.h>
  20. .br
  21. #include <libc.h>
  22. .br
  23. #include <venti.h>
  24. .ta +\w'\fLxxxx 'u
  25. .PP
  26. .ft L
  27. .nf
  28. typedef struct VtBlock
  29. {
  30. uchar *data;
  31. uchar type;
  32. uchar score[VtScoreSize];
  33. u32int addr;
  34. ...
  35. } VtBlock;
  36. .ta +\w'\fLVtBlock* 'u +\w'\fLxxxxxxxx'u
  37. .PP
  38. .B
  39. VtCache* vtcachealloc(VtConn *z, int blocksize, ulong nblocks);
  40. .PP
  41. .B
  42. void vtcachefree(VtCache *c);
  43. .PP
  44. .B
  45. u32int vtcacheblocksize(VtCache *c);
  46. .PP
  47. .B
  48. u32int vtglobaltolocal(uchar score[VtScoreSize])
  49. .br
  50. .B
  51. void vtlocaltoglobal(u32int local, uchar score[VtScoreSize])
  52. .PP
  53. .B
  54. VtBlock* vtcacheallocblock(VtCache *c, int type);
  55. .PP
  56. .B
  57. VtBlock* vtcachelocal(VtCache *c, u32int addr, int type);
  58. .PP
  59. .B
  60. VtBlock* vtcacheglobal(VtCache *c, uchar[VtScoreSize], int type);
  61. .PP
  62. .B
  63. void vtblockput(VtBlock *b);
  64. .PP
  65. .B
  66. void vtblockduplock(VtBlock *b);
  67. .PP
  68. .B
  69. int vtblockwrite(VtBlock *b);
  70. .PP
  71. .B
  72. void vtcachesetwrite(VtCache *c,
  73. .br
  74. .B
  75. int (*write)(VtConn*, uchar[VtScoreSize], uint, uchar*, int));
  76. .PP
  77. .B
  78. VtBlock* vtblockcopy(VtBlock *b);
  79. .SH DESCRIPTION
  80. These functions provide access to a simple in-memory
  81. cache of blocks already stored on a Venti server
  82. and blocks that will eventually be stored on a Venti server.
  83. .PP
  84. A
  85. .B VtBlock
  86. represents a venti data block.
  87. Blocks stored on a venti server,
  88. called
  89. .IR "global blocks" ,
  90. are named by the SHA1 hash of their contents.
  91. This hash is recorded as the block's
  92. .IR score .
  93. Such blocks are immutable.
  94. The cache also stores mutable blocks that have not yet been
  95. written to a venti server. These blocks are called
  96. .IR "local blocks" ,
  97. and have special scores that are 16 zero bytes
  98. followed by a 4-byte big-endian
  99. .IR address .
  100. The address is an index into the internal set of cache blocks.
  101. .PP
  102. The user-visible contents of a
  103. .B VtBlock
  104. are
  105. .BR data ,
  106. a pointer to the data;
  107. .BR type ,
  108. the venti block type;
  109. .BR score ,
  110. the block's score;
  111. and
  112. .BR addr ,
  113. the block's cache address.
  114. .PP
  115. .I Vtcachealloc
  116. allocates a new cache using the client connection
  117. .I z
  118. (see
  119. .IR venti-conn (2)
  120. and
  121. .IR venti-client (2)),
  122. with room for
  123. .I nblocks
  124. of maximum block size
  125. .I blocksize .
  126. .PP
  127. .I Vtcachefree
  128. frees a cache and all the associated blocks.
  129. .PP
  130. .I Vtcacheblocksize
  131. returns the cache's maximum block size.
  132. .PP
  133. .I Vtglobaltolocal
  134. returns the local address corresponding to the given
  135. local
  136. .IR score .
  137. If passed a global score,
  138. .I vtglobaltolocal
  139. returns the special constant
  140. .B NilBlock
  141. .RB ( ~0 ).
  142. .I Vtlocaltoglobal
  143. is the opposite, setting
  144. .I score
  145. to the local score for the cache address
  146. .IR local .
  147. .PP
  148. .I Vtcacheallocblock
  149. allocates a new local block with the given
  150. .IR type .
  151. .PP
  152. .I Vtcachelocal
  153. retrieves the local block at address
  154. .I addr
  155. from the cache.
  156. The given
  157. .I type
  158. must match the type of the block found at
  159. .IR addr .
  160. .PP
  161. .I Vtcacheglobal
  162. retrieves the block with the given
  163. .I score
  164. and
  165. .I dtype
  166. from the cache, consulting the Venti server
  167. if necessary.
  168. If passed a local score,
  169. .I vtcacheglobal
  170. invokes
  171. .I vtcachelocal
  172. appropriately.
  173. .PP
  174. The block references returned by
  175. .IR vtcacheallocblock ,
  176. .IR vtcachelocal ,
  177. and
  178. .I vtcacheglobal
  179. must be released when no longer needed.
  180. .I Vtblockput
  181. releases such a reference.
  182. .PP
  183. It is occasionally convenient to have multiple variables
  184. refer to the same block.
  185. .I Vtblockduplock
  186. increments the block's reference count so that
  187. an extra
  188. .I vtblockput
  189. will be required in order to release the block.
  190. .PP
  191. .I Vtblockwrite
  192. writes a local block to the Venti server,
  193. changing the block to a global block.
  194. It calls the cache's
  195. .I write
  196. function
  197. to write the block to the server.
  198. The default
  199. .I write
  200. function is
  201. .I vtwrite
  202. (see
  203. .IR venti-client (2));
  204. .I vtsetcachewrite
  205. sets it.
  206. .I Vtsetcachewrite
  207. is used by clients to install replacement functions
  208. that run writes in the background or perform other
  209. additional processing.
  210. .PP
  211. .I Vtblockcopy
  212. copies a block in preparation for modifying its contents.
  213. The old block may be a local or global block,
  214. but the new block will be a local block.
  215. .PP
  216. The cache only evicts global blocks.
  217. Local blocks can only leave the cache via
  218. .IR vtblockwrite ,
  219. which turns them into global blocks, making them candidates for
  220. eviction.
  221. .PP
  222. If a new cache block must be allocated (for
  223. .IR vtcacheallocblock ,
  224. .IR vtcachelocal ,
  225. .IR vtcacheglobal ,
  226. or
  227. .IR vtblockcopy ),
  228. but the cache is filled (with local blocks and blocks that
  229. have not yet been released with
  230. .IR vtblockput ),
  231. the library prints the score and reference count of
  232. every block in the cache and then aborts.
  233. A full cache indicates either that the cache is too small,
  234. or, more commonly, that cache blocks are being leaked.
  235. .SH SOURCE
  236. .B /sys/src/libventi
  237. .SH SEE ALSO
  238. .IR venti (2),
  239. .IR venti-client (2),
  240. .IR venti-conn (2),
  241. .IR venti-file (2),
  242. .IR venti (6)