chiark / gitweb /
Replace interleave_size with channel_hash
[stressapptest] / src / findmask.c
1 /* Copyright 2013 Google Inc. All Rights Reserved.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16
17 /*
18  * This "tool" can be used to brute force the XOR bitmask that a memory
19  * controller uses to interleave addresses onto its two channels. To use it,
20  * you need to have a bunch of addresses that are known to go to only one
21  * of the memory channels... easiest way to get these is to run stressapptest on
22  * a machine while holding a soldering iron close to the chips of one channel.
23  * Generate about a thousand failures and extract their physical addresses
24  * from the output. Write them to findmask.inc in a way that forms a valid
25  * definition for the addrs array. Make and run on a big machine.
26  *
27  * The program iterates over all possible bitmasks within the first NUM_BITS,
28  * parallelizing execution over NUM_THREADS. Every integer is masked
29  * onto all supplied addresses, counting the amount of times this results in
30  * an odd or even amount of bits. If all but NOISE addresses fall on one side,
31  * it will print that mask to stdout. Note that the script will always "find"
32  * the mask 0x0, and may also report masks such as 0x100000000 depending on
33  * your test machines memory size... you will need to use your own judgement to
34  * interpret the results.
35  *
36  * As the program might run for a long time, you can send SIGUSR1 to it to
37  * output the last mask that was processed and get a rough idea of the
38  * current progress.
39  */
40
41 #include <pthread.h>
42 #include <signal.h>
43 #include <stdint.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46
47 #define NOISE 20
48 #define NUM_BITS 32
49 #define NUM_THREADS 128  // keep this a power of two
50
51 static uint64_t addrs[] = {
52 #include "findmask.inc"
53 };
54 static uint64_t lastmask;
55
56 __attribute__((optimize(3, "unroll-loops")))
57 void* thread_func(void* arg) {
58   register uint64_t mask;
59   register uintptr_t num = (uintptr_t)arg;
60
61   for (mask = num; mask < (1ULL << (NUM_BITS + 1)); mask += NUM_THREADS) {
62     register const uint64_t* cur;
63     register int a = 0;
64     register int b = 0;
65
66     for (cur = addrs; (char*)cur < (char*)addrs + sizeof(addrs); cur++) {
67 #ifdef __x86_64__
68       register uint64_t addr asm("rdx") = *cur & mask;
69       register uint32_t tmp asm("ebx");
70
71       // Behold: the dark bit counting magic!
72       asm (
73         // Fold high and low 32 bits onto each other
74         "MOVl %%edx, %%ebx\n\t"
75         "SHRq $32, %%rdx\n\t"
76         "XORl %%ebx, %%edx\n\t"
77         // Fold high and low 16 bits onto each other
78         "MOVl %%edx, %%ebx\n\t"
79         "SHRl $16, %%edx\n\t"
80         "XORw %%bx, %%dx\n\t"
81         // Fold high and low 8 bits onto each other
82         "XORb %%dh, %%dl\n\t"
83         // Invoke ancient 8086 parity flag (only counts lowest byte)
84         "SETnp %%bl\n\t"
85         "SETp %%dl\n\t"
86         // Stupid SET instruction can only affect the lowest byte...
87         "ANDl $1, %%ebx\n\t"
88         "ANDl $1, %%edx\n\t"
89         // Increment either 'a' or 'b' without needing another branch
90         "ADDl %%ebx, %2\n\t"
91         "ADDl %%edx, %1\n\t"
92         : "=b" (tmp), "+r"(a), "+r"(b) : "d"(addr) : "cc");
93
94 #else  // generic processor
95       register uint64_t addr = *cur & mask;
96       register uint32_t low = (uint32_t)addr;
97       register uint32_t high = (uint32_t)(addr >> 32);
98
99       // Takes about twice as long as the version above... take that GCC!
100       __builtin_parity(low) ^ __builtin_parity(high) ? a++ : b++;
101 #endif
102
103       // Early abort: probably still the most valuable optimization in here
104       if (a >= NOISE && b >= NOISE) break;
105     }
106
107     if (a < NOISE) b = a;
108     if (b < NOISE) {
109       printf("Found mask with just %d deviations: 0x%llx\n", b, mask);
110       fflush(stdout);
111     }
112
113     // I'm a little paranoid about performance: don't write to memory too often
114     if (!(mask & 0x7ff)) lastmask = mask;
115   }
116
117   return 0;
118 }
119
120 void signal_handler(int signum) {
121   printf("Received signal... currently evaluating mask 0x%llx!\n", lastmask);
122   fflush(stdout);
123 }
124
125 int main(int argc, char** argv) {
126   uintptr_t i;
127   pthread_t threads[NUM_THREADS];
128
129   signal(SIGUSR1, signal_handler);
130
131   for (i = 0; i < NUM_THREADS; i++)
132     pthread_create(&threads[i], 0, thread_func, (void*)i);
133
134   for (i = 0; i < NUM_THREADS; i++)
135     pthread_join(threads[i], 0);
136
137   return 0;
138 }