node_set.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "node_set.h"
  17. #include "node_index.h"
  18. #include "node_list.h"
  19. #include "global.h"
  20. #include "node.h"
  21. DTC_USING_NAMESPACE
  22. //定义每种属性的内存大小, 至少有以下四种,可以再增加
  23. const uint32_t NODE_SET::NG_ATTR_SIZE[] = {
  24. NODE_GROUP_INCLUDE_NODES * sizeof(NODE_ID_T), //NEXT_NODE
  25. NODE_GROUP_INCLUDE_NODES * sizeof(NODE_ID_T) * 2, //TIME_LIST
  26. NODE_GROUP_INCLUDE_NODES * sizeof(MEM_HANDLE_T), //VD_HANDLE
  27. NODE_GROUP_INCLUDE_NODES / 8, //DIRTY_BMP
  28. };
  29. int NODE_SET::do_init(NODE_ID_T id)
  30. {
  31. ng_list.prev = ng_list.next = INVALID_HANDLE;
  32. ng_dele.top = 0;
  33. ng_dele.count = 0;
  34. ng_free = 0;
  35. ng_nid = id;
  36. //属性
  37. ng_attr.count = attr_count();
  38. ng_attr.offset[0] = base_header_size();
  39. for (unsigned int i = 1; i < ng_attr.count; i++) {
  40. ng_attr.offset[i] = ng_attr.offset[i - 1] + NG_ATTR_SIZE[i - 1];
  41. }
  42. /* 初始化每个Node */
  43. for (unsigned i = 0; i < NODE_GROUP_INCLUDE_NODES; ++i) {
  44. next_node_id(i) = INVALID_NODE_ID;
  45. NODE_ID_T *lru = node_lru(i);
  46. lru[LRU_PREV] = node_id(i);
  47. lru[LRU_NEXT] = node_id(i);
  48. vd_handle(i) = INVALID_HANDLE;
  49. clr_dirty(i);
  50. }
  51. return 0;
  52. }
  53. /* init system reserved zone */
  54. int NODE_SET::system_reserved_init()
  55. {
  56. Node dirtyNode = allocate_node();
  57. if (!dirtyNode) {
  58. return -2;
  59. }
  60. Node cleanNode = allocate_node();
  61. if (!cleanNode) {
  62. return -3;
  63. }
  64. Node emptyNode = allocate_node();
  65. if (!emptyNode) {
  66. return -3;
  67. }
  68. /* init node list head */
  69. INIT_NODE_LIST_HEAD(dirtyNode, dirtyNode.node_id());
  70. INIT_NODE_LIST_HEAD(cleanNode, cleanNode.node_id());
  71. INIT_NODE_LIST_HEAD(emptyNode, emptyNode.node_id());
  72. /* insert node head's node-id to node-index*/
  73. I_INSERT(dirtyNode);
  74. I_INSERT(cleanNode);
  75. I_INSERT(emptyNode);
  76. return 0;
  77. }
  78. /* check system reserved zone integrity
  79. * the main purpose is upgrade/add the missing empty lru list
  80. */
  81. int NODE_SET::system_reserved_check()
  82. {
  83. if (ng_free < 2)
  84. return -10;
  85. // ng_free==2 old format, index 2 is free & reserved
  86. // ng_free==3 new format, index 2 allocated to emptyNodeLru
  87. int hasEmptyLru1 = ng_free >= 3;
  88. // if new format, index 2 is allocated, lru pointer should be non-zero
  89. // sanity check passed
  90. if (hasEmptyLru1 == 0) {
  91. // no empty lru, allocate one
  92. Node emptyNode = allocate_node();
  93. if (!emptyNode) {
  94. return -3;
  95. }
  96. /* init node list head */
  97. INIT_NODE_LIST_HEAD(emptyNode, emptyNode.node_id());
  98. /* insert node head's node-id to node-index*/
  99. I_INSERT(emptyNode);
  100. return 1;
  101. }
  102. return 0;
  103. }
  104. Node NODE_SET::allocate_node(void)
  105. {
  106. if (is_full()) {
  107. return Node(NULL, 0);
  108. }
  109. //优先分配release掉的Node空间
  110. if (ng_dele.count > 0) {
  111. Node N(this, ng_dele.top);
  112. N.Reset();
  113. ng_dele.count--;
  114. ng_dele.top = (uint8_t)N.vd_handle();
  115. return N;
  116. }
  117. //在空闲Node中分配
  118. else {
  119. Node N(this, ng_free);
  120. N.Reset();
  121. ng_free++;
  122. return N;
  123. }
  124. }
  125. int NODE_SET::release_node(Node N)
  126. {
  127. //复用node的handle attribute空间来把释放掉的node组织为单链表
  128. N.vd_handle() = ng_dele.top;
  129. ng_dele.top = N.get_index();
  130. ng_dele.count++;
  131. return 0;
  132. }
  133. bool NODE_SET::is_full(void)
  134. {
  135. return (ng_dele.count == 0 && ng_free >= NODE_GROUP_INCLUDE_NODES);
  136. }
  137. uint32_t NODE_SET::attr_count(void)
  138. {
  139. return sizeof(NG_ATTR_SIZE) / sizeof(uint32_t);
  140. }
  141. uint32_t NODE_SET::base_header_size(void)
  142. {
  143. return OFFSETOF(NODE_SET, ng_attr) + OFFSETOF(NG_ATTR_T, offset) +
  144. sizeof(uint32_t) * attr_count();
  145. }
  146. uint32_t NODE_SET::attr_size(void)
  147. {
  148. uint32_t size = 0;
  149. for (uint32_t i = 0; i < attr_count(); i++) {
  150. size += NG_ATTR_SIZE[i];
  151. }
  152. return size;
  153. }
  154. uint32_t NODE_SET::Size(void)
  155. {
  156. return base_header_size() + attr_size();
  157. }
  158. NODE_ID_T NODE_SET::node_id(int idx) const
  159. {
  160. return (ng_nid + idx);
  161. }
  162. NODE_ID_T &NODE_SET::next_node_id(int idx)
  163. {
  164. return __CAST__<NODE_ID_T>(NEXT_NODE)[idx];
  165. }
  166. NODE_ID_T *NODE_SET::node_lru(int idx)
  167. {
  168. return &(__CAST__<NODE_ID_T>(TIME_LIST)[idx * 2]);
  169. }
  170. MEM_HANDLE_T &NODE_SET::vd_handle(int idx)
  171. {
  172. return __CAST__<MEM_HANDLE_T>(VD_HANDLE)[idx];
  173. }
  174. bool NODE_SET::is_dirty(int idx)
  175. {
  176. return FD_ISSET(idx, __CAST__<fd_set>(DIRTY_BMP));
  177. }
  178. void NODE_SET::set_dirty(int idx)
  179. {
  180. FD_SET(idx, __CAST__<fd_set>(DIRTY_BMP));
  181. }
  182. void NODE_SET::clr_dirty(int idx)
  183. {
  184. FD_CLR(idx, __CAST__<fd_set>(DIRTY_BMP));
  185. }