cache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*********************************************************************
  2. * Copyright 1993, UCAR/Unidata
  3. * See netcdf/COPYRIGHT file for copying and redistribution conditions.
  4. * $Header$
  5. *********************************************************************/
  6. #include "ncdap3.h"
  7. #include "dapodom.h"
  8. #include "dapdump.h"
  9. static int iscacheableconstraint(DCEconstraint* con);
  10. /* Return 1 if we can reuse cached data to address
  11. the current get_vara request; return 0 otherwise.
  12. Target is in the constrained tree space.
  13. Currently, if the target matches a cache that is not
  14. a whole variable, then match is false.
  15. */
  16. int
  17. iscached(NCDAPCOMMON* nccomm, CDFnode* target, NCcachenode** cachenodep)
  18. {
  19. int i,j,found,index;
  20. NCcache* cache;
  21. NCcachenode* cachenode;
  22. found = 0;
  23. if(target == NULL) goto done;
  24. /* Match the target variable against the prefetch, if any */
  25. /* Note that prefetches are always whole variable */
  26. cache = nccomm->cdf.cache;
  27. cachenode = cache->prefetch;
  28. if(cachenode!= NULL) {
  29. for(found=0,i=0;i<nclistlength(cachenode->vars);i++) {
  30. CDFnode* var = (CDFnode*)nclistget(cachenode->vars,i);
  31. if(var == target) {
  32. if(cachenodep) *cachenodep = cachenode;
  33. found=1;
  34. goto done;
  35. }
  36. }
  37. }
  38. /*search other cache nodes starting at latest first */
  39. index = 0;
  40. for(i=nclistlength(cache->nodes)-1;i>=0;i--) {
  41. cachenode = (NCcachenode*)nclistget(cache->nodes,i);
  42. /* We currently do not try to match constraints;
  43. If the cachenode is constrained by more than
  44. simple wholevariable projections, then skip it.
  45. */
  46. if(!cachenode->wholevariable) continue;
  47. for(found=0,j=0;j<nclistlength(cachenode->vars);j++) {
  48. CDFnode* var = (CDFnode*)nclistget(cachenode->vars,j);
  49. if(var == target) {found=1;index=i;break;}
  50. }
  51. if(found) break;
  52. }
  53. if(found) {
  54. ASSERT((cachenode != NULL));
  55. if(nclistlength(cache->nodes) > 1) {
  56. /* Manage the cache nodes as LRU */
  57. nclistremove(cache->nodes,index);
  58. nclistpush(cache->nodes,(ncelem)cachenode);
  59. }
  60. if(cachenodep) *cachenodep = cachenode;
  61. }
  62. done:
  63. #ifdef DEBUG
  64. fprintf(stderr,"iscached: search: %s\n",makecdfpathstring3(target,"."));
  65. if(found)
  66. fprintf(stderr,"iscached: found: %s\n",dumpcachenode(cachenode));
  67. else
  68. fprintf(stderr,"iscached: notfound\n");
  69. #endif
  70. return found;
  71. }
  72. /* Compute the set of prefetched data.
  73. Notes:
  74. 1. Even if caching is off, we will
  75. still prefetch the small variables.
  76. 2. All prefetches are whole variable fetches.
  77. 3. If the data set is unconstrainable, we
  78. will prefetch the whole thing
  79. */
  80. NCerror
  81. prefetchdata3(NCDAPCOMMON* nccomm)
  82. {
  83. int i,j;
  84. NCerror ncstat = NC_NOERR;
  85. NClist* allvars = nccomm->cdf.varnodes;
  86. DCEconstraint* urlconstraint = nccomm->oc.dapconstraint;
  87. NClist* vars = nclistnew();
  88. NCcachenode* cache = NULL;
  89. DCEconstraint* newconstraint = NULL;
  90. int isnc4 = FLAGSET(nccomm->controls,NCF_NC4);
  91. if(FLAGSET(nccomm->controls,NCF_UNCONSTRAINABLE)) {
  92. /* If we cannot constrain and caching is enabled,
  93. then pull in everything */
  94. if(FLAGSET(nccomm->controls,NCF_CACHE)) {
  95. for(i=0;i<nclistlength(allvars);i++) {
  96. nclistpush(vars,nclistget(allvars,i));
  97. }
  98. } else { /* do no prefetching */
  99. nccomm->cdf.cache->prefetch = NULL;
  100. goto done;
  101. }
  102. } else { /* can do constraints */
  103. /* pull in those variables of sufficiently small size */
  104. for(i=0;i<nclistlength(allvars);i++) {
  105. CDFnode* var = (CDFnode*)nclistget(allvars,i);
  106. size_t nelems = 1;
  107. if(!isnc4) {
  108. /* If netcdf 3 and var is a sequence or under a sequence, then never prefetch */
  109. if(var->nctype == NC_Sequence || dapinsequence(var)) continue;
  110. }
  111. /* Compute the # of elements in the variable */
  112. for(j=0;j<nclistlength(var->array.dimset0);j++) {
  113. CDFnode* dim = (CDFnode*)nclistget(var->array.dimset0,j);
  114. nelems *= dim->dim.declsize;
  115. }
  116. if(SHOWFETCH) {
  117. nclog(NCLOGDBG,"prefetch: %s=%lu",var->ncfullname,(unsigned long)nelems);
  118. }
  119. if(nelems <= nccomm->cdf.smallsizelimit) {
  120. nclistpush(vars,(ncelem)var);
  121. if(SHOWFETCH) {
  122. nclog(NCLOGDBG,"prefetch: %s",var->ncfullname);
  123. }
  124. }
  125. }
  126. }
  127. /* If there are no vars, then do nothing */
  128. if(nclistlength(vars) == 0) {
  129. nccomm->cdf.cache->prefetch = NULL;
  130. goto done;
  131. }
  132. /* Create a single constraint consisting of the projections for the variables;
  133. each projection is whole variable. The selections are passed on as is.
  134. */
  135. newconstraint = (DCEconstraint*)dcecreate(CES_CONSTRAINT);
  136. newconstraint->projections = nclistnew();
  137. newconstraint->selections = dceclonelist(urlconstraint->selections);
  138. for(i=0;i<nclistlength(vars);i++) {
  139. CDFnode* var = (CDFnode*)nclistget(vars,i);
  140. DCEprojection* varprojection;
  141. /* convert var to a projection */
  142. ncstat = dapvar2projection(var,&varprojection);
  143. if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}
  144. nclistpush(newconstraint->projections,(ncelem)varprojection);
  145. }
  146. if(SHOWFETCH) {
  147. char* s = dumpprojections(newconstraint->projections);
  148. LOG1(NCLOGNOTE,"prefetch.final: %s",s);
  149. nullfree(s);
  150. }
  151. ncstat = buildcachenode34(nccomm,newconstraint,vars,&cache,!isnc4);
  152. newconstraint = NULL; /* buildcachenode34 takes control of newconstraint */
  153. if(ncstat) goto done;
  154. cache->wholevariable = 1; /* All prefetches are whole variable */
  155. /* Make cache node be the prefetch node */
  156. nccomm->cdf.cache->prefetch = cache;
  157. if(SHOWFETCH) {
  158. LOG0(NCLOGNOTE,"prefetch.complete");
  159. }
  160. if(SHOWFETCH) {
  161. char* s = NULL;
  162. /* Log the set of prefetch variables */
  163. NCbytes* buf = ncbytesnew();
  164. ncbytescat(buf,"prefetch.vars: ");
  165. for(i=0;i<nclistlength(vars);i++) {
  166. CDFnode* var = (CDFnode*)nclistget(vars,i);
  167. ncbytescat(buf," ");
  168. s = makecdfpathstring3(var,".");
  169. ncbytescat(buf,s);
  170. nullfree(s);
  171. }
  172. ncbytescat(buf,"\n");
  173. nclog(NCLOGNOTE,"%s",ncbytescontents(buf));
  174. ncbytesfree(buf);
  175. }
  176. done:
  177. nclistfree(vars);
  178. dcefree((DCEnode*)newconstraint);
  179. if(ncstat) freenccachenode(nccomm,cache);
  180. return THROW(ncstat);
  181. }
  182. NCerror
  183. buildcachenode34(NCDAPCOMMON* nccomm,
  184. DCEconstraint* constraint,
  185. NClist* varlist,
  186. NCcachenode** cachep,
  187. int isprefetch)
  188. {
  189. NCerror ncstat = NC_NOERR;
  190. OCerror ocstat = OC_NOERR;
  191. OCconnection conn = nccomm->oc.conn;
  192. OCobject ocroot = OCNULL;
  193. CDFnode* dxdroot = NULL;
  194. NCcachenode* cachenode = NULL;
  195. char* ce = NULL;
  196. ce = buildconstraintstring3(constraint);
  197. ocstat = dap_fetch(nccomm,conn,ce,OCDATADDS,&ocroot);
  198. nullfree(ce);
  199. if(ocstat) {THROWCHK(ocerrtoncerr(ocstat)); goto done;}
  200. ncstat = buildcdftree34(nccomm,ocroot,OCDATA,&dxdroot);
  201. if(ncstat) {THROWCHK(ncstat); goto done;}
  202. /* regrid */
  203. if(!FLAGSET(nccomm->controls,NCF_UNCONSTRAINABLE)) {
  204. ncstat = regrid3(dxdroot,nccomm->cdf.ddsroot,constraint->projections);
  205. if(ncstat) {THROWCHK(ncstat); goto done;}
  206. }
  207. /* create the cache node */
  208. cachenode = createnccachenode();
  209. cachenode->prefetch = isprefetch;
  210. cachenode->vars = nclistclone(varlist);
  211. cachenode->datadds = dxdroot;
  212. /* Give the constraint over to the cachenode */
  213. cachenode->constraint = constraint;
  214. constraint = NULL;
  215. cachenode->wholevariable = iscacheableconstraint(cachenode->constraint);
  216. /* save the root content*/
  217. cachenode->ocroot = ocroot;
  218. cachenode->content = oc_data_new(conn);
  219. ocstat = oc_data_root(conn,ocroot,cachenode->content);
  220. if(ocstat) {THROWCHK(ocerrtoncerr(ocstat)); goto done;}
  221. /* capture the packet size */
  222. ocstat = oc_raw_xdrsize(conn,ocroot,&cachenode->xdrsize);
  223. if(ocstat) {THROWCHK(ocerrtoncerr(ocstat)); goto done;}
  224. #ifdef DEBUG
  225. fprintf(stderr,"buildcachenode: new cache node: %s\n",
  226. dumpcachenode(cachenode));
  227. #endif
  228. /* Insert into the cache. If not caching, then
  229. remove any previous cache node
  230. */
  231. if(!isprefetch) {
  232. NCcache* cache = nccomm->cdf.cache;
  233. if(cache->nodes == NULL) cache->nodes = nclistnew();
  234. /* remove cache nodes to get below the max cache size */
  235. while(cache->cachesize + cachenode->xdrsize > cache->cachelimit
  236. && nclistlength(cache->nodes) > 0) {
  237. NCcachenode* node = (NCcachenode*)nclistremove(cache->nodes,0);
  238. #ifdef DEBUG
  239. fprintf(stderr,"buildcachenode: purge cache node: %s\n",
  240. dumpcachenode(cachenode));
  241. #endif
  242. cache->cachesize -= node->xdrsize;
  243. freenccachenode(nccomm,node);
  244. }
  245. /* Remove cache nodes to get below the max cache count */
  246. /* If not caching, then cachecount should be 0 */
  247. while(nclistlength(cache->nodes) > cache->cachecount) {
  248. NCcachenode* node = (NCcachenode*)nclistremove(cache->nodes,0);
  249. #ifdef DEBUG
  250. fprintf(stderr,"buildcachenode: count purge cache node: %s\n",
  251. dumpcachenode(node));
  252. #endif
  253. cache->cachesize -= node->xdrsize;
  254. freenccachenode(nccomm,node);
  255. }
  256. nclistpush(nccomm->cdf.cache->nodes,(ncelem)cachenode);
  257. cache->cachesize += cachenode->xdrsize;
  258. }
  259. #ifdef DEBUG
  260. fprintf(stderr,"buildcachenode: %s\n",dumpcachenode(cachenode));
  261. #endif
  262. done:
  263. if(constraint != NULL) dcefree((DCEnode*)constraint);
  264. if(cachep) *cachep = cachenode;
  265. if(ocstat != OC_NOERR) ncstat = ocerrtoncerr(ocstat);
  266. if(ncstat) {
  267. freecdfroot34(dxdroot);
  268. freenccachenode(nccomm,cachenode);
  269. }
  270. return THROW(ncstat);
  271. }
  272. NCcachenode*
  273. createnccachenode(void)
  274. {
  275. NCcachenode* mem = (NCcachenode*)calloc(1,sizeof(NCcachenode));
  276. return mem;
  277. }
  278. void
  279. freenccachenode(NCDAPCOMMON* nccomm, NCcachenode* node)
  280. {
  281. if(node == NULL) return;
  282. oc_data_free(nccomm->oc.conn,node->content);
  283. dcefree((DCEnode*)node->constraint);
  284. freecdfroot34(node->datadds);
  285. nclistfree(node->vars);
  286. nullfree(node);
  287. }
  288. void
  289. freenccache(NCDAPCOMMON* nccomm, NCcache* cache)
  290. {
  291. int i;
  292. if(cache == NULL) return;
  293. freenccachenode(nccomm,cache->prefetch);
  294. for(i=0;i<nclistlength(cache->nodes);i++) {
  295. freenccachenode(nccomm,(NCcachenode*)nclistget(cache->nodes,i));
  296. }
  297. nclistfree(cache->nodes);
  298. nullfree(cache);
  299. }
  300. NCcache*
  301. createnccache(void)
  302. {
  303. NCcache* c = (NCcache*)calloc(1,sizeof(NCcache));
  304. c->cachelimit = DFALTCACHELIMIT;
  305. c->cachesize = 0;
  306. c->nodes = nclistnew();
  307. c->cachecount = DFALTCACHECOUNT;
  308. return c;
  309. }
  310. static int
  311. iscacheableprojection(DCEprojection* proj)
  312. {
  313. int i,cacheable;
  314. if(proj->discrim != CES_VAR) return 0;
  315. cacheable = 1; /* assume so */
  316. for(i=0;i<nclistlength(proj->var->segments);i++) {
  317. DCEsegment* segment = (DCEsegment*)nclistget(proj->var->segments,i);
  318. if(!iswholesegment(segment)) {cacheable = 0; break;}
  319. }
  320. return cacheable;
  321. }
  322. static int
  323. iscacheableconstraint(DCEconstraint* con)
  324. {
  325. int i;
  326. if(con == NULL) return 1;
  327. if(con->selections != NULL && nclistlength(con->selections) > 0)
  328. return 0; /* cant deal with selections */
  329. for(i=0;i<nclistlength(con->projections);i++) {
  330. if(!iscacheableprojection((DCEprojection*)nclistget(con->projections,i)))
  331. return 0;
  332. }
  333. return 1;
  334. }