page_cache.C 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: page_cache.cc /main/4 1996/06/11 17:44:22 cde-hal $
  24. *
  25. * (c) Copyright 1996 Digital Equipment Corporation.
  26. * (c) Copyright 1996 Hewlett-Packard Company.
  27. * (c) Copyright 1996 International Business Machines Corp.
  28. * (c) Copyright 1996 Sun Microsystems, Inc.
  29. * (c) Copyright 1996 Novell, Inc.
  30. * (c) Copyright 1996 FUJITSU LIMITED.
  31. * (c) Copyright 1996 Hitachi.
  32. */
  33. #include "utility/debug.h"
  34. #include "storage/page.h"
  35. #include "storage/heap_comp_funcs.h"
  36. #include "storage/page_rep.h"
  37. #include "storage/page_storage.h"
  38. page_cache_local_part::page_cache_local_part(unsigned int exp_cached_page) :
  39. f_num_cached_pages(0),
  40. f_free_space(fbytes_t_eq, fbytes_t_ls, exp_cached_page),
  41. f_page_pool_index(page_rep_eq, page_rep_ls),
  42. f_current_page_num(0),
  43. f_current_page_ptr(0)
  44. {
  45. }
  46. page_cache_local_part::~page_cache_local_part()
  47. {
  48. f_page_pool_index.del_elements(page_rep_del);
  49. }
  50. Boolean page_cache_local_part::init_heap(page_storage* st)
  51. {
  52. ////////////////////////////////////////////////////////////////////////////
  53. // We use a different strategy. Since pages are most of time read-only and
  54. // grows linearly when loaded, we assume the 1st to pages()-1 th page are
  55. // full.
  56. ////////////////////////////////////////////////////////////////////////////
  57. if ( st -> pages() > 0 ) {
  58. page* z = (*st)(st -> pages(), page_storage::READ);
  59. f_free_space.insert(
  60. new fbytes_t(z -> free_bytes(), z -> page_id())
  61. );
  62. f_free_space.heapify();
  63. }
  64. return true;
  65. }
  66. Boolean page_cache_local_part::quit_heap(page_storage* st)
  67. {
  68. int ind = f_free_space.first();
  69. while ( ind ) {
  70. fbytes_t *x = (fbytes_t*)f_free_space(ind);
  71. /*
  72. debug(cerr, x -> page_num);
  73. debug(cerr, x -> free_bytes);
  74. */
  75. delete x;
  76. f_free_space.next(ind);
  77. }
  78. return true;
  79. }
  80. page* page_cache_local_part::in_cache(page_storage* st, int page_num)
  81. {
  82. #ifdef DEBUG
  83. st -> total_page_access++;
  84. #endif
  85. if ( f_current_page_num == page_num ) {
  86. return f_current_page_ptr;
  87. }
  88. page_rep data(page_num, 0);
  89. page_rep *result = 0;
  90. /*
  91. cerr << "member(): " << st -> my_name() << " ";
  92. cerr << page_num ;
  93. cerr << " " << (void*)&f_page_pool_index;
  94. cerr << "\n";
  95. */
  96. if ( (result = (page_rep*)f_page_pool_index.member(&data)) ) {
  97. //cerr << "In Cache: page " << page_num << " of " << st -> my_name() << "\n";
  98. lru_page* p = result -> f_page_ptr;
  99. if ( p -> page_id() != page_num ) {
  100. throw(stringException("corrupted store"));
  101. }
  102. if ( p -> get_position() == ACTIVE ) {
  103. #ifndef C_API
  104. (p -> f_store -> f_global_pcache).f_replace_policy.promote(*p);
  105. #else
  106. (p -> f_store -> f_global_pcache_ptr -> f_replace_policy).promote(*p);
  107. #endif
  108. //cerr << "Promoting:" << (long)p << endl;
  109. }
  110. return p;
  111. } else {
  112. #ifdef DEBUG
  113. st -> pagings++;
  114. #endif
  115. return 0;
  116. }
  117. }
  118. void page_cache_local_part::adjust_heap(fbytes_t* v, Boolean new_page)
  119. {
  120. if ( new_page == false ) {
  121. if ( v -> free_bytes < 10 ) {
  122. f_free_space.delete_max();
  123. delete v;
  124. } else
  125. f_free_space.adjust_max_to(v);
  126. } else {
  127. f_free_space.insert_heapify(v);
  128. }
  129. }
  130. fbytes_t*
  131. page_cache_local_part::find_host_page(
  132. page_storage* st, Boolean& new_page, int len
  133. )
  134. {
  135. fbytes_t* x = (fbytes_t*)f_free_space.max_elm();
  136. //if ( x )
  137. // debug(cerr, x -> free_bytes);
  138. //else
  139. // MESSAGE(cerr, "no cached page");
  140. if ( x != 0 && x -> free_bytes > len ) {
  141. new_page = false;
  142. } else {
  143. st -> add_page_frames(1);
  144. new_page = true;
  145. x = new fbytes_t((*st)(st->pages(), page_storage::READ) -> free_bytes(), st->pages());
  146. }
  147. return x;
  148. }
  149. ////////////////////////////////////////////////////////////////////////////
  150. //
  151. //
  152. //
  153. ////////////////////////////////////////////////////////////////////////////
  154. page_cache_global_part::page_cache_global_part(unsigned int allowed_pages) :
  155. f_total_allowed_pages(allowed_pages),
  156. f_replace_policy(allowed_pages, 0, true)
  157. {
  158. if ( allowed_pages == 0 ) {
  159. char* s = getenv("MMDB_CACHED_PAGES");
  160. if ( s ) {
  161. f_total_allowed_pages = atoi(s);
  162. if ( f_total_allowed_pages < MIN_MMDB_CACHED_PAGES ) // minimal value
  163. f_total_allowed_pages = MIN_MMDB_CACHED_PAGES;
  164. } else
  165. f_total_allowed_pages = MMDB_CACHED_PAGES;
  166. f_replace_policy.set_params(f_total_allowed_pages, 0);
  167. } else
  168. if ( allowed_pages < MIN_MMDB_CACHED_PAGES ) {
  169. f_total_allowed_pages = MIN_MMDB_CACHED_PAGES;
  170. f_replace_policy.set_params(f_total_allowed_pages, 0);
  171. }
  172. }
  173. page_cache_global_part::~page_cache_global_part()
  174. {
  175. }
  176. page*
  177. page_cache_global_part::load_new_page(
  178. page_storage* st, int new_page_num, Boolean byte_order)
  179. {
  180. lru_pagePtr p = 0;
  181. if ( f_replace_policy.active_elmts() < (int) f_total_allowed_pages ) {
  182. /*
  183. debug(cerr, page_cache -> active_elmts());
  184. debug(cerr, num_cached_pages);
  185. MESSAGE(cerr, "new a page");
  186. */
  187. p = new lru_page(st, st -> page_sz, new_page_num, byte_order);
  188. //cerr << "New page " << new_page_num << " of " << st -> my_name() << "\n";
  189. } else {
  190. long ind = f_replace_policy.last(ACTIVE);
  191. //cerr << "Getting last:" << (long)ind << endl;
  192. if ( ind == 0 )
  193. throw(stringException("corrupted store"));
  194. p = (lru_page*)f_replace_policy(ind, ACTIVE);
  195. int pid = p -> page_id();
  196. page_rep key(pid, 0);
  197. page_storage* pst = p -> f_store;
  198. //cerr << "Removing " << pid ;
  199. //cerr << " for " << pst -> my_name() << " ";
  200. delete (page_rep*)((pst->f_local_pcache).f_page_pool_index.remove(&key));
  201. ////////////////////////////////////////////////////
  202. // Clean the current_page entry in the local cache.
  203. ////////////////////////////////////////////////////
  204. int & local_pid = (pst->f_local_pcache).f_current_page_num;
  205. if ( local_pid == pid ) local_pid = 0;
  206. if ( p -> dirty == true ) {
  207. #ifdef PORTABLE_DB
  208. ////////////////////////////////////////////////////////////
  209. // swap the count field last.
  210. // no need to flip back to the original as the page frame
  211. // is reused.
  212. ////////////////////////////////////////////////////////////
  213. p -> _swap_order(false);
  214. #endif
  215. //cerr << " swapout";
  216. int page_aoff = ( pid - 1 ) * (pst -> page_sz);
  217. pst -> storage_ptr -> updateString(
  218. page_storage::abs_off + mmdb_pos_t(page_aoff),
  219. p -> page_base(),
  220. pst->page_sz
  221. );
  222. }
  223. //cerr << "\n";
  224. p -> expand_chunk(st -> page_size());
  225. p -> reset();
  226. p -> clean_all();
  227. p -> pageid = new_page_num;
  228. p -> f_store = st;
  229. }
  230. p -> dirty = false;
  231. f_replace_policy.promote(*p);
  232. /////////////////////////
  233. // read in the content
  234. /////////////////////////
  235. page_rep* data = new page_rep(new_page_num, p);
  236. //cerr << "Inserting " << new_page_num << " for " << st -> my_name() << " to page index.";
  237. //cerr << (void*)&(st->f_local_pcache.f_page_pool_index);
  238. st->f_local_pcache.f_page_pool_index.insert(data);
  239. int offset = (new_page_num-1) * (st->page_sz);
  240. if ( offset <
  241. (((unixf_storage*)(st->storage_ptr))->bytes()-page_storage::abs_off))
  242. {
  243. //cerr << " swap in the page";
  244. //debug(cerr, offset);
  245. //debug(cerr, int(p -> page_base()));
  246. ((unixf_storage*)(st->storage_ptr)) ->
  247. readString( page_storage::abs_off + mmdb_pos_t(offset),
  248. p -> page_base(),
  249. st->page_sz
  250. );
  251. #ifdef PORTABLE_DB
  252. p -> _swap_order(true); // swap count field first
  253. #endif
  254. }
  255. //cerr << "\n";
  256. (st->f_local_pcache).f_current_page_num = new_page_num;
  257. (st->f_local_pcache).f_current_page_ptr = p;
  258. return p;
  259. }
  260. void remove_from_global_cache(const void* x)
  261. {
  262. page_rep* y = (page_rep*)x;
  263. lru_page* p = y -> f_page_ptr;
  264. page_storage* st = p -> f_store;
  265. //cerr << "removing " << y -> f_page_num << "\n";
  266. #ifndef C_API
  267. ((st -> f_global_pcache).f_replace_policy).remove(*p);
  268. #else
  269. (st -> f_global_pcache_ptr -> f_replace_policy).remove(*p);
  270. #endif
  271. delete p;
  272. }
  273. void page_cache_global_part::remove_pages(page_storage* st)
  274. {
  275. //cerr << "remove_pages(): for " << st -> my_name() << "\n";
  276. (st -> f_local_pcache).f_page_pool_index.apply(remove_from_global_cache);
  277. /*
  278. lru_pagePtr* temps = new lru_pagePtr[f_replace_policy.active_elmts()];
  279. int ind = f_replace_policy.first();
  280. int i=0;
  281. while ( ind != 0 ) {
  282. lru_page* p = (lru_page*)f_replace_policy(ind, ACTIVE);
  283. if ( p -> f_store == st )
  284. temps[i++] = p;
  285. f_replace_policy.next(ind, ACTIVE);
  286. }
  287. for ( int j=0; j<i; j++ )
  288. f_replace_policy.remove(*temps[j]);
  289. delete temps;
  290. */
  291. }
  292. ostream& page_cache_global_part::print_cached_pages(ostream& s)
  293. {
  294. MESSAGE(s, "cached pages:");
  295. long ind = f_replace_policy.first();
  296. while ( ind != 0 ) {
  297. lru_page* p = (lru_page*)f_replace_policy(ind, ACTIVE);
  298. debug(s, long(p));
  299. if ( p == 0 )
  300. throw(stringException("corrupted store"));
  301. debug(s, *p);
  302. f_replace_policy.next(ind, ACTIVE);
  303. debug(s, ind);
  304. }
  305. MESSAGE(s, "print cached pages done");
  306. return s;
  307. }