关键词搜索

源码搜索 ×
×

漫话Redis源码之四十六

发布2022-01-02浏览442次

详情内容

这里主要是内容相关的一些操作,其中有一些magic number, 很眼熟,一看就是非负整形的最大值:

  1. /*
  2. * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of Redis nor the names of its contributors may be used
  14. * to endorse or promote products derived from this software without
  15. * specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdint.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <assert.h>
  34. #include <limits.h>
  35. #include <errno.h>
  36. #include <termios.h>
  37. #include <sys/ioctl.h>
  38. #if defined(__sun)
  39. #include <stropts.h>
  40. #endif
  41. #include "config.h"
  42. #if (ULONG_MAX == 4294967295UL)
  43. #define MEMTEST_32BIT
  44. #elif (ULONG_MAX == 18446744073709551615ULL)
  45. #define MEMTEST_64BIT
  46. #else
  47. #error "ULONG_MAX value not supported."
  48. #endif
  49. #ifdef MEMTEST_32BIT
  50. #define ULONG_ONEZERO 0xaaaaaaaaUL
  51. #define ULONG_ZEROONE 0x55555555UL
  52. #else
  53. #define ULONG_ONEZERO 0xaaaaaaaaaaaaaaaaUL
  54. #define ULONG_ZEROONE 0x5555555555555555UL
  55. #endif
  56. static struct winsize ws;
  57. size_t progress_printed; /* Printed chars in screen-wide progress bar. */
  58. size_t progress_full; /* How many chars to write to fill the progress bar. */
  59. void memtest_progress_start(char *title, int pass) {
  60. int j;
  61. printf("\x1b[H\x1b[2J"); /* Cursor home, clear screen. */
  62. /* Fill with dots. */
  63. for (j = 0; j < ws.ws_col*(ws.ws_row-2); j++) printf(".");
  64. printf("Please keep the test running several minutes per GB of memory.\n");
  65. printf("Also check http://www.memtest86.com/ and http://pyropus.ca/software/memtester/");
  66. printf("\x1b[H\x1b[2K"); /* Cursor home, clear current line. */
  67. printf("%s [%d]\n", title, pass); /* Print title. */
  68. progress_printed = 0;
  69. progress_full = (size_t)ws.ws_col*(ws.ws_row-3);
  70. fflush(stdout);
  71. }
  72. void memtest_progress_end(void) {
  73. printf("\x1b[H\x1b[2J"); /* Cursor home, clear screen. */
  74. }
  75. void memtest_progress_step(size_t curr, size_t size, char c) {
  76. size_t chars = ((unsigned long long)curr*progress_full)/size, j;
  77. for (j = 0; j < chars-progress_printed; j++) printf("%c",c);
  78. progress_printed = chars;
  79. fflush(stdout);
  80. }
  81. /* Test that addressing is fine. Every location is populated with its own
  82. * address, and finally verified. This test is very fast but may detect
  83. * ASAP big issues with the memory subsystem. */
  84. int memtest_addressing(unsigned long *l, size_t bytes, int interactive) {
  85. unsigned long words = bytes/sizeof(unsigned long);
  86. unsigned long j, *p;
  87. /* Fill */
  88. p = l;
  89. for (j = 0; j < words; j++) {
  90. *p = (unsigned long)p;
  91. p++;
  92. if ((j & 0xffff) == 0 && interactive)
  93. memtest_progress_step(j,words*2,'A');
  94. }
  95. /* Test */
  96. p = l;
  97. for (j = 0; j < words; j++) {
  98. if (*p != (unsigned long)p) {
  99. if (interactive) {
  100. printf("\n*** MEMORY ADDRESSING ERROR: %p contains %lu\n",
  101. (void*) p, *p);
  102. exit(1);
  103. }
  104. return 1;
  105. }
  106. p++;
  107. if ((j & 0xffff) == 0 && interactive)
  108. memtest_progress_step(j+words,words*2,'A');
  109. }
  110. return 0;
  111. }
  112. /* Fill words stepping a single page at every write, so we continue to
  113. * touch all the pages in the smallest amount of time reducing the
  114. * effectiveness of caches, and making it hard for the OS to transfer
  115. * pages on the swap.
  116. *
  117. * In this test we can't call rand() since the system may be completely
  118. * unable to handle library calls, so we have to resort to our own
  119. * PRNG that only uses local state. We use an xorshift* PRNG. */
  120. #define xorshift64star_next() do { \
  121. rseed ^= rseed >> 12; \
  122. rseed ^= rseed << 25; \
  123. rseed ^= rseed >> 27; \
  124. rout = rseed * UINT64_C(2685821657736338717); \
  125. } while(0)
  126. void memtest_fill_random(unsigned long *l, size_t bytes, int interactive) {
  127. unsigned long step = 4096/sizeof(unsigned long);
  128. unsigned long words = bytes/sizeof(unsigned long)/2;
  129. unsigned long iwords = words/step; /* words per iteration */
  130. unsigned long off, w, *l1, *l2;
  131. uint64_t rseed = UINT64_C(0xd13133de9afdb566); /* Just a random seed. */
  132. uint64_t rout = 0;
  133. assert((bytes & 4095) == 0);
  134. for (off = 0; off < step; off++) {
  135. l1 = l+off;
  136. l2 = l1+words;
  137. for (w = 0; w < iwords; w++) {
  138. xorshift64star_next();
  139. *l1 = *l2 = (unsigned long) rout;
  140. l1 += step;
  141. l2 += step;
  142. if ((w & 0xffff) == 0 && interactive)
  143. memtest_progress_step(w+iwords*off,words,'R');
  144. }
  145. }
  146. }
  147. /* Like memtest_fill_random() but uses the two specified values to fill
  148. * memory, in an alternated way (v1|v2|v1|v2|...) */
  149. void memtest_fill_value(unsigned long *l, size_t bytes, unsigned long v1,
  150. unsigned long v2, char sym, int interactive)
  151. {
  152. unsigned long step = 4096/sizeof(unsigned long);
  153. unsigned long words = bytes/sizeof(unsigned long)/2;
  154. unsigned long iwords = words/step; /* words per iteration */
  155. unsigned long off, w, *l1, *l2, v;
  156. assert((bytes & 4095) == 0);
  157. for (off = 0; off < step; off++) {
  158. l1 = l+off;
  159. l2 = l1+words;
  160. v = (off & 1) ? v2 : v1;
  161. for (w = 0; w < iwords; w++) {
  162. #ifdef MEMTEST_32BIT
  163. *l1 = *l2 = ((unsigned long) v) |
  164. (((unsigned long) v) << 16);
  165. #else
  166. *l1 = *l2 = ((unsigned long) v) |
  167. (((unsigned long) v) << 16) |
  168. (((unsigned long) v) << 32) |
  169. (((unsigned long) v) << 48);
  170. #endif
  171. l1 += step;
  172. l2 += step;
  173. if ((w & 0xffff) == 0 && interactive)
  174. memtest_progress_step(w+iwords*off,words,sym);
  175. }
  176. }
  177. }
  178. int memtest_compare(unsigned long *l, size_t bytes, int interactive) {
  179. unsigned long words = bytes/sizeof(unsigned long)/2;
  180. unsigned long w, *l1, *l2;
  181. assert((bytes & 4095) == 0);
  182. l1 = l;
  183. l2 = l1+words;
  184. for (w = 0; w < words; w++) {
  185. if (*l1 != *l2) {
  186. if (interactive) {
  187. printf("\n*** MEMORY ERROR DETECTED: %p != %p (%lu vs %lu)\n",
  188. (void*)l1, (void*)l2, *l1, *l2);
  189. exit(1);
  190. }
  191. return 1;
  192. }
  193. l1 ++;
  194. l2 ++;
  195. if ((w & 0xffff) == 0 && interactive)
  196. memtest_progress_step(w,words,'=');
  197. }
  198. return 0;
  199. }
  200. int memtest_compare_times(unsigned long *m, size_t bytes, int pass, int times,
  201. int interactive)
  202. {
  203. int j;
  204. int errors = 0;
  205. for (j = 0; j < times; j++) {
  206. if (interactive) memtest_progress_start("Compare",pass);
  207. errors += memtest_compare(m,bytes,interactive);
  208. if (interactive) memtest_progress_end();
  209. }
  210. return errors;
  211. }
  212. /* Test the specified memory. The number of bytes must be multiple of 4096.
  213. * If interactive is true the program exists with an error and prints
  214. * ASCII arts to show progresses. Instead when interactive is 0, it can
  215. * be used as an API call, and returns 1 if memory errors were found or
  216. * 0 if there were no errors detected. */
  217. int memtest_test(unsigned long *m, size_t bytes, int passes, int interactive) {
  218. int pass = 0;
  219. int errors = 0;
  220. while (pass != passes) {
  221. pass++;
  222. if (interactive) memtest_progress_start("Addressing test",pass);
  223. errors += memtest_addressing(m,bytes,interactive);
  224. if (interactive) memtest_progress_end();
  225. if (interactive) memtest_progress_start("Random fill",pass);
  226. memtest_fill_random(m,bytes,interactive);
  227. if (interactive) memtest_progress_end();
  228. errors += memtest_compare_times(m,bytes,pass,4,interactive);
  229. if (interactive) memtest_progress_start("Solid fill",pass);
  230. memtest_fill_value(m,bytes,0,(unsigned long)-1,'S',interactive);
  231. if (interactive) memtest_progress_end();
  232. errors += memtest_compare_times(m,bytes,pass,4,interactive);
  233. if (interactive) memtest_progress_start("Checkerboard fill",pass);
  234. memtest_fill_value(m,bytes,ULONG_ONEZERO,ULONG_ZEROONE,'C',interactive);
  235. if (interactive) memtest_progress_end();
  236. errors += memtest_compare_times(m,bytes,pass,4,interactive);
  237. }
  238. return errors;
  239. }
  240. /* A version of memtest_test() that tests memory in small pieces
  241. * in order to restore the memory content at exit.
  242. *
  243. * One problem we have with this approach, is that the cache can avoid
  244. * real memory accesses, and we can't test big chunks of memory at the
  245. * same time, because we need to backup them on the stack (the allocator
  246. * may not be usable or we may be already in an out of memory condition).
  247. * So what we do is to try to trash the cache with useless memory accesses
  248. * between the fill and compare cycles. */
  249. #define MEMTEST_BACKUP_WORDS (1024*(1024/sizeof(long)))
  250. /* Random accesses of MEMTEST_DECACHE_SIZE are performed at the start and
  251. * end of the region between fill and compare cycles in order to trash
  252. * the cache. */
  253. #define MEMTEST_DECACHE_SIZE (1024*8)
  254. int memtest_preserving_test(unsigned long *m, size_t bytes, int passes) {
  255. unsigned long backup[MEMTEST_BACKUP_WORDS];
  256. unsigned long *p = m;
  257. unsigned long *end = (unsigned long*) (((unsigned char*)m)+(bytes-MEMTEST_DECACHE_SIZE));
  258. size_t left = bytes;
  259. int errors = 0;
  260. if (bytes & 4095) return 0; /* Can't test across 4k page boundaries. */
  261. if (bytes < 4096*2) return 0; /* Can't test a single page. */
  262. while(left) {
  263. /* If we have to test a single final page, go back a single page
  264. * so that we can test two pages, since the code can't test a single
  265. * page but at least two. */
  266. if (left == 4096) {
  267. left += 4096;
  268. p -= 4096/sizeof(unsigned long);
  269. }
  270. int pass = 0;
  271. size_t len = (left > sizeof(backup)) ? sizeof(backup) : left;
  272. /* Always test an even number of pages. */
  273. if (len/4096 % 2) len -= 4096;
  274. memcpy(backup,p,len); /* Backup. */
  275. while(pass != passes) {
  276. pass++;
  277. errors += memtest_addressing(p,len,0);
  278. memtest_fill_random(p,len,0);
  279. if (bytes >= MEMTEST_DECACHE_SIZE) {
  280. memtest_compare_times(m,MEMTEST_DECACHE_SIZE,pass,1,0);
  281. memtest_compare_times(end,MEMTEST_DECACHE_SIZE,pass,1,0);
  282. }
  283. errors += memtest_compare_times(p,len,pass,4,0);
  284. memtest_fill_value(p,len,0,(unsigned long)-1,'S',0);
  285. if (bytes >= MEMTEST_DECACHE_SIZE) {
  286. memtest_compare_times(m,MEMTEST_DECACHE_SIZE,pass,1,0);
  287. memtest_compare_times(end,MEMTEST_DECACHE_SIZE,pass,1,0);
  288. }
  289. errors += memtest_compare_times(p,len,pass,4,0);
  290. memtest_fill_value(p,len,ULONG_ONEZERO,ULONG_ZEROONE,'C',0);
  291. if (bytes >= MEMTEST_DECACHE_SIZE) {
  292. memtest_compare_times(m,MEMTEST_DECACHE_SIZE,pass,1,0);
  293. memtest_compare_times(end,MEMTEST_DECACHE_SIZE,pass,1,0);
  294. }
  295. errors += memtest_compare_times(p,len,pass,4,0);
  296. }
  297. memcpy(p,backup,len); /* Restore. */
  298. left -= len;
  299. p += len/sizeof(unsigned long);
  300. }
  301. return errors;
  302. }
  303. /* Perform an interactive test allocating the specified number of megabytes. */
  304. void memtest_alloc_and_test(size_t megabytes, int passes) {
  305. size_t bytes = megabytes*1024*1024;
  306. unsigned long *m = malloc(bytes);
  307. if (m == NULL) {
  308. fprintf(stderr,"Unable to allocate %zu megabytes: %s",
  309. megabytes, strerror(errno));
  310. exit(1);
  311. }
  312. memtest_test(m,bytes,passes,1);
  313. free(m);
  314. }
  315. void memtest(size_t megabytes, int passes) {
  316. #if !defined(__HAIKU__)
  317. if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
  318. ws.ws_col = 80;
  319. ws.ws_row = 20;
  320. }
  321. #else
  322. ws.ws_col = 80;
  323. ws.ws_row = 20;
  324. #endif
  325. memtest_alloc_and_test(megabytes,passes);
  326. printf("\nYour memory passed this test.\n");
  327. printf("Please if you are still in doubt use the following two tools:\n");
  328. printf("1) memtest86: http://www.memtest86.com/\n");
  329. printf("https://files.jxasp.com/image/2) memtester: http://pyropus.ca/software/memtester/\n");
  330. exit(0);
  331. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载